[Bps-public-commit] r15342 - in sd/branches/debbugs/lib/App/SD: ForeignReplica Replica Replica/debbugs Replica/hm Replica/rt

jesse at bestpractical.com jesse at bestpractical.com
Thu Aug 21 16:46:09 EDT 2008


Author: jesse
Date: Thu Aug 21 16:46:07 2008
New Revision: 15342

Added:
   sd/branches/debbugs/lib/App/SD/ForeignReplica/
   sd/branches/debbugs/lib/App/SD/ForeignReplica.pm
   sd/branches/debbugs/lib/App/SD/ForeignReplica/PullEncoder.pm
   sd/branches/debbugs/lib/App/SD/ForeignReplica/PushEncoder.pm
Modified:
   sd/branches/debbugs/lib/App/SD/Replica/debbugs.pm
   sd/branches/debbugs/lib/App/SD/Replica/debbugs/PullEncoder.pm
   sd/branches/debbugs/lib/App/SD/Replica/hm.pm
   sd/branches/debbugs/lib/App/SD/Replica/hm/PullEncoder.pm
   sd/branches/debbugs/lib/App/SD/Replica/rt.pm
   sd/branches/debbugs/lib/App/SD/Replica/rt/PullEncoder.pm

Log:
* Extracted out a bunch of the code that should be shared between various foreign replica types

