3 DBM::Deep - A pure perl multi-level hash/array DBM that supports transactions
8 my $db = DBM::Deep->new( "foo.db" );
13 $db->put('key' => 'value');
14 print $db->get('key');
16 # true multi-level support
18 'hello', { perl => 'rules' },
29 tie my %db, 'DBM::Deep', 'foo.db';
33 tied(%db)->put('key' => 'value');
34 print tied(%db)->get('key');
38 A unique flat-file database module, written in pure perl. True multi-level
39 hash/array support (unlike MLDBM, which is faked), hybrid OO / tie()
40 interface, cross-platform FTPable files, ACID transactions, and is quite fast.
41 Can handle millions of keys and unlimited levels without significant
42 slow-down. Written from the ground-up in pure perl -- this is NOT a wrapper
43 around a C-based DBM. Out-of-the-box compatibility with Unix, Mac OS X and
46 =head1 VERSION DIFFERENCES
48 B<NOTE>: 1.0000 has significant file format differences from prior versions.
49 THere is a backwards-compatibility layer at C<utils/upgrade_db.pl>. Files
50 created by 1.0000 or higher are B<NOT> compatible with scripts using prior
55 Construction can be done OO-style (which is the recommended way), or using
56 Perl's tie() function. Both are examined here.
58 =head2 OO Construction
60 The recommended way to construct a DBM::Deep object is to use the new()
61 method, which gets you a blessed I<and> tied hash (or array) reference.
63 my $db = DBM::Deep->new( "foo.db" );
65 This opens a new database handle, mapped to the file "foo.db". If this
66 file does not exist, it will automatically be created. DB files are
67 opened in "r+" (read/write) mode, and the type of object returned is a
68 hash, unless otherwise specified (see L<OPTIONS> below).
70 You can pass a number of options to the constructor to specify things like
71 locking, autoflush, etc. This is done by passing an inline hash (or hashref):
73 my $db = DBM::Deep->new(
79 Notice that the filename is now specified I<inside> the hash with
80 the "file" parameter, as opposed to being the sole argument to the
81 constructor. This is required if any options are specified.
82 See L<OPTIONS> below for the complete list.
84 You can also start with an array instead of a hash. For this, you must
85 specify the C<type> parameter:
87 my $db = DBM::Deep->new(
89 type => DBM::Deep->TYPE_ARRAY
92 B<Note:> Specifing the C<type> parameter only takes effect when beginning
93 a new DB file. If you create a DBM::Deep object with an existing file, the
94 C<type> will be loaded from the file header, and an error will be thrown if
95 the wrong type is passed in.
97 =head2 Tie Construction
99 Alternately, you can create a DBM::Deep handle by using Perl's built-in
100 tie() function. The object returned from tie() can be used to call methods,
101 such as lock() and unlock(). (That object can be retrieved from the tied
102 variable at any time using tied() - please see L<perltie> for more info.
105 my $db = tie %hash, "DBM::Deep", "foo.db";
108 my $db = tie @array, "DBM::Deep", "bar.db";
110 As with the OO constructor, you can replace the DB filename parameter with
111 a hash containing one or more options (see L<OPTIONS> just below for the
114 tie %hash, "DBM::Deep", {
122 There are a number of options that can be passed in when constructing your
123 DBM::Deep objects. These apply to both the OO- and tie- based approaches.
129 Filename of the DB file to link the handle to. You can pass a full absolute
130 filesystem path, partial path, or a plain filename if the file is in the
131 current working directory. This is a required parameter (though q.v. fh).
135 If you want, you can pass in the fh instead of the file. This is most useful for doing
138 my $db = DBM::Deep->new( { fh => \*DATA } );
140 You are responsible for making sure that the fh has been opened appropriately for your
141 needs. If you open it read-only and attempt to write, an exception will be thrown. If you
142 open it write-only or append-only, an exception will be thrown immediately as DBM::Deep
143 needs to read from the fh.
147 This is the offset within the file that the DBM::Deep db starts. Most of the time, you will
148 not need to set this. However, it's there if you want it.
150 If you pass in fh and do not set this, it will be set appropriately.
154 This parameter specifies what type of object to create, a hash or array. Use
155 one of these two constants:
159 =item * C<DBM::Deep-E<gt>TYPE_HASH>
161 =item * C<DBM::Deep-E<gt>TYPE_ARRAY>.
165 This only takes effect when beginning a new file. This is an optional
166 parameter, and defaults to C<DBM::Deep-E<gt>TYPE_HASH>.
170 Specifies whether locking is to be enabled. DBM::Deep uses Perl's flock()
171 function to lock the database in exclusive mode for writes, and shared mode
172 for reads. Pass any true value to enable. This affects the base DB handle
173 I<and any child hashes or arrays> that use the same DB file. This is an
174 optional parameter, and defaults to 1 (enabled). See L<LOCKING> below for
179 Specifies whether autoflush is to be enabled on the underlying filehandle.
180 This obviously slows down write operations, but is required if you may have
181 multiple processes accessing the same DB file (also consider enable I<locking>).
182 Pass any true value to enable. This is an optional parameter, and defaults to 1
187 See L</FILTERS> below.
191 The following parameters may be specified in the constructor the first time the
192 datafile is created. However, they will be stored in the header of the file and
193 cannot be overridden by subsequent openings of the file - the values will be set
194 from the values stored in the datafile's header.
200 This is the number of transactions that can be running at one time. The
201 default is one - the HEAD. The minimum is one and the maximum is 255. The more
202 transactions, the larger and quicker the datafile grows.
204 See L</TRANSACTIONS> below.
208 This is the number of entries that can be added before a reindexing. The larger
209 this number is made, the larger a file gets, but the better performance you will
210 have. The default and minimum number this can be is 16. The maximum is 256, but
211 more than 64 isn't recommended.
213 =item * data_sector_size
215 This is the size in bytes of a given data sector. Data sectors will chain, so
216 a value of any size can be stored. However, chaining is expensive in terms of
217 time. Setting this value to something close to the expected common length of
218 your scalars will improve your performance. If it is too small, your file will
219 have a lot of chaining. If it is too large, your file will have a lot of dead
222 The default for this is 64 bytes. The minimum value is 32 and the maximum is
225 B<Note:> There are between 6 and 10 bytes taken up in each data sector for
226 bookkeeping. (It's 4 + the number of bytes in your L</pack_size>.) This is
227 included within the data_sector_size, thus the effective value is 6-10 bytes
228 less than what you specified.
232 This is the size of the file pointer used throughout the file. The valid values
239 This uses 2-byte offsets, allowing for a maximum file size of 65 KB.
241 =item * medium (default)
243 This uses 4-byte offsets, allowing for a maximum file size of 4 GB.
247 This uses 8-byte offsets, allowing for a maximum file size of 16 XB
248 (exabytes). This can only be enabled if your Perl is compiled for 64-bit.
252 See L</LARGEFILE SUPPORT> for more information.
258 With DBM::Deep you can access your databases using Perl's standard hash/array
259 syntax. Because all DBM::Deep objects are I<tied> to hashes or arrays, you can
260 treat them as such. DBM::Deep will intercept all reads/writes and direct them
261 to the right place -- the DB file. This has nothing to do with the
262 L<TIE CONSTRUCTION> section above. This simply tells you how to use DBM::Deep
263 using regular hashes and arrays, rather than calling functions like C<get()>
264 and C<put()> (although those work too). It is entirely up to you how to want
265 to access your databases.
269 You can treat any DBM::Deep object like a normal Perl hash reference. Add keys,
270 or even nested hashes (or arrays) using standard Perl syntax:
272 my $db = DBM::Deep->new( "foo.db" );
274 $db->{mykey} = "myvalue";
276 $db->{myhash}->{subkey} = "subvalue";
278 print $db->{myhash}->{subkey} . "\n";
280 You can even step through hash keys using the normal Perl C<keys()> function:
282 foreach my $key (keys %$db) {
283 print "$key: " . $db->{$key} . "\n";
286 Remember that Perl's C<keys()> function extracts I<every> key from the hash and
287 pushes them onto an array, all before the loop even begins. If you have an
288 extremely large hash, this may exhaust Perl's memory. Instead, consider using
289 Perl's C<each()> function, which pulls keys/values one at a time, using very
292 while (my ($key, $value) = each %$db) {
293 print "$key: $value\n";
296 Please note that when using C<each()>, you should always pass a direct
297 hash reference, not a lookup. Meaning, you should B<never> do this:
300 while (my ($key, $value) = each %{$db->{foo}}) { # BAD
302 This causes an infinite loop, because for each iteration, Perl is calling
303 FETCH() on the $db handle, resulting in a "new" hash for foo every time, so
304 it effectively keeps returning the first key over and over again. Instead,
305 assign a temporary variable to C<$db->{foo}>, then pass that to each().
309 As with hashes, you can treat any DBM::Deep object like a normal Perl array
310 reference. This includes inserting, removing and manipulating elements,
311 and the C<push()>, C<pop()>, C<shift()>, C<unshift()> and C<splice()> functions.
312 The object must have first been created using type C<DBM::Deep-E<gt>TYPE_ARRAY>,
313 or simply be a nested array reference inside a hash. Example:
315 my $db = DBM::Deep->new(
316 file => "foo-array.db",
317 type => DBM::Deep->TYPE_ARRAY
321 push @$db, "bar", "baz";
324 my $last_elem = pop @$db; # baz
325 my $first_elem = shift @$db; # bah
326 my $second_elem = $db->[1]; # bar
328 my $num_elements = scalar @$db;
332 In addition to the I<tie()> interface, you can also use a standard OO interface
333 to manipulate all aspects of DBM::Deep databases. Each type of object (hash or
334 array) has its own methods, but both types share the following common methods:
335 C<put()>, C<get()>, C<exists()>, C<delete()> and C<clear()>. C<fetch()> and
336 C<store(> are aliases to C<put()> and C<get()>, respectively.
340 =item * new() / clone()
342 These are the constructor and copy-functions.
344 =item * put() / store()
346 Stores a new hash key/value pair, or sets an array element value. Takes two
347 arguments, the hash key or array index, and the new value. The value can be
348 a scalar, hash ref or array ref. Returns true on success, false on failure.
350 $db->put("foo", "bar"); # for hashes
351 $db->put(1, "bar"); # for arrays
353 =item * get() / fetch()
355 Fetches the value of a hash key or array element. Takes one argument: the hash
356 key or array index. Returns a scalar, hash ref or array ref, depending on the
359 my $value = $db->get("foo"); # for hashes
360 my $value = $db->get(1); # for arrays
364 Checks if a hash key or array index exists. Takes one argument: the hash key
365 or array index. Returns true if it exists, false if not.
367 if ($db->exists("foo")) { print "yay!\n"; } # for hashes
368 if ($db->exists(1)) { print "yay!\n"; } # for arrays
372 Deletes one hash key/value pair or array element. Takes one argument: the hash
373 key or array index. Returns true on success, false if not found. For arrays,
374 the remaining elements located after the deleted element are NOT moved over.
375 The deleted element is essentially just undefined, which is exactly how Perl's
376 internal arrays work.
378 $db->delete("foo"); # for hashes
379 $db->delete(1); # for arrays
383 Deletes B<all> hash keys or array elements. Takes no arguments. No return
386 $db->clear(); # hashes or arrays
388 =item * lock_exclusive() / lock_shared() / lock() / unlock()
390 q.v. L</LOCKING> for more info.
394 This will compress the datafile so that it takes up as little space as possible.
395 There is a freespace manager so that when space is freed up, it is used before
396 extending the size of the datafile. But, that freespace just sits in the datafile
397 unless C<optimize()> is called.
401 Unlike simple assignment, C<import()> does not tie the right-hand side. Instead,
402 a copy of your data is put into the DB. C<import()> takes either an arrayref (if
403 your DB is an array) or a hashref (if your DB is a hash). C<import()> will die
404 if anything else is passed in.
408 This returns a complete copy of the data structure at the point you do the export.
409 This copy is in RAM, not on disk like the DB is.
411 =item * begin_work() / commit() / rollback()
413 These are the transactional functions. L</TRANSACTIONS> for more information.
419 For hashes, DBM::Deep supports all the common methods described above, and the
420 following additional methods: C<first_key()> and C<next_key()>.
426 Returns the "first" key in the hash. As with built-in Perl hashes, keys are
427 fetched in an undefined order (which appears random). Takes no arguments,
428 returns the key as a scalar value.
430 my $key = $db->first_key();
434 Returns the "next" key in the hash, given the previous one as the sole argument.
435 Returns undef if there are no more keys to be fetched.
437 $key = $db->next_key($key);
441 Here are some examples of using hashes:
443 my $db = DBM::Deep->new( "foo.db" );
445 $db->put("foo", "bar");
446 print "foo: " . $db->get("foo") . "\n";
448 $db->put("baz", {}); # new child hash ref
449 $db->get("baz")->put("buz", "biz");
450 print "buz: " . $db->get("baz")->get("buz") . "\n";
452 my $key = $db->first_key();
454 print "$key: " . $db->get($key) . "\n";
455 $key = $db->next_key($key);
458 if ($db->exists("foo")) { $db->delete("foo"); }
462 For arrays, DBM::Deep supports all the common methods described above, and the
463 following additional methods: C<length()>, C<push()>, C<pop()>, C<shift()>,
464 C<unshift()> and C<splice()>.
470 Returns the number of elements in the array. Takes no arguments.
472 my $len = $db->length();
476 Adds one or more elements onto the end of the array. Accepts scalars, hash
477 refs or array refs. No return value.
479 $db->push("foo", "bar", {});
483 Fetches the last element in the array, and deletes it. Takes no arguments.
484 Returns undef if array is empty. Returns the element value.
486 my $elem = $db->pop();
490 Fetches the first element in the array, deletes it, then shifts all the
491 remaining elements over to take up the space. Returns the element value. This
492 method is not recommended with large arrays -- see L<LARGE ARRAYS> below for
495 my $elem = $db->shift();
499 Inserts one or more elements onto the beginning of the array, shifting all
500 existing elements over to make room. Accepts scalars, hash refs or array refs.
501 No return value. This method is not recommended with large arrays -- see
502 <LARGE ARRAYS> below for details.
504 $db->unshift("foo", "bar", {});
508 Performs exactly like Perl's built-in function of the same name. See L<perldoc
509 -f splice> for usage -- it is too complicated to document here. This method is
510 not recommended with large arrays -- see L<LARGE ARRAYS> below for details.
514 Here are some examples of using arrays:
516 my $db = DBM::Deep->new(
518 type => DBM::Deep->TYPE_ARRAY
521 $db->push("bar", "baz");
525 my $len = $db->length();
526 print "length: $len\n"; # 4
528 for (my $k=0; $k<$len; $k++) {
529 print "$k: " . $db->get($k) . "\n";
532 $db->splice(1, 2, "biz", "baf");
534 while (my $elem = shift @$db) {
535 print "shifted: $elem\n";
540 Enable or disable automatic file locking by passing a boolean value to the
541 C<locking> parameter when constructing your DBM::Deep object (see L<SETUP>
544 my $db = DBM::Deep->new(
549 This causes DBM::Deep to C<flock()> the underlying filehandle with exclusive
550 mode for writes, and shared mode for reads. This is required if you have
551 multiple processes accessing the same database file, to avoid file corruption.
552 Please note that C<flock()> does NOT work for files over NFS. See L<DB OVER
555 =head2 Explicit Locking
557 You can explicitly lock a database, so it remains locked for multiple
558 actions. This is done by calling the C<lock()> method, and passing an
559 optional lock mode argument (defaults to exclusive mode). This is particularly
560 useful for things like counters, where the current value needs to be fetched,
561 then incremented, then stored again.
564 my $counter = $db->get("counter");
566 $db->put("counter", $counter);
575 If you want a shared lock, you will need to call C<lock_shared()>. C<lock()> is
576 an alias to C<lock_exclusive()>.
580 Due to Win32 actually enforcing the read-only status of a shared lock, all
581 locks on Win32 and cygwin are exclusive. This is because of how autovivification
582 currently works. Hopefully, this will go away in a future release.
584 =head1 IMPORTING/EXPORTING
586 You can import existing complex structures by calling the C<import()> method,
587 and export an entire database into an in-memory structure using the C<export()>
588 method. Both are examined here.
592 Say you have an existing hash with nested hashes/arrays inside it. Instead of
593 walking the structure and adding keys/elements to the database as you go,
594 simply pass a reference to the C<import()> method. This recursively adds
595 everything to an existing DBM::Deep object for you. Here is an example:
600 array1 => [ "elem0", "elem1", "elem2" ],
602 subkey1 => "subvalue1",
603 subkey2 => "subvalue2"
607 my $db = DBM::Deep->new( "foo.db" );
608 $db->import( $struct );
610 print $db->{key1} . "\n"; # prints "value1"
612 This recursively imports the entire C<$struct> object into C<$db>, including
613 all nested hashes and arrays. If the DBM::Deep object contains exsiting data,
614 keys are merged with the existing ones, replacing if they already exist.
615 The C<import()> method can be called on any database level (not just the base
616 level), and works with both hash and array DB types.
618 B<Note:> Make sure your existing structure has no circular references in it.
619 These will cause an infinite loop when importing. There are plans to fix this
624 Calling the C<export()> method on an existing DBM::Deep object will return
625 a reference to a new in-memory copy of the database. The export is done
626 recursively, so all nested hashes/arrays are all exported to standard Perl
627 objects. Here is an example:
629 my $db = DBM::Deep->new( "foo.db" );
631 $db->{key1} = "value1";
632 $db->{key2} = "value2";
634 $db->{hash1}->{subkey1} = "subvalue1";
635 $db->{hash1}->{subkey2} = "subvalue2";
637 my $struct = $db->export();
639 print $struct->{key1} . "\n"; # prints "value1"
641 This makes a complete copy of the database in memory, and returns a reference
642 to it. The C<export()> method can be called on any database level (not just
643 the base level), and works with both hash and array DB types. Be careful of
644 large databases -- you can store a lot more data in a DBM::Deep object than an
645 in-memory Perl structure.
647 B<Note:> Make sure your database has no circular references in it.
648 These will cause an infinite loop when exporting. There are plans to fix this
653 DBM::Deep has a number of hooks where you can specify your own Perl function
654 to perform filtering on incoming or outgoing data. This is a perfect
655 way to extend the engine, and implement things like real-time compression or
656 encryption. Filtering applies to the base DB level, and all child hashes /
657 arrays. Filter hooks can be specified when your DBM::Deep object is first
658 constructed, or by calling the C<set_filter()> method at any time. There are
659 four available filter hooks.
663 This method takes two paramters - the filter type and the filter subreference.
668 =item * filter_store_key
670 This filter is called whenever a hash key is stored. It
671 is passed the incoming key, and expected to return a transformed key.
673 =item * filter_store_value
675 This filter is called whenever a hash key or array element is stored. It
676 is passed the incoming value, and expected to return a transformed value.
678 =item * filter_fetch_key
680 This filter is called whenever a hash key is fetched (i.e. via
681 C<first_key()> or C<next_key()>). It is passed the transformed key,
682 and expected to return the plain key.
684 =item * filter_fetch_value
686 This filter is called whenever a hash key or array element is fetched.
687 It is passed the transformed value, and expected to return the plain value.
691 Here are the two ways to setup a filter hook:
693 my $db = DBM::Deep->new(
695 filter_store_value => \&my_filter_store,
696 filter_fetch_value => \&my_filter_fetch
701 $db->set_filter( "filter_store_value", \&my_filter_store );
702 $db->set_filter( "filter_fetch_value", \&my_filter_fetch );
704 Your filter function will be called only when dealing with SCALAR keys or
705 values. When nested hashes and arrays are being stored/fetched, filtering
706 is bypassed. Filters are called as static functions, passed a single SCALAR
707 argument, and expected to return a single SCALAR value. If you want to
708 remove a filter, set the function reference to C<undef>:
710 $db->set_filter( "filter_store_value", undef );
714 Please read L<DBM::Deep::Manual> for examples of filters.
716 =head1 ERROR HANDLING
718 Most DBM::Deep methods return a true value for success, and call die() on
719 failure. You can wrap calls in an eval block to catch the die.
721 my $db = DBM::Deep->new( "foo.db" ); # create hash
722 eval { $db->push("foo"); }; # ILLEGAL -- push is array-only call
724 print $@; # prints error message
726 =head1 LARGEFILE SUPPORT
728 If you have a 64-bit system, and your Perl is compiled with both LARGEFILE
729 and 64-bit support, you I<may> be able to create databases larger than 4 GB.
730 DBM::Deep by default uses 32-bit file offset tags, but these can be changed
731 by specifying the 'pack_size' parameter when constructing the file.
734 filename => $filename,
735 pack_size => 'large',
738 This tells DBM::Deep to pack all file offsets with 8-byte (64-bit) quad words
739 instead of 32-bit longs. After setting these values your DB files have a
740 theoretical maximum size of 16 XB (exabytes).
742 You can also use C<pack_size =E<gt> 'small'> in order to use 16-bit file
745 B<Note:> Changing these values will B<NOT> work for existing database files.
746 Only change this for new files. Once the value has been set, it is stored in
747 the file's header and cannot be changed for the life of the file. These
748 parameters are per-file, meaning you can access 32-bit and 64-bit files, as
751 B<Note:> We have not personally tested files larger than 4 GB -- all our
752 systems have only a 32-bit Perl. However, we have received user reports that
753 this does indeed work.
755 =head1 LOW-LEVEL ACCESS
757 If you require low-level access to the underlying filehandle that DBM::Deep uses,
758 you can call the C<_fh()> method, which returns the handle:
762 This method can be called on the root level of the datbase, or any child
763 hashes or arrays. All levels share a I<root> structure, which contains things
764 like the filehandle, a reference counter, and all the options specified
765 when you created the object. You can get access to this file object by
766 calling the C<_storage()> method.
768 my $file_obj = $db->_storage();
770 This is useful for changing options after the object has already been created,
771 such as enabling/disabling locking. You can also store your own temporary user
772 data in this structure (be wary of name collision), which is then accessible from
773 any child hash or array.
775 =head1 CIRCULAR REFERENCES
777 DBM::Deep has full support for circular references. Meaning you
778 can have a nested hash key or array element that points to a parent object.
779 This relationship is stored in the DB file, and is preserved between sessions.
782 my $db = DBM::Deep->new( "foo.db" );
785 $db->{circle} = $db; # ref to self
787 print $db->{foo} . "\n"; # prints "bar"
788 print $db->{circle}->{foo} . "\n"; # prints "bar" again
790 This also works as expected with array and hash references. So, the following
793 $db->{foo} = [ 1 .. 3 ];
794 $db->{bar} = $db->{foo};
796 push @{$db->{foo}}, 42;
797 is( $db->{bar}[-1], 42 ); # Passes
799 This, however, does I<not> extend to assignments from one DB file to another.
800 So, the following will throw an error:
802 my $db1 = DBM::Deep->new( "foo.db" );
803 my $db2 = DBM::Deep->new( "bar.db" );
806 $db2->{foo} = $db1->{foo}; # dies
808 B<Note>: Passing the object to a function that recursively walks the
809 object tree (such as I<Data::Dumper> or even the built-in C<optimize()> or
810 C<export()> methods) will result in an infinite loop. This will be fixed in
811 a future release by adding singleton support.
815 As of 1.0000, DBM::Deep hass ACID transactions. Every DBM::Deep object is completely
816 transaction-ready - it is not an option you have to turn on. You do have to
817 specify how many transactions may run simultaneously (q.v. L</num_txns>).
819 Three new methods have been added to support them. They are:
825 This starts a transaction.
829 This applies the changes done within the transaction to the mainline and ends
834 This discards the changes done within the transaction to the mainline and ends
839 Transactions in DBM::Deep are done using a variant of the MVCC method, the
840 same method used by the InnoDB MySQL engine.
844 As of 1.0000, the file format has changed. Furthermore, DBM::Deep is now
845 designed to potentially change file format between point-releases, if needed to
846 support a requested feature. To aid in this, a migration script is provided
847 within the CPAN distribution called C<utils/upgrade_db.pl>.
849 B<NOTE:> This script is not installed onto your system because it carries a copy
850 of every version prior to the current version.
854 The following are items that are planned to be added in future releases. These
855 are separate from the L<CAVEATS, ISSUES & BUGS> below.
857 =head2 Sub-Transactions
859 Right now, you cannot run a transaction within a transaction. Removing this
860 restriction is technically straightforward, but the combinatorial explosion of
861 possible usecases hurts my head. If this is something you want to see
862 immediately, please submit many testcases.
866 If a client is willing to assert upon opening the file that this process will be
867 the only consumer of that datafile, then there are a number of caching
868 possibilities that can be taken advantage of. This does, however, mean that
869 DBM::Deep is more vulnerable to losing data due to unflushed changes. It also
870 means a much larger in-memory footprint. As such, it's not clear exactly how
871 this should be done. Suggestions are welcome.
875 The techniques used in DBM::Deep simply require a seekable contiguous
876 datastore. This could just as easily be a large string as a file. By using
877 substr, the STM capabilities of DBM::Deep could be used within a
878 single-process. I have no idea how I'd specify this, though. Suggestions are
881 =head2 Different contention resolution mechanisms
883 Currently, the only contention resolution mechanism is last-write-wins. This
884 is the mechanism used by most RDBMSes and should be good enough for most uses.
885 For advanced uses of STM, other contention mechanisms will be needed. If you
886 have an idea of how you'd like to see contention resolution in DBM::Deep,
889 =head1 CAVEATS, ISSUES & BUGS
891 This section describes all the known issues with DBM::Deep. These are issues
892 that are either intractable or depend on some feature within Perl working
893 exactly right. It you have found something that is not listed below, please
894 send an e-mail to L<rkinyon@cpan.org>. Likewise, if you think you know of a
895 way around one of these issues, please let me know.
899 (The following assumes a high level of Perl understanding, specifically of
900 references. Most users can safely skip this section.)
902 Currently, the only references supported are HASH and ARRAY. The other reference
903 types (SCALAR, CODE, GLOB, and REF) cannot be supported for various reasons.
909 These are things like filehandles and other sockets. They can't be supported
910 because it's completely unclear how DBM::Deep should serialize them.
914 The discussion here refers to the following type of example:
921 # In some other process ...
923 my $val = ${ $db->{key1} };
925 is( $val, 50, "What actually gets stored in the DB file?" );
927 The problem is one of synchronization. When the variable being referred to
928 changes value, the reference isn't notified, which is kind of the point of
929 references. This means that the new value won't be stored in the datafile for
930 other processes to read. There is no TIEREF.
932 It is theoretically possible to store references to values already within a
933 DBM::Deep object because everything already is synchronized, but the change to
934 the internals would be quite large. Specifically, DBM::Deep would have to tie
935 every single value that is stored. This would bloat the RAM footprint of
936 DBM::Deep at least twofold (if not more) and be a significant performance drain,
937 all to support a feature that has never been requested.
941 L<Data::Dump::Streamer> provides a mechanism for serializing coderefs,
942 including saving off all closure state. This would allow for DBM::Deep to
943 store the code for a subroutine. Then, whenever the subroutine is read, the
944 code could be C<eval()>'ed into being. However, just as for SCALAR and REF,
945 that closure state may change without notifying the DBM::Deep object storing
946 the reference. Again, this would generally be considered a feature.
950 =head2 External references and transactions
952 If you do C<my $x = $db-E<gt>{foo};>, then start a transaction, $x will be
953 referencing the database from outside the transaction. A fix for this (and other
954 issues with how external references into the database) is being looked into. This
955 is the skipped set of tests in t/39_singletons.t and a related issue is the focus
956 of t/37_delete_edge_cases.t
958 =head2 File corruption
960 The current level of error handling in DBM::Deep is minimal. Files I<are> checked
961 for a 32-bit signature when opened, but any other form of corruption in the
962 datafile can cause segmentation faults. DBM::Deep may try to C<seek()> past
963 the end of a file, or get stuck in an infinite loop depending on the level and
964 type of corruption. File write operations are not checked for failure (for
965 speed), so if you happen to run out of disk space, DBM::Deep will probably fail in
966 a bad way. These things will be addressed in a later version of DBM::Deep.
970 Beware of using DBM::Deep files over NFS. DBM::Deep uses flock(), which works
971 well on local filesystems, but will NOT protect you from file corruption over
972 NFS. I've heard about setting up your NFS server with a locking daemon, then
973 using C<lockf()> to lock your files, but your mileage may vary there as well.
974 From what I understand, there is no real way to do it. However, if you need
975 access to the underlying filehandle in DBM::Deep for using some other kind of
976 locking scheme like C<lockf()>, see the L<LOW-LEVEL ACCESS> section above.
978 =head2 Copying Objects
980 Beware of copying tied objects in Perl. Very strange things can happen.
981 Instead, use DBM::Deep's C<clone()> method which safely copies the object and
982 returns a new, blessed and tied hash or array to the same level in the DB.
984 my $copy = $db->clone();
986 B<Note>: Since clone() here is cloning the object, not the database location, any
987 modifications to either $db or $copy will be visible to both.
991 Beware of using C<shift()>, C<unshift()> or C<splice()> with large arrays.
992 These functions cause every element in the array to move, which can be murder
993 on DBM::Deep, as every element has to be fetched from disk, then stored again in
994 a different location. This will be addressed in a future version.
996 This has been somewhat addressed so that the cost is constant, regardless of
997 what is stored at those locations. So, small arrays with huge data structures in
998 them are faster. But, large arrays are still large.
1000 =head2 Writeonly Files
1002 If you pass in a filehandle to new(), you may have opened it in either a
1003 readonly or writeonly mode. STORE will verify that the filehandle is writable.
1004 However, there doesn't seem to be a good way to determine if a filehandle is
1005 readable. And, if the filehandle isn't readable, it's not clear what will
1006 happen. So, don't do that.
1008 =head2 Assignments Within Transactions
1010 The following will I<not> work as one might expect:
1018 is( $x->{a}, 1 ); # This will fail!
1020 The problem is that the moment a reference used as the rvalue to a DBM::Deep
1021 object's lvalue, it becomes tied itself. This is so that future changes to
1022 C<$x> can be tracked within the DBM::Deep file and is considered to be a
1023 feature. By the time the rollback occurs, there is no knowledge that there had
1024 been an C<$x> or what memory location to assign an C<export()> to.
1026 B<NOTE:> This does not affect importing because imports do a walk over the
1027 reference to be imported in order to explicitly leave it untied.
1029 =head1 CODE COVERAGE
1031 L<Devel::Cover> is used to test the code coverage of the tests. Below is the
1032 L<Devel::Cover> report on this distribution's test suite.
1034 ------------------------------------------ ------ ------ ------ ------ ------
1035 File stmt bran cond sub total
1036 ------------------------------------------ ------ ------ ------ ------ ------
1037 blib/lib/DBM/Deep.pm 97.2 90.9 83.3 100.0 95.4
1038 blib/lib/DBM/Deep/Array.pm 100.0 95.7 100.0 100.0 99.0
1039 blib/lib/DBM/Deep/Engine.pm 95.6 84.7 81.6 98.4 92.5
1040 blib/lib/DBM/Deep/File.pm 97.2 81.6 66.7 100.0 91.9
1041 blib/lib/DBM/Deep/Hash.pm 100.0 100.0 100.0 100.0 100.0
1042 Total 96.7 87.5 82.2 99.2 94.1
1043 ------------------------------------------ ------ ------ ------ ------ ------
1045 =head1 MORE INFORMATION
1047 Check out the DBM::Deep Google Group at L<http://groups.google.com/group/DBM-Deep>
1048 or send email to L<DBM-Deep@googlegroups.com>. You can also visit #dbm-deep on
1051 The source code repository is at L<http://svn.perl.org/modules/DBM-Deep>
1055 Rob Kinyon, L<rkinyon@cpan.org>
1057 Originally written by Joseph Huckaby, L<jhuckaby@cpan.org>
1061 Stonehenge Consulting (L<http://www.stonehenge.com/>) sponsored the
1062 developement of transactions and freespace management, leading to the 1.0000
1063 release. A great debt of gratitude goes out to them for their continuing
1064 leadership in and support of the Perl community.
1068 The following have contributed greatly to make DBM::Deep what it is today:
1072 =item * Adam Sah and Rich Gaushell for innumerable contributions early on.
1074 =item * Dan Golden and others at YAPC::NA 2006 for helping me design through transactions.
1080 perltie(1), Tie::Hash(3), Digest::MD5(3), Fcntl(3), flock(2), lockf(3), nfs(5),
1081 Digest::SHA256(3), Crypt::Blowfish(3), Compress::Zlib(3)
1085 Copyright (c) 2007 Rob Kinyon. All Rights Reserved.
1086 This is free software, you may use it and distribute it under the same terms