Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 9d32118

Browse files
committed
Fix caching & indexing, add indexed property
1 parent 588c3e0 commit 9d32118

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

orbitdbapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0.rc.1.dev3'
1+
__version__ = '0.1.0.rc.1.dev4'

orbitdbapi/db.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, client, params, **kwargs):
1818
self.logger = logging.getLogger(__name__)
1919

2020
if hasattr( self.params, 'indexBy'):
21-
self.index_by = self.__params.indexBy
21+
self.__index_by = self.__params.indexBy
2222

2323
def clear_cache(self):
2424
self.__cache = {}
@@ -32,6 +32,10 @@ def cache_remove(self, item):
3232
if item in self.__cache:
3333
del self.__cache[item]
3434

35+
@property
36+
def indexBy(self):
37+
return self.__params.get('indexBy')
38+
3539
@property
3640
def cache(self):
3741
return deepcopy(self.__cache)
@@ -59,6 +63,10 @@ def putable(self):
5963
def removeable(self):
6064
return 'remove' in self.__params.get('capabilities', {})
6165

66+
@property
67+
def indexed(self):
68+
return hasattr(self, '__index_by')
69+
6270
def info(self):
6371
endpoint = '/'.join(['db', self.__id_safe])
6472
return self.__client._call('get', endpoint)
@@ -67,12 +75,13 @@ def get(self, item, cache=None):
6775
if cache is None: cache = self.__use_cache
6876
item = str(item)
6977
if cache and item in self.__cache:
70-
result = deepcopy(self.__cache[item])
78+
result = self.__cache[item]
7179
else:
7280
endpoint = '/'.join(['db', self.__id_safe, item])
7381
result = self.__client._call('get', endpoint)
7482
if cache: self.__cache[item] = result
7583
if isinstance(result, Hashable): return deepcopy(result)
84+
if isinstance(result, Iterable): return deepcopy(result)
7685
#if isinstance(result, Iterable): return deepcopy(next(result, {}))
7786
#if isinstance(result, list): return deepcopy(next(iter(result), {}))
7887
return result
@@ -84,11 +93,14 @@ def get_raw(self, item):
8493
def put(self, item, cache=None):
8594
if cache is None: cache = self.__use_cache
8695
if cache:
87-
if hasattr(self, 'index_by'):
88-
if hasattr(item, self.index_by):
89-
index_val = getattr(item, self.index_by)
96+
if hasattr(self, '__index_by'):
97+
if hasattr(item, self.__index_by):
98+
index_val = getattr(item, self.__index_by)
9099
else:
91-
index_val = item[self.index_by]
100+
raise MissingIndexError("The provided document doesn't contain field '{}'".format(self.__index_by))
101+
else:
102+
index_val = item.get('key')
103+
if index_val:
92104
self.__cache[index_val] = item
93105
endpoint = '/'.join(['db', self.__id_safe, 'put'])
94106
entry_hash = self.__client._call('post', endpoint, item)
@@ -129,3 +141,6 @@ def unload(self):
129141

130142
class CapabilityError(Exception):
131143
pass
144+
145+
class MissingIndexError(Exception):
146+
pass

0 commit comments

Comments
 (0)