Skip to content

Commit

Permalink
add function to persistence Classes (table_lenght() and search_ids_by…
Browse files Browse the repository at this point in the history
…_value())
  • Loading branch information
quentin on chickenita committed Jul 4, 2024
1 parent bb388ff commit ffc23a1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pypeman/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ async def get(self, namespace, key, default=SENTINEL):
else:
return self._data[namespace][key]

async def search_ids_by_value(self, namespace, value):
found_ids = []
for id, val in self._data[namespace].items():
if val == value:
found_ids.append(id)
return found_ids

async def table_lenght(self, namespace):
return len(self._data[namespace])


class SqliteBackend():
"""
Expand Down Expand Up @@ -112,6 +122,18 @@ def _sync_get(self, namespace, key, default):
else:
return pdict[key]

async def _search_ids_by_value(self, namespace, value):
found_ids = []
with SqliteDict(self.path, tablename=namespace) as pdict:
for id, val in pdict.items():
if val == value:
found_ids.append(id)
return found_ids

def _get_table_lenght(self, namespace):
with SqliteDict(self.path, tablename=namespace) as pdict:
return len(pdict)

async def store(self, namespace, key, value):
""" Store the value in a dict saved in sqlite db.
Expand All @@ -130,3 +152,9 @@ async def get(self, namespace, key, default=SENTINEL):
:param default: Default value if key missing.
"""
return await self.loop.run_in_executor(self.executor, self._sync_get, namespace, key, default)

async def table_lenght(self, namespace):
return await self.loop.run_in_executor(self.executor, self._get_table_lenght, namespace)

async def search_ids_by_value(self, namespace):
return await self.loop.run_in_executor(self.executor, self._search_ids_by_value, namespace)

0 comments on commit ffc23a1

Please sign in to comment.