Added: sd/branches/debbugs/lib/App/SD/ForeignReplica.pm
==============================================================================
--- (empty file)
+++ sd/branches/debbugs/lib/App/SD/ForeignReplica.pm	Thu Aug 21 16:46:07 2008
@@ -0,0 +1,127 @@
+package App::SD::ForeignReplica;
+use Moose;
+use Params::Validate;
+
+extends 'Prophet::ForeignReplica';
+
+=head2 prophet_has_seen_transaction $transaction_id
+
+Given an transaction id, will return true if this transaction originated in Prophet 
+and was pushed to RT or originated in RT and has already been pulled to the prophet replica.
+
+=cut
+
+# This is a mapping of all the transactions we have pushed to the
+# remote replica we'll only ever care about remote sequence #s greater
+# than the last transaction # we've pulled from the remote replica
+# once we've done a pull from the remote replica, we can safely expire
+# all records of this type for the remote replica (they'll be
+# obsolete)
+
+# we use this cache to avoid integrating changesets we've pushed to the remote replica when doing a subsequent pull
+
+my $TXN_METATYPE = 'txn-source';
+
+sub _txn_storage {
+    my $self = shift;
+    return $self->state_handle->metadata_storage( $TXN_METATYPE,
+        'prophet-txn-source' );
+}
+
+sub prophet_has_seen_transaction {
+    my $self = shift;
+    my ($id) = validate_pos( @_, 1 );
+    return $self->_txn_storage->( $self->uuid . '-txn-' . $id );
+}
+
+sub record_pushed_transaction {
+    my $self = shift;
+    my %args = validate( @_,
+        { transaction => 1, changeset => { isa => 'Prophet::ChangeSet' } } );
+
+    $self->_txn_storage->(
+        $self->uuid . '-txn-' . $args{transaction},
+        join( ':',
+            $args{changeset}->original_source_uuid,
+            $args{changeset}->original_sequence_no )
+    );
+}
+
+
+sub remote_uri_path_for_id {
+    die "your subclass needds to implement this to be able to map a remote id to /ticket/id or soemthing";
+
+}
+
+sub uuid_for_remote_id {
+    my ( $self, $id ) = @_;
+    return $self->_lookup_uuid_for_remote_id($id)
+        || $self->uuid_for_url(
+        $self->remote_url . $self->remote_uri_path_for_id($id) );
+}
+
+sub _lookup_uuid_for_remote_id {
+    my $self = shift;
+    my ($id) = validate_pos( @_, 1 );
+
+    return $self->_remote_id_storage(
+        $self->uuid_for_url(
+            $self->remote_url . $self->remote_uri_path_for_id($id)
+        )
+    );
+}
+
+sub _set_uuid_for_remote_id {
+    my $self = shift;
+    my %args = validate( @_, { uuid => 1, remote_id => 1 } );
+    return $self->_remote_id_storage(
+        $self->uuid_for_url(
+                  $self->remote_url
+                . $self->remote_uri_path_for_id( $args{'remote_id'} )
+        ),
+        $args{uuid}
+    );
+}
+
+# This cache stores uuids for tickets we've synced from a remote RT
+# Basically, if we created the ticket to begin with, then we'll know its uuid
+# if we pulled the ticket from RT then its uuid will be generated based on a UUID-from-ticket-url scheme
+# This cache is PERMANENT. - aka not a cache but a mapping table
+
+sub remote_id_for_uuid {
+    my ( $self, $uuid_for_remote_id ) = @_;
+
+
+    # XXX: should not access CLI handle
+    my $ticket = Prophet::Record->new(
+        handle => Prophet::CLI->new->app_handle->handle,
+        type   => 'ticket'
+    );
+    $ticket->load( uuid => $uuid_for_remote_id );
+    my $id =  $ticket->prop( $self->uuid . '-id' );
+    return $id;
+}
+
+sub _set_remote_id_for_uuid {
+    my $self = shift;
+    my %args = validate(
+        @_,
+        {   uuid      => 1,
+            remote_id => 1
+        }
+    );
+
+    # XXX: should not access CLI handle
+    my $ticket = Prophet::Record->new(
+        handle => Prophet::CLI->new->app_handle->handle,
+        type   => 'ticket'
+    );
+    $ticket->load( uuid => $args{'uuid'});
+    $ticket->set_props( props => {  $self->uuid.'-id' => $args{'remote_id'}});
+
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;

Added: sd/branches/debbugs/lib/App/SD/ForeignReplica/PullEncoder.pm
==============================================================================
--- (empty file)
+++ sd/branches/debbugs/lib/App/SD/ForeignReplica/PullEncoder.pm	Thu Aug 21 16:46:07 2008
@@ -0,0 +1,17 @@
+package App::SD::ForeignReplica::PullEncoder;
+use Moose;
+
+sub warp_list_to_old_value {
+    my $self         = shift;
+    my $current_value = shift ||'';
+    my $add          = shift;
+    my $del          = shift;
+
+    my @new = grep { defined } split( /\s*,\s*/, $current_value );
+    my @old = (grep { defined $_ && $_ ne $add } @new, $del ) || ();
+    return join( ", ", @old );
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;

Added: sd/branches/debbugs/lib/App/SD/ForeignReplica/PushEncoder.pm
==============================================================================
--- (empty file)
+++ sd/branches/debbugs/lib/App/SD/ForeignReplica/PushEncoder.pm	Thu Aug 21 16:46:07 2008
@@ -0,0 +1,10 @@
+package App::SD::ForeignReplica::PushEncoder;
+use Moose;
+
+
+
+
+no Moose;
+__PACKAGE__->meta->make_immutable;
+
+1;

Modified: sd/branches/debbugs/lib/App/SD/Replica/debbugs.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/debbugs.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/debbugs.pm	Thu Aug 21 16:46:07 2008
@@ -52,7 +52,7 @@
 sub uuid_for_remote_id {
     my ( $self, $id ) = @_;
     return $self->_lookup_uuid_for_remote_id($id) ||
-        $self->uuid_for_url( $self->rt_url . "/ticket/$id" );
+        $self->uuid_for_url( $self->remote_url . "/ticket/$id" );
 }
 
 sub record_pushed_ticket {

Modified: sd/branches/debbugs/lib/App/SD/Replica/debbugs/PullEncoder.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/debbugs/PullEncoder.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/debbugs/PullEncoder.pm	Thu Aug 21 16:46:07 2008
@@ -5,7 +5,7 @@
 use Memoize;
 
 has sync_source => (
-    isa => 'App::SD::Replica::Debbugs',
+    isa => 'App::SD::Replica::debbugs',
     is => 'rw',
 );
 

Modified: sd/branches/debbugs/lib/App/SD/Replica/hm.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/hm.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/hm.pm	Thu Aug 21 16:46:07 2008
@@ -1,6 +1,6 @@
 package App::SD::Replica::hm;
 use Moose;
-extends 'Prophet::ForeignReplica';
+extends 'App::SD::ForeignReplica';
 use Params::Validate qw(:all);
 use UNIVERSAL::require;
 use URI;
@@ -9,7 +9,7 @@
 use File::Temp 'tempdir';
 
 has hm => ( isa => 'Net::Jifty', is => 'rw');
-has hm_url => ( isa => 'Str', is => 'rw');
+has remote_url => ( isa => 'Str', is => 'rw');
 has hm_username => ( isa => 'Str', is => 'rw');
 
 use constant scheme => 'hm';
@@ -37,13 +37,13 @@
         ( $username, $password ) = split /:/, $auth, 2;
         $uri->userinfo(undef);
     }
-    $self->hm_url("$uri");
+    $self->remote_url("$uri");
 
     ( $username, $password ) = $self->prompt_for_login( $uri, $username ) unless $password;
 
     $self->hm(
         Net::Jifty->new(
-            site        => $self->hm_url,
+            site        => $self->remote_url,
             cookie_name => 'JIFTY_SID_HIVEMINDER',
 
             email    => $username,
@@ -62,7 +62,7 @@
 
 sub uuid {
     my $self = shift;
-    return $self->uuid_for_url( join( '/', $self->hm_url, $self->hm_username ) );
+    return $self->uuid_for_url( join( '/', $self->remote_url, $self->hm_username ) );
 }
 
 sub traverse_changesets {
@@ -103,23 +103,11 @@
     return $tasks;
 }
 
-sub prophet_has_seen_transaction {
-    goto \&App::SD::Replica::rt::prophet_has_seen_transaction;
-}
-
-sub record_pushed_transaction {
-    goto \&App::SD::Replica::rt::record_pushed_transaction;
-}
-
 sub record_pushed_transactions {
 
     # don't need this for hm
 }
 
-sub _txn_storage {
-    goto \&App::SD::Replica::rt::_txn_storage;
-}
-
 # hiveminder transaction ~= prophet changeset
 # hiveminder taskhistory ~= prophet change
 # hiveminder taskemail ~= prophet change
@@ -155,49 +143,14 @@
     $recoder->integrate_change($change,$changeset);
 }
 
-
-
-{
-
-    # XXXXXXXX
-    # XXXXXXXXX
-    # XXX todo code in this block cargo culted from the RT Replica type
-
-    sub remote_id_for_uuid {
-        my ( $self, $uuid_for_remote_id ) = @_;
-
-        # XXX: should not access CLI handle
-        my $ticket = Prophet::Record->new( handle => Prophet::CLI->new->handle, type => 'ticket' );
-        $ticket->load( uuid => $uuid_for_remote_id );
-        return $ticket->prop( $self->uuid . '-id' );
-    }
-
-    sub uuid_for_remote_id {
-        my ( $self, $id ) = @_;
-        return $self->_lookup_remote_id($id) || $self->uuid_for_url( $self->hm_url . "/task/$id" );
-    }
-
-    sub _lookup_remote_id {
-        my $self = shift;
-        my ($id) = validate_pos( @_, 1 );
-
-        return $self->_remote_id_storage( $self->uuid_for_url( $self->hm_url . "/task/$id" ) );
-    }
-
-    sub _set_remote_id {
-        my $self = shift;
-        my %args = validate(
-            @_,
-            {   uuid      => 1,
-                remote_id => 1
-            }
-        );
-        return $self->_remote_id_storage( $self->uuid_for_url( $self->hm_url . "/task/" . $args{'remote_id'} ),
-            $args{uuid} );
-    }
-
+sub remote_uri_path_for_id {
+    my $self = shift;
+    my $id = shift;
+    return "/task/".$id;
 }
 
+
+# XXX TODO, can this get generalized out (take the rt one to ForeignReplica.pm?
 sub record_pushed_ticket {
     my $self = shift;
     my %args = validate(
@@ -206,7 +159,7 @@
             remote_id => 1
         }
     );
-    $self->_set_remote_id(%args);
+    $self->_set_uuid_for_remote_id(%args);
 }
 
 __PACKAGE__->meta->make_immutable;

Modified: sd/branches/debbugs/lib/App/SD/Replica/hm/PullEncoder.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/hm/PullEncoder.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/hm/PullEncoder.pm	Thu Aug 21 16:46:07 2008
@@ -140,7 +140,7 @@
     my $change = Prophet::Change->new(
         {   record_type => 'comment',
             record_uuid =>
-                $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/transaction/" . $args{'txn'}->{'id'} ),
+                $self->sync_source->uuid_for_url( $self->sync_source->remote_url . "/transaction/" . $args{'txn'}->{'id'} ),
             change_type => 'add_file'
         }
     );

Modified: sd/branches/debbugs/lib/App/SD/Replica/rt.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/rt.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/rt.pm	Thu Aug 21 16:46:07 2008
@@ -1,6 +1,6 @@
 package App::SD::Replica::rt;
 use Moose;
-extends qw/Prophet::ForeignReplica/;
+extends qw/App::SD::ForeignReplica/;
 
 use Params::Validate qw(:all);
 use Path::Class;
@@ -12,7 +12,7 @@
 use Prophet::ChangeSet;
 
 has rt => ( isa => 'RT::Client::REST', is => 'rw');
-has rt_url => ( isa => 'Str', is => 'rw');
+has remote_url => ( isa => 'Str', is => 'rw');
 has rt_queue => ( isa => 'Str', is => 'rw');
 has rt_query => ( isa => 'Str', is => 'rw');
 
@@ -41,7 +41,7 @@
         ( $username, $password ) = split /:/, $auth, 2;
         $uri->userinfo(undef);
     }
-    $self->rt_url($uri->as_string);
+    $self->remote_url($uri->as_string);
     $self->rt_queue($type);
     $self->rt_query( ( $query ?  "($query) AND " :"") . " Queue = '$type'" );
     $self->rt( RT::Client::REST->new( server => $server ) );
@@ -74,107 +74,6 @@
         );
     }
 }
