NAME
FlatFile::DataStore::Lite - Perl module that implements a lightweight flat file data store.
SYNOPSYS
use FlatFile::DataStore::Lite;
# new datastore object
my $dir = "/my/datastore/area";
my $name = "dsname";
my $ds = FlatFile::DataStore::Lite->new( { dir => $dir, name => $name } );
# create a record
my $record_data = "This is a test record.";
my $user_data = "Test1";
my $record = $ds->create( $record_data, $user_data );
my $record_number = $record->keynum;
# retrieve it
$record = $ds->retrieve( $record_number );
# update it
$record->data( "Updating the test record." );
$record = $ds->update( $record );
# delete it
$record = $ds->delete( $record );
DESCRIPTION
FlatFile::DataStore::Lite implements a lightweight flat file data store. When you create (store) a new record, it is appended to the flat file. When you update an existing record, the updated record is appended to the flat file. When you delete a record, a deleted record is appended to the flat file.
Methods support the following actions:
- create
- retrieve
- update
- delete
Scripts supplied in the distribution perform:
- validation
- migration
VERSION
FlatFile::DataStore::Lite version 0.01
CLASS METHODS
FlatFile::DataStore::Lite->new();
Constructs a new FlatFile::DataStore::Lite object.
Accepts hash ref giving values for dir
and name
.
my $ds = FlatFile::DataStore::Lite->new( { dir => $dir, name => $name } );
Returns a reference to the FlatFile::DataStore::Lite object.
OBJECT METHODS, Record Processing (CRUD)
create( $record_data[, $user_data] )
Creates a record. The parm $record_data
may be one of
- data string
- scalar reference (to the data string)
- FlatFile::DataStore::Lite::Record object
The parm $user_data
may be omitted if $record_data
is an object, in which case the user data will be gotten from it.
Returns a Flatfile::DataStore::Lite::Record object.
Note: the record data (but not user data) is stored in the FF::DS::Record object as a scalar reference. This is done for efficiency in the cases where the record data may be very large. Likewise, the first parm to create() is allowed to be a scalar reference.
retrieve( $num[, $pos] )
Retrieves a record. The parm $num
may be one of
- a key number, i.e., record sequence number
- a file number
The parm $pos
is required if $num
is a file number.
Returns a Flatfile::DataStore::Lite::Record object.
update( $object_or_string[, $record_data][, $user_data] )
Updates a record. The parm $object_or_string may be one of:
- FlatFile::DataStore::Lite::Record object
- FlatFile::DataStore::Lite::Preamble object
- Preamble string
The parms $record_data
and $user_data
may be omitted only if $object_or_string
is a FF::DS::Record object, in which case the record and user data will be gotten from it.
Returns a Flatfile::DataStore::Lite::Record object.
delete( $object_or_string[, $record_data][, $user_data] )
Deletes a record. The parm $object_or_string may be one of:
- FlatFile::DataStore::Lite::Record object
- FlatFile::DataStore::Lite::Preamble object
- Preamble string
The parms $record_data
and $user_data
may be omitted only if $object_or_string
is a FF::DS::Record object, in which case the record and user data will be gotten from it.
Returns a Flatfile::DataStore::Lite::Record object.
OBJECT METHODS, Accessors
$ds->specs( [$omap] )
Sets and returns the specs
attribute value if $omap
is given, otherwise just returns the value.
An 'omap' is an ordered hash as defined in
http://yaml.org/type/omap.html
That is, it's an array of single-key hashes. This ordered hash contains the specifications for constructing and parsing a record preamble as defined in the name.uri file.
$ds->dir( [$dir] )
Sets and returns the dir
attribute value if $dir
is given, otherwise just returns the value.
If $dir
is given, the directory must already exist.
Preamble accessors
The following methods set and return their respective attribute values if $value
is given. Otherwise, they just return the value.
$ds->indicator( [$value] ); # from uri (length-characters)
$ds->keynum( [$value] ); # from uri (length-base)
$ds->reclen( [$value] ); # from uri (length-base)
$ds->thisfnum( [$value] ); # from uri (length-base)
$ds->thisseek( [$value] ); # from uri (length-base)
$ds->user( [$value] ); # from uri (length-characters)
Other accessors
$ds->name( [$value] ); # from uri, name of data store
$ds->desc( [$value] ); # from uri, description of data store
$ds->recsep( [$value] ); # from uri (character(s))
$ds->uri( [$value] ); # full uri as is
$ds->preamblelen( [$value] ); # length of full preamble string
$ds->toclen( [$value] ); # length of toc entry
$ds->keylen( [$value] ); # length of stored keynum
$ds->keybase( [$value] ); # base of stored keynum
$ds->fnumlen( [$value] ); # length of stored file number
$ds->fnumbase( [$value] ); # base of stored file number
$ds->regx( [$value] ); # capturing regx for preamble string
$ds->datamax( [$value] ); # maximum bytes in a data file
$ds->crud( [$value] ); # hash ref, e.g.,
{
create => '+',
update => '=',
delete => '-'
}
(translates logical actions into their symbolic indicators)
Optional accessors
$ds->dirmax( [$value] ); # maximum files in a directory
$ds->dirlev( [$value] ); # number of directory levels
$ds->tocmax( [$value] ); # maximum toc entries
$ds->keymax( [$value] ); # maximum key entries
If no dirmax
, directories will keep being added to.
If no dirlev
, toc, key, and data files will reside in top-level directory. If dirmax
given, dirlev
defaults to 1.
If no tocmax
, there will be only one toc file, which will grow indefinitely.
If no keymax
, there will be only one key file, which will grow indefinitely.
OBJECT METHODS, UTILITARIAN
howmany( [$regx] )
Returns count of records whose indicators match regx, e.g.,
$self->howmany( qr/create|update/ );
$self->howmany( qr/delete/ );
$self->howmany( qr/oldupd|olddel/ );
If no regx, howmany() returns numrecs from the toc file.
CAVEATS
This module is still in an experimental state. The tests and pod are sparse. When I start using it in production, I'll up the version to 1.00.
Until then (afterwards, too) please use with care.
TODO
- iteration
- cgi to analyze data store configuation (for uri to point to)
- more tests
- more pod
AUTHOR
Brad Baxter, <bbaxter@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2009 by Brad Baxter
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.