|
NAMEDWH_File 0.22 - data and object persistence in deep and wide hashesSYNOPSISuse DWH_File qw/ GDBM_File /; # the use argument set the DBM module used tie( %h, DWH_File, 'myFile', O_RDWR|O_CREAT, 0644 ); untie( %h ); # essential! DESCRIPTIONNote: the files produced by DWH_File 0.22 are in a different format and are incompatible with the files produced by previous versions.DWH_File is used in a manner resembling NDBM_File, DB_File etc. These DBM modules are limited to storing flat scalar values. References to data such as arrays or hashes are stored as useless strings and the data in the referenced structures will be lost. DWH_File uses one of the DBM modules (configurable through the parameters to "use()"), but extends the functionality to not only save referenced data structures but even object systems. This is why I made it. It makes it extremely simple to achieve persistence in object oriented Perl programs and you can skip the cumbersome interaction with a conventional database. DWH_File tries to make the tied hash behave as much like a standard Perl hash as possible. Besides the capability to store nested data structures DWH_File also implements "exists()", "delete()" and "undef()" functionality like that of a standard hash (as opposed to all the DBM modules). MULTIPLE DBM FILESIt is possible to distribute for instance an object system over several files if wanted. This might be practical to avoid huge single files and may also make it easier make a reasonable structure in the data. If this feature is used the same set of files should be tied each time if any of the contents that may refer across files is altered. See MODELS.GARBAGE COLLECTIONDWH_File uses a garbage collection scheme similar to that of Perl itself. This means that you actually don't have to worry about freeing anything (see the cyclic reference caveat though). Just like Perl DWH_File will remove entries that nothing is pointing to (and therefore noone can ever get at). If you've got a key whose value refers to an array for instance, that array will be swept away if you assign something else to the key. Unless there's a reference to the array somewhere else in the structure. This works even across different dbm files when using multiple files.The garbage collection housekeeping is performed at untie time - so it is mandatory to call untie (and if you keep any references to the tied object to undef those in advance). Otherwise you'll leave the object at the mercy of global destruction and garbage won't be properly collected. MUTUAL EXCLUSIONEalier versions had some specialized locking schemes to deal with concurrency in eg. web-applications. I havn't put any into this version, and I think I'll leave them out to avoid scope creep.The reason for having those features were that locking dbm-files isn't as straightforward as locking ordinary files. I find now, that the best solution is to use some of the generalized mechanisms for handling concurrency. There are some fine perl modules for facilitating the use of semaphores for instance. LOGGINGEarlier versions had a logging feature. I haven't put it into this new generation of DWH_File yet. If you need it, send me a mail. That might tempt me.FURTHER INFORMATIONhttp://www.orqwood.dk/perl5/dwh/ - home of the DWH_File.As of this writing, there's nothing much there, but I hope to find time to make a series of examples and templates to show just how beautiful life can be if you only remember to use DWH_File; MODELSA typical script using DWH_Fileuse Fcntl; use DWH_File; # with no extra parameters to use() DWH_File defaults to: # AnyDBM_File tie( %h, DWH_File, 'myFile.dbm', O_RDWR|O_CREAT, 0644 ); # use the hash ... # cleanup # (necessary whenever reference values have been tampered with) untie %h; A script using data in three different filesThe data in one file may refer to that in another and even that reference will be persistent.use Fcntl; use DWH_File; tie( %a, DWH_File, 'fileA', O_RDWR|O_CREAT, 0644 ); tie( %b, DWH_File, 'fileB', O_RDWR|O_CREAT, 0644 ); tie( %c, DWH_File, 'fileC', O_RDWR|O_CREAT, 0644 ); $a{ doo } = [ qw(doo bi dee), { a => "ah", u => "uh" } ]; $b{ bi } = $a{ doo }[ 3 ]; # this will work print "$b{ bi }{ a }\n"; # prints "ah"; $b{ bi }{ a } = "I've changed"; print "$a{ doo }[ 3 ]{ a }\n"; # prints "I've changed" # note that if - in another program - you tie %g to 'fileB' # without also having tied some other hash variable to 'fileA') # then $g{ bi } will be undefined. The actual data is in 'fileA'. # Moreover there will be a high risk of corrupting the data. # cleanup # (necessary whenever reference values have been tampered with) untie %a; untie %b; untie %c; Earlier versions of DWH_File used a tag in each individual data file to identlify it in the context of other file. From 0.22 a file-URI is used in stead. This removes some problems related to the tagging scheme, but it introduces an obligation that your data files don't move around. A persistent classEarlier versions of DWH_File demanded, that the package defining a class set a special variable to a special value for the class to be persistent in DWH_File.The intention was to avoid weird results of trying to save objects of classes which didn't know about DWH_File and thus would not work properly when retrieved (because they didn't define methods to restore state that DWH_File couln't restore automatically - like open files etc.). I've removed this limitation, so DWH_File has become more sticky. I'm thinking of a new and more elegant way, like demanding that classes have DWH_File::Object in their heritage. Something may appear later. NOTESPLATFORMSThis version of DWH_File seems to work on all unices and I expect it to work on Windows too. I'm eager to hear from anybody who's tested it on anything.MLDBMIt appears that DWH_File does much of the same stuff that the MLDBM module from CPAN does. There are substantial differences though, which means that both modules outperform the other in certain situations. DWH_Files main attractions are (a) it only has to load the data actually acessed into memory (b) it restores all referential identity (MLDBM sometimes makes duplicates) (c) it has an approach to setting up dynamic state elements (like sockets, pipes etc.) of objects at load time.Also at this point, MLDBM is a near-canon part of institutionalized Perl-culture. It may be expected to be thouroughly tested and stable. DWH_File is just the work of a single mind (one that you have no particular reason to trust), and to my surprise it hasn't gained much acceptance since I first put it on CPAN sometime 2000 (I think). (IN)COMPATIBILITYThis version cannot share files with versions 0.01 to 0.21 of DWH_File. If you have legacy data that you'd really like to convert to the new format, send me an email. I may write a convertion facility if persuaded. I don't need one myself, so if nobody asks for it, I probably won't make it.CAVEATS
BUGSPlease let me know if you find any.As the version number indicates this is an early beta state piece of software. Please contact me if you have any comments or suggestions - also language corrections or other comments on the documentation. COPYRIGHTCopyright (c) Jakob Schmidt/Orqwood Software 2003The DWH_File distribution is free software and may be used and distributed under the same terms as Perl itself. AUTHOR(S)Jakob Schmidt <schmidt@orqwood.dk>
Visit the GSP FreeBSD Man Page Interface. |