AddressBook

 view release on metacpan or  search on metacpan

lib/AddressBook.pm  view on Meta::CPAN

  if ($args{config}) {
    $self->{config} = $args{config};
  } else {
    $self->{config} = AddressBook::Config->new(config_file=>$args{config_file});
  }
  if(defined $args{source}) {
    my ($type, $dsn) = split(':', $args{source}, 2);
    $dsn = '' unless $dsn; 
    delete $args{source};
    my (%bedb_args,$k,$v);
    foreach ($self->{config}->{db}->{$type}, \%args) {
      next if (ref($_) ne "HASH" || ! %{$_} );
      while (($k,$v) = each %{$_}) {
	$bedb_args{$k} = $v;
      }
    }
    my $driverName = $self->{config}->{db}->{$type}->{driver} || croak "Uknown driver type for source = \"$type\"";
    eval qq{
      require AddressBook::DB::$driverName;
      \$self = AddressBook::DB::$driverName->new(dsn => "$dsn",
						 config => \$self->{config},
						 \%bedb_args,
						 );
    };
    croak "Couldn't load backend `$driverName': $@" if $@;
    $self->{db_name}=$type;
  } else {
    bless ($self,$class);
  }
  return $self;
}

=head2 sync

  AddressBook::sync(master=>$master_db, slave=>$slave_db)
  AddressBook::sync(master=>$master_db, slave=>$slave_db,debug=>1)

Synchronizes the "master" and "slave" databases.  The "master" database type must be
one that supports random-access methods.  The "slave" database type must
be one that supports sequential-access methods.

When the 'debug' option is true, debug messages will be printed to stdout.  The 
msg_function paramater, if included, should be a subroutine reference which will
be called with a status message is the argument.

=over 4

=item 1

For each record in the slave, look for a corresponding record in the master, using
the key_fields of each.

=over 6

=item Z<>

If no match is found, the entry is added to the master.

=item Z<>

If multiple matches are found, an error occurrs.

=item Z<>

If one match is found, then:

=over 8

=item Z<>

If the records match, nothing is done.

=item Z<>

If the records do not match, then:

=over 10

=item Z<>

If the slave record's timestamp is newer, the master's entry is merged (see below) 
with the slave entry's data.

=item Z<>

If the master record's timestamp is newer, nothing is done.

=back

=back

=back

=item 2

The slave database is truncated.

=item 3

Each record of the master is added to the slave

=back

The 'merging' of the master and slave entries involves taking each attribute in the
slave's entry and replacing the corresponding attribute in the master's entry.  
Note that attributes that are deleted only on the slave are therefore effectively ignored 
during synchronization.

Similarly, deletions made on the slave database are effectively ignored during
synchronization.

=cut

sub sync {
  my %args = @_;
  my $master = $args{master};
  my $slave = $args{slave};
  unless ($master->{key_fields} && $slave->{key_fields}) {
    croak "Key fields must be defined for both master and slave backends";
  }
  $slave->reset;



( run in 1.430 second using v1.01-cache-2.11-cpan-2398b32b56e )