Collection::Storable - class for collections of data, stored in files.


Collection documentation Contained in the Collection distribution.

Index


Code Index:

NAME

Top

Collection::Storable - class for collections of data, stored in files.

SYNOPSIS

Top

    use File::Temp qw/ tempfile tempdir /;
    my $tmp_dir = tempdir();
    my $coll = new Collection::Storable:: $tmp_dir

DESCRIPTION

Top

Class for collections of data, stored in files.

METHODS

Top

new <path_to_root_of_store>

Creates a new Collection::Storable object.

      my $coll = new Collection::Storable:: $tmp_dir

key2path <key1>[, <key2>, <keyn> ...]

translate keys to store path

return hash of

    {
      <key1> => <relative path to key>

    }

path2key <path1>[, <path1>, <pathX> ...]

translate store path to key

return hash of

    {
      <relative path to key>=><key1>

    }

SEE ALSO

Top

Collection, README

AUTHOR

Top

Zahatski Aliaksandr, <zag@cpan.org>

COPYRIGHT AND LICENSE

Top


Collection documentation Contained in the Collection distribution.
package Collection::Storable;

use Collection;
use Collection::Utl::Base;
use Data::Dumper;
use Collection::Utl::ActiveRecord;
use Storable qw(lock_nstore lock_retrieve);

use strict;
use warnings;

our @ISA     = qw(Collection);
our $VERSION = '0.01';

attributes qw/ _store_path  /;

sub _init {
    my $self = shift;
    my $path = shift || return undef;
    $path .= "/" unless $path =~ m%/$%;
    $self->_store_path($path);
    $self->SUPER::_init();
    return 1;
}

sub key2path {
    my $self = shift;
    my %res  = ();
    @res{@_} = @_;
    return \%res;
}

sub path2key {
    my $self = shift;
    my %res  = ();
    @res{@_} = @_;
    return \%res;
}

sub _delete {
    my $self = shift;
    my @ids  =  @_;
    my $path = $self->_store_path;

    #convert ids to pathes
    my $key2path = $self->key2path(@ids);
    unlink( $path . $_ ) for values %$key2path;
    [ keys %$key2path ];
}

sub _create {
    my $self    = shift;
    my %to_save = @_;
    $self->_store( \%to_save );
    return \%to_save;
}

sub _fetch {
    my $self = shift;
    my @ids  =  @_;
    my $path = $self->_store_path;

    #convert keys to path
    my $key2path = $self->key2path(@ids);
    my %res      = ();
    while ( my ( $key, $kpath ) = each %$key2path ) {
        my $fpath = $path . $kpath;
        next unless -e $fpath;
        $res{$key} = lock_retrieve($fpath);
    }
    \%res;
}

sub _prepare_record {
    my ( $self, $key, $ref ) = @_;
    my %hash;
    tie %hash, 'Collection::Utl::ActiveRecord', hash => $ref;
    return \%hash;
}

sub _store {
    my $self = shift;
    my $in   = shift;
    my $path = $self->_store_path;
    while ( my ( $key, $val ) = each %$in ) {
        my $file_name = $path . $self->key2path($key)->{$key};
        lock_nstore( {%$val} || {}, $file_name );
    }
}

sub __get_list_files {
    my $self = shift;
    my $path = shift;
    my @res  = ();
    if ( opendir DIR, $path ) {
        while ( my $name = readdir DIR ) {
            next if ( $name eq '.' ) or ( $name eq '..' );
            my $fpath = $path . $name;
            if ( -d $fpath ) {
                push @res, $self->__get_list_files($fpath);
            }
            else {
                push @res, $fpath;
            }
        }
        closedir DIR;
    }
    return @res

}

sub list_ids {
    my $self = shift;
    my $path = $self->_store_path;

    #get content of path
    my @paths      = $self->__get_list_files($path);
    my $prefix_len = length $path;

    #cut root path
    $_ = substr( $_, $prefix_len, length($_) - $prefix_len ) for @paths;
    return [ values %{ $self->path2key(@paths) } ];
}

1;
__END__