Commit 0ceb7cf7 authored by Jim Fulton's avatar Jim Fulton

DemoStorages now accept constructor athuments, close_base_on_close

and close_changes_on_close, to control whether underlying storages
are closed when the DemoStorage is closed.

https://bugs.launchpad.net/zodb/+bug/118512
parent 1b15dc76
...@@ -2,6 +2,18 @@ ...@@ -2,6 +2,18 @@
Change History Change History
================ ================
3.10.0b1 (2010-05-??)
=====================
New Features
------------
- DemoStorages now accept constructor athuments, close_base_on_close
and close_changes_on_close, to control whether underlying storages
are closed when the DemoStorage is closed.
https://bugs.launchpad.net/zodb/+bug/118512
3.10.0a2 (2010-05-04) 3.10.0a2 (2010-05-04)
===================== =====================
......
...@@ -38,24 +38,34 @@ class DemoStorage(object): ...@@ -38,24 +38,34 @@ 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,
if base is None: close_base_on_close=None, close_changes_on_close=None):
base = ZODB.MappingStorage.MappingStorage()
self._temporary_base = True if close_base_on_close is None:
else: if base is None:
self._temporary_base = False base = ZODB.MappingStorage.MappingStorage()
close_base_on_close = False
else:
close_base_on_close = True
self.base = base self.base = base
self.close_base_on_close = close_base_on_close
if changes is None: if changes is None:
self._temporary_changes = True
changes = ZODB.MappingStorage.MappingStorage() changes = ZODB.MappingStorage.MappingStorage()
zope.interface.alsoProvides(self, ZODB.interfaces.IBlobStorage) zope.interface.alsoProvides(self, ZODB.interfaces.IBlobStorage)
self._temporary_changes = True if close_changes_on_close is None:
close_changes_on_close = False
else: else:
if ZODB.interfaces.IBlobStorage.providedBy(changes): if ZODB.interfaces.IBlobStorage.providedBy(changes):
zope.interface.alsoProvides(self, ZODB.interfaces.IBlobStorage) zope.interface.alsoProvides(self, ZODB.interfaces.IBlobStorage)
self._temporary_changes = False if close_changes_on_close is None:
close_changes_on_close = True
self.changes = changes self.changes = changes
self.close_changes_on_close = close_changes_on_close
self._issued_oids = set() self._issued_oids = set()
self._stored_oids = set() self._stored_oids = set()
...@@ -88,9 +98,9 @@ class DemoStorage(object): ...@@ -88,9 +98,9 @@ class DemoStorage(object):
self.changes.cleanup() self.changes.cleanup()
def close(self): def close(self):
if not self._temporary_base: if self.close_base_on_close:
self.base.close() self.base.close()
if not self._temporary_changes: if self.close_changes_on_close:
self.changes.close() self.changes.close()
def _copy_methods_from_changes(self, changes): def _copy_methods_from_changes(self, changes):
...@@ -222,9 +232,9 @@ class DemoStorage(object): ...@@ -222,9 +232,9 @@ class DemoStorage(object):
def pack(self, t, referencesf, gc=None): def pack(self, t, referencesf, gc=None):
if gc is None: if gc is None:
if self._temporary_base: if self._temporary_changes:
return self.changes.pack(t, referencesf) return self.changes.pack(t, referencesf)
elif self._temporary_base: elif self._temporary_changes:
return self.changes.pack(t, referencesf, gc=gc) return self.changes.pack(t, referencesf, gc=gc)
elif gc: elif gc:
raise TypeError( raise TypeError(
......
...@@ -133,6 +133,30 @@ Undo methods are simply copied from the changes storage: ...@@ -133,6 +133,30 @@ Undo methods are simply copied from the changes storage:
>>> db.close() >>> db.close()
Closing demo storages
=====================
Normally, when a demo storage is closed, it's base and changes
storage are closed:
>>> from ZODB.MappingStorage import MappingStorage
>>> demo = DemoStorage(base=MappingStorage(), changes=MappingStorage())
>>> demo.close()
>>> demo.base.opened(), demo.changes.opened()
(False, False)
You can pass constructor arguments to control whether the base and
changes storages should be closed when the demo storage is closed:
>>> demo = DemoStorage(
... base=MappingStorage(), changes=MappingStorage(),
... close_base_on_close=False, close_changes_on_close=False,
... )
>>> demo.close()
>>> demo.base.opened(), demo.changes.opened()
(True, True)
Storage Stacking Storage Stacking
================ ================
...@@ -147,7 +171,6 @@ creates a new demo storage who's base is the original demo storage: ...@@ -147,7 +171,6 @@ creates a new demo storage who's base is the original demo storage:
We can also supply an explicit changes storage, if we wish: We can also supply an explicit changes storage, if we wish:
>>> from ZODB.MappingStorage import MappingStorage
>>> changes = MappingStorage() >>> changes = MappingStorage()
>>> demo3 = demo2.push(changes) >>> demo3 = demo2.push(changes)
>>> demo3.changes is changes, demo3.base is demo2 >>> demo3.changes is changes, demo3.base is demo2
...@@ -166,12 +189,7 @@ Special backward compatibility support ...@@ -166,12 +189,7 @@ Special backward compatibility support
-------------------------------------- --------------------------------------
Normally, when a demo storage is closed, it's base and changes Normally, when a demo storage is closed, it's base and changes
storage are closed: storage are closed.
>>> demo = DemoStorage(base=MappingStorage(), changes=MappingStorage())
>>> demo.close()
>>> demo.base.opened(), demo.changes.opened()
(False, False)
Older versions of DemoStorage didn't have a separate changes storage Older versions of DemoStorage didn't have a separate changes storage
and didn't close or discard their changes when they were closed. When and didn't close or discard their changes when they were closed. When
...@@ -354,7 +372,7 @@ First we'll get a single OID. ...@@ -354,7 +372,7 @@ First we'll get a single OID.
>>> storage = DemoStorage.push(storage) >>> storage = DemoStorage.push(storage)
>>> random.seed(47) >>> random.seed(47)
>>> storage.new_oid() >>> storage.new_oid()
'\x1a,S\xa4\xe9\xbb\x17\xbd' '8\xaa-\tz\x1dP\x07'
Then we'll force the random number generator to use the same seed for the Then we'll force the random number generator to use the same seed for the
subsequent call to "new_oid" and show that we get a different OID. subsequent call to "new_oid" and show that we get a different OID.
...@@ -362,7 +380,7 @@ subsequent call to "new_oid" and show that we get a different OID. ...@@ -362,7 +380,7 @@ subsequent call to "new_oid" and show that we get a different OID.
>>> random.seed(47) >>> random.seed(47)
>>> oid = storage.new_oid() >>> oid = storage.new_oid()
>>> oid >>> oid
'\x1a,S\xa4\xe9\xbb\x17\xbe' '8\xaa-\tz\x1dP\x08'
DemoStorage keeps up with the issued OIDs to know when not to reissue them... DemoStorage keeps up with the issued OIDs to know when not to reissue them...
......
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