[CalendarServer-changes] [6576] CalendarServer/branches/users/glyph/conn-limit/txdav/base/datastore/ asyncsqlpool.py
source_changes at macosforge.org
source_changes at macosforge.org
Mon Nov 8 09:14:12 PST 2010
Revision: 6576
http://trac.macosforge.org/projects/calendarserver/changeset/6576
Author: glyph at apple.com
Date: 2010-11-08 09:14:08 -0800 (Mon, 08 Nov 2010)
Log Message:
-----------
Add SpooledTxn (sketch, not yet tested) to do the work of spooling SQL commands.
Modified Paths:
--------------
CalendarServer/branches/users/glyph/conn-limit/txdav/base/datastore/asyncsqlpool.py
Modified: CalendarServer/branches/users/glyph/conn-limit/txdav/base/datastore/asyncsqlpool.py
===================================================================
--- CalendarServer/branches/users/glyph/conn-limit/txdav/base/datastore/asyncsqlpool.py 2010-11-08 17:13:41 UTC (rev 6575)
+++ CalendarServer/branches/users/glyph/conn-limit/txdav/base/datastore/asyncsqlpool.py 2010-11-08 17:14:08 UTC (rev 6576)
@@ -158,6 +158,71 @@
+class SpooledTxn(object):
+ """
+ A L{SpooledTxn} is an implementation of L{IAsyncTransaction} which cannot
+ yet actually execute anything, so it spools SQL reqeusts for later
+ execution. When a L{BaseSqlTxn} becomes available later, it can be
+ unspooled onto that.
+ """
+
+ implements(IAsyncTransaction)
+
+ def __init__(self):
+ self._spool = []
+
+
+ def _enspool(self, cmd, a=(), kw={}):
+ d = Deferred()
+ self._spool.append((d, cmd, a, kw))
+ return d
+
+
+ def _iterDestruct(self):
+ """
+ Iterate the spool list destructively, while popping items from the
+ beginning. This allows code which executes more SQL in the callback of
+ a Deferred to not interfere with the originally submitted order of
+ commands.
+ """
+ while self._spool:
+ yield self._spool.pop(0)
+
+
+ def _unspool(self, other):
+ """
+ Unspool this transaction onto another transaction.
+
+ @param other: another provider of L{IAsyncTransaction} which will
+ actually execute the SQL statements we have been buffering.
+ """
+ for (d, cmd, a, kw) in self._iterDestruct():
+ self._relayCommand(other, d, cmd, a, kw)
+
+
+ def _relayCommand(self, other, d, cmd, a, kw):
+ """
+ Relay a single command to another transaction.
+ """
+ def cb(nothing):
+ return getattr(other, cmd)(*a, **kw)
+ d.addCallback(cb)
+ d.callback(None)
+
+
+ def execSQL(self, *a, **kw):
+ return self._enspool('execSQL', a, kw)
+
+
+ def commit(self):
+ return self._enspool('commit')
+
+
+ def abort(self):
+ return self._enspool('abort')
+
+
+
class PooledSqlTxn(proxyForInterface(iface=IAsyncTransaction,
originalAttribute='_baseTxn')):
"""
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20101108/b0adbd05/attachment-0001.html>
More information about the calendarserver-changes
mailing list