| 1 | package Thesaurus::BerkeleyDB; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Thesaurus; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base 'Thesaurus'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use BerkeleyDB qw( DB_CREATE DB_INIT_MPOOL DB_INIT_CDB DB_DUP |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | DB_WRITECURSOR DB_NOTFOUND DB_SET DB_KEYLAST | ||
| 10 | 0 | DB_NEXT_DUP ); | |
| 11 | |||
| 12 | |||
| 13 | use File::Basename; | ||
| 14 | use Params::Validate qw( validate SCALAR BOOLEAN ); | ||
| 15 | use Storable; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my $class = shift; | ||
| 20 | |||
| 21 | return $class->SUPER::new(@_); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _init | ||
| 25 | { | ||
| 26 | my $self = shift; | ||
| 27 | my %p = validate( @_, | ||
| 28 | { filename => { type => SCALAR }, | ||
| 29 | locking => { type => BOOLEAN, default => 0 }, | ||
| 30 | mode => { type => SCALAR, default => 0644 }, | ||
| 31 | }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | my $flags = DB_CREATE | DB_INIT_MPOOL; | ||
| 35 | $flags |= DB_INIT_CDB if $p{locking}; | ||
| 36 | |||
| 37 | my $env = | ||
| 38 | BerkeleyDB::Env->new | ||
| 39 | ( -Flags => $flags, | ||
| 40 | -Home => dirname( $p{filename} ), | ||
| 41 | ) | ||
| 42 | or die "Cannot create BerkeleyDB::Env: $BerkeleyDB::Error"; | ||
| 43 | |||
| 44 | my %db; | ||
| 45 | tie %db, 'BerkeleyDB::Hash', ( -Env => $env, | ||
| 46 | -Filename => $p{filename}, | ||
| 47 | -Flags => DB_CREATE, | ||
| 48 | -Property => DB_DUP | ||
| 49 | -Mode => $p{mode}, | ||
| 50 | ); | ||
| 51 | |||
| 52 | die "Cannot tie to BerkeleyDB::Hash: $BerkeleyDB::Error" | ||
| 53 | unless tied %db; | ||
| 54 | |||
| 55 | $self->{hash} = \%db; | ||
| 56 | $self->{db} = tied %db; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _add_list | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | my $list = shift; | ||
| 63 | |||
| 64 | my %items = $self->_hash_from_list($list); | ||
| 65 | |||
| 66 | my $cursor = $self->{db}->db_cursor( DB_WRITECURSOR ) | ||
| 67 | or die "cannot make db cursor: $BerkeleyDB::Error"; | ||
| 68 | |||
| 69 | foreach my $k ( keys %items ) | ||
| 70 | { | ||
| 71 | my $val; | ||
| 72 | my $status = $cursor->c_get( $k, $val, DB_SET ); | ||
| 73 | |||
| 74 | die "c_get failed for key $k: $status" | ||
| 75 | if $status && $status != DB_NOTFOUND; | ||
| 76 | |||
| 77 | if ( $status != DB_NOTFOUND ) | ||
| 78 | { | ||
| 79 | $status = $cursor->c_del; | ||
| 80 | |||
| 81 | die "c_del failed for key $k: $status" if $status; | ||
| 82 | } | ||
| 83 | |||
| 84 | foreach my $v ( @{ $items{$k} } ) | ||
| 85 | { | ||
| 86 | my $status = $cursor->c_put( $k, Storable::nfreeze(\$v), DB_KEYLAST ); | ||
| 87 | |||
| 88 | die "c_put failed for $k: $status" if $status; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _find | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | my $cursor = $self->{db}->db_cursor | ||
| 98 | or die "cannot make db cursor: $BerkeleyDB::Error"; | ||
| 99 | |||
| 100 | my %lists; | ||
| 101 | foreach my $key (@_) | ||
| 102 | { | ||
| 103 | my $search_key = $self->{params}{ignore_case} ? lc $key : $key; | ||
| 104 | |||
| 105 | # ignore duplicates | ||
| 106 | next if $lists{$key}; | ||
| 107 | |||
| 108 | my $value; | ||
| 109 | my $status = $cursor->c_get( $search_key, $value, DB_SET ); | ||
| 110 | |||
| 111 | if ($status) | ||
| 112 | { | ||
| 113 | if ( $status == DB_NOTFOUND ) | ||
| 114 | { | ||
| 115 | $lists{$key} = []; | ||
| 116 | next; | ||
| 117 | } | ||
| 118 | |||
| 119 | die "c_get failed for $search_key: $status"; | ||
| 120 | } | ||
| 121 | |||
| 122 | push @{ $lists{$key} }, ${ Storable::thaw($value) }; | ||
| 123 | |||
| 124 | while (1) | ||
| 125 | { | ||
| 126 | $status = $cursor->c_get( $search_key, $value, DB_NEXT_DUP ); | ||
| 127 | |||
| 128 | if ($status) | ||
| 129 | { | ||
| 130 | last if $status == DB_NOTFOUND; | ||
| 131 | |||
| 132 | die "c_get failed for $search_key: $status"; | ||
| 133 | } | ||
| 134 | |||
| 135 | push @{ $lists{$key} }, ${ Storable::thaw($value) }; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | return \%lists; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub delete | ||
| 144 | { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | |||
| 148 | my $cursor = $self->{db}->db_cursor( DB_WRITECURSOR ) | ||
| 149 | or die "cannot make db cursor: $BerkeleyDB::Error"; | ||
| 150 | |||
| 151 | foreach my $item (@_) | ||
| 152 | { | ||
| 153 | foreach my $key ( $self->find($item) ) | ||
| 154 | { | ||
| 155 | # needs to be called multiple times to delete all duplicates | ||
| 156 | while (1) | ||
| 157 | { | ||
| 158 | my $val; | ||
| 159 | my $status = $cursor->c_get( $key, $val, DB_SET ); | ||
| 160 | |||
| 161 | die "c_get failed for key $key: $status" | ||
| 162 | if $status && $status != DB_NOTFOUND; | ||
| 163 | |||
| 164 | last if $status == DB_NOTFOUND; | ||
| 165 | |||
| 166 | $status = $cursor->c_del; | ||
| 167 | |||
| 168 | die "c_del failed for key $key: $status" if $status; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | |||
| 176 | __END__ | ||