1 package Onis::Data::Persistent::Gdbm;
6 use Carp qw(carp confess);
9 use Onis::Config (qw(get_config));
13 Onis::Data::Persistent::Gdbm - Storage backend using GDBM_File.
17 Storage backend that uses GDBM files for storing data permanently.
19 =head1 CONFIGURATION OPTIONS
23 =item B<gdbm_directory>: I<E<lt>dirE<gt>>
25 Directory in which the GDBM-files are kept.
33 our $GDBMDirectory = get_config ('gdbm_directory') || 'var';
34 $GDBMDirectory =~ s#/$##g;
36 if (!$GDBMDirectory or !-d $GDBMDirectory)
39 The directory ``$GDBMDirectory'' does not exist or is not useable. Please
40 create it before running onis.
47 if ($::DEBUG & 0x0200)
60 my $caller = caller ();
66 my $id = $caller . ':' . $name;
68 $filename = "$GDBMDirectory/$id.gdbm";
70 if (exists ($Tables{$id}))
72 print STDERR $/, __FILE__, ": Name $name has been used in context $caller before.";
76 $Tables{$id} = tie (%hash, 'GDBM_File', $filename, &GDBM_WRCREAT, 0664);
78 $obj->{'data'} = $Tables{$id};
80 $obj->{'fields'} = [@fields];
81 $obj->{'num_fields'} = scalar (@fields);
82 $obj->{'field_index'} = {map { $_ => $i++ } (@fields)};
85 if ($::DEBUG & 0x0200)
87 my $prefix = __FILE__ . ': ';
88 my $dbg = Data::Dumper->Dump ([$obj], ['obj']);
89 $dbg =~ s/^/$prefix/mg; chomp ($dbg);
90 print STDOUT $/, $dbg;
93 return (bless ($obj, $pkg));
102 if ($obj->{'num_fields'} != scalar (@fields))
104 my $id = $obj->{'id'};
105 carp ("Number of fields do not match ($id).");
109 if ($::DEBUG & 0x0200)
111 print STDOUT $/, __FILE__, ': PUT(', $obj->{'id'}, ', ', $key, ') = (' . join (', ', @fields) . ')';
114 $obj->{'data'}{$key} = join ($Alarm, @fields);
123 if (!exists ($obj->{'data'}{$key}))
128 @ret = split ($Alarm, $obj->{'data'}{$key});
130 if ($::DEBUG & 0x0200)
132 print STDOUT $/, __FILE__, ': GET(', $obj->{'id'}, ', ', $key, ') = (' . join (', ', @ret) . ')';
142 my @field_indizes = ();
143 my @keys = keys %{$obj->{'data'}};
153 $data->{$_} = [split ($Alarm, $obj->{'data'}{$_})];
159 if (!defined ($obj->{'field_index'}{$field}))
161 my $id = $obj->{'id'};
162 print STDERR $/, __FILE__, ": $field is not a valid field ($id).";
165 push (@field_indizes, $obj->{'field_index'}{$field});
172 my $d = $data->{$a}[$_] cmp $data->{$b}[$_];
183 if (exists ($obj->{'data'}{$key}))
185 delete ($obj->{'data'}{$key});
194 untie (%{$Tables{$key}});
200 Florian octo Forster, L<octo at verplant.org>