ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/TDB_FileX/TDB_FileX.pm
Revision: 1.23
Committed: Mon May 5 08:20:52 2025 UTC (16 months, 3 weeks ago) by root
Branch: MAIN
Changes since 1.22: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package TDB_FileX;
2
3 use common::sense;
4
5 use Exporter ();
6 use XSLoader ();
7
8 our @ISA = qw(Exporter);
9
10 # Items to export into callers namespace by default. Note: do not export
11 # names by default without a very good reason. Use EXPORT_OK instead.
12 # Do not simply export all your public functions/methods/constants.
13
14 our %EXPORT_TAGS = (
15 flags => [qw(
16 ALLOW_NESTING
17 BIGENDIAN
18 CLEAR_IF_FIRST
19 CONVERT
20 DEFAULT
21 DISALLOW_NESTING
22 INCOMPATIBLE_HASH
23 INTERNAL
24 MUTEX_LOCKING
25 NOLOCK
26 NOMMAP
27 NOSYNC
28 SEQNUM
29 VOLATILE
30 )],
31 insert => [qw(
32 INSERT
33 MODIFY
34 REPLACE
35 )],
36 error => [qw(
37 SUCCESS
38 ERR_CORRUPT
39 ERR_EXISTS
40 ERR_IO
41 ERR_LOCK
42 ERR_LOCK_TIMEOUT
43 ERR_NOEXIST
44 ERR_NOLOCK
45 ERR_OOM
46 ERR_EINVAL
47 ERR_RDONLY
48 )],
49 debug => [qw(
50 DEBUG_FATAL
51 DEBUG_ERROR
52 DEBUG_WARNING
53 DEBUG_TRACE}
54 )],
55 );
56
57 our @EXPORT_OK;
58
59 Exporter::export_ok_tags qw(flags insert error debug);
60
61 $EXPORT_TAGS{all} = \@EXPORT_OK;
62
63 our $VERSION = '0.97';
64
65 XSLoader::load __PACKAGE__, $VERSION;
66
67 1;
68 __END__
69
70 =head1 NAME
71
72 TDB_FileX - Perl access to the trivial database library
73
74 =head1 SYNOPSIS
75
76 use TDB_FileX;
77
78 # tie interface
79 tie %hash, TDB_FileX => $filename,
80 hash_size => 8000,
81 mutex => 1,
82 ;
83 $hash{key} = 'value';
84 while (my ($k, $v) = each %hash) { print "$k -> $v\n" }
85
86 # OO interface
87 my $tdb = TDB_FileX->open ($filename, flags => TDB_FileX::CLEAR_IF_FIRST)
88 or die $!;
89
90 $tdb->store (key => 'value') or die $tdb->errorstr;
91 $tdb->traverse (sub { print "$_[0] -> $_[1]\n" });
92
93 =head1 DESCRIPTION
94
95 TDB is a simple database similar to GDBM, but allows multiple simultaneous
96 writers. It's main drawback is the need to manually configure a hash table
97 size in advance - see the C<hash_size> option for C<open>.
98
99 TDB_FileX provides a simple C<tie> interface, similar to DB_File and
100 friends; and an object-oriented interface, which provides access to most
101 of the functions in the TDB library.
102
103 =head2 COMPARISON TO OTHER DBMS
104
105 TDB stands for trivial database - and indeed, the database structure is
106 very simple, requiring manual sizing at creation time, and being limited
107 to 4GB size.
108
109 But otherwise, TDB has features that no other simple DBM has -
110 fine-grained locking, multiprocess database access and transactions.
111
112 GDBM_File for example has a single per-process lock, so only one process
113 can write to the database. Most others have no locking and will happily
114 corrupt your database. TDB_FileX is safe to use from multiple processes.
115
116 As for data safety, GDBM databases easily corrupt (in current version of
117 GDBM), and while GDBM databases can be 30% more compact than equivalent
118 TDB databases, they tend to grow over time (I regularly find 10GB GDBM
119 databases (actual disk usage) that reorganize to 300MB files and have
120 never stored more than that). TDB files do not usually have such bad
121 growth behaviour, but also have no reorganize function to shrink the
122 database again.
123
124 So, if you want safe access from multiple proceses and have a good idea
125 of how many keys to store, TDB_FileX is the thing. If you only want
126 protectioon against obvious data corruption and processes can wait, GDBM
127 is a probably a better choice.
128
129 =head1 ERROR HANDLKING
130
131 The TDB C API is not well designed - among other things, error handling is
132 a bit erratic. To compensate, many TDB functions that normally succeed are
133 marked with a [CROAK] - these will throw an exception on "unusual" error
134 conditions, the exception string being the error message. C<$!> is set to
135 the numerical error value in that case (and will stringify into the wrong
136 OS-error string, so don't do that!).
137
138 This is so that you can concentrate on the important parts without having
139 to check for an error status on every call, while there are still no
140 silent unexpected errors, as TDB_FileX will croak for you.
141
142 The other functions will not generally croak - check their description for
143 details on how errors are handled.
144
145 Functions that are not part of the TDB API (such as
146 C<register_hash_function>) may croak on errors without being marked as
147 such. The above only refers to TDB API functions, as they cannot throw
148 Perl exceptions themselves.
149
150 =head2 FUNCTIONS
151
152 =over 4
153
154 =item $tdb = tie %hash, TDB_FileX => $path[ , key => value...]
155
156 =item $tdb = TDB_FileX->open ($path[, key => value...])
157
158 TDB_FileX constructor (same as C<TIE>). Opens $path and returns a
159 TDB_FileX object. The same arguments may be passed to the C<tie>
160 function. On error, C<undef> is returned.
161
162 You should consider specifying at least C<log_cb> and C<hash_size> (and
163 possibly C<mutex>), everything else has sensible defaults.
164
165 To open a tdb file that is created and used by another application
166 (maximising compatibility):
167
168 my $tdb = TDB_FileX->open ($path, log_cb => sub { warn $_[1] })
169 or die "$path: failed to open\n";
170
171 To successfully open another database, you might have to duplicate some of
172 the settings, e.g. whether mutex locking is used or the hash function. The
173 C<log_cb> output usually will tell you what's wrong.
174
175 To open a tdb file with good performance for many thousands of large keys,
176 maybe not compatible to other programs:
177
178 my $tdb = TDB_FileX->open ($path,
179 hash_size => 10000,
180 hash => "xxh3",
181 mutex => 1,
182 nocow => 1,
183 log_cb => sub { warn $_[1] },
184 ) or die "$path: failed to open\n";
185
186 The following key-value pairs are understood:
187
188 =over
189
190 =item tdb_flags => $flags (default: C<TDB_FileX::DEFAULT>)
191
192 A set of flags that influence the behaviour and format of the database.
193
194 =over
195
196 =item C<DEFAULT>
197
198 Same as C<0> - no flags.
199
200 =item C<CLEAR_IF_FIRST>
201
202 If this is the first open, wipe the db.
203
204 =item C<INTERNAL>
205
206 In-memory database only, path will be ignored.
207
208 =item C<NOLOCK>
209
210 Don't do any locking.
211
212 =item C<NOMMAP>
213
214 Don't use mmap.
215
216 =item C<NOSYNC>
217
218 Don't use synchronous transactions.
219
220 =item C<SEQNUM>
221
222 Maintain a sequence number.
223
224 =item C<VOLATILE>
225
226 Activate the per-hashchain freelist, default 5. (Same as calling
227 C<set_max_dead> with C<5> instead of the default C<0>).
228
229 =item C<ALLOW_NESTING>
230
231 Allow transactions to nest.
232
233 =item C<DISALLOW_NESTING>
234
235 Disallow transactions to nest.
236
237 =item C<INCOMPATIBLE_HASH>
238
239 Better default hash function, but can't be opened by tdb < 1.2.6.
240
241 =item C<MUTEX_LOCKING>
242
243 Optimized locking using robust mutexes if supported,
244
245 =back
246
247 =item open_flags => $flags (default: C<Fcntl::O_RDWR | Fcntl::O_CREAT>)
248
249 Standard open flags, as used in C<sysopen>.
250
251 =item mode => $mode (default: C<0666>)
252
253 Standard file open mode, as used in C<sysopen>. Only used when creating
254 the database file.
255
256 =item hash_size => $int (default: internal to libtdb, but normally C<131>)
257
258 The size of the internal hash table - only used when creating the
259 database. The default is usually very low and only good for a few thousand
260 keys. As a rule of thumb, it should be at least one percent of the number
261 of keys you plan to store, e.g. for C<800000> keys you should use around a
262 size of C<8000>.
263
264 Every hash entry is only 4 octets, so usually it isn't an issue to make the
265 hash table too large.
266
267 To give you an idea of the performance, I inserted about 600000 records,
268 3GB of data, into a TDB file, using different hash sizes.
269
270 size time
271 131 160s
272 256 85s
273 1024 12s
274 4096 5s
275 8192 4s
276
277 As you can see, the default hash table size for this case caused it to use
278 40 times the time to insert than a larger hash table, the optimum being
279 around 75 keys per hash entry. But this database was easily cached in
280 memory - if that is not the case, you might want to consider a hash table
281 that is larger than the number of keys you want to store - each hash slot
282 only uses 4 octets.
283
284 =item log_cb => $cb->($level, $msg)
285
286 Specifies a code reference that is called with a message and a log level
287 (lower means more important, there are C<DEBUG_FATAL>, C<DEBUG_ERROR>,
288 C<DEBUG_WARNING> and C<DEBUG_TRACE}>). Unlike the "debug" in the name
289 might indicate, if you want to find out why, for instance, you could not
290 open a database, you need to use a logging callback.
291
292 =item hash => $hash (default: C<undef>)
293
294 Selects a hash function to use.
295
296 =over
297
298 =item C<"default"> or C<undef>
299
300 Use autodetection and use either C<"jenkins"> or the original tdb hash function.
301
302 =item C<"jenkins">
303
304 The jenkins hash function. Relatively fast, and recommended for modern tdb databases
305 that need to be interoperable between implementations.
306
307 =item C<"fnv1ax">
308
309 The FNV1-A hash with 32 bit post-mixing. Pretty good and very fast for
310 keys up to 10-20 octets.
311
312 =item C<"xxh3">
313
314 The XXH3 hash. Very good and fast especially for long keys.
315
316 =item a value returned by C<register_hash_function>
317
318 Use a custom hash function registered via C<register_hash_function>.
319
320 =back
321
322 =item mutex => $bool (default: C<0>)
323
324 TDB can take advantage of fast interprocess mutexes, which can be orders
325 of magnitude faster than the syscall-based locking used by default, but
326 only work on the same machine (the default C<fcntl>-based locking works
327 e.g. over NFS).
328
329 Normally, you need to call C<TDB_FileX::runtime_check_for_robust_mutexes>
330 and set the C<MUTEX_LOCKING> flag if support is indicated.
331
332 This option, when true, enables C<MUTEX_LOCKING> if it is supported
333 by the platform - which involves forking the process when opening the
334 database. When false, it will remove the flag.
335
336 It is recommended to keep this on, but enabling this changes the format
337 of the database, so might not be an option if interoperability with other
338 programs is required.
339
340 =item nocow => $bool (default: C<0>)
341
342 Set the no-copy-on-write flag I<iff> this flag is true, C<open_flags>
343 contains C<O_CREAT>, C<tdb_flags> do not contain C<INTERNAL> and this is
344 supported on the platform and filesystem. Copy-on-write filesystems have
345 to make a copy of every block written to, which can both be costly and can
346 cause massive fragmentation of the database file.
347
348 Setting the no-copy-on-write flag (same as C<chattr +C>) disables this,
349 usually at the expense of data protection (checksumming), reducing the
350 safety to the level of a normal filesystem such as ext4.
351
352 This is best effort and usually only takes effect when the
353 database is initially created. If it fails, TDB_FileX will simply
354 continue. Correctness should not be affected either way.
355
356 =back
357
358 There is no explicit close function. The database is closed implicitly
359 when there are no remaining references.
360
361 =item $tdb->set_logging_function ($cb->($level, $msg))
362
363 Set the logging function to use when this database object encounters
364 errors.
365
366 $cb is called with the severity level (an integer) and the message (a
367 string).
368
369 =item $code = $tdb->error
370
371 Returns the current error state of the C<$tdb> object. See the list of
372 error codes given in L<EXPORTS>.
373
374 =item $mesage = $tdb->errorstr
375
376 Returns a printable string that describes the error state of the
377 database.
378
379 =item $tdb->store ($key, $value[, $flag=REPLACE]) [CROAK]
380
381 Store $value in the database with key w$key. The $flag defaults to
382 C<REPLACE>, but can also be C<INSERT> or C<MODIFY>, see tdb_store(3) for
383 details.
384
385 =item $tdb->append ($key, $value) [CROAK]
386
387 Appends $data to the data already stored for $key, or creates a new entry with it.
388
389 =item $data = $tdb->fetch ($key) [CROAK]
390
391 Fetch the value associated with $key, or C<undef> if it is not found (or
392 on any error).
393
394 =item $tdb->delete ($key) [CROAK]
395
396 Delete the value associated with $key.
397
398 =item $bool = $tdb->exists ($key)
399
400 Return true if the $key is found, false otherwise.
401
402 =item $key = $tdb->firstkey
403
404 Return the key of the first value in the database. Returns C<undef> on
405 failure or if there are no keys in the database. See tdb_firstkey(3)
406 for details.
407
408 =item $key = $tdb->nextkey ($lastkey)
409
410 Return the next key in the database after $lastkey. Returns C<undef> on
411 failure or if there are no more keys in the database. See tdb_nextkey(3)
412 for details.
413
414 =item $tdb->reopen [CROAK]
415
416 Closes and reopens the database. Required after a
417 L<fork|perlfunc/fork>, if both processes wish to use the database.
418
419 B<NB:> If C<reopen> fails, then it is unsafe to call any further methods
420 on C<$tdb>. Thus, the only way to find out I<why> C<reopen> failed is to
421 use a logging function.
422
423 =item TDB_FileX::reopen_all [CROAK]
424
425 Closes and reopens all open databases. See C<reopen>.
426
427 B<NB:> If C<reopen_all> fails, there is no indication of I<which> C<$tdb>
428 objects failed or why. If you have to survive failures, you may wish to do
429 your own C<reopen> loop instead.
430
431 =item $tdb->traverse ($cb->($key, $data)) [CROAK]
432
433 Call $cb for each entry in the database. The callback should return false
434 to continue, or a true value to abort the traversal.
435
436 The callback is called with the key and value as arguments and should
437 return a false value if you wish to continue traversal, and a true value
438 if the traversal should be aborted.
439
440 C<traverse> returns the number of elements traversed. If $cb is C<undef>,
441 then this function simply counts the number of elements.
442
443 =item $tdb->traverse_read ($cb->($key, $data)) [CROAK]
444
445 Like C<traverse>, but only acquires a read lock.
446
447 =item $tdb->lockall [CROAK]
448
449 Lock an entire database with an exclusive write lock, returning false on
450 error. The purpose of this call is to avoid locking overhead for many
451 operations, but the database has to be unlocked manually when done.
452
453 =item $tdb->unlockall [CROAK]
454
455 Unlock an entire database previously locked with
456 C<lockall>.
457
458 =item $tdb->lockall_read [CROAK]
459
460 Same as C<lockall>, but uses a shared read lock instead of a writer lock.
461
462 =item $tdb->unlockall_read [CROAK]
463
464 Opposite of C<lockall_read>.
465
466 =item $tdb->lockall_mark [CROAK]
467
468 =item $tdb->lockall_unmark [CROAK]
469
470 These apparently mark and unmark locks internally, but do not actually do
471 locking. Probably you should not use this, but feel free to tell me when
472 these are useful.
473
474 =item $tdb->lockall_nonblock [CROAK]
475
476 =item $success = $tdb->lockall_read_nonblock [CROAK]
477
478 Try to lock, but instead of waiting, fail if the lock could not
479 be acquired. Returns true if the lock could be acquired, false
480 otherwise. Croaks on all other errors.
481
482 =item $tdb->transaction_start [CROAK]
483
484 Starts a transaction - all operations will be queued, but not applied
485 to the database, until the transaction is either committed C<transaction_commit>
486 or aborted/thrown away, with C<transaction_cancel>.
487
488 =item $tdb->transaction_start_nonblock [CROAK]
489
490 Tries to start a transaction, but instead of waiting, fail if the lock
491 could not be acquired. Returns true if the transaction could be started,
492 false otherwise. Croaks on all other errors.
493
494 Please tell me what this does.
495
496 =item $tdb->transaction_commit [CROAK]
497
498 Applies all changes in the transaction.
499
500 =item $tdb->transaction_cancel [CROAK]
501
502 Throws away all changes in the transaction.
503
504 =item $tdb->transaction_prepare_commit [CROAK]
505
506 Instead of calling C<transaction_commit> you can do the commit in two
507 phases by calling this method before C<transaction_commit>, which does the
508 expensive steps first.
509
510 =item $bool = $tdb->transaction_active
511
512 Returns true if a transaction is currently active.
513
514 =item $tdb->enable_seqnum
515
516 Enables sequence number generation support for the database.
517
518 =item $seq = $tdb->get_seqnum
519
520 Returns the current sequence number. Internally, this is a 32 bit unsigned
521 integer, but the API converts it into a native integer, so the same
522 internal sequence number might be represented differently on different
523 machines.
524
525 =item $tdb->increment_seqnum_nonblock
526
527 Increments the sequence number. Note that the sequence number is also
528 incremented by TDB itself oon many operations.
529
530 =item $size = $tdb->hash_size
531
532 Returns the size of the hash table. The size cannot be changed other than
533 by recreating the database.
534
535 =item $octets = $tdb->map_size
536
537 Returns the current mmap size for the database.
538
539 =item $flags = $tdb->get_flags
540
541 Returns the flags for the database (the same as the C<tdb_flags> in C<open>).
542
543 =item $tdb->add_flags ($flag)
544
545 Tried to add flags to the database - yes, the parameter says C<$flag> (singular)
546 but the documentation and the code say I<flags> (plural).
547
548 =item $tdb->remove_flags ($flag)
549
550 Attempts to remove flags from the database.
551
552 =item $tdb->set_max_dead ($max_dead)
553
554 Set the maximum number of dead records per hash chain.
555
556 =item $fileno = $tdb->fd
557
558 Returns the file descriptor (not file handle) for the underlying database file.
559
560 =item $path = $tdb->name
561
562 Returns the path used to open the database.
563
564 =item $tdb->wipe_all [CROAK]
565
566 Efficientlly deletes all entries in the database. This does not shrink the
567 file itself.
568
569 =item $tdb->repack [CROAK]
570
571 Tries to improve layout of the database by copying all items into a
572 temporary in-memory database, wiping the database, and copying all items
573 back. Yes, everything must fit into memory.
574
575 =item $bool = $tdb->check ([$cb->($key, $value)])
576
577 Does extensive checks on the database, optionally (if not C<undef> or
578 missing) calling a check function for each pair, which must return true if
579 the data is valid.
580
581 Returns a boolean indicating whether the database was found healthy and
582 all the calls to the callback returned true.
583
584 =item $bool = $tdb->rescue ($cb->($key, $value))
585
586 Tries to recover some or all key-value pairs from a potentially damanged
587 database file. For each recovered pair it calls the given callback.
588
589 Returns a boolean indicating whether the database was found healthy and
590 all the calls to the callback returned true.
591
592 =item $bool = TDB_FileX::runtime_check_for_robust_mutexes
593
594 Tests whether robust mutexes are available for locking. This involves
595 forking the process, so it can be costly and problematic. This function
596 needs to be called before using the C<MUTEX_LOCKING> flag. But see the
597 C<mutex> parametrer to C<open> for an alternative.
598
599 =item $tdb->dump_all
600
601 Dump the records and freelist to STDOUT in an almost human readable
602 form.
603
604 =item $summary = $tdb->summary
605
606 Return a textual summary of the database. The format isn't documented,
607 but for some random databas,e I got this output:
608
609 Size of file/data: 325001216/238324906
610 Header offset/logical size: 4001792/320999424
611 Number of records: 117209
612 Incompatible hash: no
613 Active/supported feature flags: 0x00000001/0x00000001
614 Robust mutexes locking: yes
615 Smallest/average/largest keys: 11/36/102
616 Smallest/average/largest data: 10/1997/2921430
617 Smallest/average/largest padding: 9/530/749374
618 Number of dead records: 0
619 Smallest/average/largest dead records: 0/0/0
620 Number of free records: 1126
621 Smallest/average/largest free records: 12/15312/14681136
622 Number of hash chains: 100000
623 Smallest/average/largest hash chains: 0/1/8
624 Number of uncoalesced records: 0
625 Smallest/average/largest uncoalesced runs: 0/0/0
626 Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: 1/72/19/5/0/1/0
627
628 =item $octets = $tdb->freelist_size
629
630 Returns the total number of free (unused) octets in the file.
631
632 =item $tdb->printfreelist
633
634 Dump the freelist to STDOUT.
635
636 =item $num_entries = $tdb->validate_freelist [CROAK]
637
638 Verifies consistency of freelist and return the number of entries. This
639 loads the whole freelist into memory, using an in-memory tdb database,
640 which did strike Jeremy as extremely clever.
641
642 =item $func_id = TDB_FileX::register_hash_function $callback->($key)
643
644 Registers a custom hash function. The callback should take a key and
645 return a 32 bit integer hash of it.
646
647 Returns a value that is suitable to be used as the name of a hash function
648 (C<hash> argument to C<open>).
649
650 A maximum of four custom hash functions can be registered.
651
652 =item TDB_FileX::unregister_hash_function $func_id
653
654 Frees the hash function registered by a previous call to
655 C<register_hash_function>. Note that you I<MUST NOT> unregister a function
656 that is still in use.
657
658 =back
659
660 =head2 EXPORTS
661
662 The tag C<:all> exports all of the constants. Nothing is exported by
663 default.
664
665 Individually or with the tag C<:flags>:
666
667 DEFAULT
668 CLEAR_IF_FIRST
669 INTERNAL
670 NOLOCK
671 NOMMAP
672 CONVERT
673 BIGENDIAN
674 NOSYNC
675 SEQNUM
676 VOLATILE
677 ALLOW_NESTING
678 DISALLOW_NESTING
679 INCOMPATIBLE_HASH
680 MUTEX_LOCKING
681
682 Individually or with the tag C<:insert>:
683
684 REPLACE
685 INSERT
686 MODIFY
687
688 Individually or with the tag C<:error>:
689
690 SUCCESS
691 ERR_CORRUPT
692 ERR_IO
693 ERR_LOCK
694 ERR_OOM
695 ERR_EXISTS
696 ERR_NOLOCK
697 ERR_LOCK_TIMEOUT
698 ERR_NOEXIST
699 ERR_EINVAL
700 ERR_RDONLY
701
702 Individually or with the tag C<:debug>:
703
704 DEBUG_FATAL
705 DEBUG_ERROR
706 DEBUG_WARNING
707 DEBUG_TRACE
708
709 =head1 UNICODE HANDLING
710
711 TDB databases can only store octet strings. Unlike most other database
712 interfaces, TDB_FileX will safely handle Perl strings by downgrading
713 them. Perl will warn about strings that cannot be downgraded.
714
715 =head1 DISK USAGE
716
717 TDB databses use 24 octets for every key-value pair, plus the octet size
718 of the key and sata, e.g. the pair "key" => "value" takes up 24+3+5 octets
719 on disk.
720
721 The database header is 168 octets (if I haven't miscounted).
722
723 Each hashtable entry is 4 octets, and one more than the hash size is
724 allocated, so the hash table size is (hash_size + 1) * 4.
725
726 =head1 LIMITATIONS
727
728 =head2 Database Size
729
730 As far as I can see, TDB databases are limited to 4 GB.
731
732 =head2 Hash Functions
733
734 Hash functioons need to be set globally - they are limited to a maximum of
735 4, but this can be easily extended, but requires source code editing. This
736 is due to a limitation of the TDB C API.
737
738 =head2 No recovery after failed C<reopen_all>
739
740 There is no way to survive an error during C<reopen_all>.
741 Unfortunately this is a limitation in the TDB C API.
742
743 =head2 No way to resize hash table
744
745 Performance degrades majorly when the databse grows larger then
746 accomodated by the hash table size, and the hash table size needs to be
747 set at database creation time and cannot be resized later.
748
749 =head1 SEE ALSO
750
751 tdb(3), L<perltie>.
752
753 =head1 AUTHOR
754
755 Angus Lees, E<lt>gus@inodes.org>
756
757 Currently maintained by Marc A. Lehmann <schmorp@schmorp.de>
758 http://home.schmorp.de/
759
760 =cut