/usr/share/pyshared/ZODB/DemoStorage.test is in python-zodb 1:3.10.5-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | ==========================
DemoStorage demo (doctest)
==========================
DemoStorages provide a way to provide incremental updates to an
existing, base, storage without updating the storage.
.. We need to mess with time to prevent spurious test failures on windows
>>> now = 1231019584.0
>>> def faux_time_time():
... global now
... now += .1
... return now
>>> import time
>>> real_time_time = time.time
>>> time.time = faux_time_time
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:
>>> from ZODB.FileStorage import FileStorage
>>> base = FileStorage('base.fs')
>>> from ZODB.DB import DB
>>> db = DB(base)
>>> from persistent.mapping import PersistentMapping
>>> conn = db.open()
>>> conn.root()['1'] = PersistentMapping({'a': 1, 'b':2})
>>> import transaction
>>> transaction.commit()
>>> db.close()
>>> import os
>>> original_size = os.path.getsize('base.fs')
Now, lets reopen the base storage in read-only mode:
>>> base = FileStorage('base.fs', read_only=True)
And open a new storage to store changes:
>>> changes = FileStorage('changes.fs')
and combine the 2 in a demofilestorage:
>>> from ZODB.DemoStorage import DemoStorage
>>> storage = DemoStorage(base=base, changes=changes)
If there are no transactions, the storage reports the lastTransaction
of the base database:
>>> storage.lastTransaction() == base.lastTransaction()
True
Let's add some data:
>>> db = DB(storage)
>>> conn = db.open()
>>> items = conn.root()['1'].items()
>>> items.sort()
>>> items
[('a', 1), ('b', 2)]
>>> conn.root()['2'] = PersistentMapping({'a': 3, 'b':4})
>>> transaction.commit()
>>> conn.root()['2']['c'] = 5
>>> transaction.commit()
Here we can see that we haven't modified the base storage:
>>> original_size == os.path.getsize('base.fs')
True
But we have modified the changes database:
>>> len(changes)
2
Our lastTransaction reflects the lastTransaction of the changes:
>>> storage.lastTransaction() > base.lastTransaction()
True
>>> storage.lastTransaction() == changes.lastTransaction()
True
Let's walk over some of the methods so we can see how we delegate to
the new underlying storages:
>>> from ZODB.utils import p64, u64
>>> storage.load(p64(0), '') == changes.load(p64(0), '')
True
>>> storage.load(p64(0), '') == base.load(p64(0), '')
False
>>> storage.load(p64(1), '') == base.load(p64(1), '')
True
>>> serial = base.getTid(p64(0))
>>> storage.loadSerial(p64(0), serial) == base.loadSerial(p64(0), serial)
True
>>> serial = changes.getTid(p64(0))
>>> storage.loadSerial(p64(0), serial) == changes.loadSerial(p64(0),
... serial)
True
The object id of the new object is quite random, and typically large:
>>> print u64(conn.root()['2']._p_oid)
3553260803050964942
Let's look at some other methods:
>>> storage.getName()
"DemoStorage('base.fs', 'changes.fs')"
>>> storage.sortKey() == changes.sortKey()
True
>>> storage.getSize() == changes.getSize()
True
>>> len(storage) == len(changes)
True
Undo methods are simply copied from the changes storage:
>>> [getattr(storage, name) == getattr(changes, name)
... for name in ('supportsUndo', 'undo', 'undoLog', 'undoInfo')
... ]
[True, True, True, True]
>>> 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
================
A common use case is to stack demo storages. DemoStorage provides
some helper functions to help with this. The push method, just
creates a new demo storage who's base is the original demo storage:
>>> demo = DemoStorage()
>>> demo2 = demo.push()
>>> demo2.base is demo
True
We can also supply an explicit changes storage, if we wish:
>>> changes = MappingStorage()
>>> demo3 = demo2.push(changes)
>>> demo3.changes is changes, demo3.base is demo2
(True, True)
The pop method closes the changes storage and returns the base
*without* closing it:
>>> demo3.pop() is demo2
True
>>> changes.opened()
False
If storage returned by push is closed, the original storage isn't:
>>> demo3.push().close()
>>> demo2.opened()
True
Blob Support
============
DemoStorage supports Blobs if the changes database supports blobs.
>>> import ZODB.blob
>>> base = ZODB.blob.BlobStorage('base', FileStorage('base.fs'))
>>> db = DB(base)
>>> conn = db.open()
>>> conn.root()['blob'] = ZODB.blob.Blob()
>>> conn.root()['blob'].open('w').write('state 1')
>>> transaction.commit()
>>> db.close()
>>> base = ZODB.blob.BlobStorage('base',
... FileStorage('base.fs', read_only=True))
>>> changes = ZODB.blob.BlobStorage('changes',
... FileStorage('changes.fs', create=True))
>>> storage = DemoStorage(base=base, changes=changes)
>>> db = DB(storage)
>>> conn = db.open()
>>> conn.root()['blob'].open().read()
'state 1'
>>> _ = transaction.begin()
>>> conn.root()['blob'].open('w').write('state 2')
>>> transaction.commit()
>>> conn.root()['blob'].open().read()
'state 2'
>>> storage.temporaryDirectory() == changes.temporaryDirectory()
True
>>> db.close()
It isn't necessary for the base database to support blobs.
>>> base = FileStorage('base.fs', read_only=True)
>>> changes = ZODB.blob.BlobStorage('changes', FileStorage('changes.fs'))
>>> storage = DemoStorage(base=base, changes=changes)
>>> db = DB(storage)
>>> conn = db.open()
>>> conn.root()['blob'].open().read()
'state 2'
>>> _ = transaction.begin()
>>> conn.root()['blob2'] = ZODB.blob.Blob()
>>> conn.root()['blob2'].open('w').write('state 1')
>>> conn.root()['blob2'].open().read()
'state 1'
>>> db.close()
If the changes database is created implicitly, it will get a blob
storage wrapped around it when necessary:
>>> base = ZODB.blob.BlobStorage('base',
... FileStorage('base.fs', read_only=True))
>>> storage = DemoStorage(base=base)
>>> type(storage.changes).__name__
'MappingStorage'
>>> db = DB(storage)
>>> conn = db.open()
>>> conn.root()['blob'].open().read()
'state 1'
>>> type(storage.changes).__name__
'BlobStorage'
>>> _ = transaction.begin()
>>> conn.root()['blob'].open('w').write('state 2')
>>> transaction.commit()
>>> conn.root()['blob'].open().read()
'state 2'
>>> storage.temporaryDirectory() == storage.changes.temporaryDirectory()
True
>>> db.close()
.. Check that the temporary directory is gone
For now, it won't go until the storage does.
>>> transaction.abort()
>>> blobdir = storage.temporaryDirectory()
>>> del storage, _
>>> import gc
>>> _ = gc.collect()
>>> import os
>>> os.path.exists(blobdir)
False
ZConfig support
===============
You can configure demo storages using ZConfig, using name, changes,
and base options:
>>> import ZODB.config
>>> storage = ZODB.config.storageFromString("""
... <demostorage>
... </demostorage>
... """)
>>> storage.getName()
"DemoStorage('MappingStorage', 'MappingStorage')"
>>> storage = ZODB.config.storageFromString("""
... <demostorage>
... <filestorage base>
... path base.fs
... </filestorage>
...
... <filestorage changes>
... path changes.fs
... </filestorage>
... </demostorage>
... """)
>>> storage.getName()
"DemoStorage('base.fs', 'changes.fs')"
>>> storage.close()
>>> storage = ZODB.config.storageFromString("""
... <demostorage>
... name bob
... <filestorage>
... path base.fs
... </filestorage>
...
... <filestorage changes>
... path changes.fs
... </filestorage>
... </demostorage>
... """)
>>> storage.getName()
'bob'
>>> storage.base.getName()
'base.fs'
>>> storage.close()
Generating OIDs
===============
When asked for a new OID DemoStorage chooses a value and then
verifies that neither the base or changes storages already contain
that OID. It chooses values sequentially from random starting
points, picking new starting points whenever a chosen value us already
in the changes or base.
Under rare circumstances an OID can be chosen that has already been
handed out, but which hasn't yet been comitted. Lets verify that if
the same OID is chosen twice during a transaction that everything will
still work.
To test this, we need to hack random.randint a bit.
>>> import random
>>> randint = random.randint
>>> rv = 42
>>> def faux_randint(min, max):
... print 'called randint'
... global rv
... rv += 1000
... return rv
>>> random.randint = faux_randint
Now, we create a demostorage.
>>> storage = DemoStorage()
called randint
If we ask for an oid, we'll get 1042.
>>> u64(storage.new_oid())
1042
oids are allocated seuentially:
>>> u64(storage.new_oid())
1043
Now, we'll save 1044 in changes so that it has to pick a new one randomly.
>>> t = transaction.get()
>>> ZODB.tests.util.store(storage.changes, 1044)
>>> u64(storage.new_oid())
called randint
2042
Now, we hack rv to 1042 is given out again and we'll save 2043 in base
to force another attempt:
>>> rv -= 1000
>>> ZODB.tests.util.store(storage.changes, 2043)
>>> oid = storage.new_oid()
called randint
called randint
>>> u64(oid)
3042
DemoStorage keeps up with the issued OIDs to know when not to reissue them...
>>> oid in storage._issued_oids
True
...but once data is stored with a given OID...
>>> ZODB.tests.util.store(storage, oid)
...there's no need to remember it any longer:
>>> oid in storage._issued_oids
False
>>> storage.close()
.. restore randint
>>> random.randint = randint
.. restore time
>>> time.time = real_time_time
|