NAME

Podius::Persistence::Media::FileBased - File-based persistence layer


SYNOPSIS

  # This example is not up to date and will not work
  use Podius::Persistence::Media::FileBased;
  $data_root_dir = '/usr/local/project/data';
  $pm = new Podius::Persistence::Media::FileBased($data_root_dir);
  $pm->insert(
    'students',
    [1, 2],
    [{ name => 'Tom', age => '22' }, { name => 'Bob', age => '34' }]
  );
  my $first_student_name = $pm->retrieve('students', [1])->[0]->{name};
  $pm->delete('students', [1]);


DESCRIPTION

This package implements File Based Persistence Media for Podius Persistence Layer.

Table inheritance and single level transactions are supported.

Data is stored in files. Each file is named ``<key>.dat'' and placed in directory named ``table/<table>''. The directories are in the data base directory, which is specified in the constructor.


REQUIREMENTS

the Podius::Persistence::Media manpage, the File::Operations manpage, the Exception manpage, the Debug manpage, the Podius::Exceptions manpage.


CONSTRUCTORS

new

description
Class constructor.

parameters
data_base_dir - data base directory, where all table directories are

returns
New formed Media reference.


METHODS

begin

description
Starts transaction, which can be them submitted by submint method or aborted by rollback method.

parameters
None.

returns
None. Throws exception on failure.

commit

description
Commits transaction, started by begin method. All changes done during transaction to Persistence Media are commited.

parameters
None.

returns
None. Throws exception on failure.

rollback

description
Aborts transaction, started by begin method. All changes done during transaction to Persistence Media are restored.

parameters
None.

returns
None. Throws exception on failure.

insert

description
Inserts (new) given 'rows', referenced by 'keys' into 'table'.

parameters
  * table - name of table to insert data into.
If table is ARRAY ref - the last name in the array is taken as table name
and all rest are taken as supertable names and links in supertables to table
instance are created.
  * keys - array ref of row-keys to insert data into
  * revisions - array of row revisions
  * complex_rows - array of elements, each representing data of one row to save.
Some of the row data will be saved in specified additional tables, instead of
the main table. One complex_row is an array of the form:
  [
    [ [ field, ftype, value ]* ],
    [ add_table, [ [ kfield, kvalue ]* ],
      [ [ rfield, rvalue ]* ], [ [ lfield, [ lvalue* ] ]* ] ]*
  ]
returns
1 on success, otherwise exception is thrown.

update

description
Updates given 'rows', referenced by 'keys' inside 'table'.

parameters
  * table - name of table to update data in
  * keys - array ref of row-keys to insert data into
  * revisions - array of row revisions
  * complex_rows - array of elements, each representing data of one row to save.
Some of the row data will be saved in specified additional tables, instead of
the main table. One complex_row is an array of the form:
  [
    [ [ unmodified_field* ], [ unmodified_add_table_property* ] ],
    [ [ field, ftype, value ]* ],
    [ add_table, [ [ kfield, kvalue ]* ],
      [ [ rfield, rvalue ]* ], [ [ lfield, [ lvalue* ] ]* ] ]*
  ]
returns
1 on success, otherwise exception is thrown.

get_real_tables

description
Returns real tables for given keys and super table.

parameters
  * table - name of super table to look at
  * keys  - array ref of row-keys to look at
returns
Array of scalars (names of tables). On error, Exception::Podius::NoTableKeyError is thrown.

get_revisions

description
Gets the revision values for given table keys.

parameters
  * table - name of table to query
  * keys - array ref of row-keys to query
  * out_of_tx - optional boolean flag, work outside of the transaction
returns
1 on success, otherwise exception is thrown.

retrieve

description
Retrieves and returns the 'rows' inside table 'table', referenced by 'keys'.

parameters
  * table - name of real table to retrieve data from
  * table_data - array of the form:
  [
    [ [ field, ftype ]* ],
    [ add_table, [ ifield, [ cfield, cvalue ]* ],
                [ rfield* ], [ lfield* ] ]*
  ]
  * keys - array ref of row-keys to retrieve data from
  * out_of_tx - optional boolean flag, work outside of the transaction
returns
Array of complex_row. On failure exception is thrown. One complex_row (for one key) is an array of the form (according to table_data):
  [
    [ value* ],
    [ [ rvalue* ], [ [ lvalue* ]* ] ]*
  ]

If any table key does not exist, Exception::Podius::NoTableKeyError is thrown.

delete

description
Deletes rows, referenced by 'keys' from table 'table'.

parameters
  * table - name of table to delete from.
If table is ARRAY ref - the last name in the array is taken as table name
and all rest are taken as supertable names and links in supertables to table
instance are deleted.
  * table_data - array of the form:
  [
    [ add_table, [ ifield, [ cfield, cvalue ]* ] ]*
  ]
  * keys - array ref of row-keys to delete
returns
1 on success, otherwise exception is thrown.

trash

description
Restores rows (undo), referenced by 'keys' in table 'table'. This forgets all update changes done to rows pointed by keys.

Calling this method outside a transaction should have no effect.

parameters
The same as for delete method.

returns
1 on success, otherwise exception is thrown.

grep_changed_keys

description
This method could be used before update to determine conflicts. Returns a subset of keys for which the revision (timestamp) was changed.

parameters
  * table - name of table to delete from.
  * keys - array ref of row-keys to query.
  * revisions - array ref of original revisions to check against.
returns
Array ref of keys (the subset of given keys).

reset_high_key

description
Resets the high key for the given base table.

The exception must be thrown if there is still any data in the given table and all subtables.

parameters
  * table - name of table to reset high key for
  * optional value of the high key to set (default: -1). Not implemented.
returns
1 on success, otherwise exception is thrown.

get_unique_key

description
Evaluates unique key for given table. This is done by implementing high/low key approach. High key part is taken from high_key table (it is stored for every given table), low part grows from 1 to low_key_limit (1000).

When first time unique key is requested for given table, high key part is read, incremented and stored back. This high key part is used for assigning first low_key_limit new keys of given table, after that new high key is evaluated.

Resulting key is evaluated by this formula: high_key * low_key_limit + low_key.

parameters
table - name of table to get unique key for

returns
New unique (in the table) key. On failure exception is thrown.

get_all_keys

description
Returns all existing keys for given 'table'.

parameters
  * table - name of table to get unique key for
  * concrete_only - optional flag
returns
Reference to array of scalars (all given 'table' keys).

get_all_tables

description
Returns all existing table names in database.

parameters
none

returns
Reference to array of scalars (all table names).