NAME

Collection - Collections framework for CRUD of the data or objects.

SYNOPSIS

package MyCollection;
use Collection;
@MyCollection::ISA = qw(Collection);

DESCRIPTION

A collection - sometimes called a container - is simply an object that groups multiple elements into a single unit. Collection are used to store, retrieve, manipulate, and communicate aggregate data.

The primary advantages of a Collection framework are that it reduces programming effort by providing useful data structures and algorithms so you don't have to write them yourself.

The Collection framework consists of:

  • Wrapper Implementations - Add functionality, such as mirroring and lazy load, to other implementations.

  • Algorithms - methods that perform useful functions, such as caching.

This module has a task - to be a base class for ather Collections. You can inherit the methods _create, _delete, _fetch, _store and may be _prepare_record for new source of data. As you see this is similar to CRUD (Create - Read - Update- Delete).

Sample:

my $col = new MyCollection:: <some params>;
#fetch objects or data by keys
my $data = $col->fetch(1,2,3,4,5);
#do something
foreach my $item ( values %$data) {
    $_->attr->{inc} ++
}
#You can use "lazy" functionality
my $not_actualy_fetch = $col->get_lazy(6,7,8,9);
#store changed data or objects
$col->store;
#free memory
$col->release;

Sample from Collection::AutoSQL:

my $beers = new Collection::AutoSQL::
 dbh     => $dbh,          #database connect
 table   => 'beers',       #table name
 field   => 'bid',         #key field (IDs), usually primary,autoincrement
 cut_key => 1;             #delete field 'bid' from readed records,
   
   my $heineken = $beers->fetch_one(1);
   #SELECT * FROM beers WHERE bid in (1)

Sample from Collection::Memcached:

  use Collection::Memcached;
  use Cache::Memcached;
  $memd = new Cache::Memcached {
  'servers' => [ "127.0.0.1:11211" ],
  'debug' => 0,
  'compress_threshold' => 10_000,
};
my $collection = new Collection::Memcached:: $memd;
my $collection_prefix = new Collection::Memcached:: $memd, 'prefix';

METHODS

_store( {ID1 => <ref to object1>[, ID2 => <ref to object2>, ...]} )

Method for store changed objects. Called with ref to hash :

{
   ID1 => <reference to object1>
   [,ID2 => <reference to object2>,...]
}

_fetch(ID1[, ID2, ...])

Read data for given IDs. Must return reference to hash, where keys is IDs, values is readed data. For example:

return {1=>[1..3],2=>[5..6]}

_create(<user defined>)

Create recods in data storage.

Parametrs:

user defined format

Result: Must return reference to hash, where keys is IDs, values is create records of data

_delete(ID1[, ID2, ...])

Delete records in data storage for given IDs.

Parametrs: array id IDs

ID1, ID2, ...

or array of refs to HASHes

   {  id=>ID1 }, {id => ID2 }, ...

Format of parametrs depend method delete

_prepare_record( ID1, <reference to readed by _create record>)

Called before insert readed objects into collection. Must return ref to data or object, which will insert to callection.

create(<user defined>)

Public method for create objects.

fetch_one(ID1), get_one(ID1)

Public methods. Fetch object from collection for given ID. Return ref to objects or undef unless exists.

fetch(ID1 [, ID2, ...]) , get(ID1 [, ID2, ...])

Public methods. Fetch objects from collection for given IDs. Return ref to HASH, where where keys is IDs, values is objects refs.

Parametrs:

release(ID1[, ID2, ...])

Release from collection objects with IDs. Only delete given keys from collection or all if empty

store([ID1,[ID2,...]])

Call _store for changed objects. Store all loaded objects without parameters:

$simple_collection->store(); #store all changed

or (for 1,2,6 IDs )

$simple_collection->store(1,2,6);

delete(ID1[,ID2, ...])

Release from collections and delete from storage (by calling _delete) objects ID1,ID2...

$simple_collection->delete(1,5,84);

get_lazy(ID1)

Method for base support lazy load objects from data storage. Not really return lazy object.

SEE ALSO

Collection::Memcached, Collection::Mem, Collection::AutoSQL, README

AUTHOR

Zahatski Aliaksandr, <zag@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2005-2008 by Zahatski Aliaksandr

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.