MogileFS-Server
view release on metacpan or search on metacpan
lib/MogileFS/FID.pm view on Meta::CPAN
my ($class, @fids) = @_;
my $sto = Mgd::get_store();
my $locs = $sto->fid_devids_multiple(map { $_->id } @fids);
my @ret;
foreach my $fid (@fids) {
$fid->{_devids} = $locs->{$fid->id} || [];
}
}
# --------------------------------------------------------------------------
sub exists {
my $self = shift;
$self->_tryload;
return $self->{_loaded};
}
sub classid {
my $self = shift;
$self->_load;
return $self->{classid};
}
sub dmid {
my $self = shift;
$self->_load;
return $self->{dmid};
}
sub length {
my $self = shift;
$self->_load;
return $self->{length};
}
sub devcount {
my $self = shift;
$self->_load;
return $self->{devcount};
}
sub id { $_[0]{fidid} }
# force loading, or die.
sub _load {
return 1 if $_[0]{_loaded};
my $self = shift;
croak("FID\#$self->fidid} doesn't exist") unless $self->_tryload;
}
# return 1 if loaded, or 0 if not exist
sub _tryload {
return 1 if $_[0]{_loaded};
my $self = shift;
my $row = Mgd::get_store()->file_row_from_fidid($self->{fidid})
or return 0;
$self->{$_} = $row->{$_} foreach qw(dmid dkey length classid devcount);
$self->{_loaded} = 1;
return 1;
}
sub update_devcount {
my ($self, %opts) = @_;
my $no_lock = delete $opts{no_lock};
croak "Bogus options" if %opts;
return 1 if MogileFS::Config->server_setting_cached('skip_devcount');
my $fidid = $self->{fidid};
my $sto = Mgd::get_store();
if ($no_lock) {
return $sto->update_devcount($fidid);
} else {
return $sto->update_devcount_atomic($fidid);
}
}
sub update_class {
my ($self, %opts) = @_;
my $classid = delete $opts{classid};
croak "Bogus options" if %opts;
my $sto = Mgd::get_store();
return $sto->update_classid($self->{fidid}, $classid);
}
sub enqueue_for_replication {
my ($self, %opts) = @_;
my $in = delete $opts{in};
my $from_dev = delete $opts{from_device}; # devid or Device object
croak("Unknown options to enqueue_for_replication") if %opts;
my $from_devid = (ref $from_dev ? $from_dev->id : $from_dev) || undef;
# Still schedule for the future, but don't delay long
$in = 1 if (TESTING && $in);
Mgd::get_store()->enqueue_for_replication($self->id, $from_devid, $in);
}
sub delete {
my $fid = shift;
my $sto = Mgd::get_store();
my $memc = MogileFS::Config->memcache_client;
if ($memc) {
$fid->_tryload;
}
$sto->delete_fidid($fid->id);
if ($memc && $fid->{_loaded}) {
$memc->delete("mogfid:$fid->{dmid}:$fid->{dkey}");
}
}
# returns 1 on success, 0 on duplicate key error, dies on exception
sub rename {
my ($fid, $to_key) = @_;
my $sto = Mgd::get_store();
return $sto->rename_file($fid->id, $to_key);
}
# returns array of devids that this fid is on
# NOTE: TODO: by default, this doesn't cache. callers might be surprised from
# having an old version later on. before caching is added, auditing needs
# to be done.
sub devids {
my $self = shift;
# if it was mass-loaded and stored in _devids arrayref, use
# that instead of going to db...
return @{$self->{_devids}} if $self->{_devids};
# else get it from the database
return Mgd::get_store()->read_store->fid_devids($self->id);
}
sub devs {
my $self = shift;
return map { Mgd::device_factory()->get_by_id($_) } $self->devids;
}
( run in 0.746 second using v1.01-cache-2.11-cpan-39bf76dae61 )