-
-=head2 prophet_has_seen_transaction $transaction_id
-
-Given an transaction id, will return true if this transaction originated in Prophet 
-and was pushed to RT or originated in RT and has already been pulled to the prophet replica.
-
-=cut
-
-# This is a mapping of all the transactions we have pushed to the
-# remote replica we'll only ever care about remote sequence #s greater
-# than the last transaction # we've pulled from the remote replica
-# once we've done a pull from the remote replica, we can safely expire
-# all records of this type for the remote replica (they'll be
-# obsolete)
-
-# we use this cache to avoid integrating changesets we've pushed to the remote replica when doing a subsequent pull
-
-my $TXN_METATYPE = 'txn-source';
-
-sub _txn_storage {
-    my $self = shift;
-    return $self->state_handle->metadata_storage( $TXN_METATYPE,
-        'prophet-txn-source' );
-}
-
-sub prophet_has_seen_transaction {
-    my $self = shift;
-    my ($id) = validate_pos( @_, 1 );
-    return $self->_txn_storage->( $self->uuid . '-txn-' . $id );
-}
-
-sub record_pushed_transaction {
-    my $self = shift;
-    my %args = validate( @_,
-        { transaction => 1, changeset => { isa => 'Prophet::ChangeSet' } } );
-
-    $self->_txn_storage->(
-        $self->uuid . '-txn-' . $args{transaction},
-        join( ':',
-            $args{changeset}->original_source_uuid,
-            $args{changeset}->original_sequence_no )
-    );
-}
-
-# This cache stores uuids for tickets we've synced from a remote RT
-# Basically, if we created the ticket to begin with, then we'll know its uuid
-# if we pulled the ticket from RT then its uuid will be generated based on a UUID-from-ticket-url scheme
-# This cache is PERMANENT. - aka not a cache but a mapping table
-
-sub remote_id_for_uuid {
-    my ( $self, $uuid_for_remote_id ) = @_;
-
-
-    # XXX: should not access CLI handle
-    my $ticket = Prophet::Record->new(
-        handle => Prophet::CLI->new->app_handle->handle,
-        type   => 'ticket'
-    );
-    $ticket->load( uuid => $uuid_for_remote_id );
-    my $id =  $ticket->prop( $self->uuid . '-id' );
-    return $id;
-}
-
-sub _set_remote_id_for_uuid {
-    my $self = shift;
-    my %args = validate(
-        @_,
-        {   uuid      => 1,
-            remote_id => 1
-        }
-    );
-
-    # XXX: should not access CLI handle
-    my $ticket = Prophet::Record->new(
-        handle => Prophet::CLI->new->app_handle->handle,
-        type   => 'ticket'
-    );
-    $ticket->load( uuid => $args{'uuid'});
-    $ticket->set_props( props => {  $self->uuid.'-id' => $args{'remote_id'}});
-
-}
-
-
-sub uuid_for_remote_id {
-    my ( $self, $id ) = @_;
-    return $self->_lookup_uuid_for_remote_id($id) || $self->uuid_for_url( $self->rt_url . "/ticket/$id" );
-}
-
-sub _lookup_uuid_for_remote_id {
-    my $self = shift;
-    my ($id) = validate_pos( @_, 1 );
-
-    return $self->_remote_id_storage( $self->uuid_for_url( $self->rt_url . "/ticket/$id" ) );
-}
-
-sub _set_uuid_for_remote_id {
-    my $self = shift;
-    my %args = validate( @_, {   uuid      => 1, remote_id => 1 });
-    return $self->_remote_id_storage( $self->uuid_for_url( $self->rt_url . "/ticket/" . $args{'remote_id'} ), $args{uuid});
-}
-
 sub record_pushed_ticket {
     my $self = shift;
     my %args = validate(
@@ -208,7 +107,7 @@
 
 sub uuid {
     my $self = shift;
-    return $self->uuid_for_url( join( '/', $self->rt_url, $self->rt_query ) );
+    return $self->uuid_for_url( join( '/', $self->remote_url, $self->rt_query ) );
 
 }
 
@@ -226,6 +125,12 @@
 
 }
 
+sub remote_uri_path_for_id {
+    my $self = shift;
+    my $id = shift;
+    return "/ticket/".$id;
+}
+
 
 =head1 NOTES ON PUSH
 

Modified: sd/branches/debbugs/lib/App/SD/Replica/rt/PullEncoder.pm
==============================================================================
--- sd/branches/debbugs/lib/App/SD/Replica/rt/PullEncoder.pm	(original)
+++ sd/branches/debbugs/lib/App/SD/Replica/rt/PullEncoder.pm	Thu Aug 21 16:46:07 2008
@@ -1,5 +1,6 @@
 package App::SD::Replica::rt::PullEncoder;
 use Moose;
+extends 'App::SD::ForeignReplica::PullEncoder';
 
 use Params::Validate qw(:all);
 use Memoize;
@@ -166,7 +167,7 @@
     my %args   = validate( @_, { ticket => 1, txn => 1, changeset => 1, attachment => 1 } );
     my $change = Prophet::Change->new(
         {   record_type => 'attachment',
-            record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/attachment/" . $args{'attachment'}->{'id'} ),
+            record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->remote_url . "/attachment/" . $args{'attachment'}->{'id'} ),
             change_type => 'add_file'
         }
     );
