Commit c4b2c78b authored by Vincent Pelletier's avatar Vincent Pelletier Committed by Julien Muchembled

client.Storage: Patch instance when read_only is true.

Replace a per-call cost with a constant one.
Signed-off-by: Julien Muchembled's avatarJulien Muchembled <jm@nexedi.com>
parent 647dbc86
...@@ -23,12 +23,8 @@ from neo.lib import logging ...@@ -23,12 +23,8 @@ from neo.lib import logging
from .app import Application from .app import Application
from .exception import NEOStorageNotFoundError, NEOStorageDoesNotExistError from .exception import NEOStorageNotFoundError, NEOStorageDoesNotExistError
def check_read_only(func): def raiseReadOnlyError(*args, **kw):
def wrapped(self, *args, **kw):
if self._is_read_only:
raise POSException.ReadOnlyError() raise POSException.ReadOnlyError()
return func(self, *args, **kw)
return wraps(func)(wrapped)
class Storage(BaseStorage.BaseStorage, class Storage(BaseStorage.BaseStorage,
ConflictResolution.ConflictResolvingStorage): ConflictResolution.ConflictResolvingStorage):
...@@ -61,6 +57,20 @@ class Storage(BaseStorage.BaseStorage, ...@@ -61,6 +57,20 @@ class Storage(BaseStorage.BaseStorage,
BaseStorage.BaseStorage.__init__(self, 'NEOStorage(%s)' % (name, )) BaseStorage.BaseStorage.__init__(self, 'NEOStorage(%s)' % (name, ))
# Warning: _is_read_only is used in BaseStorage, do not rename it. # Warning: _is_read_only is used in BaseStorage, do not rename it.
self._is_read_only = read_only self._is_read_only = read_only
if read_only:
for method_id in (
'new_oid',
'tpc_begin',
'tpc_vote',
'tpc_abort',
'store',
'deleteObject',
'undo',
'undoLog',
'abortVersion',
'commitVersion',
):
setattr(self, method_id, raiseReadOnlyError)
if _app is None: if _app is None:
_app = Application(master_nodes, name, compress=compress, **kw) _app = Application(master_nodes, name, compress=compress, **kw)
self.app = _app self.app = _app
...@@ -86,34 +96,28 @@ class Storage(BaseStorage.BaseStorage, ...@@ -86,34 +96,28 @@ class Storage(BaseStorage.BaseStorage,
except NEOStorageNotFoundError: except NEOStorageNotFoundError:
raise POSException.POSKeyError(oid) raise POSException.POSKeyError(oid)
@check_read_only
def new_oid(self): def new_oid(self):
return self.app.new_oid() return self.app.new_oid()
@check_read_only
def tpc_begin(self, transaction, tid=None, status=' '): def tpc_begin(self, transaction, tid=None, status=' '):
""" """
Note: never blocks in NEO. Note: never blocks in NEO.
""" """
return self.app.tpc_begin(transaction, tid, status) return self.app.tpc_begin(transaction, tid, status)
@check_read_only
def tpc_vote(self, transaction): def tpc_vote(self, transaction):
return self.app.tpc_vote(transaction, self.tryToResolveConflict) return self.app.tpc_vote(transaction, self.tryToResolveConflict)
@check_read_only
def tpc_abort(self, transaction): def tpc_abort(self, transaction):
return self.app.tpc_abort(transaction) return self.app.tpc_abort(transaction)
def tpc_finish(self, transaction, f=None): def tpc_finish(self, transaction, f=None):
return self.app.tpc_finish(transaction, self.tryToResolveConflict, f) return self.app.tpc_finish(transaction, self.tryToResolveConflict, f)
@check_read_only
def store(self, oid, serial, data, version, transaction): def store(self, oid, serial, data, version, transaction):
assert version == '', 'Versions are not supported' assert version == '', 'Versions are not supported'
return self.app.store(oid, serial, data, version, transaction) return self.app.store(oid, serial, data, version, transaction)
@check_read_only
def deleteObject(self, oid, serial, transaction): def deleteObject(self, oid, serial, transaction):
self.app.store(oid, serial, None, None, transaction) self.app.store(oid, serial, None, None, transaction)
...@@ -140,11 +144,9 @@ class Storage(BaseStorage.BaseStorage, ...@@ -140,11 +144,9 @@ class Storage(BaseStorage.BaseStorage,
return self.app.iterator(start, stop) return self.app.iterator(start, stop)
# undo # undo
@check_read_only
def undo(self, transaction_id, txn): def undo(self, transaction_id, txn):
return self.app.undo(transaction_id, txn, self.tryToResolveConflict) return self.app.undo(transaction_id, txn, self.tryToResolveConflict)
@check_read_only
def undoLog(self, first=0, last=-20, filter=None): def undoLog(self, first=0, last=-20, filter=None):
return self.app.undoLog(first, last, filter) return self.app.undoLog(first, last, filter)
...@@ -154,11 +156,9 @@ class Storage(BaseStorage.BaseStorage, ...@@ -154,11 +156,9 @@ class Storage(BaseStorage.BaseStorage,
def supportsTransactionalUndo(self): def supportsTransactionalUndo(self):
return True return True
@check_read_only
def abortVersion(self, src, transaction): def abortVersion(self, src, transaction):
return self.app.abortVersion(src, transaction) return self.app.abortVersion(src, transaction)
@check_read_only
def commitVersion(self, src, dest, transaction): def commitVersion(self, src, dest, transaction):
return self.app.commitVersion(src, dest, transaction) return self.app.commitVersion(src, dest, transaction)
......
...@@ -39,7 +39,7 @@ class ZODBTestCase(TestCase): ...@@ -39,7 +39,7 @@ class ZODBTestCase(TestCase):
kw['temp_dir'] = self.getTempDirectory() kw['temp_dir'] = self.getTempDirectory()
self.neo = NEOCluster(**kw) self.neo = NEOCluster(**kw)
self.neo.start() self.neo.start()
self._storage = self.neo.getZODBStorage() self.open()
def _tearDown(self, success): def _tearDown(self, success):
self._storage.cleanup() self._storage.cleanup()
...@@ -50,7 +50,5 @@ class ZODBTestCase(TestCase): ...@@ -50,7 +50,5 @@ class ZODBTestCase(TestCase):
assertEquals = failUnlessEqual = TestCase.assertEqual assertEquals = failUnlessEqual = TestCase.assertEqual
assertNotEquals = failIfEqual = TestCase.assertNotEqual assertNotEquals = failIfEqual = TestCase.assertNotEqual
def open(self, read_only=False): def open(self, **kw):
# required for some tests (see PersitentTests), no-op for NEO ? self._storage = self.neo.getZODBStorage(**kw)
self._storage._is_read_only = read_only
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