1DBM::Deep(3)          User Contributed Perl Documentation         DBM::Deep(3)
2
3
4

NAME

6       DBM::Deep - A pure perl multi-level hash/array DBM that supports
7       transactions
8

VERSION

10       2.0016
11

SYNOPSIS

13         use DBM::Deep;
14         my $db = DBM::Deep->new( "foo.db" );
15
16         $db->{key} = 'value';
17         print $db->{key};
18
19         $db->put('key' => 'value');
20         print $db->get('key');
21
22         # true multi-level support
23         $db->{my_complex} = [
24             'hello', { perl => 'rules' },
25             42, 99,
26         ];
27
28         $db->begin_work;
29
30         # Do stuff here
31
32         $db->rollback;
33         $db->commit;
34
35         tie my %db, 'DBM::Deep', 'foo.db';
36         $db{key} = 'value';
37         print $db{key};
38
39         tied(%db)->put('key' => 'value');
40         print tied(%db)->get('key');
41

DESCRIPTION

43       A unique flat-file database module, written in pure perl. True multi-
44       level hash/array support (unlike MLDBM, which is faked), hybrid OO /
45       tie() interface, cross-platform FTPable files, ACID transactions, and
46       is quite fast.  Can handle millions of keys and unlimited levels
47       without significant slow-down. Written from the ground-up in pure perl
48       -- this is NOT a wrapper around a C-based DBM. Out-of-the-box
49       compatibility with Unix, Mac OS X and Windows.
50

VERSION DIFFERENCES

52       NOTE: 2.0000 introduces Unicode support in the File back end. This
53       necessitates a change in the file format. The version 1.0003 format is
54       still supported, though, so we have added a db_version() method. If you
55       are using a database in the old format, you will have to upgrade it to
56       get Unicode support.
57
58       NOTE: 1.0020 introduces different engines which are backed by different
59       types of storage. There is the original storage (called 'File') and a
60       database storage (called 'DBI'). q.v. "PLUGINS" for more information.
61
62       NOTE: 1.0000 has significant file format differences from prior
63       versions.  There is a backwards-compatibility layer at
64       "utils/upgrade_db.pl". Files created by 1.0000 or higher are NOT
65       compatible with scripts using prior versions.
66

PLUGINS

68       DBM::Deep is a wrapper around different storage engines. These are:
69
70   File
71       This is the traditional storage engine, storing the data to a custom
72       file format. The parameters accepted are:
73
74       •   file
75
76           Filename of the DB file to link the handle to. You can pass a full
77           absolute filesystem path, partial path, or a plain filename if the
78           file is in the current working directory. This is a required
79           parameter (though q.v. fh).
80
81       •   fh
82
83           If you want, you can pass in the fh instead of the file. This is
84           most useful for doing something like:
85
86             my $db = DBM::Deep->new( { fh => \*DATA } );
87
88           You are responsible for making sure that the fh has been opened
89           appropriately for your needs. If you open it read-only and attempt
90           to write, an exception will be thrown. If you open it write-only or
91           append-only, an exception will be thrown immediately as DBM::Deep
92           needs to read from the fh.
93
94       •   file_offset
95
96           This is the offset within the file that the DBM::Deep db starts.
97           Most of the time, you will not need to set this. However, it's
98           there if you want it.
99
100           If you pass in fh and do not set this, it will be set
101           appropriately.
102
103       •   locking
104
105           Specifies whether locking is to be enabled. DBM::Deep uses Perl's
106           flock() function to lock the database in exclusive mode for writes,
107           and shared mode for reads. Pass any true value to enable. This
108           affects the base DB handle and any child hashes or arrays that use
109           the same DB file. This is an optional parameter, and defaults to 1
110           (enabled). See "LOCKING" below for more.
111
112       When you open an existing database file, the version of the database
113       format will stay the same. But if you are creating a new file, it will
114       be in the latest format.
115
116   DBI
117       This is a storage engine that stores the data in a relational database.
118       Funnily enough, this engine doesn't work with transactions (yet) as
119       InnoDB doesn't do what DBM::Deep needs it to do.
120
121       The parameters accepted are:
122
123       •   dbh
124
125           This is a DBH that's already been opened with "connect" in DBI.
126
127       •   dbi
128
129           This is a hashref containing:
130
131           •   dsn
132
133           •   username
134
135           •   password
136
137           •   connect_args
138
139           These correspond to the 4 parameters "connect" in DBI takes.
140
141       NOTE: This has only been tested with MySQL and SQLite (with
142       disappointing results). I plan on extending this to work with
143       PostgreSQL in the near future. Oracle, Sybase, and other engines will
144       come later.
145
146   Planned engines
147       There are plans to extend this functionality to (at least) the
148       following:
149
150       •   BDB (and other hash engines like memcached)
151
152       •   NoSQL engines (such as Tokyo Cabinet)
153
154       •   DBIx::Class (and other ORMs)
155

SETUP

