Algorithm-VectorClocks
view release on metacpan or search on metacpan
lib/Algorithm/VectorClocks.pm view on Meta::CPAN
use List::Util qw(max);
use Perl6::Export::Attrs;
use Sys::Hostname;
use overload (
'""' => \&serialize,
'++' => \&increment,
'+=' => \&merge,
'==' => \&equal,
'eq' => \&equal,
'!=' => \¬_equal,
'ne' => \¬_equal,
fallback => undef,
);
use base qw(Class::Accessor::Fast Class::Data::Inheritable);
__PACKAGE__->mk_accessors(qw(clocks));
__PACKAGE__->mk_classdata($_) for qw(id json);
__PACKAGE__->id(hostname);
__PACKAGE__->json(JSON::Any->new);
lib/Algorithm/VectorClocks.pm view on Meta::CPAN
my @vcs = @_;
$_ = __PACKAGE__->new($_) for @vcs;
my @ids = _list_ids(@vcs);
for my $id (@ids) {
return 0
unless ($vcs[0]->clocks->{$id} || 0) == ($vcs[1]->clocks->{$id} || 0);
}
return 1;
}
sub not_equal { !equal(@_) }
sub order_vector_clocks :Export(:DEFAULT) {
my($vcs) = @_;
my @vcs;
while (my($id, $vc) = each %$vcs) {
$vc = __PACKAGE__->new($vc);
$vc->{_id} = $id;
push @vcs, $vc;
}
@vcs = sort { _compare($b, $a) } @vcs;
lib/Algorithm/VectorClocks.pm view on Meta::CPAN
=head2 $vc->equal($other_vc)
Returns true if $vc equals $other_vc.
This module overloads a comparison operator,
and the following code does the same thing:
$vc == $other_vc;
$vc eq $other_vc;
=head2 $vc->not_equal($other_vc)
Returns true unless $vc equals $other_vc.
This module overloads a comparison operator,
and the following code does the same thing:
$vc != $other_vc;
$vc ne $other_vc;
=head2 $vc->clocks
ok $vc_b == $serialized_b;
ok $vc_b eq $serialized_b;
ok !($vc_b != $serialized_b);
ok !($vc_b ne $serialized_b);
### in server C ###
Algorithm::VectorClocks->id('C');
ok !$vc_b->equal($serialized_a);
ok $vc_b->not_equal($serialized_a);
$vc_c += $serialized_b;
is_deeply $vc_c->clocks, { A => 1, B => 1 };
$vc_c++;
is_deeply $vc_c->clocks, { A => 1, B => 1, C => 1 };
my $serialized_c = "$vc_c";
### in client ###
( run in 1.163 second using v1.01-cache-2.11-cpan-cc502c75498 )