Storage

Storage

Storage is a self-generating and database-independant Storable objects system. It will be used in order to map data that are used by Movim.

This module has multiple objectives. One of which is to allow the seamless storage of data in cache, session or user storage.

How it works

Storage is a introspection-based set of classes that use database drivers to access their physical storage. Provided a set of linked classes, Storage is able to synthesize data schemas and prepare its physical container.

StorageBase is the provided class that all storable classes need to extend. It provides convenient methods for saving, loading, modifying and deleting data and the children classes of a class.

StorageContainer is a container of storage data. For instance, you can save classes in the Session (extended) container.

Integration

Integrating isn't complex. However the problem is mapping raw xmpp data to Storage-extended classes structures. Some of which are rather complex, like vcards.

The data retrieved from Jaxl is in the form of a jagged array-structure. These could be automatically mapped and manually refined, or manually mapped. Which is implemented has yet to be decided.

The installer will need to be modified in order to initialize the physical storage associated with system classes.

The users physical storage will need to be initialised upon the first login (conversation cache etc. etc.).

Status

  • Most Storage classes are already implemented.
  • Storage containers need to be created.

Specifications

Here are the details of how all classes work in Storage.

Loading the library

Storage comes with a handy function for loading all of its gear. Do as follows:

require('Storage/loader.php');

$drivers = array('sqlite', 'mysql');

load_storage($drivers);

Main classes

  • StorageBase is the main class of the library. All storable objects need to extend StorageBase.
  • StorageEngineBase is an abstraction of the storage back-end. It is used to store objects.
  • StorageTypeBase is contains many classes which are datatypes definitions.
  • StorageType is a purely static class that contains only helper functions to instanciate datatype objects.
  • StorageCollection is an container of StorageBase objects. It typically contains children of StorageBase objects and has methods for cascading actions on them.
  • StorageSchema is a static class that registers created Storage objects and generates the appropriate schema.

Drivers

Drivers implement all the database-related logic. They must extend the StorageEngineBase class, and implement the interface storageDriver.

Drivers are never loaded straight-away. Instead, they must have a specific folder structure:

Storage
`-- drivers
    +-- sqlite
    |   +-- init.php
    |   `-- StorageEngineSQLite.php
    `-- mysql
        +-- init.php
        `-- StorageEngineMySQL.php

Where init.php contains this sort of code:

require('StorageEngineSQLite.php');

Example

Here's a simple object structure, its creation and storage.

// Declaring the structure
class Account extends StorageBase
{
    // Storable fields.
    protected $balance;
    protected $interest;
    protected $owners;

    protected function type_init()
    {
        $this->balance = StorageType::float();
        $this->interest = StorageType::float();
    }
}

class Owner extends StorageBase
{
    protected $name;
    protected $dob;
    protected $account;

    protected function type_init()
    {
        $this->name = StorageType::varchar(256);
        $this->dob = StorageType::date();
        $this->foreignkey('account', 'Account');
    }
}

// Small example
$db = new StorageEngineSQLite('test.db');
$myaccount = new Account();

/* Creating the schema.
 * Note that invoking create() on Account will also create Owner (linked by foreign key). */
$db->create($myaccount);

// Saving the data
$db->save($myaccount);

// Saving into another storage (opened elsewhere)
$db2->save($myaccount);


In Other Languages
Translations of this page: