File Coverage

blib/lib/TM/ResourceAble/MLDBM.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package TM::ResourceAble::MLDBM;
2              
3 2     2   13557 use TM;
  2         7  
  2         171  
4 2     2   21 use base qw (TM);
  2         4  
  2         294  
5 2     2   2571 use Class::Trait qw(TM::ResourceAble);
  0            
  0            
6              
7             use Data::Dumper;
8              
9             use BerkeleyDB ;
10             use MLDBM qw(BerkeleyDB::Hash Storable) ;
11             #use MLDBM qw(BerkeleyDB::Hash Data::Dumper) ;
12             #use MLDBM qw(BerkeleyDB::Hash Data::Dumper) ;
13             use Fcntl;
14              
15             =pod
16              
17             =head1 NAME
18              
19             TM::ResourceAble::MLDBM - Topic Maps, DBM Storage (synchronous)
20              
21             =head1 SYNOPSIS
22              
23             use TM::ResourceAble::MLDBM;
24             {
25             my $tm = new TM::ResourceAble::MLDBM (file => '/tmp/map.dbm');
26             # modify the map here.....
27              
28             } # it goes out of scope here, and all changes are written back automagically
29              
30             # later in the game
31             {
32             my $tm = new TM::ResourceAble::MLDBM (file => '/tmp/map.dbm');
33             # we are back in business, no sync necessary
34             }
35              
36             =head1 DESCRIPTION
37              
38             This package just implements L with a BerkeleyDB store. Unlike L this
39             module does not need explicit synchronisation with the external resource (the DBM file here). It
40             ties content-wise with the DBM file at constructor time and unties at DESTROY time.
41              
42             This implementation technique is not so memory-efficient as I had thought. Whenever an assertion
43             or a toplet is referenced, the whole block of toplets, resp. assertions, is loaded from the DB database.
44             For small maps this is really fast, but it can become a drag for larger maps. See L
45             for a more efficient solution.
46              
47             B: Be careful to use this together with L. The indices will be held as part of the
48             map, and so will be stored along side. If you heavily use the map, this can result in many swapin/swapouts.
49             Better to look at L for that matter.
50              
51             =head1 INTERFACE
52              
53             =head2 Constructor
54              
55             The constructor expects a hash with the following keys:
56              
57             =over
58              
59             =item B (no default)
60              
61             This contains the file name of the DBM file to tie to.
62              
63             =back
64              
65             =cut
66              
67             sub new {
68             my $class = shift;
69             my %options = @_;
70              
71             my $file = delete $options{file} or die $TM::log->logdie ("no file specified");
72             my $whatever = $class->SUPER::new (%options, url => 'file:'.$file); # this ensures that we have a proper url component
73              
74             my %self; # forget about the object itself, make a new one
75              
76             #warn "file exists $file?";
77             if (-e $file && -s $file) { # file does exist already (and is not empty)
78             tie %self, 'MLDBM', -Filename => $file
79             or $TM::log->logdie ( "Cannot create DBM file '$file: $!");
80             # oh, we are done now
81             } else { # no file yet
82             #warn "file not exists $file!";
83             tie %self, 'MLDBM', -Filename => $file, # bind to one
84             -Flags => DB_CREATE # which we create here
85             or $TM::log->logdie ( "Cannot create DBM file '$file: $!");
86              
87             foreach (keys %$whatever) { # clone all components
88             next if /indices|rindex/; # we do not want these
89             $self{$_} = $whatever->{$_}; # this makes sure that Berkeley'ed tie picks it up
90             }
91             }
92             return bless \%self, $class; # give the reference a blessing
93             }
94              
95             sub DESTROY { # if an object went out of scope
96             my $self = shift;
97             untie %$self; # release the tie with the underlying resource
98             }
99              
100             =pod
101              
102             =head1 SEE ALSO
103              
104             L, L
105              
106             =head1 AUTHOR INFORMATION
107              
108             Copyright 200[68], Robert Barta , All rights reserved.
109              
110             This library is free software; you can redistribute it and/or modify it under the same terms as Perl
111             itself. http://www.perl.com/perl/misc/Artistic.html
112              
113             =cut
114              
115             our $VERSION = '0.03';
116              
117             1;
118              
119             __END__