<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Project><name>zc.demostorage2</name>
<shortdesc>ZODB storage that stores changes relative to a base storage</shortdesc>
<description>Second-generation demo storage
==============================

The zc.demostorage2 module provides a storage implementation that
wraps two storages, a base storage and a storage to hold changes.
The base storage is never written to.  All new records are written to
the changes storage.  Both storages are expected to:

- Use packed 64-bit unsigned integers as object ids,

- Allocate object ids sequentially, starting from 0, and

- in the case of the changes storage, accept object ids assigned externally.

In addition, it is assumed that less than 2**63 object ids have been
allocated in the first storage. 

Note that DemoStorage also assumes that it's base storage uses 64-bit
unsigned integer object ids allocated sequentially.

.. contents::

Change History
--------------

0.1.1 (2008-02-07)
******************

Fixed a packaging bug that caused some files to be omitted.

0.1 (2008-02-04)
****************

Initial release.

Configuration
-------------

The section below shows how to create zc.demostorage2 storages from
Python. If you're using ZConfig, you need to:

- import zc.demostroage2

- include a demostroage2 section

Here's an example that shows how to configure demo storage and how to
use the configuration from python:

    &gt;&gt;&gt; import ZODB.config
    &gt;&gt;&gt; storage = ZODB.config.storageFromString("""
    ... 
    ... %import zc.demostorage2
    ... 
    ... &lt;demostorage2&gt;
    ...    &lt;filestorage base&gt;
    ...       path base.fs
    ...    &lt;/filestorage&gt;
    ...    &lt;filestorage changes&gt;
    ...       path changes.fs
    ...    &lt;/filestorage&gt;
    ... &lt;/demostorage2&gt;
    ... """)

This creates a demo storage that gets base data from a file storage
named base.fs and stores changes in a file storage named changes.fs.

    &gt;&gt;&gt; storage
    &lt;DemoStorage2: DemoStorage2(base.fs, changes.fs)&gt;

    &gt;&gt;&gt; storage.close()

Demo (doctest)
--------------

Note that most people will configure the storage through ZConfig.  If
you are one of those people, you may want to stop here. :)  The
examples below show you how to use the storage from Python, but they
also exercise lots of details you might not be interested in.

To see how this works, we'll start by creating a base storage and
puting an object (in addition to the root object) in it:

    &gt;&gt;&gt; from ZODB.FileStorage import FileStorage
    &gt;&gt;&gt; base = FileStorage('base.fs')
    &gt;&gt;&gt; from ZODB.DB import DB
    &gt;&gt;&gt; db = DB(base)
    &gt;&gt;&gt; from persistent.mapping import PersistentMapping
    &gt;&gt;&gt; conn = db.open()
    &gt;&gt;&gt; conn.root()['1'] = PersistentMapping({'a': 1, 'b':2})
    &gt;&gt;&gt; import transaction
    &gt;&gt;&gt; transaction.commit()
    &gt;&gt;&gt; db.close()
    &gt;&gt;&gt; import os
    &gt;&gt;&gt; original_size = os.path.getsize('base.fs')

Now, lets reopen the base storage in read-only mode:

    &gt;&gt;&gt; base = FileStorage('base.fs', read_only=True)

And open a new storage to store changes:

    &gt;&gt;&gt; changes = FileStorage('changes.fs')

and combine the 2 in a demofilestorage:

    &gt;&gt;&gt; from zc.demostorage2 import DemoStorage2
    &gt;&gt;&gt; storage = DemoStorage2(base, changes)

If there are no transactions, the storage reports the lastTransaction
of the base database:

    &gt;&gt;&gt; storage.lastTransaction() == base.lastTransaction()
    True

Let's add some data:

    &gt;&gt;&gt; db = DB(storage)
    &gt;&gt;&gt; conn = db.open()
    &gt;&gt;&gt; items = conn.root()['1'].items()
    &gt;&gt;&gt; items.sort()
    &gt;&gt;&gt; items
    [('a', 1), ('b', 2)]

    &gt;&gt;&gt; conn.root()['2'] = PersistentMapping({'a': 3, 'b':4})
    &gt;&gt;&gt; transaction.commit()

    &gt;&gt;&gt; conn.root()['2']['c'] = 5
    &gt;&gt;&gt; transaction.commit()

Here we can see that we haven't modified the base storage:

    &gt;&gt;&gt; original_size == os.path.getsize('base.fs')
    True

But we have modified the changes database:

    &gt;&gt;&gt; len(changes)
    2

Our lastTransaction reflects the lastTransaction of the changes:

    &gt;&gt;&gt; storage.lastTransaction() &gt; base.lastTransaction()
    True

    &gt;&gt;&gt; storage.lastTransaction() == changes.lastTransaction()
    True

Let's walk over some of the methods so ewe can see how we delegate to
the new oderlying storages:

    &gt;&gt;&gt; from ZODB.utils import p64, u64
    &gt;&gt;&gt; storage.load(p64(0), '') == changes.load(p64(0), '')
    True
    &gt;&gt;&gt; storage.load(p64(0), '') == base.load(p64(0), '')
    False
    &gt;&gt;&gt; storage.load(p64(1), '') == base.load(p64(1), '')
    True

    &gt;&gt;&gt; serial = base.getTid(p64(0))
    &gt;&gt;&gt; storage.loadSerial(p64(0), serial) == base.loadSerial(p64(0), serial)
    True

    &gt;&gt;&gt; serial = changes.getTid(p64(0))
    &gt;&gt;&gt; storage.loadSerial(p64(0), serial) == changes.loadSerial(p64(0),
    ...                                                          serial)
    True

The object id of the new object is quite large:

    &gt;&gt;&gt; u64(conn.root()['2']._p_oid)
    4611686018427387905L

Let's look at some other methods:

    &gt;&gt;&gt; storage.getName()
    'DemoStorage2(base.fs, changes.fs)'

    &gt;&gt;&gt; storage.sortKey() == changes.sortKey()
    True

    &gt;&gt;&gt; storage.getSize() == changes.getSize()
    True
    
    &gt;&gt;&gt; len(storage) == len(changes)
    True

    
Undo methods are simply copied from the changes storage:

    &gt;&gt;&gt; [getattr(storage, name) == getattr(changes, name)
    ...  for name in ('supportsUndo', 'undo', 'undoLog', 'undoInfo')
    ...  ]
    [True, True, True, True]


Download
--------</description>
<maintainer><foaf:Person><foaf:name>Jim Fulton</foaf:name>
<foaf:mbox_sha1sum>0491e393764bbbfa13324a845db4ee27f7641b78</foaf:mbox_sha1sum></foaf:Person></maintainer>
<release><Version><revision>0.1.3</revision></Version></release>
</Project></rdf:RDF>