| 1 | package Data::Localize::Storage::BerkeleyDB; | ||
| 2 | use Moo; | ||
| 3 | use BerkeleyDB; | ||
| 4 | use Carp (); | ||
| 5 | use Encode (); | ||
| 6 | use File::Spec (); | ||
| 7 | use File::Temp (); | ||
| 8 | use Data::Localize (); | ||
| 9 | BEGIN { | ||
| 10 | if (Data::Localize::DEBUG) { | ||
| 11 | require Data::Localize::Log; | ||
| 12 | Data::Localize::Log->import; | ||
| 13 | } | ||
| 14 | } | ||
| 15 | with 'Data::Localize::Storage'; | ||
| 16 | |||
| 17 | has '_db' => ( | ||
| 18 | is => 'rw', | ||
| 19 | init_arg => 'db', | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'store_as_refs' => ( | ||
| 23 | is => 'ro', | ||
| 24 | default => sub { 0 }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub is_volatile { 0 } | ||
| 28 | |||
| 29 | sub BUILD { | ||
| 30 | my ($self, $args) = @_; | ||
| 31 | if (! $self->_db) { | ||
| 32 | my $class = $args->{bdb_class} || 'Hash'; | ||
| 33 | if ($class !~ s/^\+//) { | ||
| 34 | $class = "BerkeleyDB::$class"; | ||
| 35 | } | ||
| 36 | Module::Load::load($class); | ||
| 37 | |||
| 38 | my $dir = ($args->{dir} ||= File::Temp::tempdir(CLEANUP => 1)); | ||
| 39 | $args->{bdb_args} ||= { | ||
| 40 | -Filename => File::Spec->catfile($dir, $self->lang), | ||
| 41 | -Flags => BerkeleyDB::DB_CREATE(), | ||
| 42 | }; | ||
| 43 | |||
| 44 | if (Data::Localize::DEBUG) { | ||
| 45 | local $Log::Minimal::AUTODUMP = 1; | ||
| 46 | debugf("Storage::BerkeleyDB Automatically building storage db with class = %s, args = %s", $class, $args->{bdb_args}); | ||
| 47 | } | ||
| 48 | $self->_db( $class->new( $args->{bdb_args} || {} ) || | ||
| 49 | Carp::confess("Failed to create $class: $BerkeleyDB::Error") | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | |||
| 53 | if ( $self->store_as_refs ) { | ||
| 54 | require Storable; | ||
| 55 | } | ||
| 56 | |||
| 57 | $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub get { | ||
| 61 | my ($self, $key, $flags) = @_; | ||
| 62 | my $value; | ||
| 63 | my $rc = $self->_db->db_get($key, $value, $flags || 0); | ||
| 64 | if ($rc == 0) { | ||
| 65 | if ( $self->store_as_refs ) { | ||
| 66 | # Storeable handles utf8 correctly | ||
| 67 | my $thawed = Storable::thaw( $value ); | ||
| 68 | return $thawed->{'__' . __PACKAGE__ . '::key__'} | ||
| 69 | if exists $thawed->{'__' . __PACKAGE__ . '::key__'}; | ||
| 70 | return $thawed; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | # BerkeleyDB gives us values with the flags off, so put them back on | ||
| 74 | return Encode::decode_utf8($value); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | return (); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub set { | ||
| 81 | my ($self, $key, $value, $flags) = @_; | ||
| 82 | |||
| 83 | if (Data::Localize::DEBUG) { | ||
| 84 | debugf("Storage::BerkeleyDB: Set %s -> %s", $key, $value); | ||
| 85 | } | ||
| 86 | |||
| 87 | if ( $self->store_as_refs ) { | ||
| 88 | unless ( ref $value ) { | ||
| 89 | $value = { ('__' . __PACKAGE__ . '::key__') => $value }; | ||
| 90 | } | ||
| 91 | $value = Storable::freeze( $value ); | ||
| 92 | } | ||
| 93 | |||
| 94 | my $rc = $self->_db->db_put($key, $value, $flags || 0); | ||
| 95 | if ($rc != 0) { | ||
| 96 | Carp::confess("Failed to set value $key"); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ |