File Coverage

blib/lib/Data/LUID/Table.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::LUID::Table;
2              
3 1     1   210152 use Moose;
  0            
  0            
4             use Data::LUID::Carp;
5              
6             use BerkeleyDB qw/DB_NOTFOUND/;
7             use Path::Class;
8              
9             has path => qw/is ro lazy_build 1/;
10             sub _build_path {
11             return './luid';
12             }
13              
14             has bdb_manager => qw/is ro lazy_build 1/;
15             sub _build_bdb_manager {
16             require BerkeleyDB::Manager;
17             my $self = shift;
18             my $home = dir $self->path;
19             $home->mkpath unless -d $home;
20             return BerkeleyDB::Manager->new( home => $home, create => 1 );
21             }
22              
23             sub bdb_table {
24             my $self = shift;
25             return $self->bdb_manager->open_db( 'table', class => 'BerkeleyDB::Hash' );
26             }
27              
28             has generator => qw/is rw lazy_build 1/; # initializer _initialize_generator/;
29             sub _build_generator {
30             require Data::LUID::Generator::TUID;
31             return Data::LUID::Generator::TUID->new;
32             }
33             sub _initialize_generator {
34             my ( $self, $value, $set );
35             croak "What are you doing?!";
36             }
37              
38             sub luid_key ($) {
39             return join '', 'luid:', shift;
40             }
41              
42             sub take {
43             my $self = shift;
44             my $key = shift;
45             croak "No key given to take" unless defined $key;
46             $self->store( luid_key $key );
47             }
48              
49             sub taken {
50             my $self = shift;
51             my $key = shift;
52             croak "No key given to check if taken" unless defined $key;
53             return $self->exists( luid_key $key )
54             }
55              
56             sub make {
57             my $self = shift;
58             # TODO Add throttle
59             $self->bdb_manager->txn_do( sub {
60             while( 1 ) {
61             my $key = $self->generator->next;
62             croak "Got undefined value from luid generator ", $self->generator unless defined $key;
63             next if $self->taken( $key );
64             $self->take( $key );
65             return $key;
66             }
67             } );
68             }
69              
70             sub next {
71             my $self = shift;
72             return $self->make;
73             }
74              
75             sub store {
76             my $self = shift;
77             my $key = shift;
78             my $value = shift || 1;
79              
80             my $status = $self->bdb_table->db_put( $key, $value );
81             croak "Problem storing \"$key\" => \"$value\": $status" if $status;
82             return $value;
83             }
84              
85             sub exists {
86             my $self = shift;
87             my $key = shift;
88              
89             my $value;
90              
91             my $status = $self->bdb_table->db_get( $key, $value );
92             return 0 if $status == DB_NOTFOUND;
93             return 1 unless $status;
94             croak "Problem checking existence of \"$key\": $status";
95             }
96              
97             sub delete {
98             my $self = shift;
99             my $key = shift;
100              
101             my $status = $self->bdb_table->db_del( $key );
102             return if $status == DB_NOTFOUND;
103             croak "Problem deleting \"$key\": $status" if $status;
104             }
105              
106             1;