Fix some of the DB interface
This commit is contained in:
parent
be2475f617
commit
07515c6a0d
@ -520,7 +520,7 @@ class BlockProcessor(LoggedClass):
|
|||||||
prefix = b'H' + hash168
|
prefix = b'H' + hash168
|
||||||
deletes = []
|
deletes = []
|
||||||
puts = {}
|
puts = {}
|
||||||
for key, hist in self.db.iterator(reverse=True, prefix=prefix):
|
for key, hist in self.db.iterator(prefix=prefix, reverse=True):
|
||||||
a = array.array('I')
|
a = array.array('I')
|
||||||
a.frombytes(hist)
|
a.frombytes(hist)
|
||||||
# Remove all history entries >= self.tx_count
|
# Remove all history entries >= self.tx_count
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
class Storage(object):
|
class Storage(object):
|
||||||
@ -19,7 +20,7 @@ class Storage(object):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def iterator(self, prefix=b""):
|
def iterator(self, prefix=b'', reverse=False):
|
||||||
"""
|
"""
|
||||||
Returns an iterator that yields (key, value) pairs from the database sorted by key.
|
Returns an iterator that yields (key, value) pairs from the database sorted by key.
|
||||||
If `prefix` is set, only keys starting with `prefix` will be included.
|
If `prefix` is set, only keys starting with `prefix` will be included.
|
||||||
@ -37,18 +38,10 @@ class LevelDB(Storage):
|
|||||||
import plyvel
|
import plyvel
|
||||||
self.db = plyvel.DB(name, create_if_missing=create_if_missing,
|
self.db = plyvel.DB(name, create_if_missing=create_if_missing,
|
||||||
error_if_exists=error_if_exists, compression=compression)
|
error_if_exists=error_if_exists, compression=compression)
|
||||||
|
self.get = self.db.get
|
||||||
def get(self, key):
|
self.put = self.db.put
|
||||||
return self.db.get(key)
|
self.iterator = self.db.iterator
|
||||||
|
self.write_batch = partial(self.db.write_batch, transaction=True)
|
||||||
def write_batch(self):
|
|
||||||
return self.db.write_batch(transaction=True)
|
|
||||||
|
|
||||||
def iterator(self, prefix=b""):
|
|
||||||
return self.db.iterator(prefix=prefix)
|
|
||||||
|
|
||||||
def put(self, key, value):
|
|
||||||
self.db.put(key, value)
|
|
||||||
|
|
||||||
|
|
||||||
class RocksDB(Storage):
|
class RocksDB(Storage):
|
||||||
@ -65,9 +58,8 @@ class RocksDB(Storage):
|
|||||||
compression=compression,
|
compression=compression,
|
||||||
target_file_size_base=33554432,
|
target_file_size_base=33554432,
|
||||||
max_open_files=1024))
|
max_open_files=1024))
|
||||||
|
self.get = self.db.get
|
||||||
def get(self, key):
|
self.put = self.db.put
|
||||||
return self.db.get(key)
|
|
||||||
|
|
||||||
class WriteBatch(object):
|
class WriteBatch(object):
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
@ -85,8 +77,10 @@ class RocksDB(Storage):
|
|||||||
return RocksDB.WriteBatch(self.db)
|
return RocksDB.WriteBatch(self.db)
|
||||||
|
|
||||||
class Iterator(object):
|
class Iterator(object):
|
||||||
def __init__(self, db, prefix):
|
def __init__(self, db, prefix, reverse):
|
||||||
self.it = db.iteritems()
|
self.it = db.iteritems()
|
||||||
|
if reverse:
|
||||||
|
self.it = reversed(self.it)
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
@ -100,11 +94,8 @@ class RocksDB(Storage):
|
|||||||
raise StopIteration
|
raise StopIteration
|
||||||
return k, v
|
return k, v
|
||||||
|
|
||||||
def iterator(self, prefix=b""):
|
def iterator(self, prefix=b'', reverse=False):
|
||||||
return RocksDB.Iterator(self.db, prefix)
|
return RocksDB.Iterator(self.db, prefix, reverse)
|
||||||
|
|
||||||
def put(self, key, value):
|
|
||||||
return self.db.put(key, value)
|
|
||||||
|
|
||||||
|
|
||||||
class LMDB(Storage):
|
class LMDB(Storage):
|
||||||
@ -128,15 +119,16 @@ class LMDB(Storage):
|
|||||||
def write_batch(self):
|
def write_batch(self):
|
||||||
return self.env.begin(db=self.db, write=True)
|
return self.env.begin(db=self.db, write=True)
|
||||||
|
|
||||||
def iterator(self, prefix=b""):
|
def iterator(self, prefix=b'', reverse=False):
|
||||||
return LMDB.lmdb.Iterator(self.db, self.env, prefix)
|
return LMDB.Iterator(self.db, self.env, prefix, reverse)
|
||||||
|
|
||||||
class Iterator:
|
class Iterator:
|
||||||
def __init__(self, db, env, prefix):
|
def __init__(self, db, env, prefix, reverse):
|
||||||
self.transaction = env.begin(db=db)
|
self.transaction = env.begin(db=db)
|
||||||
self.transaction.__enter__()
|
self.transaction.__enter__()
|
||||||
self.db = db
|
self.db = db
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
|
self.reverse = reverse # FIXME
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
self.iterator = LMDB.lmdb.Cursor(self.db, self.transaction)
|
self.iterator = LMDB.lmdb.Cursor(self.db, self.transaction)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user