|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from copy import deepcopy |
| 4 | +from collections.abc import Hashable, Iterable |
| 5 | +from urllib.parse import quote as urlquote |
| 6 | + |
| 7 | + |
| 8 | +class DB (): |
| 9 | + def __init__(self, client, params, **kwargs): |
| 10 | + self.__cache = {} |
| 11 | + self.__client = client |
| 12 | + self.__params = params |
| 13 | + self.__dbname = params['dbname'] |
| 14 | + self.__id = params['id'] |
| 15 | + self.__id_safe = urlquote(self.__id, safe='') |
| 16 | + self.__type = params['type'] |
| 17 | + self.__use_cache = kwargs.get('use_db_cache') |
| 18 | + self.logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + if hasattr( self.params, 'indexBy'): |
| 21 | + self.index_by = self.__params.indexBy |
| 22 | + |
| 23 | + def clear_cache(self): |
| 24 | + self.__cache = {} |
| 25 | + |
| 26 | + def cache_get(self, item): |
| 27 | + item = str(item) |
| 28 | + return deepcopy(self.__cache.get(item)) |
| 29 | + |
| 30 | + def cache_remove(self, item): |
| 31 | + item = str(item) |
| 32 | + if item in self.__cache: |
| 33 | + del self.__cache[item] |
| 34 | + |
| 35 | + @property |
| 36 | + def cache(self): |
| 37 | + return deepcopy(self.__cache) |
| 38 | + |
| 39 | + @property |
| 40 | + def params(self): |
| 41 | + return deepcopy(self.__params) |
| 42 | + |
| 43 | + @property |
| 44 | + def dbname(self): |
| 45 | + return self.__dbname |
| 46 | + |
| 47 | + @property |
| 48 | + def dbtype(self): |
| 49 | + return self.__type |
| 50 | + |
| 51 | + def info(self): |
| 52 | + endpoint = '/'.join(['db', self.__id_safe]) |
| 53 | + return self.__client._call('get', endpoint) |
| 54 | + |
| 55 | + def get(self, item, cache=None): |
| 56 | + if cache is None: cache = self.__use_cache |
| 57 | + item = str(item) |
| 58 | + if cache and item in self.__cache: |
| 59 | + result = deepcopy(self.__cache[item]) |
| 60 | + else: |
| 61 | + endpoint = '/'.join(['db', self.__id_safe, item]) |
| 62 | + result = self.__client._call('get', endpoint) |
| 63 | + if cache: self.__cache[item] = result |
| 64 | + if isinstance(result, Hashable): return deepcopy(result) |
| 65 | + #if isinstance(result, Iterable): return deepcopy(next(result, {})) |
| 66 | + #if isinstance(result, list): return deepcopy(next(iter(result), {})) |
| 67 | + return result |
| 68 | + |
| 69 | + def get_raw(self, item): |
| 70 | + endpoint = '/'.join(['db', self.__id_safe, 'raw', str(item)]) |
| 71 | + return (self.__client._call('get', endpoint)) |
| 72 | + |
| 73 | + def put(self, item, cache=None): |
| 74 | + if cache is None: cache = self.__use_cache |
| 75 | + if cache: |
| 76 | + if hasattr(self, 'index_by'): |
| 77 | + if hasattr(item, self.index_by): |
| 78 | + index_val = getattr(item, self.index_by) |
| 79 | + else: |
| 80 | + index_val = item[self.index_by] |
| 81 | + self.__cache[index_val] = item |
| 82 | + endpoint = '/'.join(['db', self.__id_safe, 'put']) |
| 83 | + entry_hash = self.__client._call('post', endpoint, item) |
| 84 | + if cache: self.__cache[entry_hash] = item |
| 85 | + return entry_hash |
| 86 | + |
| 87 | + def add(self, item, cache=None): |
| 88 | + if cache is None: cache = self.__use_cache |
| 89 | + endpoint = '/'.join(['db', self.__id_safe, 'add']) |
| 90 | + entry_hash = self.__client._call('post', endpoint, item) |
| 91 | + if cache: self.__cache[entry_hash] = item |
| 92 | + return entry_hash |
| 93 | + |
| 94 | + def iterator_raw(self, params): |
| 95 | + endpoint = '/'.join(['db', self.__id_safe, 'rawiterator']) |
| 96 | + return self.__client._call('get', endpoint, params) |
| 97 | + |
| 98 | + def iterator(self, params): |
| 99 | + endpoint = '/'.join(['db', self.__id_safe, 'iterator']) |
| 100 | + return self.__client._call('get', endpoint, params) |
| 101 | + |
| 102 | + def index(self): |
| 103 | + endpoint = '/'.join(['db', self.__id_safe, 'index']) |
| 104 | + return self.__client._call('get', endpoint) |
| 105 | + |
| 106 | + def unload(self): |
| 107 | + endpoint = '/'.join(['db', self.__id_safe]) |
| 108 | + return self.__client._call('delete', endpoint) |
0 commit comments