1 package Onis::Data::Persistent::Dbm;
8 @AnyDBM_File::ISA = (qw(DB_File GDBM_File SDBM_File NDBM_File ODBM_File));
11 use Carp qw(carp confess);
12 use Fcntl (qw(O_RDWR O_CREAT));
15 use Onis::Config (qw(get_config));
19 Onis::Data::Persistent::Dbm - Storage backend using AnyDBM_File.
23 Storage backend that uses DBM files for storing data permanently.
25 =head1 CONFIGURATION OPTIONS
29 =item B<dbm_directory>: I<E<lt>dirE<gt>>
31 Directory in which the DBM-files are kept. Defaults to the B<var>-directory in
32 onis' main directory..
38 our $DBMDirectory = 'var';
39 if (get_config ('storage_dir'))
41 $DBMDirectory = get_config ('storage_dir');
45 $DBMDirectory = $ENV{'HOME'} . '/.onis/data';
47 $DBMDirectory =~ s#/+$##g;
49 if (!$DBMDirectory or !-d $DBMDirectory)
53 The directory ``$DBMDirectory'' does not exist or is not useable. Please
54 create it before running onis.
62 if ($::DEBUG & 0x0200)
75 my $caller = caller ();
81 my $id = $caller . ':' . $name;
84 $filename = "$DBMDirectory/$id.dbm";
86 if (exists ($Objects{$id}))
88 print STDERR $/, __FILE__, ": Name $name has been used in context $caller before.";
93 tie (%hash, 'AnyDBM_File', $filename, O_RDWR | O_CREAT, 0666) or die ("tie: $!");
95 $obj->{'data'} = tied %hash;
97 $obj->{'fields'} = [@fields];
98 $obj->{'num_fields'} = scalar (@fields);
99 $obj->{'field_index'} = {map { $_ => $i++ } (@fields)};
101 $obj->{'cache'} = {};
103 if ($::DEBUG & 0x0200)
105 my $prefix = __FILE__ . ': ';
106 my $dbg = Data::Dumper->Dump ([$obj], ['obj']);
107 $dbg =~ s/^/$prefix/mg; chomp ($dbg);
108 print STDOUT $/, $dbg;
111 $Objects{$id} = bless ($obj, $pkg);
112 return ($Objects{$id});
121 if ($obj->{'num_fields'} != scalar (@fields))
123 my $id = $obj->{'id'};
124 carp ("Number of fields do not match ($id).");
128 if ($::DEBUG & 0x0200)
130 print STDOUT $/, __FILE__, ': PUT(', $obj->{'id'}, ', ', $key, ') = (' . join (', ', @fields) . ')';
133 $obj->{'cache'}{$key} = [@fields];
142 my $db = $obj->{'data'};
144 if (!exists ($obj->{'cache'}{$key}))
146 $val = $db->FETCH ($key);
149 $obj->{'cache'}{$key} = undef;
153 $obj->{'cache'}{$key} = [split ($Alarm, $val)];
157 if (!defined ($obj->{'cache'}{$key}))
163 @ret = @{$obj->{'cache'}{$key}};
166 if ($::DEBUG & 0x0200)
168 print STDOUT $/, __FILE__, ': GET(', $obj->{'id'}, ', ', $key, ') = (' . join (', ', @ret) . ')';
178 my @field_indizes = ();
179 my $db = $obj->{'data'};
183 no strict (qw(subs));
184 for (($key, $val) = $db->FIRSTKEY (); defined ($key) and defined ($val); ($key, $val) = $db->NEXTKEY ($key))
186 next if (defined ($obj->{'cache'}{$key}));
188 $obj->{'cache'}{$key} = [split ($Alarm, $val)];
193 return (keys %{$obj->{'cache'}});
199 if (!defined ($obj->{'field_index'}{$field}))
201 my $id = $obj->{'id'};
202 print STDERR $/, __FILE__, ": $field is not a valid field ($id).";
205 push (@field_indizes, $obj->{'field_index'}{$field});
212 my $d = $obj->{'cache'}{$a}[$_] cmp $obj->{'cache'}{$b}[$_];
215 } (keys %{$obj->{'cache'}}));
222 my $db = $obj->{'data'};
224 if (exists ($obj->{'cache'}{$key}))
226 if (defined ($obj->{'cache'}{$key}))
229 $obj->{'cache'}{$key} = undef;
231 # It's known that the key doesn't exist..
236 $obj->{'cache'}{$key} = undef;
243 my $db = $obj->{'data'};
245 for (CORE::keys %{$obj->{'cache'}})
248 next unless (defined ($obj->{'cache'}{$key}));
250 my $val = join ($Alarm, @{$obj->{'cache'}{$key}});
252 $db->STORE ($key, $val);
253 delete ($obj->{'cache'}{$key});
261 for (CORE::keys (%Objects))
264 my $obj = $Objects{$key};
271 Florian octo Forster, L<octo at verplant.org>