Commit 3e226350 authored by Julien Muchembled's avatar Julien Muchembled

client: optimize generation of new oids

parent edefaca7
...@@ -80,7 +80,7 @@ class Application(object): ...@@ -80,7 +80,7 @@ class Application(object):
self.uuid = None self.uuid = None
self._cache = ClientCache() self._cache = ClientCache()
self._loading_oid = None self._loading_oid = None
self.new_oid_list = [] self.new_oid_list = ()
self.last_oid = '\0' * 8 self.last_oid = '\0' * 8
self.last_tid = None self.last_tid = None
self.storage_event_handler = storage.StorageEventHandler(self) self.storage_event_handler = storage.StorageEventHandler(self)
...@@ -246,7 +246,7 @@ class Application(object): ...@@ -246,7 +246,7 @@ class Application(object):
with self._connecting_to_master_node: with self._connecting_to_master_node:
result = self.master_conn result = self.master_conn
if result is None: if result is None:
self.new_oid_list = [] self.new_oid_list = ()
result = self.master_conn = self._connectToPrimaryNode() result = self.master_conn = self._connectToPrimaryNode()
return result return result
...@@ -324,16 +324,16 @@ class Application(object): ...@@ -324,16 +324,16 @@ class Application(object):
"""Get a new OID.""" """Get a new OID."""
self._oid_lock_acquire() self._oid_lock_acquire()
try: try:
if len(self.new_oid_list) == 0: if not self.new_oid_list:
# Get new oid list from master node # Get new oid list from master node
# we manage a list of oid here to prevent # we manage a list of oid here to prevent
# from asking too many time new oid one by one # from asking too many time new oid one by one
# from master node # from master node
self._askPrimary(Packets.AskNewOIDs(100)) self._askPrimary(Packets.AskNewOIDs(100))
if len(self.new_oid_list) <= 0: if not self.new_oid_list:
raise NEOStorageError('new_oid failed') raise NEOStorageError('new_oid failed')
self.last_oid = self.new_oid_list.pop(0) self.last_oid = oid = self.new_oid_list.pop()
return self.last_oid return oid
finally: finally:
self._oid_lock_release() self._oid_lock_release()
......
...@@ -189,6 +189,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler): ...@@ -189,6 +189,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler):
self.app.setHandlerData(ttid) self.app.setHandlerData(ttid)
def answerNewOIDs(self, conn, oid_list): def answerNewOIDs(self, conn, oid_list):
oid_list.reverse()
self.app.new_oid_list = oid_list self.app.new_oid_list = oid_list
def answerTransactionFinished(self, conn, _, tid): def answerTransactionFinished(self, conn, _, tid):
......
...@@ -168,7 +168,7 @@ class MasterAnswersHandlerTests(MasterHandlerTests): ...@@ -168,7 +168,7 @@ class MasterAnswersHandlerTests(MasterHandlerTests):
conn = self.getFakeConnection() conn = self.getFakeConnection()
oid1, oid2, oid3 = self.getOID(0), self.getOID(1), self.getOID(2) oid1, oid2, oid3 = self.getOID(0), self.getOID(1), self.getOID(2)
self.handler.answerNewOIDs(conn, [oid1, oid2, oid3]) self.handler.answerNewOIDs(conn, [oid1, oid2, oid3])
self.assertEqual(self.app.new_oid_list, [oid1, oid2, oid3]) self.assertEqual(self.app.new_oid_list, [oid3, oid2, oid1])
def test_answerTransactionFinished(self): def test_answerTransactionFinished(self):
conn = self.getFakeConnection() conn = self.getFakeConnection()
......
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