changeset 19:132ae1c0255a

V1.7: new connection caching. some cleanup.
author casties
date Fri, 11 Jan 2013 17:58:56 +0100
parents 60fea3a6c695
children 0e87d5d80709
files DBInterface.py WritableRestDbInterface.py version.txt
diffstat 3 files changed, 37 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/DBInterface.py	Thu Feb 23 21:17:14 2012 +0100
+++ b/DBInterface.py	Fri Jan 11 17:58:56 2013 +0100
@@ -15,45 +15,6 @@
 from Products.ZSQLMethods.SQL import SQLConnectionIDs
 from Shared.DC.ZRDB.Results import Results
 
-
-def unicodify(s,alternate='latin-1'):
-    """decode str (utf-8 or latin-1 representation) into unicode object"""
-    if not s:
-        return u""
-    if isinstance(s, str):
-        try:
-            return s.decode('utf-8')
-        except:
-            return s.decode(alternate)
-    else:
-        return s
-
-def utf8ify(s):
-    """encode unicode object or string into byte string in utf-8 representation.
-       assumes string objects to be utf-8"""
-    if not s:
-        return ""
-    if isinstance(s, str):
-        return s
-    else:
-        return s.encode('utf-8')
-
-def getTextFromNode(node):
-    """get the cdata content of a XML node"""
-    if node is None:
-        return ""
-    
-    if isinstance(node, list):
-        nodelist = node
-    else:
-        nodelist=node.childNodes
-
-    rc = ""
-    for node in nodelist:
-        if node.nodeType == node.TEXT_NODE:
-           rc = rc + node.data
-    return rc
-
 def sqlName(s, lc=True, more=''):
     """returns restricted ASCII-only version of string"""
     if s is None:
@@ -84,7 +45,22 @@
 
     def getDB(self):
         """returns DB object"""
-        # TODO: can we cache and reuse a DB object?
+        db = None
+
+        # connection caching according to http://pypi.python.org/pypi/alm.solrindex
+        jar = self._p_jar
+        oid = self._p_oid
+        fc = None
+        if jar is not None and oid is not None:
+            fc = getattr(jar, 'foreign_connections', None)
+            if fc is None:
+                jar.foreign_connections = fc = {}
+
+            db = fc.get(oid, None)
+            if db is not None:
+                logging.debug("getDb: using cached db=%s"%repr(db))
+                return db
+
         if self.connection_id is None:
             # try to take the first existing ID
             connids = self.getConnectionIDs()
@@ -93,7 +69,7 @@
                 self.connection_id = connection_id
                 logging.debug("connection_id: %s"%repr(connection_id))
 
-        # get Connection instance
+        # get Connection/DA instance
         con = getattr(self, self.connection_id)
         # call to get db object
         db = con()
@@ -101,6 +77,10 @@
             # force our transaction isolation level
             db.tilevel = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
             
+        if fc is not None and oid is not None:
+            # cache db
+            fc[oid] = db
+            
         return db
     
     def executeZSQL(self, query, args=None, max_rows=None):
--- a/WritableRestDbInterface.py	Thu Feb 23 21:17:14 2012 +0100
+++ b/WritableRestDbInterface.py	Fri Jan 11 17:58:56 2013 +0100
@@ -12,6 +12,21 @@
 
 from RestDbInterface import *
 
+def getTextFromNode(node):
+    """get the cdata content of a XML DOM-node or nodelist"""
+    if node is None:
+        return ""
+    
+    if isinstance(node, list):
+        nodelist = node
+    else:
+        nodelist=node.childNodes
+
+    rc = ""
+    for node in nodelist:
+        if node.nodeType == node.TEXT_NODE:
+           rc = rc + node.data
+    return rc
 
 
 class WritableRestDbInterface(RestDbInterface):
--- a/version.txt	Thu Feb 23 21:17:14 2012 +0100
+++ b/version.txt	Fri Jan 11 17:58:56 2013 +0100
@@ -1,1 +1,1 @@
-1.6
\ No newline at end of file
+1.7
\ No newline at end of file