Modification in the Signature to manage xml and reduce this size in the ZODB

Use a Pdata instead of the simple string


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@29209 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3d5a571d
...@@ -37,13 +37,16 @@ from Products.ERP5Type.Core.Folder import Folder ...@@ -37,13 +37,16 @@ from Products.ERP5Type.Core.Folder import Folder
from Products.ERP5Type.Base import Base from Products.ERP5Type.Base import Base
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from Products.ERP5Type import PropertySheet from Products.ERP5Type import PropertySheet
from Products.ERP5.Document import Document
from DateTime import DateTime from DateTime import DateTime
from zLOG import LOG, DEBUG, INFO from zLOG import LOG, DEBUG, INFO
import cStringIO
from OFS.Image import Pdata
from OFS.Image import File
import md5 import md5
from base64 import b64encode, b64decode, b16encode, b16decode from base64 import b64encode, b64decode, b16encode, b16decode
class Signature(Folder, SyncCode): class Signature(Folder, SyncCode, File):
""" """
status -- SENT, CONFLICT... status -- SENT, CONFLICT...
md5_object -- An MD5 value of a given document md5_object -- An MD5 value of a given document
...@@ -62,8 +65,10 @@ class Signature(Folder, SyncCode): ...@@ -62,8 +65,10 @@ class Signature(Folder, SyncCode):
id=None, id=None,
rid=None, rid=None,
status=None, status=None,
xml_string=None, xml_string='',
object=None): object=None):
Folder.__init__(self, id)
File.__init__(self, id, '', xml_string)
if object is not None: if object is not None:
self.setPath(object.getPhysicalPath()) self.setPath(object.getPhysicalPath())
self.setObjectId(object.getId()) self.setObjectId(object.getId())
...@@ -73,7 +78,7 @@ class Signature(Folder, SyncCode): ...@@ -73,7 +78,7 @@ class Signature(Folder, SyncCode):
self.setRid(rid) self.setRid(rid)
self.status = status self.status = status
self.setXML(xml_string) self.setXML(xml_string)
self.partial_xml = None self.setPartialXML(None)
self.action = None self.action = None
self.setTempXML(None) self.setTempXML(None)
self.resetConflictList() self.resetConflictList()
...@@ -81,7 +86,7 @@ class Signature(Folder, SyncCode): ...@@ -81,7 +86,7 @@ class Signature(Folder, SyncCode):
self.force = 0 self.force = 0
self.setSubscriberXupdate(None) self.setSubscriberXupdate(None)
self.setPublisherXupdate(None) self.setPublisherXupdate(None)
Folder.__init__(self,id) self.last_data_partial_xml = None
def setStatus(self, status): def setStatus(self, status):
""" """
...@@ -170,21 +175,45 @@ class Signature(Folder, SyncCode): ...@@ -170,21 +175,45 @@ class Signature(Folder, SyncCode):
""" """
setattr(self, 'synchronization_date', value) setattr(self, 'synchronization_date', value)
def hasXML(self):
"""
return True if the xml is available
"""
return bool(getattr(self, 'xml', None))
def setXML(self, xml): def setXML(self, xml):
""" """
set the XML corresponding to the object set the XML corresponding to the object
""" """
self.xml = xml if xml is not None:
if self.xml is not None: # convert the string to Pdata if the big size
file = cStringIO.StringIO(xml)
self.xml, size = self._read_data(file)
self.setTempXML(None) # We make sure that the xml will not be erased self.setTempXML(None) # We make sure that the xml will not be erased
self.setMD5(xml) self.setMD5(xml)
else:
self.xml = None
def getXML(self): def getXML(self):
""" """
get the XML corresponding to the object get the XML corresponding to the object
""" """
#Never return empty string #Never return empty string
return getattr(self, 'xml', None) or None if self.hasXML():
if isinstance(self.xml, Pdata):
return str(self.xml)
elif isinstance(self.xml, str):
return self.xml
else:
raise "ErrorType the self.xml haven't good type"
else:
return None
def hasTempXML(self):
"""
Return true if the temp_xml is available
"""
return bool(getattr(self, 'temp_xml', None))
def setTempXML(self, xml): def setTempXML(self, xml):
""" """
...@@ -192,13 +221,25 @@ class Signature(Folder, SyncCode): ...@@ -192,13 +221,25 @@ class Signature(Folder, SyncCode):
be stored with setXML when we will receive be stored with setXML when we will receive
the confirmation of synchronization the confirmation of synchronization
""" """
self.temp_xml = xml if xml is not None:
file = cStringIO.StringIO(xml)
self.temp_xml, size = self._read_data(file)
else:
self.temp_xml = None
def getTempXML(self): def getTempXML(self):
""" """
get the temp xml get the temp xml
""" """
if self.hasTempXML():
if isinstance(self.temp_xml, Pdata):
return str(self.temp_xml)
elif isinstance(self.temp_xml, str):
return self.temp_xml return self.temp_xml
else:
raise "ErrorType the self.xml haven't good type"
else:
return None
def setSubscriberXupdate(self, xupdate): def setSubscriberXupdate(self, xupdate):
""" """
...@@ -293,19 +334,66 @@ class Signature(Folder, SyncCode): ...@@ -293,19 +334,66 @@ class Signature(Folder, SyncCode):
""" """
return getattr(self, 'object_id', None) return getattr(self, 'object_id', None)
def hasPartialXML(self):
"""
Return true is the partial xml is available
"""
return bool(getattr(self, 'partial_xml', None))
def setPartialXML(self, xml): def setPartialXML(self, xml):
""" """
Set the partial string we will have to Set the partial string we will have to
deliver in the future deliver in the future
""" """
self.partial_xml = xml if xml is not None:
# change encoding of xml to convert in file
try:
xml = xml.encode('utf-8')
except UnicodeDecodeError:
xml = xml.decode('utf-8').encode('ascii','xmlcharrefreplace')
# convert the string to Pdata if the big size
file = cStringIO.StringIO(xml)
self.partial_xml, size = self._read_data(file)
if not isinstance(self.partial_xml, Pdata):
self.partial_xml = Pdata(self.partial_xml)
self.last_data_partial_xml = self.partial_xml.getLastPdata()
else:
self.partial_xml = None
self.last_data_partial_xml = None
def appendPartialXML(self, xml):
"""
Append the partial string we will have to deliver in the future
"""
if xml is not None:
try:
xml = xml.encode('utf-8')
except UnicodeDecodeError:
xml = xml.decode('utf-8').encode('ascii','xmlcharrefreplace')
file = cStringIO.StringIO(xml)
xml_append, size = self._read_data(file)
if not isinstance(xml_append, Pdata):
xml_append = Pdata(xml_append)
last_data = xml_append.getLastPdata()
if self.last_data_partial_xml is not None:
self.last_data_partial_xml.next = xml_append
else:
self.partial_xml = xml_append
self.last_data_partial_xml = last_data
def getPartialXML(self): def getPartialXML(self):
""" """
Set the partial string we will have to Set the partial string we will have to
deliver in the future deliver in the future
""" """
return self.partial_xml if self.hasPartialXML():
if isinstance(self.partial_xml, Pdata):
return str(self.partial_xml)
else:
raise "ErrorType the self.xml haven't good type"
else:
return None
def getAction(self): def getAction(self):
""" """
......
...@@ -771,7 +771,7 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -771,7 +771,7 @@ class XMLSyncUtilsMixin(SyncCode):
if signature is not None and signature.getXMLMapping() is None: if signature is not None and signature.getXMLMapping() is None:
pass pass
elif signature is None or\ elif signature is None or\
(signature.getXML() is None and\ (not signature.hasXML() and\
signature.getStatus() != self.PARTIAL) or\ signature.getStatus() != self.PARTIAL) or\
self.getAlertCodeFromXML(remote_xml) == self.SLOW_SYNC: self.getAlertCodeFromXML(remote_xml) == self.SLOW_SYNC:
#LOG('getSyncMLData', DEBUG, 'Current object.getPath: %s' % object.getPath()) #LOG('getSyncMLData', DEBUG, 'Current object.getPath: %s' % object.getPath())
...@@ -864,7 +864,7 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -864,7 +864,7 @@ class XMLSyncUtilsMixin(SyncCode):
# may not apply correctly # may not apply correctly
xml_update = signature.getPartialXML() xml_update = signature.getPartialXML()
conduit.updateNode( conduit.updateNode(
xml=signature.getPartialXML(), xml=xml_update,
object=object, object=object,
previous_xml=signature.getXML(), previous_xml=signature.getXML(),
force=1) force=1)
...@@ -974,9 +974,9 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -974,9 +974,9 @@ class XMLSyncUtilsMixin(SyncCode):
if not self.checkActionMoreData(action): if not self.checkActionMoreData(action):
data_subnode = None data_subnode = None
if partial_data: if partial_data:
signature_partial_xml = signature.getPartialXML() if signature.hasPartialXML():
if signature_partial_xml: signature.appendPartialXML(partial_data)
data_subnode = signature_partial_xml + partial_data data_subnode = signature.getPartialXML()
else: else:
data_subnode = partial_data data_subnode = partial_data
#LOG('applyActionList', DEBUG, 'data_subnode: %s' % data_subnode) #LOG('applyActionList', DEBUG, 'data_subnode: %s' % data_subnode)
...@@ -1057,7 +1057,7 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -1057,7 +1057,7 @@ class XMLSyncUtilsMixin(SyncCode):
conflict_list += conduit.updateNode( conflict_list += conduit.updateNode(
xml=data_subnode, xml=data_subnode,
object=object, object=object,
previous_xml=signature.getXML(), previous_xml=previous_xml,
force=force, force=force,
simulate=simulate) simulate=simulate)
xml_object = conduit.getXMLFromObjectWithId(object,\ xml_object = conduit.getXMLFromObjectWithId(object,\
...@@ -1108,10 +1108,7 @@ class XMLSyncUtilsMixin(SyncCode): ...@@ -1108,10 +1108,7 @@ class XMLSyncUtilsMixin(SyncCode):
remote_xml=action)) remote_xml=action))
else: # We want to retrieve more data else: # We want to retrieve more data
signature.setStatus(self.PARTIAL) signature.setStatus(self.PARTIAL)
previous_partial = signature.getPartialXML() or '' signature.appendPartialXML(partial_data)
previous_partial += partial_data
#LOG('applyActionList', DEBUG, 'setPartialXML: %s' % str(previous_partial))
signature.setPartialXML(previous_partial)
#LOG('applyActionList', DEBUG, 'previous_partial: %s' % str(previous_partial)) #LOG('applyActionList', DEBUG, 'previous_partial: %s' % str(previous_partial))
#LOG('applyActionList', DEBUG, 'waiting more data for :%s' % signature.getId()) #LOG('applyActionList', DEBUG, 'waiting more data for :%s' % signature.getId())
xml_confirmation_list.append(self.SyncMLConfirmation( xml_confirmation_list.append(self.SyncMLConfirmation(
......
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