IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20091


Ignore:
Timestamp:
Oct 13, 2008, 10:41:17 AM (18 years ago)
Author:
jhoblitt
Message:

add Nebulous::Server->swap_objects() method

Location:
trunk/Nebulous-Server
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/Changes

    r20057 r20091  
    2929    - change Nebulous::Server->new() to call ->db() instead of setting up the
    3030      DBH itself; cleanup Nebulous::Server->db()
     31    - add Nebulous::Server->swap_objects() method
    3132
    32330.15 Thu Sep 11 13:00:59 HST 2008
  • trunk/Nebulous-Server/MANIFEST

    r19887 r20091  
    4949t/14_server_xattr.t
    5050t/15_mounts.t
     51t/16_server_swap_objects.t
    5152t/75_parse_neb_key.t
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r20090 r20091  
    11# Copyright (c) 2004-2008  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.92 2008-10-13 20:09:38 jhoblitt Exp $
     3# $Id: Server.pm,v 1.93 2008-10-13 20:41:17 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    296296
    297297
     298sub swap_objects
     299{
     300    my $self = shift;
     301
     302    my ($key1, $key2) = validate_pos(@_,
     303        {
     304            type        => SCALAR,
     305            callbacks   => {
     306                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     307            },
     308        },
     309        {
     310            type        => SCALAR,
     311            callbacks   => {
     312                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     313            },
     314        },
     315    );
     316
     317    my $log = $self->log;
     318    my $sql = $self->sql;
     319    my $db  =$self->db;
     320
     321    $log->debug("entered - @_");
     322
     323    # ignore volumes
     324    $key1 = parse_neb_key($key1);
     325    $key2 = parse_neb_key($key2);
     326
     327    # order of operations for the swap:
     328    # key1 -> key1.swap
     329    # key2 -> key1
     330    # key1.swap -> key2
     331
     332    eval {
     333        {
     334            # key1 -> key1.swap
     335            my $query = $db->prepare_cached($sql->rename_object);
     336            # this SQL statment takes the new key name as the first param
     337            my $rows = $query->execute($key1->path . ".swap", $key1->path);
     338
     339            # if we affected more then one row something very bad has happened.
     340            unless ($rows == 1) {
     341                $query->finish;
     342                $log->logdie("affected row count is $rows instead of 1");
     343            }
     344        }
     345
     346        {
     347            # key2 -> key1
     348            my $query = $db->prepare_cached($sql->rename_object);
     349            # this SQL statment takes the new key name as the first param
     350            my $rows = $query->execute($key1->path, $key2->path);
     351
     352            # if we affected more then one row something very bad has happened.
     353            unless ($rows == 1) {
     354                $query->finish;
     355                $log->logdie("affected row count is $rows instead of 1");
     356            }
     357        }
     358
     359        {
     360            # key1.swap -> key2
     361            my $query = $db->prepare_cached($sql->rename_object);
     362            # this SQL statment takes the new key name as the first param
     363            my $rows = $query->execute($key2->path, $key1->path . ".swap");
     364
     365            # if we affected more then one row something very bad has happened.
     366            unless ($rows == 1) {
     367                $query->finish;
     368                $log->logdie("affected row count is $rows instead of 1");
     369            }
     370        }
     371
     372        $db->commit;
     373        $log->debug("commit");
     374    };
     375    if ($@) {
     376        $db->rollback;
     377        $log->debug("rollback");
     378        $log->logdie("database error: $@");
     379    }
     380
     381    $log->debug("leaving");
     382
     383    return 1;
     384}
     385
     386
    298387sub replicate_object
    299388{
Note: See TracChangeset for help on using the changeset viewer.