shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit 10b2e91ab22fde2726948a6a28b6673e3a587a6a
parent c150f7cc84f801cc2a6dad9dec63a902338cc29b
Author: lash <dev@holbrook.no>
Date:   Wed, 20 Apr 2022 18:59:32 +0000

Complete redis and rocksdb tests for factory ls

Diffstat:
Msetup.cfg | 2+-
Mshep/store/file.py | 1+
Mshep/store/redis.py | 18+++++++++++++++---
Mshep/store/rocksdb.py | 12++++++------
Mtests/test_file.py | 14++++++++++++++
Mtests/test_redis.py | 19+++++++++++++++++++
Mtests/test_rocksdb.py | 15+++++++++++++++
Mtests/test_store.py | 1-
8 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = shep -version = 0.2.1a2 +version = 0.2.1rc1 description = Multi-state key stores using bit masks author = Louis Holbrook author_email = dev@holbrook.no diff --git a/shep/store/file.py b/shep/store/file.py @@ -153,6 +153,7 @@ class SimpleFileStoreFactory(StoreFactory): def ls(self): r = [] + import sys for v in os.listdir(self.__path): if re.match(re_processedname, v): r.append(v) diff --git a/shep/store/redis.py b/shep/store/redis.py @@ -21,7 +21,7 @@ class RedisStore: def __from_path(self, s): - (left, right) = s.split('.', maxsplit=1) + (left, right) = s.split(b'.', maxsplit=1) return right @@ -55,7 +55,7 @@ class RedisStore: r = [] for s in matches: k = self.__from_path(s) - v = self.redis.get(v) + v = self.redis.get(k) r.append((k, v,)) return r @@ -91,7 +91,7 @@ class RedisStore: class RedisStoreFactory(StoreFactory): - def __init__(self, host='localhost', port=6379, db=0, binary=False): + def __init__(self, host='localhost', port=6379, db=2, binary=False): self.redis = redis.Redis(host=host, port=port, db=db) self.__binary = binary @@ -103,3 +103,15 @@ class RedisStoreFactory(StoreFactory): def close(self): self.redis.close() + + + def ls(self): + r = [] + (c, ks) = self.redis.scan(match='*') + for k in ks: + v = k.rsplit(b'.', maxsplit=1) + if v != k: + v = v[0].decode('utf-8') + if v not in r: + r.append(v) + return r diff --git a/shep/store/rocksdb.py b/shep/store/rocksdb.py @@ -136,12 +136,12 @@ class RocksDbStoreFactory(StoreFactory): def ls(self): it = self.db.iterkeys() + it.seek_to_first() r = [] for k in it: - kstr = k.decode('utf-8') - v = None - try: - k.index('/') - except ValueError: - r.append(kstr) + v = k.rsplit(b'.', maxsplit=1) + if v != k: + v = v[0].decode('utf-8') + if v not in r: + r.append(v) return r diff --git a/tests/test_file.py b/tests/test_file.py @@ -242,6 +242,20 @@ class TestFileStore(unittest.TestCase): self.assertEqual(r, 'foo') + def test_factory_ls(self): + self.states.put('abcd') + self.states.put('xxxx', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('yyyy', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('zzzz', state=self.states.BAR) + r = self.factory.ls() + self.assertEqual(len(r), 3) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_redis.py b/tests/test_redis.py @@ -23,6 +23,7 @@ class TestRedisStore(unittest.TestCase): def setUp(self): from shep.store.redis import RedisStoreFactory self.factory = RedisStoreFactory() + self.factory.redis.flushall() self.states = PersistedState(self.factory.add, 3) self.states.add('foo') self.states.add('bar') @@ -66,6 +67,24 @@ class TestRedisStore(unittest.TestCase): self.assertEqual(v, 'bar') + def test_factory_ls(self): + r = self.factory.ls() + self.assertEqual(len(r), 0) + + self.states.put('abcd') + self.states.put('xxxx', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('yyyy', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('zzzz', state=self.states.BAR) + r = self.factory.ls() + self.assertEqual(len(r), 3) + + if __name__ == '__main__': noredis = False redis = None diff --git a/tests/test_rocksdb.py b/tests/test_rocksdb.py @@ -73,6 +73,21 @@ class TestRedisStore(unittest.TestCase): self.assertEqual(v, 'bar') + def test_factory_ls(self): + self.states.put('abcd') + self.states.put('xxxx', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('yyyy', state=self.states.BAZ) + r = self.factory.ls() + self.assertEqual(len(r), 2) + + self.states.put('zzzz', state=self.states.BAR) + r = self.factory.ls() + self.assertEqual(len(r), 3) + + if __name__ == '__main__': norocksdb = False rocksdb = None diff --git a/tests/test_store.py b/tests/test_store.py @@ -84,6 +84,5 @@ class TestStateItems(unittest.TestCase): self.assertIsNone(self.mockstore.v.get(item)) - if __name__ == '__main__': unittest.main()