157       Construction can be done OO-style (which is the recommended way), or
158       using Perl's tie() function. Both are examined here.
159
160   OO Construction
161       The recommended way to construct a DBM::Deep object is to use the new()
162       method, which gets you a blessed and tied hash (or array) reference.
163
164         my $db = DBM::Deep->new( "foo.db" );
165
166       This opens a new database handle, mapped to the file "foo.db". If this
167       file does not exist, it will automatically be created. DB files are
168       opened in "r+" (read/write) mode, and the type of object returned is a
169       hash, unless otherwise specified (see "Options" below).
170
171       You can pass a number of options to the constructor to specify things
172       like locking, autoflush, etc. This is done by passing an inline hash
173       (or hashref):
174
175         my $db = DBM::Deep->new(
176             file      => "foo.db",
177             locking   => 1,
178             autoflush => 1
179         );
180
181       Notice that the filename is now specified inside the hash with the
182       "file" parameter, as opposed to being the sole argument to the
183       constructor. This is required if any options are specified.  See
184       "Options" below for the complete list.
185
186       You can also start with an array instead of a hash. For this, you must
187       specify the "type" parameter:
188
189         my $db = DBM::Deep->new(
190             file => "foo.db",
191             type => DBM::Deep->TYPE_ARRAY
192         );
193
194       Note: Specifying the "type" parameter only takes effect when beginning
195       a new DB file. If you create a DBM::Deep object with an existing file,
196       the "type" will be loaded from the file header, and an error will be
197       thrown if the wrong type is passed in.
198
199   Tie Construction
200       Alternately, you can create a DBM::Deep handle by using Perl's built-in
201       tie() function. The object returned from tie() can be used to call
202       methods, such as lock() and unlock(). (That object can be retrieved
203       from the tied variable at any time using tied() - please see perltie
204       for more info.)
205
206         my %hash;
207         my $db = tie %hash, "DBM::Deep", "foo.db";
208
209         my @array;
210         my $db = tie @array, "DBM::Deep", "bar.db";
211
212       As with the OO constructor, you can replace the DB filename parameter
213       with a hash containing one or more options (see "Options" just below
214       for the complete list).
215
216         tie %hash, "DBM::Deep", {
217             file => "foo.db",
218             locking => 1,
219             autoflush => 1
220         };
221
222   Options
223       There are a number of options that can be passed in when constructing
224       your DBM::Deep objects. These apply to both the OO- and tie- based
225       approaches.
226
227       •   type
228
229           This parameter specifies what type of object to create, a hash or
230           array. Use one of these two constants:
231
232           •   "DBM::Deep->TYPE_HASH"
233
234           •   "DBM::Deep->TYPE_ARRAY"
235
236           This only takes effect when beginning a new file. This is an
237           optional parameter, and defaults to "DBM::Deep->TYPE_HASH".
238
239       •   autoflush
240
241           Specifies whether autoflush is to be enabled on the underlying
242           filehandle.  This obviously slows down write operations, but is
243           required if you may have multiple processes accessing the same DB
244           file (also consider enable locking).  Pass any true value to
245           enable. This is an optional parameter, and defaults to 1 (enabled).
246
247       •   filter_*
248
249           See "FILTERS" below.
250
251       The following parameters may be specified in the constructor the first
252       time the datafile is created. However, they will be stored in the
253       header of the file and cannot be overridden by subsequent openings of
254       the file - the values will be set from the values stored in the
255       datafile's header.
256
257       •   num_txns
258
259           This is the number of transactions that can be running at one time.
260           The default is one - the HEAD. The minimum is one and the maximum
261           is 255. The more transactions, the larger and quicker the datafile
262           grows.
263
264           Simple access to a database, regardless of how many processes are
265           doing it, already counts as one transaction (the HEAD). So, if you
266           want, say, 5 processes to be able to call begin_work at the same
267           time, "num_txns" must be at least 6.
268
269           See "TRANSACTIONS" below.
270
271       •   max_buckets
272
273           This is the number of entries that can be added before a
274           reindexing. The larger this number is made, the larger a file gets,
275           but the better performance you will have. The default and minimum
276           number this can be is 16. The maximum is 256, but more than 64
277           isn't recommended.
278
279       •   data_sector_size
280
281           This is the size in bytes of a given data sector. Data sectors will
282           chain, so a value of any size can be stored. However, chaining is
283           expensive in terms of time. Setting this value to something close
284           to the expected common length of your scalars will improve your
285           performance. If it is too small, your file will have a lot of
286           chaining. If it is too large, your file will have a lot of dead
287           space in it.
288
289           The default for this is 64 bytes. The minimum value is 32 and the
290           maximum is 256 bytes.
291
292           Note: There are between 6 and 10 bytes taken up in each data sector
293           for bookkeeping. (It's 4 + the number of bytes in your
294           "pack_size".) This is included within the data_sector_size, thus
295           the effective value is 6-10 bytes less than what you specified.
296
297           Another note: If your strings contain any characters beyond the
298           byte range, they will be encoded as UTF-8 before being stored in
299           the file. This will make all non-ASCII characters take up more than
300           one byte each.
301
302       •   pack_size
303
304           This is the size of the file pointer used throughout the file. The
305           valid values are:
306
307           •   small
308
309               This uses 2-byte offsets, allowing for a maximum file size of
310               65 KB.
311
312           •   medium (default)
313
314               This uses 4-byte offsets, allowing for a maximum file size of 4
315               GB.
316
317           •   large
318
319               This uses 8-byte offsets, allowing for a maximum file size of
320               16 XB (exabytes). This can only be enabled if your Perl is
321               compiled for 64-bit.
322
323           See "LARGEFILE SUPPORT" for more information.
324
325       •   external_refs
326
327           This is a boolean option. When enabled, it allows external
328           references to database entries to hold on to those entries, even
329           when they are deleted.
330
331           To illustrate, if you retrieve a hash (or array) reference from the
332           database,
333
334             $foo_hash = $db->{foo};
335
336           the hash reference is still tied to the database. So if you
337
338             delete $db->{foo};
339
340           $foo_hash will point to a location in the DB that is no longer
341           valid (we call this a stale reference). So if you try to retrieve
342           the data from $foo_hash,
343
344             for(keys %$foo_hash) {
345
346           you will get an error.
347
348           The "external_refs" option causes $foo_hash to 'hang on' to the DB
349           entry, so it will not be deleted from the database if there is
350           still a reference to it in a running program. It will be deleted,
351           instead, when the $foo_hash variable no longer exists, or is
352           overwritten.
353
354           This has the potential to cause database bloat if your program
355           crashes, so it is not enabled by default. (See also the "export"
356           method for an alternative workaround.)
357

TIE INTERFACE

359       With DBM::Deep you can access your databases using Perl's standard
360       hash/array syntax. Because all DBM::Deep objects are tied to hashes or
361       arrays, you can treat them as such (but see "external_refs", above, and
362       "Stale References", below). DBM::Deep will intercept all reads/writes
363       and direct them to the right place -- the DB file. This has nothing to
364       do with the "Tie Construction" section above. This simply tells you how
365       to use DBM::Deep using regular hashes and arrays, rather than calling
366       functions like get() and put() (although those work too). It is
367       entirely up to you how to want to access your databases.
368
369   Hashes
370       You can treat any DBM::Deep object like a normal Perl hash reference.
371       Add keys, or even nested hashes (or arrays) using standard Perl syntax:
372
373         my $db = DBM::Deep->new( "foo.db" );
374
375         $db->{mykey} = "myvalue";
376         $db->{myhash} = {};
377         $db->{myhash}->{subkey} = "subvalue";
378
379         print $db->{myhash}->{subkey} . "\n";
380
381       You can even step through hash keys using the normal Perl keys()
382       function:
383
384         foreach my $key (keys %$db) {
385             print "$key: " . $db->{$key} . "\n";
386         }
387
388       Remember that Perl's keys() function extracts every key from the hash
389       and pushes them onto an array, all before the loop even begins. If you
390       have an extremely large hash, this may exhaust Perl's memory. Instead,
391       consider using Perl's each() function, which pulls keys/values one at a
392       time, using very little memory:
393
394         while (my ($key, $value) = each %$db) {
395             print "$key: $value\n";
396         }
397
398       Please note that when using each(), you should always pass a direct
399       hash reference, not a lookup. Meaning, you should never do this:
400
401         # NEVER DO THIS
402         while (my ($key, $value) = each %{$db->{foo}}) { # BAD
403
404       This causes an infinite loop, because for each iteration, Perl is
405       calling FETCH() on the $db handle, resulting in a "new" hash for foo
406       every time, so it effectively keeps returning the first key over and
407       over again. Instead, assign a temporary variable to "$db->{foo}", then
408       pass that to each().
409
410   Arrays
411       As with hashes, you can treat any DBM::Deep object like a normal Perl
412       array reference. This includes inserting, removing and manipulating
413       elements, and the push(), pop(), shift(), unshift() and splice()
414       functions.  The object must have first been created using type
415       "DBM::Deep->TYPE_ARRAY", or simply be a nested array reference inside a
416       hash. Example:
417
418         my $db = DBM::Deep->new(
419             file => "foo-array.db",
420             type => DBM::Deep->TYPE_ARRAY
421         );
422
423         $db->[0] = "foo";
424         push @$db, "bar", "baz";
425         unshift @$db, "bah";
426
427         my $last_elem   = pop @$db;   # baz
428         my $first_elem  = shift @$db; # bah
429         my $second_elem = $db->[1];   # bar
430
431         my $num_elements = scalar @$db;
432

OO INTERFACE

434       In addition to the tie() interface, you can also use a standard OO
435       interface to manipulate all aspects of DBM::Deep databases. Each type
436       of object (hash or array) has its own methods, but both types share the
437       following common methods: put(), get(), exists(), delete() and clear().
438       fetch() and store() are aliases to put() and get(), respectively.
439
440new() / clone()
441
442           These are the constructor and copy-functions.
443
444put() / store()
445
446           Stores a new hash key/value pair, or sets an array element value.
447           Takes two arguments, the hash key or array index, and the new
448           value. The value can be a scalar, hash ref or array ref. Returns
449           true on success, false on failure.
450
451             $db->put("foo", "bar"); # for hashes
452             $db->put(1, "bar"); # for arrays
453
454get() / fetch()
455
456           Fetches the value of a hash key or array element. Takes one
457           argument: the hash key or array index. Returns a scalar, hash ref
458           or array ref, depending on the data type stored.
459
460             my $value = $db->get("foo"); # for hashes
461             my $value = $db->get(1); # for arrays
462
463exists()
464
465           Checks if a hash key or array index exists. Takes one argument: the
466           hash key or array index. Returns true if it exists, false if not.
467
468             if ($db->exists("foo")) { print "yay!\n"; } # for hashes
469             if ($db->exists(1)) { print "yay!\n"; } # for arrays
470
471delete()
472
473           Deletes one hash key/value pair or array element. Takes one
474           argument: the hash key or array index. Returns the data that the
475           element used to contain (just like Perl's "delete" function), which
476           is "undef" if it did not exist. For arrays, the remaining elements
477           located after the deleted element are NOT moved over. The deleted
478           element is essentially just undefined, which is exactly how Perl's
479           internal arrays work.
480
481             $db->delete("foo"); # for hashes
482             $db->delete(1); # for arrays
483
484clear()
485
486           Deletes all hash keys or array elements. Takes no arguments. No
487           return value.
488
489             $db->clear(); # hashes or arrays
490
491lock() / unlock() / lock_exclusive() / lock_shared()
492
493           q.v. "LOCKING" for more info.
494
495optimize()
496
497           This will compress the datafile so that it takes up as little space
498           as possible.  There is a freespace manager so that when space is
499           freed up, it is used before extending the size of the datafile.
500           But, that freespace just sits in the datafile unless optimize() is
501           called.
502
503           "optimize" basically copies everything into a new database, so, if
504           it is in version 1.0003 format, it will be upgraded.
505
506import()
507
508           Unlike simple assignment, import() does not tie the right-hand
509           side. Instead, a copy of your data is put into the DB. import()
510           takes either an arrayref (if your DB is an array) or a hashref (if
511           your DB is a hash). import() will die if anything else is passed
512           in.
513
514export()
515
516           This returns a complete copy of the data structure at the point you
517           do the export.  This copy is in RAM, not on disk like the DB is.
518
519begin_work() / commit() / rollback()
520
521           These are the transactional functions. "TRANSACTIONS" for more
522           information.
523
524       •   supports( $option )
525
526           This returns a boolean indicating whether this instance of
527           DBM::Deep supports that feature. $option can be one of:
528
529           •   transactions
530
531           •   unicode
532
533db_version()
534
535           This returns the version of the database format that the current
536           database is in. This is specified as the earliest version of
537           DBM::Deep that supports it.
538
539           For the File back end, this will be 1.0003 or 2.
540
541           For the DBI back end, it is currently always 1.0020.
542
543   Hashes
544       For hashes, DBM::Deep supports all the common methods described above,
545       and the following additional methods: first_key() and next_key().
546
547first_key()
548
549           Returns the "first" key in the hash. As with built-in Perl hashes,
550           keys are fetched in an undefined order (which appears random).
551           Takes no arguments, returns the key as a scalar value.
552
553             my $key = $db->first_key();
554
555next_key()
556
557           Returns the "next" key in the hash, given the previous one as the
558           sole argument.  Returns undef if there are no more keys to be
559           fetched.
560
561             $key = $db->next_key($key);
562
563       Here are some examples of using hashes:
564
565         my $db = DBM::Deep->new( "foo.db" );
566
567         $db->put("foo", "bar");
568         print "foo: " . $db->get("foo") . "\n";
569
570         $db->put("baz", {}); # new child hash ref
571         $db->get("baz")->put("buz", "biz");
572         print "buz: " . $db->get("baz")->get("buz") . "\n";
573
574         my $key = $db->first_key();
575         while ($key) {
576             print "$key: " . $db->get($key) . "\n";
577             $key = $db->next_key($key);
578         }
579
580         if ($db->exists("foo")) { $db->delete("foo"); }
581
582   Arrays
583       For arrays, DBM::Deep supports all the common methods described above,
584       and the following additional methods: length(), push(), pop(), shift(),
585       unshift() and splice().
586
587length()
588
589           Returns the number of elements in the array. Takes no arguments.
590
591             my $len = $db->length();
592
593push()
594
595           Adds one or more elements onto the end of the array. Accepts
596           scalars, hash refs or array refs. No return value.
597
598             $db->push("foo", "bar", {});
599
600pop()
601
602           Fetches the last element in the array, and deletes it. Takes no
603           arguments.  Returns undef if array is empty. Returns the element
604           value.
605
606             my $elem = $db->pop();
607
608shift()
609
610           Fetches the first element in the array, deletes it, then shifts all
611           the remaining elements over to take up the space. Returns the
612           element value. This method is not recommended with large arrays --
613           see "Large Arrays" below for details.
614
615             my $elem = $db->shift();
616
617unshift()
618
619           Inserts one or more elements onto the beginning of the array,
620           shifting all existing elements over to make room. Accepts scalars,
621           hash refs or array refs.  No return value. This method is not
622           recommended with large arrays -- see <Large Arrays> below for
623           details.
624
625             $db->unshift("foo", "bar", {});
626
627splice()
628
629           Performs exactly like Perl's built-in function of the same name.
630           See "splice" in perlfunc for usage -- it is too complicated to
631           document here. This method is not recommended with large arrays --
632           see "Large Arrays" below for details.
633
634       Here are some examples of using arrays:
635
636         my $db = DBM::Deep->new(
637             file => "foo.db",
638             type => DBM::Deep->TYPE_ARRAY
639         );
640
641         $db->push("bar", "baz");
642         $db->unshift("foo");
643         $db->put(3, "buz");
644
645         my $len = $db->length();
646         print "length: $len\n"; # 4
647
648         for (my $k=0; $k<$len; $k++) {
649             print "$k: " . $db->get($k) . "\n";
650         }
651
652         $db->splice(1, 2, "biz", "baf");
653
654         while (my $elem = shift @$db) {
655             print "shifted: $elem\n";
656         }
657

LOCKING

659       Enable or disable automatic file locking by passing a boolean value to
660       the "locking" parameter when constructing your DBM::Deep object (see
661       "SETUP" above).
662
663         my $db = DBM::Deep->new(
664             file => "foo.db",
665             locking => 1
666         );
667
668       This causes DBM::Deep to flock() the underlying filehandle with
669       exclusive mode for writes, and shared mode for reads. This is required
670       if you have multiple processes accessing the same database file, to
671       avoid file corruption.  Please note that flock() does NOT work for
672       files over NFS. See "DB over NFS" below for more.
673
674   Explicit Locking
675       You can explicitly lock a database, so it remains locked for multiple
676       actions. This is done by calling the lock_exclusive() method (for when
677       you want to write) or the lock_shared() method (for when you want to
678       read).  This is particularly useful for things like counters, where the
679       current value needs to be fetched, then incremented, then stored again.
680
681         $db->lock_exclusive();
682         my $counter = $db->get("counter");
683         $counter++;
684         $db->put("counter", $counter);
685         $db->unlock();
686
687         # or...
688
689         $db->lock_exclusive();
690         $db->{counter}++;
691         $db->unlock();
692
693   Win32/Cygwin
694       Due to Win32 actually enforcing the read-only status of a shared lock,
695       all locks on Win32 and cygwin are exclusive. This is because of how
696       autovivification currently works. Hopefully, this will go away in a
697       future release.
698

IMPORTING/EXPORTING

700       You can import existing complex structures by calling the import()
701       method, and export an entire database into an in-memory structure using
702       the export() method. Both are examined here.
703
704   Importing
705       Say you have an existing hash with nested hashes/arrays inside it.
706       Instead of walking the structure and adding keys/elements to the
707       database as you go, simply pass a reference to the import() method.
708       This recursively adds everything to an existing DBM::Deep object for
709       you. Here is an example:
710
711         my $struct = {
712             key1 => "value1",
713             key2 => "value2",
714             array1 => [ "elem0", "elem1", "elem2" ],
715             hash1 => {
716                 subkey1 => "subvalue1",
717                 subkey2 => "subvalue2"
718             }
719         };
720
721         my $db = DBM::Deep->new( "foo.db" );
722         $db->import( $struct );
723
724         print $db->{key1} . "\n"; # prints "value1"
725
726       This recursively imports the entire $struct object into $db, including
727       all nested hashes and arrays. If the DBM::Deep object contains existing
728       data, keys are merged with the existing ones, replacing if they already
729       exist.  The import() method can be called on any database level (not
730       just the base level), and works with both hash and array DB types.
731
732       Note: Make sure your existing structure has no circular references in
733       it.  These will cause an infinite loop when importing. There are plans
734       to fix this in a later release.
735
736   Exporting
737       Calling the export() method on an existing DBM::Deep object will return
738       a reference to a new in-memory copy of the database. The export is done
739       recursively, so all nested hashes/arrays are all exported to standard
740       Perl objects. Here is an example:
741
742         my $db = DBM::Deep->new( "foo.db" );
743
744         $db->{key1} = "value1";
745         $db->{key2} = "value2";
746         $db->{hash1} = {};
747         $db->{hash1}->{subkey1} = "subvalue1";
748         $db->{hash1}->{subkey2} = "subvalue2";
749
750         my $struct = $db->export();
751
752         print $struct->{key1} . "\n"; # prints "value1"
753
754       This makes a complete copy of the database in memory, and returns a
755       reference to it. The export() method can be called on any database
756       level (not just the base level), and works with both hash and array DB
757       types. Be careful of large databases -- you can store a lot more data
758       in a DBM::Deep object than an in-memory Perl structure.
759
760       Note: Make sure your database has no circular references in it.  These
761       will cause an infinite loop when exporting. There are plans to fix this
762       in a later release.
763

FILTERS

765       DBM::Deep has a number of hooks where you can specify your own Perl
766       function to perform filtering on incoming or outgoing data. This is a
767       perfect way to extend the engine, and implement things like real-time
768       compression or encryption. Filtering applies to the base DB level, and
769       all child hashes / arrays. Filter hooks can be specified when your
770       DBM::Deep object is first constructed, or by calling the set_filter()
771       method at any time. There are four available filter hooks.
772
773   set_filter()
774       This method takes two parameters - the filter type and the filter
775       subreference.  The four types are:
776
777       •   filter_store_key
778
779           This filter is called whenever a hash key is stored. It is passed
780           the incoming key, and expected to return a transformed key.
781
782       •   filter_store_value
783
784           This filter is called whenever a hash key or array element is
785           stored. It is passed the incoming value, and expected to return a
786           transformed value.
787
788       •   filter_fetch_key
789
790           This filter is called whenever a hash key is fetched (i.e. via
791           first_key() or next_key()). It is passed the transformed key, and
792           expected to return the plain key.
793
794       •   filter_fetch_value
795
796           This filter is called whenever a hash key or array element is
797           fetched.  It is passed the transformed value, and expected to
798           return the plain value.
799
800       Here are the two ways to setup a filter hook:
801
802         my $db = DBM::Deep->new(
803             file => "foo.db",
804             filter_store_value => \&my_filter_store,
805             filter_fetch_value => \&my_filter_fetch
806         );
807
808         # or...
809
810         $db->set_filter( "store_value", \&my_filter_store );
811         $db->set_filter( "fetch_value", \&my_filter_fetch );
812
813       Your filter function will be called only when dealing with SCALAR keys
814       or values. When nested hashes and arrays are being stored/fetched,
815       filtering is bypassed. Filters are called as static functions, passed a
816       single SCALAR argument, and expected to return a single SCALAR value.
817       If you want to remove a filter, set the function reference to "undef":
818
819         $db->set_filter( "store_value", undef );
820
821   Examples
822       Please read DBM::Deep::Cookbook for examples of filters.
823

ERROR HANDLING

825       Most DBM::Deep methods return a true value for success, and call die()
826       on failure. You can wrap calls in an eval block to catch the die.
827
828         my $db = DBM::Deep->new( "foo.db" ); # create hash
829         eval { $db->push("foo"); }; # ILLEGAL -- push is array-only call
830
831         print $@;           # prints error message
832

LARGEFILE SUPPORT

834       If you have a 64-bit system, and your Perl is compiled with both
835       LARGEFILE and 64-bit support, you may be able to create databases
836       larger than 4 GB.  DBM::Deep by default uses 32-bit file offset tags,
837       but these can be changed by specifying the 'pack_size' parameter when
838       constructing the file.
839
840         DBM::Deep->new(
841             file      => $filename,
842             pack_size => 'large',
843         );
844
845       This tells DBM::Deep to pack all file offsets with 8-byte (64-bit) quad
846       words instead of 32-bit longs. After setting these values your DB files
847       have a theoretical maximum size of 16 XB (exabytes).
848
849       You can also use "pack_size => 'small'" in order to use 16-bit file
850       offsets.
851
852       Note: Changing these values will NOT work for existing database files.
853       Only change this for new files. Once the value has been set, it is
854       stored in the file's header and cannot be changed for the life of the
855       file. These parameters are per-file, meaning you can access 32-bit and
856       64-bit files, as you choose.
857
858       Note: We have not personally tested files larger than 4 GB -- all our
859       systems have only a 32-bit Perl. However, we have received user reports
860       that this does indeed work.
861

LOW-LEVEL ACCESS

863       If you require low-level access to the underlying filehandle that
864       DBM::Deep uses, you can call the _fh() method, which returns the
865       handle:
866
867         my $fh = $db->_fh();
868
869       This method can be called on the root level of the database, or any
870       child hashes or arrays. All levels share a root structure, which
871       contains things like the filehandle, a reference counter, and all the
872       options specified when you created the object. You can get access to
873       this file object by calling the _storage() method.
874
875         my $file_obj = $db->_storage();
876
877       This is useful for changing options after the object has already been
878       created, such as enabling/disabling locking. You can also store your
879       own temporary user data in this structure (be wary of name collision),
880       which is then accessible from any child hash or array.
881

CIRCULAR REFERENCES

883       DBM::Deep has full support for circular references. Meaning you can
884       have a nested hash key or array element that points to a parent object.
885       This relationship is stored in the DB file, and is preserved between
886       sessions.  Here is an example:
887
888         my $db = DBM::Deep->new( "foo.db" );
889
890         $db->{foo} = "bar";
891         $db->{circle} = $db; # ref to self
892
893         print $db->{foo} . "\n"; # prints "bar"
894         print $db->{circle}->{foo} . "\n"; # prints "bar" again
895
896       This also works as expected with array and hash references. So, the
897       following works as expected:
898
899         $db->{foo} = [ 1 .. 3 ];
900         $db->{bar} = $db->{foo};
901
902         push @{$db->{foo}}, 42;
903         is( $db->{bar}[-1], 42 ); # Passes
904
905       This, however, does not extend to assignments from one DB file to
906       another.  So, the following will throw an error:
907
908         my $db1 = DBM::Deep->new( "foo.db" );
909         my $db2 = DBM::Deep->new( "bar.db" );
910
911         $db1->{foo} = [];
912         $db2->{foo} = $db1->{foo}; # dies
913
914       Note: Passing the object to a function that recursively walks the
915       object tree (such as Data::Dumper or even the built-in optimize() or
916       export() methods) will result in an infinite loop. This will be fixed
917       in a future release by adding singleton support.
918

TRANSACTIONS

920       As of 1.0000, DBM::Deep has ACID transactions. Every DBM::Deep object
921       is completely transaction-ready - it is not an option you have to turn
922       on. You do have to specify how many transactions may run simultaneously
923       (q.v. "num_txns").
924
925       Three new methods have been added to support them. They are:
926
927begin_work()
928
929           This starts a transaction.
930
931commit()
932
933           This applies the changes done within the transaction to the
934           mainline and ends the transaction.
935
936rollback()
937
938           This discards the changes done within the transaction to the
939           mainline and ends the transaction.
940
941       Transactions in DBM::Deep are done using a variant of the MVCC method,
942       the same method used by the InnoDB MySQL engine.
943

MIGRATION

945       As of 1.0000, the file format has changed. To aid in upgrades, a
946       migration script is provided within the CPAN distribution, called
947       utils/upgrade_db.pl.
948
949       NOTE: This script is not installed onto your system because it carries
950       a copy of every version prior to the current version.
951
952       As of version 2.0000, databases created by old versions back to 1.0003
953       can be read, but new features may not be available unless the database
954       is upgraded first.
955

TODO

957       The following are items that are planned to be added in future
958       releases. These are separate from the "CAVEATS, ISSUES & BUGS" below.
959
960   Sub-Transactions
961       Right now, you cannot run a transaction within a transaction. Removing
962       this restriction is technically straightforward, but the combinatorial
963       explosion of possible usecases hurts my head. If this is something you
964       want to see immediately, please submit many testcases.
965
966   Caching
967       If a client is willing to assert upon opening the file that this
968       process will be the only consumer of that datafile, then there are a
969       number of caching possibilities that can be taken advantage of. This
970       does, however, mean that DBM::Deep is more vulnerable to losing data
971       due to unflushed changes. It also means a much larger in-memory
972       footprint. As such, it's not clear exactly how this should be done.
973       Suggestions are welcome.
974
975   Ram-only
976       The techniques used in DBM::Deep simply require a seekable contiguous
977       datastore. This could just as easily be a large string as a file. By
978       using substr, the STM capabilities of DBM::Deep could be used within a
979       single-process. I have no idea how I'd specify this, though.
980       Suggestions are welcome.
981
982   Different contention resolution mechanisms
983       Currently, the only contention resolution mechanism is last-write-wins.
984       This is the mechanism used by most RDBMSes and should be good enough
985       for most uses.  For advanced uses of STM, other contention mechanisms
986       will be needed. If you have an idea of how you'd like to see contention
987       resolution in DBM::Deep, please let me know.
988

CAVEATS, ISSUES & BUGS

990       This section describes all the known issues with DBM::Deep. These are
991       issues that are either intractable or depend on some feature within
992       Perl working exactly right. It you have found something that is not
993       listed below, please send an e-mail to bug-DBM-Deep@rt.cpan.org
994       <mailto:bug-DBM-Deep@rt.cpan.org>.  Likewise, if you think you know of
995       a way around one of these issues, please let me know.
996
997   References
998       (The following assumes a high level of Perl understanding, specifically
999       of references. Most users can safely skip this section.)
1000
1001       Currently, the only references supported are HASH and ARRAY. The other
1002       reference types (SCALAR, CODE, GLOB, and REF) cannot be supported for
1003       various reasons.
1004
1005       •   GLOB
1006
1007           These are things like filehandles and other sockets. They can't be
1008           supported because it's completely unclear how DBM::Deep should
1009           serialize them.
1010
1011       •   SCALAR / REF
1012
1013           The discussion here refers to the following type of example:
1014
1015             my $x = 25;
1016             $db->{key1} = \$x;
1017
1018             $x = 50;
1019
1020             # In some other process ...
1021
1022             my $val = ${ $db->{key1} };
1023
1024             is( $val, 50, "What actually gets stored in the DB file?" );
1025
1026           The problem is one of synchronization. When the variable being
1027           referred to changes value, the reference isn't notified, which is
1028           kind of the point of references. This means that the new value
1029           won't be stored in the datafile for other processes to read. There
1030           is no TIEREF.
1031
1032           It is theoretically possible to store references to values already
1033           within a DBM::Deep object because everything already is
1034           synchronized, but the change to the internals would be quite large.
1035           Specifically, DBM::Deep would have to tie every single value that
1036           is stored. This would bloat the RAM footprint of DBM::Deep at least
1037           twofold (if not more) and be a significant performance drain, all
1038           to support a feature that has never been requested.
1039
1040       •   CODE
1041
1042           Data::Dump::Streamer provides a mechanism for serializing coderefs,
1043           including saving off all closure state. This would allow for
1044           DBM::Deep to store the code for a subroutine. Then, whenever the
1045           subroutine is read, the code could be eval()'ed into being.
1046           However, just as for SCALAR and REF, that closure state may change
1047           without notifying the DBM::Deep object storing the reference.
1048           Again, this would generally be considered a feature.
1049
1050   External references and transactions
1051       If you do "my $x = $db->{foo};", then start a transaction, $x will be
1052       referencing the database from outside the transaction. A fix for this
1053       (and other issues with how external references into the database) is
1054       being looked into. This is the skipped set of tests in
1055       t/39_singletons.t and a related issue is the focus of
1056       t/37_delete_edge_cases.t
1057
1058   File corruption
1059       The current level of error handling in DBM::Deep is minimal. Files are
1060       checked for a 32-bit signature when opened, but any other form of
1061       corruption in the datafile can cause segmentation faults. DBM::Deep may
1062       try to seek() past the end of a file, or get stuck in an infinite loop
1063       depending on the level and type of corruption. File write operations
1064       are not checked for failure (for speed), so if you happen to run out of
1065       disk space, DBM::Deep will probably fail in a bad way. These things
1066       will be addressed in a later version of DBM::Deep.
1067
1068   DB over NFS
1069       Beware of using DBM::Deep files over NFS. DBM::Deep uses flock(), which
1070       works well on local filesystems, but will NOT protect you from file
1071       corruption over NFS. I've heard about setting up your NFS server with a
1072       locking daemon, then using lockf() to lock your files, but your mileage
1073       may vary there as well.  From what I understand, there is no real way
1074       to do it. However, if you need access to the underlying filehandle in
1075       DBM::Deep for using some other kind of locking scheme like lockf(), see
1076       the "LOW-LEVEL ACCESS" section above.
1077
1078   Copying Objects
1079       Beware of copying tied objects in Perl. Very strange things can happen.
1080       Instead, use DBM::Deep's clone() method which safely copies the object
1081       and returns a new, blessed and tied hash or array to the same level in
1082       the DB.
1083
1084         my $copy = $db->clone();
1085
1086       Note: Since clone() here is cloning the object, not the database
1087       location, any modifications to either $db or $copy will be visible to
1088       both.
1089
1090   Stale References
1091       If you take a reference to an array or hash from the database, it is
1092       tied to the database itself. This means that if the datum in question
1093       is subsequently deleted from the database, the reference to it will
1094       point to an invalid location and unpredictable things will happen if
1095       you try to use it.
1096
1097       So a seemingly innocuous piece of code like this:
1098
1099         my %hash = %{ $db->{some_hash} };
1100
1101       can fail if another process deletes or clobbers "$db->{some_hash}"
1102       while the data are being extracted, since "%{ ... }" is not atomic.
1103       (This actually happened.) The solution is to lock the database before
1104       reading the data:
1105
1106         $db->lock_exclusive;
1107         my %hash = %{ $db->{some_hash} };
1108         $db->unlock;
1109
1110       As of version 1.0024, if you assign a stale reference to a location in
1111       the database, DBM::Deep will warn, if you have uninitialized warnings
1112       enabled, and treat the stale reference as "undef". An attempt to use a
1113       stale reference as an array or hash reference will cause an error.
1114
1115   Large Arrays
1116       Beware of using shift(), unshift() or splice() with large arrays.
1117       These functions cause every element in the array to move, which can be
1118       murder on DBM::Deep, as every element has to be fetched from disk, then
1119       stored again in a different location. This will be addressed in a
1120       future version.
1121
1122       This has been somewhat addressed so that the cost is constant,
1123       regardless of what is stored at those locations. So, small arrays with
1124       huge data structures in them are faster. But, large arrays are still
1125       large.
1126
1127   Writeonly Files
1128       If you pass in a filehandle to new(), you may have opened it in either
1129       a readonly or writeonly mode. STORE will verify that the filehandle is
1130       writable.  However, there doesn't seem to be a good way to determine if
1131       a filehandle is readable. And, if the filehandle isn't readable, it's
1132       not clear what will happen. So, don't do that.
1133
1134   Assignments Within Transactions
1135       The following will not work as one might expect:
1136
1137         my $x = { a => 1 };
1138
1139         $db->begin_work;
1140         $db->{foo} = $x;
1141         $db->rollback;
1142
1143         is( $x->{a}, 1 ); # This will fail!
1144
1145       The problem is that the moment a reference used as the rvalue to a
1146       DBM::Deep object's lvalue, it becomes tied itself. This is so that
1147       future changes to $x can be tracked within the DBM::Deep file and is
1148       considered to be a feature. By the time the rollback occurs, there is
1149       no knowledge that there had been an $x or what memory location to
1150       assign an export() to.
1151
1152       NOTE: This does not affect importing because imports do a walk over the
1153       reference to be imported in order to explicitly leave it untied.
1154

CODE COVERAGE

1156       Devel::Cover is used to test the code coverage of the tests. Below is
1157       the Devel::Cover report on this distribution's test suite.
1158
1159         ---------------------------- ------ ------ ------ ------ ------ ------ ------
1160         File                           stmt   bran   cond    sub    pod   time  total
1161         ---------------------------- ------ ------ ------ ------ ------ ------ ------
1162         blib/lib/DBM/Deep.pm          100.0   89.1   82.9  100.0  100.0   32.5   98.1
1163         blib/lib/DBM/Deep/Array.pm    100.0   94.4  100.0  100.0  100.0    5.2   98.8
1164         blib/lib/DBM/Deep/Engine.pm   100.0   92.9  100.0  100.0  100.0    7.4  100.0
1165         ...ib/DBM/Deep/Engine/DBI.pm   95.0   73.1  100.0  100.0  100.0    1.5   90.4
1166         ...b/DBM/Deep/Engine/File.pm   92.3   78.5   88.9  100.0  100.0    4.9   90.3
1167         blib/lib/DBM/Deep/Hash.pm     100.0  100.0  100.0  100.0  100.0    3.8  100.0
1168         .../lib/DBM/Deep/Iterator.pm  100.0    n/a    n/a  100.0  100.0    0.0  100.0
1169         .../DBM/Deep/Iterator/DBI.pm  100.0  100.0    n/a  100.0  100.0    1.2  100.0
1170         ...DBM/Deep/Iterator/File.pm   92.5   84.6    n/a  100.0   66.7    0.6   90.0
1171         ...erator/File/BucketList.pm  100.0   75.0    n/a  100.0   66.7    0.4   93.8
1172         ...ep/Iterator/File/Index.pm  100.0  100.0    n/a  100.0  100.0    0.2  100.0
1173         blib/lib/DBM/Deep/Null.pm      87.5    n/a    n/a   75.0    n/a    0.0   83.3
1174         blib/lib/DBM/Deep/Sector.pm    91.7    n/a    n/a   83.3    0.0    6.7   74.4
1175         ...ib/DBM/Deep/Sector/DBI.pm   96.8   83.3    n/a  100.0    0.0    1.0   89.8
1176         ...p/Sector/DBI/Reference.pm  100.0   95.5  100.0  100.0    0.0    2.2   91.2
1177         ...Deep/Sector/DBI/Scalar.pm  100.0  100.0    n/a  100.0    0.0    1.1   92.9
1178         ...b/DBM/Deep/Sector/File.pm   96.0   87.5  100.0   92.3   25.0    2.2   91.0
1179         ...Sector/File/BucketList.pm   98.2   85.7   83.3  100.0    0.0    3.3   89.4
1180         .../Deep/Sector/File/Data.pm  100.0    n/a    n/a  100.0    0.0    0.1   90.9
1181         ...Deep/Sector/File/Index.pm  100.0   80.0   33.3  100.0    0.0    0.8   83.1
1182         .../Deep/Sector/File/Null.pm  100.0  100.0    n/a  100.0    0.0    0.0   91.7
1183         .../Sector/File/Reference.pm  100.0   90.0   80.0  100.0    0.0    1.4   91.5
1184         ...eep/Sector/File/Scalar.pm   98.4   87.5    n/a  100.0    0.0    0.8   91.9
1185         blib/lib/DBM/Deep/Storage.pm  100.0    n/a    n/a  100.0  100.0    0.0  100.0
1186         ...b/DBM/Deep/Storage/DBI.pm   97.3   70.8    n/a  100.0   38.5    6.7   87.0
1187         .../DBM/Deep/Storage/File.pm   96.6   77.1   80.0   95.7  100.0   16.0   91.8
1188         Total                          99.3   85.2   84.9   99.8   63.3  100.0   97.6
1189         ---------------------------- ------ ------ ------ ------ ------ ------ ------
1190

MORE INFORMATION

1192       Check out the DBM::Deep Google Group at
1193       <http://groups.google.com/group/DBM-Deep> or send email to
1194       DBM-Deep@googlegroups.com <mailto:DBM-Deep@googlegroups.com>.  You can
1195       also visit #dbm-deep on irc.perl.org
1196
1197       The source code repository is at <http://github.com/robkinyon/dbm-deep>
1198

MAINTAINERS

1200       Rob Kinyon, rkinyon@cpan.org <mailto:rkinyon@cpan.org>
1201
1202       Originally written by Joseph Huckaby, jhuckaby@cpan.org
1203       <mailto:jhuckaby@cpan.org>
1204

SPONSORS

1206       Stonehenge Consulting (<http://www.stonehenge.com/>) sponsored the
1207       development of transactions and freespace management, leading to the
1208       1.0000 release. A great debt of gratitude goes out to them for their
1209       continuing leadership in and support of the Perl community.
1210

CONTRIBUTORS

1212       The following have contributed greatly to make DBM::Deep what it is
1213       today:
1214
1215       •   Adam Sah and Rich Gaushell for innumerable contributions early on.
1216
1217       •   Dan Golden and others at YAPC::NA 2006 for helping me design
1218           through transactions.
1219
1220       •   James Stanley for bug fix
1221
1222       •   David Steinbrunner for fixing typos and adding repository cpan
1223           metadata
1224
1225       •   H. Merijn Brandt for fixing the POD escapes.
1226
1227       •   Breno G. de Oliveira for minor packaging tweaks
1228

SEE ALSO

1230       DBM::Deep::Cookbook(3)
1231
1232       perltie(1), Tie::Hash(3), Fcntl(3), flock(2), lockf(3), nfs(5)
1233

LICENSE

1235       Copyright (c) 2007-14 Rob Kinyon. All Rights Reserved.  This is free
1236       software, you may use it and distribute it under the same terms as Perl
1237       itself.
1238
1239
1240
1241perl v5.36.0                      2023-01-20                      DBM::Deep(3)
Impressum