IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 23, 2009, 4:14:20 PM (17 years ago)
Author:
jhoblitt
Message:

add Nebulous::Server->prune_object() API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r24637 r24915  
    844844
    845845
     846sub prune_object
     847{
     848    my $self = shift;
     849
     850    my $log = $self->log;
     851    $log->debug("entered - @_");
     852
     853    my ($key) = validate_pos(@_,
     854        {
     855            type        => SCALAR,
     856            callbacks   => {
     857                'is valid object key'
     858                    => sub { $self->_is_valid_object_key($_[0]) },
     859            },
     860        },
     861    );
     862
     863    my $sql = $self->sql;
     864
     865    eval {
     866        $key = parse_neb_key($key);
     867    };
     868    $log->logdie("$@") if $@;
     869
     870    my $db  = $self->db($key);
     871
     872    my $rows_removed = 0;
     873TRANS: while (1) {
     874        eval {
     875            # remove key from cache
     876            $self->cache->delete($key->path) if defined $self->cache;
     877
     878            my $so_id;
     879            {
     880                my $query = $db->prepare_cached( $sql->find_object_by_ext_id );
     881                $query->execute( $key->path );
     882                $so_id = $query->fetchrow_hashref->{'so_id'};
     883                $query->finish;
     884            }
     885
     886            # record the path of the innaccesible files for deferred
     887            # deletion
     888            my $rows_copied;
     889            {
     890                my $query = $db->prepare_cached( $sql->copy_dead_instances_to_deleted );
     891                $rows_copied = $query->execute( $so_id );
     892            }
     893
     894            # check to see if there is anything to be done
     895            unless ($rows_copied > 0) {
     896                $db->rollback;
     897                return;
     898            }
     899
     900            # In MySQL you can't select from a table your deleting rows from so
     901            # we first have to get a list of instances to be removed, and then
     902            # removed them.
     903            my $rows_found;
     904            {
     905                my $query = $db->prepare_cached( $sql->find_dead_instances_by_so_id );
     906                $rows_found = $query->execute( $so_id );
     907                foreach my $row ($query->fetchrow_hashref) {
     908                    # remove dead instances
     909                    my $query = $db->prepare_cached( $sql->delete_instance_by_ins_id);
     910                    $rows_removed += $query->execute( $row->{ins_id} );
     911                }
     912                $query->finish;
     913            }
     914
     915            # sanity check
     916            die("instances inaccessible ($rows_copied) != instances removed ($rows_removed)")
     917                unless $rows_copied == $rows_removed;
     918           
     919            $db->commit;
     920            $log->debug("commit");
     921        };
     922        if ($@) {
     923            $db->rollback;
     924            $log->debug("rollback");
     925            if ($@ =~ $trans_regex) {
     926                $log->warn("database error, retrying transaction: $@");
     927                sleep TRANS_RETRY_WAIT;
     928                redo TRANS;
     929            }
     930            $log->logdie("error: $@");
     931        }
     932        last;
     933    }
     934
     935    $log->debug("leaving");
     936
     937    return $rows_removed;
     938}
     939
     940
    846941sub lock_object
    847942{
     
    16741769                # remove key from cache
    16751770                $self->cache->delete($key->path) if defined $self->cache;
    1676 
    1677                 # record the path of the innaccesible files for deferred
    1678                 # deletion
    1679                 {
    1680                     my $query = $db->prepare_cached( $sql->copy_instances_to_deleted );
    1681                     $query->execute( $so_id );
    1682                 }
    1683                 # remove all instances... not strictly nessicary as the delete
    1684                 # from storage_object should cascade but the fkey was specified
    1685                 # without the cascade
    1686                 {
    1687                     my $query = $db->prepare_cached( $sql->delete_instance_by_so_id );
    1688                     $query->execute( $so_id );
     1771               
     1772                if ($total > $available) {
     1773                    $self->prune_object("$key");
    16891774                }
    16901775               
Note: See TracChangeset for help on using the changeset viewer.