Stas Bekman wrote: > > > tied(%dbm_array)->sync(); > > I believe that's not enough, because the reader may read data during the > write, resulting in corrupted data read. You have to add locking. see > the DBM chapter in the guide. >
You might add MLDBM::Sync to the docs, which easily adds locking to MLDBM. MLDBM is a front end to store complex data structures http://www.perl.com/CPAN-local/modules/by-module/MLDBM/CHAMAS/MLDBM-Sync-0.25.readme What's nice about MLDBM is you can easily swap in & out various dbms like SDBM_File, DB_File, GDBM_File, etc. More recently it even supports Tie::TextDir too, which provides key per file type storage which is good when you have a fast file system & big data you want to store. SYNOPSIS use MLDBM::Sync; # this gets the default, SDBM_File use MLDBM qw(DB_File Storable); # use Storable for serializing use MLDBM qw(MLDBM::Sync::SDBM_File); # use extended SDBM_File, handles values > 1024 bytes use Fcntl qw(:DEFAULT); # import symbols O_CREAT & O_RDWR for use with DBMs # NORMAL PROTECTED read/write with implicit locks per i/o request my $sync_dbm_obj = tie %cache, 'MLDBM::Sync' [..other DBM args..] or die $!; $cache{"AAAA"} = "BBBB"; my $value = $cache{"AAAA"}; ... DESCRIPTION This module wraps around the MLDBM interface, by handling concurrent access to MLDBM databases with file locking, and flushes i/o explicity per lock/unlock. The new [Read]Lock()/UnLock() API can be used to serialize requests logically and improve performance for bundled reads & writes. Here's some benchmarks on my 2.4.x linux box dual PIII 450 with a couple 7200 RPM IDE drives & raid-1 ext3 fs mounted default async. MLDBM-Sync-0.25]# perl bench/bench_sync.pl NUMBER OF PROCESSES IN TEST: 4 === INSERT OF 50 BYTE RECORDS === Time for 100 writes + 100 reads for SDBM_File 0.17 seconds 12288 bytes Time for 100 writes + 100 reads for MLDBM::Sync::SDBM_File 0.20 seconds 12288 bytes Time for 100 writes + 100 reads for GDBM_File 1.06 seconds 18066 bytes Time for 100 writes + 100 reads for DB_File 0.63 seconds 12288 bytes Time for 100 writes + 100 reads for Tie::TextDir .04 0.38 seconds 13192 bytes === INSERT OF 500 BYTE RECORDS === (skipping test for SDBM_File 100 byte limit) Time for 100 writes + 100 reads for MLDBM::Sync::SDBM_File 0.58 seconds 261120 bytes Time for 100 writes + 100 reads for GDBM_File 1.09 seconds 63472 bytes Time for 100 writes + 100 reads for DB_File 0.64 seconds 98304 bytes Time for 100 writes + 100 reads for Tie::TextDir .04 0.33 seconds 58192 bytes === INSERT OF 5000 BYTE RECORDS === (skipping test for SDBM_File 100 byte limit) Time for 100 writes + 100 reads for MLDBM::Sync::SDBM_File 1.37 seconds 4128768 bytes Time for 100 writes + 100 reads for GDBM_File 1.13 seconds 832400 bytes Time for 100 writes + 100 reads for DB_File 1.08 seconds 831488 bytes Time for 100 writes + 100 reads for Tie::TextDir .04 0.52 seconds 508192 bytes === INSERT OF 20000 BYTE RECORDS === (skipping test for SDBM_File 100 byte limit) (skipping test for MLDBM::Sync db size > 1M) Time for 100 writes + 100 reads for GDBM_File 1.76 seconds 2063912 bytes Time for 100 writes + 100 reads for DB_File 1.78 seconds 2060288 bytes Time for 100 writes + 100 reads for Tie::TextDir .04 1.27 seconds 2008192 bytes === INSERT OF 50000 BYTE RECORDS === (skipping test for SDBM_File 100 byte limit) (skipping test for MLDBM::Sync db size > 1M) Time for 100 writes + 100 reads for GDBM_File 3.52 seconds 5337944 bytes Time for 100 writes + 100 reads for DB_File 3.37 seconds 5337088 bytes Time for 100 writes + 100 reads for Tie::TextDir .04 2.80 seconds 5008192 bytes --Josh _________________________________________________________________ Joshua Chamas Chamas Enterprises Inc. NodeWorks Founder Huntington Beach, CA USA http://www.nodeworks.com 1-714-625-4051