Skip to content

Commit

Permalink
Switch to the new storage implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhukovAlexander committed Oct 20, 2016
1 parent 7103aa4 commit 93e3efe
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 39 deletions.
2 changes: 1 addition & 1 deletion rafter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self,
self.host, self.port = address

self.log = log if log is not None else storage.RaftLog()
self.storage = storage or storage.Storage()
self.storage = storage or storage.PersistentDict()

self.invocations = storage.AsyncDictWrapper(invocations or {})

Expand Down
20 changes: 3 additions & 17 deletions rafter/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,18 @@ def entry(self, term, command, args=(), kwargs=None):
commit_index = MetaDataField(b'commit_index', from_raw=int, to_raw=lambda x: str(x).encode(), default=0)


class Storage:

def __init__(self, env=None, db=None):

self.env = env or lmdb.open('/tmp/rafter.lmdb', max_dbs=10)
self.attrs_store = self.env.open_db(b'store')

term = MetaDataField(b'term', from_raw=int, to_raw=lambda x: str(x).encode(), default=0)
voted_for = MetaDataField(b'voted_for', from_raw=lambda x: x.decode(), default='')
peers = MetaDataField(b'peers', from_raw=lambda x: json.loads(x.decode()), to_raw=lambda x: json.dumps(x).encode(), default={})
id = MetaDataField(b'id', to_raw=lambda x: x.encode(), from_raw=lambda x: x.decode(), default=uuid4().hex)


class PersistentDict(collections.abc.MutableMapping):

def __init__(self, env, db, *args, **kwargs):
def __init__(self, env=None, db=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.env = env
self.db = db
self.env = env or lmdb.open('/tmp/rafter.lmdb', max_dbs=10)
self.db = db or self.env.open_db(b'storage')

def __setitem__(self, key, value):
with self.env.begin(write=True, db=self.db) as txn:
txn.replace(packb(key), packb(value))

def __getitem__(self, key):
val = None
with self.env.begin(db=self.db) as txn:
val = txn.get(packb(key))
if val is None:
Expand Down
22 changes: 1 addition & 21 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lmdb

from rafter.models import LogEntry
from rafter.storage import RaftLog, Storage, MetaDataField
from rafter.storage import RaftLog, PersistentDict, MetaDataField


class LMDBLogTest(unittest.TestCase):
Expand Down Expand Up @@ -94,23 +94,3 @@ def test_cmp(self):
self.assertFalse(self.log.cmp(entry.index - 1, entry.term - 1))
self.assertFalse(self.log.cmp(entry.index + 1, entry.term - 1))
self.assertTrue(self.log.cmp(entry.index - 1, entry.term + 1))


class StorageTest(unittest.TestCase):

def get_storage(self):
return Storage(env=lmdb.open(self.db_dir, max_dbs=10))

def setUp(self):
self.db_dir = tempfile.mkdtemp()
self.storage = self.get_storage()

def tearDown(self):
shutil.rmtree(self.db_dir)

def test_medadata_attribute(self):
self.assertIsInstance(Storage.id, MetaDataField)

def test_defaults(self):
self.assertEqual(self.storage.term, 0)
self.assertEqual(self.storage.id, self.get_storage().id)

0 comments on commit 93e3efe

Please sign in to comment.