[Bps-public-commit] UNNAMED PROJECT branch, master, updated. 558f26cb8437bdf8b2bf30234bd579308f815ec4

jesse jesse at bestpractical.com
Thu Jan 15 22:51:46 EST 2009


The branch, master has been updated
       via  558f26cb8437bdf8b2bf30234bd579308f815ec4 (commit)
      from  dd539446ccda58cac9331cc7e76c44e1a56d2464 (commit)

Summary of changes:
 lib/Prophet/Conflict.pm       |    2 +-
 lib/Prophet/Record.pm         |   16 ++++-----
 lib/Prophet/Replica.pm        |   22 +++++++++++-
 lib/Prophet/Replica/sqlite.pm |   81 +++++++++++++++++++++++++++++++++++++++--
 t/simple-push.t               |   14 ++++---
 5 files changed, 114 insertions(+), 21 deletions(-)

- Log -----------------------------------------------------------------
commit 558f26cb8437bdf8b2bf30234bd579308f815ec4
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Thu Jan 15 22:50:05 2009 -0500

    Move SQLite replicas to keeping luids in the database rows where they
    belong

diff --git a/lib/Prophet/Conflict.pm b/lib/Prophet/Conflict.pm
index 563b8a9..d948dea 100644
--- a/lib/Prophet/Conflict.pm
+++ b/lib/Prophet/Conflict.pm
@@ -155,7 +155,7 @@ sub _generate_change_conflicts {
         {
             record_type          => $change->record_type,
             record_uuid          => $change->record_uuid,
-            target_record_exists => $file_exists,
+            target_record_exists => ($file_exists ? 1 : 0 ),
             change_type          => $change->change_type,
             $file_op_conflict ? ( file_op_conflict => $file_op_conflict ) : (),
         }
diff --git a/lib/Prophet/Record.pm b/lib/Prophet/Record.pm
index 557d2b0..b8c7a3e 100644
--- a/lib/Prophet/Record.pm
+++ b/lib/Prophet/Record.pm
@@ -38,15 +38,13 @@ has type => (
 has uuid => (
     is      => 'rw',
     isa     => 'Str',
-    trigger => sub {
-        my $self = shift;
-        $self->find_or_create_luid;
-    },
 );
 
 has luid => (
     is  => 'rw',
-    isa => 'Str',
+    isa => 'Maybe[Str]',
+    lazy => 1,
+    default => sub { my $self = shift; $self->find_or_create_luid; },
 );
 
 class_has REFERENCES => (
@@ -275,15 +273,15 @@ sub load {
     if ( $args{luid} ) {
         $self->luid( $args{luid} );
         $self->uuid( $self->handle->find_uuid_by_luid( luid => $args{luid} ) );
+        return($self->uuid) if ($self->uuid);
     } else {
         $self->uuid( $args{uuid} );
+        $self->luid( $self->handle->find_or_create_luid( uuid => $args{uuid}));
+        return($self->luid) if ($self->luid);
     }
 
     delete $self->{props};
-    return $self->handle->record_exists(
-        uuid => $self->uuid,
-        type => $self->type
-    );
+    return undef;
 }
 
 sub loaded {
diff --git a/lib/Prophet/Replica.pm b/lib/Prophet/Replica.pm
index f70f42d..78fb407 100644
--- a/lib/Prophet/Replica.pm
+++ b/lib/Prophet/Replica.pm
@@ -10,9 +10,15 @@ use Prophet::App;
 has state_handle => (
     is  => 'rw',
     isa => 'Prophet::Replica',
-    documentation => 'Where metadata about foreign replicas is stored.',
 );
 
+has metadata_store => (
+    is => 'rw',
+    isa => 'Prophet::MetadataStore',
+    documentation => 'Where metadata about other replicas is stored.',
+);
+
+
 has resolution_db_handle => (
     is  => 'rw',
     isa => 'Prophet::Replica',
@@ -578,6 +584,20 @@ sub find_or_create_luid {
     return $mapping->{ $args{'uuid'} };
 }
 
+sub find_luid_by_uuid {
+    my $self = shift;
+    my %args = validate( @_, { uuid => 1 } );
+    my $mapping = $self->_read_guid2luid_mappings;
+
+    if (!exists($mapping->{ $args{'uuid'} })) {
+        return undef;
+    }
+
+    return $mapping->{ $args{'uuid'} };
+
+}
+
+
 =head3 find_uuid_by_luid { luid => LUID }
 
 Finds the UUID for the given LUID. Returns C<undef> if the LUID is not known.
diff --git a/lib/Prophet/Replica/sqlite.pm b/lib/Prophet/Replica/sqlite.pm
index 47b8149..d7624d6 100644
--- a/lib/Prophet/Replica/sqlite.pm
+++ b/lib/Prophet/Replica/sqlite.pm
@@ -16,6 +16,8 @@ has dbh => (
         my $self = shift;
         DBI->connect( "dbi:SQLite:" . $self->db_file , undef, undef, {RaiseError =>1, AutoCommit => 1 });
      }
+
+
     );
 
 sub db_file { shift->fs_root ."/db.sqlite"}
@@ -89,11 +91,21 @@ sub BUILD {
         #s/^sqlite://;    # url-based constructor in ::replica should do better
         s{/$}{};
     }
+   $self->_check_for_upgrades if ($self->replica_exists);
         
-    
 
 }
 
+sub _check_for_upgrades {
+    my $self = shift;
+   if ($self->replica_version && $self->replica_version < 2) {
+        $self->_upgrade_replica_to_v2();
+   } 
+
+}
+
+
+
 sub state_handle { return shift; }
 
 sub __fetch_data {
@@ -213,6 +225,7 @@ for (
 
 q{
 CREATE TABLE records (
+    luid INTEGER PRIMARY KEY,
     uuid text,
     type text
 )
@@ -275,7 +288,7 @@ q{create index keyidx on userdata(key)}
 
     $self->set_db_uuid( $args{'db_uuid'} || Data::UUID->new->create_str );
     $self->set_replica_uuid( Data::UUID->new->create_str );
-    $self->set_replica_version(1);
+    $self->set_replica_version(2);
     $self->resolution_db_handle->initialize( db_uuid => $args{resdb_uuid} ) if !$self->is_resdb;
     $self->after_initialize->($self);
 }
@@ -623,7 +636,7 @@ sub set_record_props {
 
 sub get_record_props {
     my $self = shift;
-    my %args = validate( @_, { uuid => 1, type => 1 } );
+    my %args = (uuid => undef, type => undef, @_); # validate is slooow validate( @_, { uuid => 1, type => 1 } );
     my $sth = $self->dbh->prepare( "SELECT prop, value from record_props WHERE uuid = ?");
     $sth->execute($args{uuid});
     my $items = $sth->fetchall_arrayref;
@@ -635,7 +648,7 @@ sub record_exists {
     my %args = validate( @_, { uuid => 1, type => 1 } );
     return undef unless $args{'uuid'};
 
-    my $sth = $self->dbh->prepare("SELECT COUNT(uuid) from records WHERE type = ? AND uuid = ?");
+    my $sth = $self->dbh->prepare("SELECT luid from records WHERE type = ? AND uuid = ?");
     $sth->execute($args{type}, $args{uuid});
     return $sth->fetchrow_array;
 
@@ -696,6 +709,66 @@ sub write_userdata {
     );
 }
 
+
+=head1 Working with luids
+
+=cut
+
+sub find_or_create_luid {
+    my $self = shift;
+    my %args = (uuid => undef, type => undef, @_); # validate is slooow validate( @_, { uuid => 1, type => 1 } );
+    return undef unless $args{'uuid'};
+
+    my $sth = $self->dbh->prepare("SELECT luid from records WHERE uuid = ?");
+    $sth->execute( $args{uuid});
+    return $sth->fetchrow_array;
+}
+
+
+sub find_luid_by_uuid {
+    my $self = shift;
+    my %args = validate( @_, { uuid => 1 } );
+
+    my $sth = $self->dbh->prepare("SELECT luid from records WHERE uuid = ?");
+    $sth->execute( $args{uuid});
+    return $sth->fetchrow_array;
+}
+
+
+sub find_uuid_by_luid {
+    my $self = shift;
+    my %args = validate( @_, { luid => 1 } );
+    return undef unless $args{'luid'};
+
+    my $sth = $self->dbh->prepare("SELECT uuid from records WHERE luid = ?");
+    $sth->execute( $args{luid});
+    return $sth->fetchrow_array;
+}
+
+
+
+
+sub _upgrade_replica_to_v2 {
+    my $self = shift;
+    $self->dbh->begin_work;
+
+for (
+    q{CREATE TABLE new_records (luid INTEGER PRIMARY KEY, uuid TEXT, type TEXT)},
+   q{INSERT INTO new_records (uuid, type) SELECT uuid, type FROM records},
+   q{DROP TABLE records},
+   q{ALTER TABLE new_records RENAME TO records}
+
+) {
+        $self->dbh->do($_) || warn $self->dbh->errstr;
+    }
+
+    $self->set_replica_version(2);
+    $self->dbh->commit;
+
+}
+
+
+
 sub DEMOLISH { shift->dbh->disconnect }
 __PACKAGE__->meta->make_immutable;
 no Moose;
diff --git a/t/simple-push.t b/t/simple-push.t
index 34686a0..9537dd0 100644
--- a/t/simple-push.t
+++ b/t/simple-push.t
@@ -3,7 +3,7 @@
 use warnings;
 use strict;
 
-use Prophet::Test tests => 16;
+use Prophet::Test tests => 17;
 
 as_alice {
     run_ok( 'prophet', [qw(init)] );
@@ -30,13 +30,15 @@ as_bob {
         [qw(create --type Bug -- --status open-bob --from bob )],
         "Created a record as bob"
     );
-    run_output_matches(
-        'prophet',
-        [qw(search --type Bug --regex .)],
-        [ qr/open-bob/, qr/new-alice/ ],
-        " Found our record"
+    run_output_matches( 'prophet', [qw(search --type Bug --regex new-alice)],
+        [ qr/new-alice/ ], " Found our record"
     );
 
+    run_output_matches( 'prophet', [qw(search --type Bug --regex open-bob)],
+        [ qr/open-bob/ ], " Found our record"
+    );
+
+
     # update the record
     # show the record history
     # show the record

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list