@@ -309,7 +310,7 @@
     my %args   = validate( @_, { txn => 1, ticket => 1, changeset => 1 } );
     my $change = Prophet::Change->new(
         {   record_type => 'comment',
-            record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/transaction/" . $args{'txn'}->{'id'} ),
+            record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->remote_url . "/transaction/" . $args{'txn'}->{'id'} ),
             change_type => 'add_file'
         }
     );
@@ -417,17 +418,6 @@
 
 memoize 'resolve_user_id_to';
 
-sub warp_list_to_old_value {
-    my $self         = shift;
-    my $current_value = shift ||'';
-    my $add          = shift;
-    my $del          = shift;
-
-    my @new = grep { defined } split( /\s*,\s*/, $current_value );
-    my @old = (grep { defined $_ && $_ ne $add } @new, $del ) || ();
-    return join( ", ", @old );
-}
-
 use HTTP::Date;
 
 sub date_to_iso {
@@ -481,12 +471,6 @@
                 }
             next if ( $prop->old_value eq $prop->new_value);
 
-#
-#            if ( $prop->name eq 'id' || $prop->name eq 'queue') {
-#                $prop->old_value( $prop->old_value . '@' . $changeset->original_source_uuid ) if ( $prop->old_value);
-#                $prop->old_value( $prop->new_value . '@' . $changeset->original_source_uuid ) if ( $prop->new_value);
-#            }
-
             if ( $prop->name =~ /^cf-(.*)$/ ) {
                 $prop->name( 'custom-' . $1 );
             }



More information about the Bps-public-commit mailing list