Commit 009fd6f6 authored by Jim Fulton's avatar Jim Fulton

Added an option to leave the base storage open when a demo storage is

closed.
parent f1f419cf
...@@ -36,7 +36,9 @@ class DemoStorage(object): ...@@ -36,7 +36,9 @@ class DemoStorage(object):
ZODB.interfaces.IStorageIteration, ZODB.interfaces.IStorageIteration,
) )
def __init__(self, name=None, base=None, changes=None): def __init__(self, name=None, base=None, changes=None,
keep_base_open=False):
self._keep_base_open = keep_base_open
if base is None: if base is None:
base = ZODB.MappingStorage.MappingStorage() base = ZODB.MappingStorage.MappingStorage()
self.base = base self.base = base
...@@ -71,7 +73,8 @@ class DemoStorage(object): ...@@ -71,7 +73,8 @@ class DemoStorage(object):
self.changes.cleanup() self.changes.cleanup()
def close(self): def close(self):
self.base.close() if not self._keep_base_open:
self.base.close()
self.changes.close() self.changes.close()
if getattr(self, '_blob_dir', ''): if getattr(self, '_blob_dir', ''):
ZODB.blob.remove_committed_dir(self._blob_dir) ZODB.blob.remove_committed_dir(self._blob_dir)
......
...@@ -2,10 +2,8 @@ ...@@ -2,10 +2,8 @@
DemoStorage demo (doctest) DemoStorage demo (doctest)
========================== ==========================
Note that most people will configure the storage through ZConfig. If DemoStorages provide a way to provide incremental updates to an
you are one of those people, you may want to stop here. :) The existing, base, storage without updating the storage.
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 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: puting an object (in addition to the root object) in it:
...@@ -122,7 +120,37 @@ Undo methods are simply copied from the changes storage: ...@@ -122,7 +120,37 @@ Undo methods are simply copied from the changes storage:
... ] ... ]
[True, True, True, True] [True, True, True, True]
Normally, when we close a demo storage, the changes and base storages
are closed:
>>> db.close() >>> db.close()
>>> base._file.closed
True
>>> changes._file.closed
True
A common use case is to stack multiple DemoStorages, returning to a
previous state by popping a DemoStorage off the stack. In this case,
we want to leave the base storage open:
>>> base = FileStorage('base.fs', read_only=True)
>>> storage = DemoStorage(base=base, keep_base_open=True)
Here, we didn't specify a changes storage. A MappingStorage was
automatically created:
>>> type(storage.changes).__name__
'MappingStorage'
Because we specified the keep_base_open option, the base storage is
left open when we close the DemoStorage:
>>> storage.close()
>>> base._file.closed
False
>>> storage.changes.opened()
False
Blob Support Blob Support
============ ============
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment