def read(self, row, col="", greater_than_col=None, less_than_col=None, **kwargs): murd = json.loads(self.murd) matched = list(murd.keys()) prefix = "{}{}{}".format(row, MurdMemory.row_col_sep, col) matched = [key for key in matched if prefix in key[:len(prefix)]] if less_than_col is not None: maximum = MurdMemory.row_col_to_key(row, less_than_col) matched = [key for key in matched if key < maximum] if greater_than_col is not None: minimum = MurdMemory.row_col_to_key(row, greater_than_col) matched = [key for key in matched if key > minimum] results = [MurdMemory(**murd[key]) for key in matched] if 'Limit' in kwargs: results = results[:kwargs['Limit']] return results
def delete(self, mems): murd = json.loads(self.murd) primed_mems = MurdMemory.prime_mems(mems) keys = [MurdMemory.mem_to_key(m) for m in primed_mems] for key in keys: if key not in murd: raise Exception("MurdMemory {} not found!".format(key)) for key in keys: murd.pop(key) self.murd = json.dumps(murd)
def delete(self, mems): mems = self.prime_mems(mems) delete_request = {'mems': json.dumps(mems), 'route': 'delete'} self.ws.send(json.dumps(delete_request)) stubborn_mems = self.ws.recv() stubborn_mems = json.loads(stubborn_mems) return [MurdMemory(**sm) for sm in stubborn_mems]
def update(self, mems=[], identifier="Unidentified"): if mems is None: return primed_mems = MurdMemory.prime_mems(mems) print("Storing {} memories".format(len(primed_mems))) murd = json.loads(self.murd) murd = dict(**murd, **primed_mems) self.murd = json.dumps(murd)
def read(self, row, col=None, greater_than_col=None, less_than_col=None): data = {"row": row} if col is not None: data['col'] = col if greater_than_col is not None: data['greater_than_col'] = greater_than_col if less_than_col is not None: data['less_than_col'] = less_than_col read_request = {'route': 'read', 'request': json.dumps(data)} self.ws.send(json.dumps(read_request)) # Listen for read response read_data = self.ws.recv() read_data = json.loads(read_data) read_data = [MurdMemory(**rd) for rd in read_data] return read_data
def update(self, mems, identifier="Unidentified"): primed_mems = self.prime_mems(mems) if len(primed_mems) > 0: latest_murd = self.get_latest_murd() print("Sending {} mems to {} table".format(len(primed_mems), latest_murd.table_name)) # Store Memories in DynamoDB table with latest_murd.batch_writer() as writer: count = 0 for count, mem in enumerate(primed_mems): mem = MurdMemory(**mem) writer.put_item(Item=mem) else: print("No observations to upload")
def complete_table_query(self, table, kwargs): query_result = table.query(**kwargs) items = query_result['Items'] while 'LastEvaluatedKey' in query_result: kwargs['ExclusiveStartKey'] = query_result['LastEvaluatedKey'] query_result = table.query(**kwargs) items.extend(query_result['Items']) if 'Limit' in kwargs and len(items) >= kwargs['Limit']: break results = [MurdMemory(**item) for item in items] if 'Limit' in kwargs: results = results[:kwargs['Limit']] return results
def read(self, row, col=None, greater_than_col=None, less_than_col=None, **kwargs): if type(row) is list: rows = row arg_sets = [{ "row": row, "col": col, "greater_than_col": greater_than_col, "less_than_col": less_than_col, **kwargs } for row in rows] results = run_async(self.remember, arg_sets) mem_mems = {arg_set['mem']: mem for arg_set, mem in results} return mem_mems else: kce = Key("ROW").eq(row) if col is not None: kce = kce & Key("COL").begins_with(col) elif greater_than_col is not None and less_than_col is not None: kce = kce & Key("COL").between(greater_than_col, less_than_col) elif greater_than_col is not None: kce = kce & Key("COL").gt(greater_than_col) elif less_than_col is not None: kce = kce & Key("COL").lt(less_than_col) kwargs['KeyConditionExpression'] = kce arg_sets = [[table, kwargs] for table in self.tables.values()] arg_sets, results = zip(*run_async( self.complete_table_query, arg_sets, log=default_log)) items = [] for results in results: items.extend(results) mems = [MurdMemory(**item) for item in items] return mems
def prime_mems(mems): return list({(MurdMemory(**ob)['ROW'], MurdMemory(**ob)['COL']): ob for ob in mems}.values())