ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/TDB_FileX/TDB_FileX.pm
Revision: 1.9
Committed: Fri May 2 21:01:26 2025 UTC (17 months, 1 week ago) by root
Branch: MAIN
Changes since 1.8: +28 -0 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 =head1 ERROR HANDLKING
104
105 The TDB C API is not well designed - among other things, error handling is
106 a bit erratic. Many functions that normally should just work are marked
107 with a [CROAK] - these will throw an exception on error, the exception
108 string being the error message. C<$!> is set to the numerical error value
109 in that case (and will stringify into the wrong OS-error string, so don't
110 do that!).
111
112 This is so that you can concentrate on the important parts, while there
113 are still no silent unexpected errors.
114
115 The other functions will not generally croak - check their description for
116 details on how errors are handled.
117
118 =head2 FUNCTIONS
119
120 =over 4
121
122 =item $tdb = tie %hash, TDB_FileX => $path[ , key => value...]
123
124 =item $tdb = TDB_FileX->open ($path[, key => value...])
125
126 TDB_FileX constructor (same as C<TIE>). Opens $path and returns a
127 TDB_FileX object. The same arguments may be passed to the C<tie>
128 function. On error, C<undef> is returned.
129
130 To open a tdb file that is created and used by another application
131 (maximising compatibility):
132
133 my $tdb = TDB_FileX->open ($path, log_cb => sub { warn $_[1] })
134 or die "$path: failed to open\n";
135
136 To open a tdb file with good performance for many thousands of large keys,
137 maybe not compatible to others:
138
139 my $tdb = TDB_FileX->open ($path,
140 hash_size => 10000,
141 hash => "xxh3",
142 mutex => 1,
143 nocow => 1,
144 log_cb => sub { warn $_[1] },
145 ) or die "$path: failed to open\n";
146
147 A number of key-value pairs are accepted, with hopefully sensible defaults
148 for most of these (but you should consider at least setting C<hash_size>
149 if you want to store more than a thousand or so pairs, and C<mutex> if you
150 want to take advantage of much faster locking.
151
152 =over
153
154 =item tdb_flags => $flags (default: C<TDB_FileX::DEFAULT>)
155
156 A set of flags that influence the behaviour and format of the database.
157
158 See tdb_open(3) for the meanings and possible values (note that the
159 C<TDB_> prefix has to be removed, see the L<EXPORTS> section for a list.
160
161 =item open_flags => $flags (default: C<Fcntl::O_RDWR | Fcntl::O_CREAT>)
162
163 Standard open flags, as used in C<sysopen>.
164
165 =item mode => $mode (default: C<0666>)
166
167 Standard file open mode, as use din C<sysopen>. Only used when creating
168 the database file.
169
170 =item hash_size => $int (default: internal to libtdb, but normally C<131>)
171
172 The size of the internal hash table - only used when creating the
173 database. The default is usually very low and only good for a few thousand
174 keys. As a rule of thumb, it should be at least one percent of the number
175 of keys you plan to store, e.g. for C<800000> keys you should use around a
176 size of C<8000>.
177
178 Every hash entry is only 4 octets, os usually it isn't an issue to make the
179 hash table too large.
180
181 To give you an idea of the performance, I inserted about 600000 records,
182 3GB of data, into a TDB file, using different hash sizes.
183
184 size time
185 131 160s
186 256 85s
187 1024 12s
188 4096 5s
189 8192 4s
190
191 As you can see, the default hash table size for this case caused it to
192 use 40 times the time to insert than a larger hash table, the optimum
193 being around 75 keys per hash entry. But the databse easily was cached in
194 memory. If that is not the case, you might want to consider a hash table
195 that is larger than the number of keys you want to store - each hash slot
196 only uses 4 octets.
197
198 =item log_cb => $cb->($level, $msg)
199
200 Sets a code reference that is called with a message and a log level
201 (lower means more important, there are C<DEBUG_FATAL>, C<DEBUG_ERROR>,
202 C<DEBUG_WARNING> and C<DEBUG_TRACE}>). Unlike the "debug" in the name
203 might indicate, if you want to find out why, for instance, you could not
204 open a database, you need to use a logging callback.
205
206 =item hash => $hash (default: C<undef>)
207
208 Selects a hash function to use.
209
210 =over
211
212 =item C<"default"> or C<undef>
213
214 Use autodetection and use either C<"jenkins"> or the original tdb hash function.
215
216 =item C<"jenkins">
217
218 The jenkins hash function. Relatively fast, and recommended for modern tdb databases
219 that need to be interoperable between implementations.
220
221 =item C<"fnv1ax">
222
223 The FNV1-A hash with 32 bit post-mixing. Pretty good and verxy fast for
224 keys up to 10-20 octets.
225
226 =item C<"xxh3">
227
228 The XXH3 hash. Very good and fast especially for long keys.
229
230 =item C<1> .. C<4>
231
232 Specify one of up to four custom hash functions (see L<set_hash_function>).
233
234 =back
235
236 =item mutex => $bool (default: C<0>)
237
238 TDB can take advantage of fast interprocess mutexes, which can be
239 orders of magnitude faster than the syscall-based locking used by
240 default, but only works on the same machine.
241
242 Normally, you need to call C<TDB_FileX::runtime_check_for_robust_mutexes>
243 and set the C<MUTEX_LOCKING> flag if support is indicated.
244
245 This option, when enabled, enables C<MUTEX_LOCKING> if it is supported
246 by the platform - which involves a fork when opening the database. When
247 disabled, it will remove the flag.
248
249 It is recommended to keep this one, but enabling this changes the format
250 of the database, so might not be an option if interoperability with other
251 programs is required.
252
253 =item nocow => $bool (default: C<0>)
254
255 Set the no-copy-on-write flag I<iff> this flag is true, C<open_flags>
256 contains C<O_CREAT>, C<tdb_flags> do not contain C<INTERNAL> and this is
257 supported on the platform and filesystem. Copy-on-write filesystems have
258 to make a copy of every block written to, which can both be costly and can
259 cause massive fragmentation of the database file.
260
261 Setting the no-copy-on-write flag (same as C<chattr +C>) disables this,
262 usually at the expense of data protection (checksumming), reducing the
263 safety to the level of a normal filesystem such as ext4.
264
265 This is best effort and usually only takes effect when the
266 database is initially created. If it fails, TDB_FileX will simply
267 continue. Correctness should not be affected either way.
268
269 =back
270
271 There is no explicit close function. The database is closed implicitly
272 when there are no remaining references.
273
274 =item $tdb->store ($key, $value[, $flag=REPLACE]) [CROAK]
275
276 Store $value in the database with key w$key. The $flag defaults to
277 C<REPLACE>, but can also be C<INSERT> or C<MODIFY>, see tdb_store(3) for
278 details.
279
280 =item $tdb->append ($key, $value) [CROAK]
281
282 Appends $data to the data already stored for $key, or creates a new entry with it.
283
284 =item $tdb->fetch ($key)
285
286 Fetch the value associated with $key, or C<undef> if it is not found (or
287 on any error).
288
289 =item $tdb->delete ($key) [CROAK]
290
291 Delete the value associated with $key.
292
293 On failure, a perl false is returned. See L</$tdb->error> and
294 L</$tdb->errorstr> for the reason.
295
296 =item $bool = $tdb->exists ($key)
297
298 Return true if the $key is found, false otherwise.
299
300 =item $tdb->firstkey
301
302 Return the key of the first value in the database. Returns C<undef> on
303 failure or if there are no keys in the database. See tdb_firstkey(3)
304 for details.
305
306 =item $tdb->nextkey ($lastkey)
307
308 Return the next key in the database after $lastkey. Returns C<undef> on
309 failure or if there are no more keys in the database. See tdb_nextkey(3)
310 for details.
311
312 =item $tdb->error
313
314 Returns the current error state of the C<$tdb> object. See the list of
315 error codes given in L<EXPORTS>.
316
317 =item $tdb->errorstr
318
319 Returns a printable string that describes the error state of the
320 database.
321
322 =item $tdb->reopen [CROAK]
323
324 Closes and reopens the database. Required after a
325 L<fork|perlfunc/fork>, if both processes wish to use the database.
326
327 B<NB:> If C<reopen> fails, then it is unsafe to call any further methods
328 on C<$tdb>. Thus, the only way to find out I<why> C<reopen> failed is to
329 use a logging function.
330
331 =item TDB_FileX::reopen_all [CROAK]
332
333 Closes and reopens all open databases. See L</$tdb->reopen>.
334
335 B<NB:> If C<reopen_all> fails, there is no indication of I<which> C<$tdb>
336 objects failed or why. If you have to survive failures, you may wish to do
337 your own C<reopen> loop instead.
338
339 =item $tdb->traverse ($cb->($key, $data)) [CROAK]
340
341 Call $cb for each entry in the database. The callback should return false
342 to continue, or a true value to abort the traversal.
343
344 The callback is called with the key and value as arguments and should
345 return a false value if you wish to continue traversal, and a true value
346 if the traversal should be aborted.
347
348 C<traverse> returns the number of elements traversed. If $cb is C<undef>,
349 then this function simply counts the number of elements.
350
351 =item $tdb->traverse_read ($cb->($key, $data)) [CROAK]
352
353 Like C<traverse>, but only acquires a read lock.
354
355 =item $tdb->set_logging_function ($cb->($level, $msg))
356
357 Set the logging function to use when this database object encounters
358 errors.
359
360 $cb is called with the severity level (an integer) and the message (a
361 string).
362
363 =item $tdb->lockall [CROAK]
364
365 Lock an entire database with an exclusive write lock, returning false on
366 error. The purpose of this call is to avoid locking overhead for many
367 operations, but the database has to be unlocked manually when done.
368
369 =item $tdb->unlockall [CROAK]
370
371 Unlock an entire database previously locked with
372 L</$tdb->lockall>.
373
374 =item $tdb->lockall_read [CROAK]
375
376 Same as L</$tdb->lockall>, but uses a shared read lock instead of a writer lock.
377
378 =item $tdb->unlockall_read [CROAK]
379
380 Opposite of L</$tdb->lockall_read>.
381
382 =item $tdb->lockall_mark [CROAK]
383
384 =item $tdb->lockall_unmark [CROAK]
385
386 These apparently mark and unmark locks internally, but do not actually do
387 locking. Probably you should not use this, but feel free to tell me when
388 these are useful.
389
390 =item $tdb->lockall_nonblock [CROAK]
391
392 =item $success = $tdb->lockall_read_nonblock [CROAK]
393
394 Try to lock, but instead of waiting, fail if the lock could not
395 be acquired. Returns true if the lock could be acquired, false
396 otherwise. Croaks on all other errors.
397
398 =item $tdb->transaction_start [CROAK]
399
400 Starts a transaction - all operations will be queued, but not applied
401 to the database, until the transaction is either committed L</$tdb->transaction_commit>
402 or aborted/thrown away, with L</$tdb->transaction_cancel>.
403
404 =item $tdb->transaction_start_nonblock [CROAK]
405
406 Tries to start a transaction, but instead of waiting, fail if the lock
407 could not be acquired. Returns true if the transaction could be started,
408 false otherwise. Croaks on all other errors.
409
410 Please tell me what this does.
411
412 =item $tdb->transaction_commit [CROAK]
413
414 Applies all changes in the transaction.
415
416 =item $tdb->transaction_cancel [CROAK]
417
418 Throws away all changes in the transaction.
419
420 =item $tdb->transaction_prepare_commit [CROAK]
421
422 Instead of calling L</$tdb->transaction_commit> you can do the commit in
423 two phases by calling this method before commit, which does the expensive
424 steps first.
425
426 =item $bool = $tdb->transaction_active
427
428 Returns true if a transaction is currently active.
429
430 =item $tdb->enable_seqnum
431
432 Enables sequence number generatiuon supporet for the database.
433
434 =item $seq = $tdb->get_seqnum
435
436 Returns the current sequence number. Internally, this is a 32 bit unsigned
437 integer, but the API converts it into a native integer, so the same
438 internal sequence number might be represented differently on different
439 machines.
440
441 =item $tdb->increment_seqnum_nonblock
442
443 Increments the sequence number. Note that the sequence number is also
444 incremented by TDB itself oon many operations.
445
446 =item $size = $tdb->hash_size
447
448 Returns the size of the hash table. The size cannot be changed other than
449 by recreating the database.
450
451 =item $octets = $tdb->map_size
452
453 Returns the current mmap size for the database.
454
455 =item $flags = $tdb->get_flags
456
457 Returns the flags for the database (the same as the C<tdb_flags> in C<open>).
458
459 =item $tdb->add_flags ($flag)
460
461 Tried to add flags to the database - yes, the parameter says C<$flag> (singular)
462 but the documentation and the code say I<flags> (plural).
463
464 =item $tdb->remove_flags ($flag)
465
466 Attempts to remove flags from the database.
467
468 =item $fileno = $tdb->fd
469
470 Returns the file descriptor (not file handle) for the underlying database file.
471
472 =item $path = $tdb->name
473
474 Returns the path used to open the database.
475
476 =item $tdb->wipe_all [CROAK]
477
478 Efficientlly deletes all entries in the database. This does not shrink the
479 file itself.
480
481 =item $tdb->repack [CROAK]
482
483 Tries to improve layout of the database by copying all items into a
484 temporary in-memory database, wiping the database, and copying all items
485 back. Yes, everything must fit into memory.
486
487 =item $bool = $tdb->check ($cb->($key, $value))
488
489 Does extensive checks on the database, optionally (if not C<undef>)
490 calling a check function for each pair, which must return true if the data
491 is valid.
492
493 Returns a boolean indicating whether the database was found healthy and
494 all the calls to the callback returned true.
495
496 =item $bool = $tdb->rescue ($cb->($key, $value))
497
498 Tries to recover some or all key-value pairs from a potentially damanged
499 database file. For each recovered pair it calls the given callback.
500
501 Returns a boolean indicating whether the database was found healthy and
502 all the calls to the callback returned true.
503
504 =item $bool = TDB_FileX::runtime_check_for_robust_mutexes
505
506 Tests whether robust mutexes are available for locking. This involves
507 forking the process, so it can be costly and problematic. This function
508 needs to be called before using the C<MUTEX_LOCKING> flag. But see the
509 C<mutex> parametrer to C<open> for an alternative.
510
511 =item $tdb->dump_all
512
513 Dump the records and freelist to STDOUT in an almost human readable
514 form.
515
516 =item $summary = $tdb->summary
517
518 Return a textual summary of the database. The format isn't documented,
519 but for some random databas,e I got this output:
520
521 Size of file/data: 325001216/238324906
522 Header offset/logical size: 4001792/320999424
523 Number of records: 117209
524 Incompatible hash: no
525 Active/supported feature flags: 0x00000001/0x00000001
526 Robust mutexes locking: yes
527 Smallest/average/largest keys: 11/36/102
528 Smallest/average/largest data: 10/1997/2921430
529 Smallest/average/largest padding: 9/530/749374
530 Number of dead records: 0
531 Smallest/average/largest dead records: 0/0/0
532 Number of free records: 1126
533 Smallest/average/largest free records: 12/15312/14681136
534 Number of hash chains: 100000
535 Smallest/average/largest hash chains: 0/1/8
536 Number of uncoalesced records: 0
537 Smallest/average/largest uncoalesced runs: 0/0/0
538 Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: 1/72/19/5/0/1/0
539
540 =item $octets = $tdb->freelist_size
541
542 Returns the total number of free (unused) octets in the file.
543
544 =item $tdb->printfreelist
545
546 Dump the freelist to STDOUT.
547
548 =back
549
550 =head2 EXPORTS
551
552 Nothing constants are exported by default.
553
554 The tag C<:all> exports allpo of the constants.
555
556 Individually or with the tag C<:flags>:
557
558 DEFAULT
559 CLEAR_IF_FIRST
560 INTERNAL
561 NOLOCK
562 NOMMAP
563 CONVERT
564 BIGENDIAN
565 NOSYNC
566 SEQNUM
567 VOLATILE
568 ALLOW_NESTING
569 DISALLOW_NESTING
570 INCOMPATIBLE_HASH
571 MUTEX_LOCKING
572
573 Individually or with the tag C<:insert>:
574
575 REPLACE
576 INSERT
577 MODIFY
578
579 Individually or with the tag C<:error>:
580
581 SUCCESS
582 ERR_CORRUPT
583 ERR_IO
584 ERR_LOCK
585 ERR_OOM
586 ERR_EXISTS
587 ERR_NOLOCK
588 ERR_LOCK_TIMEOUT
589 ERR_NOEXIST
590 ERR_EINVAL
591 ERR_RDONLY
592
593 Individually or with the tag C<:debug>:
594
595 DEBUG_FATAL
596 DEBUG_ERROR
597 DEBUG_WARNING
598 DEBUG_TRACE
599
600 =head1 UNICODE HANDLING
601
602 TDB databases can only store octet strings. Unlike most other database
603 interfaces, TDB_FileX will safely handle Perl strings by downgrading
604 them. Perl will warn about strings that cnanot be downgraded.
605
606 =head1 DISK USAGE
607
608 TDB databses use 24 octets for every key-value pair, plus the octet size
609 of the key and sata, e.g. the pair "key" => "value" takes up 24+3+5 octets
610 on disk.
611
612 The database header is 168 octets (if I haven't miscounted).
613
614 Each hashtable entry is 4 octets, and one more than the hash size is
615 allocated, so the hahs table size is (hash_size + 1) * 4.
616
617 =head1 LIMITATIONS
618
619 =head2 Database Size
620
621 As far as I can see, TDB dastabases are limited to 4 GB.
622
623 =head2 Hash Functions
624
625 Hash functioons need to be set globally - they are limited to a maximum of
626 4, but this can be easily extended, but requires source code editing. This
627 is due to a limitation of the TDB C API.
628
629 =head2 No recoivery after failed C<reopoen_all>
630
631 There is no way to survive an error during C<reopen_all>.
632 Unfortunately this is a limitation in the TDB C API.
633
634 =head1 SEE ALSO
635
636 tdb(3), L<perltie>.
637
638 =head1 AUTHOR
639
640 Angus Lees, E<lt>gus@inodes.org>
641
642 Currently maintained by Marc A. Lehmann <schmorp@schmorp.de>
643 http://home.schmorp.de/
644
645 =cut