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

storage: use defaultdict to track data of uncommitted objects

parent a0bd2ae8
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import defaultdict
from functools import wraps from functools import wraps
from neo.lib import logging, util from neo.lib import logging, util
from neo.lib.protocol import ZERO_TID from neo.lib.protocol import ZERO_TID
...@@ -59,15 +60,16 @@ class DatabaseManager(object): ...@@ -59,15 +60,16 @@ class DatabaseManager(object):
""" """
if reset: if reset:
self.erase() self.erase()
self._uncommitted_data = defaultdict(int)
self._setup(app) self._setup(app)
def _setup(self, app): def _setup(self, app):
"""To be overriden by the backend to set up a database """To be overriden by the backend to set up a database
It must recover self._uncommitted_data from temporary object table. It must recover self._uncommitted_data from temporary object table.
_uncommitted_data is a dict containing refcounts to data of _uncommitted_data is already instantiated and must be updated with
write-locked objects, except in case of undo, where the refcount is refcounts to data of write-locked objects, except in case of undo,
increased later, when the object is read-locked. where the refcount is increased later, when the object is read-locked.
Keys are data ids and values are number of references. Keys are data ids and values are number of references.
""" """
raise NotImplementedError raise NotImplementedError
...@@ -316,20 +318,20 @@ class DatabaseManager(object): ...@@ -316,20 +318,20 @@ class DatabaseManager(object):
""" """
raise NotImplementedError raise NotImplementedError
def holdData(self, checksum_or_id, data=None, compression=None): def holdData(self, checksum_or_id, *args):
"""Store raw data of temporary object """Store raw data of temporary object
checksum must be the result of neo.lib.util.makeChecksum(data) If 'checksum_or_id' is a checksum, it must be the result of
'compression' indicates if 'data' is compressed. makeChecksum(data) and extra parameters must be (data, compression)
where 'compression' indicates if 'data' is compressed.
A volatile reference is set to this data until 'releaseData' is called A volatile reference is set to this data until 'releaseData' is called
with this checksum. with this checksum.
If called with only an id, it only increment the volatile If called with only an id, it only increment the volatile
reference to the data matching the id. reference to the data matching the id.
""" """
refcount = self._uncommitted_data if args:
if data is not None: checksum_or_id = self.storeData(checksum_or_id, *args)
checksum_or_id = self.storeData(checksum_or_id, data, compression) self._uncommitted_data[checksum_or_id] += 1
refcount[checksum_or_id] = 1 + refcount.get(checksum_or_id, 0)
return checksum_or_id return checksum_or_id
def releaseData(self, data_id_list, prune=False): def releaseData(self, data_id_list, prune=False):
......
...@@ -217,8 +217,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -217,8 +217,8 @@ class MySQLDatabaseManager(DatabaseManager):
PRIMARY KEY (tid, oid) PRIMARY KEY (tid, oid)
) ENGINE = InnoDB""") ) ENGINE = InnoDB""")
self._uncommitted_data = dict(q("SELECT data_id, count(*)" self._uncommitted_data.update(q("SELECT data_id, count(*)"
" FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id") or ()) " FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id"))
def getConfiguration(self, key): def getConfiguration(self, key):
try: try:
......
...@@ -182,7 +182,7 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -182,7 +182,7 @@ class SQLiteDatabaseManager(DatabaseManager):
PRIMARY KEY (tid, oid)) PRIMARY KEY (tid, oid))
""") """)
self._uncommitted_data = dict(q("SELECT data_id, count(*)" self._uncommitted_data.update(q("SELECT data_id, count(*)"
" FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id")) " FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id"))
def getConfiguration(self, key): def getConfiguration(self, key):
......
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