1 package MogileFS
::Domain
;
4 use MogileFS
::Util
qw(throw);
6 # --------------------------------------------------------------------------
8 # --------------------------------------------------------------------------
10 my %singleton; # dmid -> MogileFS::Domain
12 my %id2name; # dmid -> domainname(namespace)
13 my %name2id; # domainname(namespace) -> dmid
17 # return singleton MogileFS::Domain, given a dmid
19 my ($pkg, $dmid) = @_;
20 return undef unless $dmid;
21 return $singleton{$dmid} if $singleton{$dmid};
23 my $ns = $pkg->name_of_id($dmid)
26 return $singleton{$dmid} = bless {
32 # return singleton MogileFS::Domain, given a domain(namespace)
35 return undef unless $ns;
36 my $dmid = $pkg->id_of_name($ns)
38 return MogileFS
::Domain
->of_dmid($dmid);
41 # name to dmid, reloading if not in cache
43 my ($pkg, $domain) = @_;
44 return $name2id{$domain} if $name2id{$domain};
46 return $name2id{$domain};
49 # dmid to name, reloading if not in cache
51 my ($pkg, $dmid) = @_;
52 return $id2name{$dmid} if $id2name{$dmid};
54 return $id2name{$dmid};
57 # force reload of cache
60 my $sto = Mgd
::get_store
();
61 %name2id = $sto->get_all_domains;
63 while (my ($k, $v) = each %name2id) {
67 # Blow singleton cache on reload. Otherwise a *change* in data may not be
74 # FIXME: should probably have an invalidate_cache variant that only
75 # flushes locally (for things like "get_domains" or "get_hosts", where
76 # it needs to be locally correct for the semantics of the command, but
77 # no need to propagate a cache invalidation to our peers)
78 sub invalidate_cache
{
82 if (my $worker = MogileFS
::ProcManager
->is_child) {
83 $worker->invalidate_meta("domain");
90 return if $last_load > $now - 5;
91 MogileFS
::Domain
->reload_domains;
97 return map { $pkg->of_dmid($_) } keys %id2name;
100 # create a new domain given a name, returns MogileFS::Domain object on success.
101 # throws errors on failure. error codes include:
102 # "dup" -- on duplicate name
104 my ($pkg, $name) = @_;
107 my $dmid = Mgd
::get_store
()->create_domain($name)
108 or die "create domain didn't return a dmid";
110 # return the domain id we created
111 MogileFS
::Domain
->invalidate_cache;
112 return MogileFS
::Domain
->of_dmid($dmid);
115 # --------------------------------------------------------------------------
117 # --------------------------------------------------------------------------
119 sub id
{ $_[0]->{dmid
} }
120 sub name
{ $_[0]->{ns
} }
124 return 1 if $Mgd::_T_DOM_HAS_FILES
;
125 return Mgd
::get_store
()->domain_has_files($self->id);
130 # return a bunch of class objects for this domain
131 return MogileFS
::Class
->classes_of_domain($dom);
134 # returns true if deleted. throws exceptions on errors. exception codes:
135 # 'has_files' if it has files.
138 throw
("has_files") if $self->has_files;
139 # TODO: delete its classes
140 my $rv = Mgd
::get_store
()->delete_domain($self->id);
141 MogileFS
::Domain
->invalidate_cache;
145 # returns named class of domain
147 my ($dom, $clname) = @_;
148 foreach my $cl (MogileFS
::Class
->classes_of_domain($dom)) {
149 return $cl if $cl->name eq $clname;
155 my ($dom, $clname) = @_;
156 return MogileFS
::Class
->create_class($dom, $clname);