[Zodb-checkins] CVS: ZODB3/ZODB - serialize.py:1.1.2.1
Connection.py:1.98.6.12
Jeremy Hylton
jeremy at zope.com
Wed Jul 16 16:11:09 EDT 2003
Update of /cvs-repository/ZODB3/ZODB
In directory cvs.zope.org:/tmp/cvs-serv1804/ZODB
Modified Files:
Tag: zodb33-devel-branch
Connection.py
Added Files:
Tag: zodb33-devel-branch
serialize.py
Log Message:
Small step towards isolating serialization logic in a single module.
=== Added File ZODB3/ZODB/serialize.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Support for ZODB object serialization.
ZODB serializes objects using a custom format based on Python pickles.
When an object is unserialized, it can be loaded as either a ghost or
a real object. A ghost is a persistent object of the appropriate type
but without any state. The first time a ghost is accessed, the
persistence machinery traps access and loads the actual state. A
ghost allows many persistent objects to be loaded while minimizing the
memory consumption of referenced but otherwise unused objects.
Pickle format
-------------
ZODB stores serialized objects using a custom format based on pickle.
Each serialized object has two parts: the class metadata and the
object state. The class description must provide enough information
to call the class's ``__new__`` and create an empty object. Once the
object exists as a ghost, its state is passed to ``__setstate__``.
The class metadata can be represented in two different ways, in order
to provide backwards compatibility with many earlier versions of ZODB.
The class metadata is always a two-tuple. The first element may also
be a tuple, containing two string elements: name of a module and the
name of a class. The second element of the class metadata tuple is a
tuple of arguments to pass to the class's ``__new__``.
Persistent references
---------------------
A persistent reference is a pair containing an oid and class metadata.
When one persistent object pickle refers to another persistent object,
the database uses a persistent reference. The format allows a
significant optimization, because ghosts can be created directly from
persistent references. If the reference was just an oid, a database
access would be required to determine the class of the ghost.
Because the persistent reference includes the class, it is not
possible to change the class of a persistent object. If a transaction
changed the class of an object, a new record with new class metadata
would be written but all the old references would still include the
old class.
"""
import cPickle
import cStringIO
from ZODB.coptimizations import new_persistent_id
def getClassMetadata(obj):
klass = obj.__class__
if issubclass(klass, type):
# Handle ZClasses.
d = obj.__dict__.copy()
del d["_p_jar"]
args = obj.__name__, obj.__bases__, d
return klass, args
else:
getinitargs = getattr(klass, "__getinitargs__", None)
if getinitargs is None:
args = None
else:
args = getinitargs()
mod = getattr(klass, "__module__", None)
if mod is None:
return klass, args
else:
return (mod, klass.__name__), args
class BaseObjectWriter:
"""Serializes objects for storage in the database.
The ObjectWriter creates object pickles in the ZODB format. It
also detects new persistent objects reachable from the current
object.
The client is responsible for calling the close() method to avoid
leaking memory. The ObjectWriter uses a Pickler internally, and
Pickler objects do not participate in garbage collection.
"""
def __init__(self, jar=None):
self._file = cStringIO.StringIO()
self._p = cPickle.Pickler(self._file, 1)
self._stack = []
self._p.persistent_id = new_persistent_id(jar, self._stack)
if jar is not None:
assert hasattr(jar, "new_oid")
self._jar = jar
def serialize(self, obj):
return self._dump(getClassMetadata(obj), obj.__getstate__())
def _dump(self, classmeta, state):
# To reuse the existing cStringIO object, we must reset
# the file position to 0 and truncate the file after the
# new pickle is written.
self._file.seek(0)
self._p.clear_memo()
self._p.dump(classmeta)
self._p.dump(state)
self._file.truncate()
return self._file.getvalue()
class ObjectWriter(BaseObjectWriter):
def __init__(self, obj):
BaseObjectWriter.__init__(self, obj._p_jar)
self._stack.append(obj)
def __iter__(self):
return NewObjectIterator(self._stack)
class NewObjectIterator:
# The pickler is used as a forward iterator when the connection
# is looking for new objects to pickle.
def __init__(self, stack):
self._stack = stack
def __iter__(self):
return self
def next(self):
if self._stack:
elt = self._stack.pop()
return elt
else:
raise StopIteration
=== ZODB3/ZODB/Connection.py 1.98.6.11 => 1.98.6.12 ===
--- ZODB3/ZODB/Connection.py:1.98.6.11 Tue Jul 15 14:02:57 2003
+++ ZODB3/ZODB/Connection.py Wed Jul 16 15:11:02 2003
@@ -31,7 +31,8 @@
import ConflictError, ReadConflictError, TransactionError
from ZODB.TmpStore import TmpStore
from ZODB.Transaction import Transaction, get_transaction
-from ZODB.utils import oid_repr
+from ZODB.utils import oid_repr, z64
+from ZODB.serialize import ObjectWriter, getClassMetadata
global_code_timestamp = 0
@@ -161,7 +162,7 @@
object._p_serial = serial
self._cache[oid] = object
- if oid=='\0\0\0\0\0\0\0\0':
+ if oid == z64:
self._root_=object # keep a ref
return object
@@ -315,99 +316,31 @@
# Nothing to do
return
- stack = [object]
-
- # Create a special persistent_id that passes T and the subobject
- # stack along:
- #
- # def persistent_id(object,
- # self=self,
- # stackup=stackup, new_oid=self.new_oid):
- # if (not hasattr(object, '_p_oid') or
- # type(object) is ClassType): return None
- #
- # oid=object._p_oid
- #
- # if oid is None or object._p_jar is not self:
- # oid = self.new_oid()
- # object._p_jar=self
- # object._p_oid=oid
- # stackup(object)
- #
- # klass=object.__class__
- #
- # if klass is type: return oid
- #
- # if hasattr(klass, '__getinitargs__'): return oid
- #
- # module=getattr(klass,'__module__','')
- # if module: klass=module, klass.__name__
- #
- # return oid, klass
-
- file=StringIO()
- seek=file.seek
- pickler=Pickler(file,1)
- pickler.persistent_id=new_persistent_id(self, stack)
- dbstore=self._storage.store
- file=file.getvalue
- cache=self._cache
- dump=pickler.dump
- clear_memo=pickler.clear_memo
-
-
- version=self._version
-
- while stack:
- object=stack[-1]
- del stack[-1]
- oid=object._p_oid
- serial=getattr(object, '_p_serial', '\0\0\0\0\0\0\0\0')
- if serial == '\0\0\0\0\0\0\0\0':
+ w = ObjectWriter(object)
+ for obj in w:
+ oid = obj._p_oid
+ serial = getattr(obj, '_p_serial', z64)
+ if serial == z64:
# new object
self._creating.append(oid)
else:
#XXX We should never get here
if invalid(oid) and not hasattr(object, '_p_resolveConflict'):
- raise ConflictError(object=object)
+ raise ConflictError(object=obj)
self._modified.append(oid)
- klass = object.__class__
-
- if klass is type:
- # Yee Ha!
- dict={}
- dict.update(object.__dict__)
- del dict['_p_jar']
- args=object.__name__, object.__bases__, dict
- state=None
- else:
- if hasattr(klass, '__getinitargs__'):
- args = object.__getinitargs__()
- len(args) # XXX Assert it's a sequence
- else:
- args = None # New no-constructor protocol!
-
- module=getattr(klass,'__module__','')
- if module: klass=module, klass.__name__
- __traceback_info__=klass, oid, self._version
- state=object.__getstate__()
-
- seek(0)
- clear_memo()
- dump((klass,args))
- dump(state)
- p=file(1)
- s=dbstore(oid,serial,p,version,transaction)
+ p = w.serialize(obj)
+ s = self._storage.store(oid, serial, p, self._version, transaction)
self._store_count = self._store_count + 1
# Put the object in the cache before handling the
# response, just in case the response contains the
# serial number for a newly created object
- try: cache[oid]=object
+ try:
+ self._cache[oid] = obj
except:
# Dang, I bet its wrapped:
- if hasattr(object, 'aq_base'):
- cache[oid]=object.aq_base
+ if hasattr(obj, 'aq_base'):
+ self._cache[oid] = obj.aq_base
else:
raise
@@ -519,7 +452,7 @@
self.getTransaction().register(object)
def root(self):
- return self['\0\0\0\0\0\0\0\0']
+ return self[z64]
def setstate(self, obj):
oid = obj._p_oid
More information about the Zodb-checkins
mailing list