def __iter__(self): "Note: this method is EXPENSIVE! PLease only use if absolutely needed" cmp_key = LPCMKey(self.name, 'dummy_key') result = DynamoDB.query(cmp_key, attributes_to_get=['table', 'key']) for item in result: key = item['key'] yield base64.b32decode(key).decode('utf-8')
def atomic_add_value(self, key, value): cmp_key = LPCMKey(self.name, key) try: item = DynamoDB.get_item(cmp_key) except DynamoDBKeyNotFoundError: item = DynamoDB.create_item(cmp_key) item.add_attribute('value', value) item.save()
def delete(self, key): "Deletes a key-value map from dynamodb. Ignores it if item does not exist" cmp_key = LPCMKey(self.name, key) try: item = DynamoDB.get_item(cmp_key) except DynamoDBKeyNotFoundError: return # delete fails silently item.delete()
def atomic_delete_values(self, key, values): """Deletes a set of values from an item. Fails silently if item does not exist""" cmp_key = LPCMKey(self.name, key) try: item = DynamoDB.get_item(cmp_key) except DynamoDBKeyNotFoundError: return # Nothing to do item.delete_attribute('value', values) item.save()
def __getitem__(self, key): cmp_key = LPCMKey(self.name, key) try: item = DynamoDB.get_item(cmp_key) except DynamoDBKeyNotFoundError: raise KeyError(u"{name}:{key}".format( name=self.name, key=cmp_key.original_key_obj)) value = item['value'] value = self._postprocess_value_after_ddb_load(value) return value
def __setitem__(self, key, value): Signals.pre_update.send(sender=self.__class__, map_name=self.name, key=key, action='put') cmp_key = LPCMKey(self.name, key) self.cache.set(cmp_key.cache_key, value) self.lpm[key] = value Signals.post_update.send(sender=self.__class__, map_name=self.name, key=key, action='put')
def _atomic_delete_values(self, key, values): Signals.pre_update.send(sender=self.__class__, map_name=self.name, key=key, action='put') cmp_key = LPCMKey(self.name, key) self.lpm.atomic_delete_values(key, values) self.cache.delete(cmp_key.cache_key) Signals.post_update.send(sender=self.__class__, map_name=self.name, key=key, action='put')
def delete(self, key): "Deletes a key-value map from memcached and dynamodb. Ignores it if item does not exist" Signals.pre_update.send(sender=self.__class__, map_name=self.name, key=key, action='delete') cmp_key = LPCMKey(self.name, key) self.cache.delete(cmp_key.cache_key) self.lpm.delete(key) Signals.post_update.send(sender=self.__class__, map_name=self.name, key=key, action='delete')
def _atomic_add_value(self, key, value): Signals.pre_update.send(sender=self.__class__, map_name=self.name, key=key, action='put') cmp_key = LPCMKey(self.name, key) self.cache.atomic_update(cmp_key.cache_key, value, update_operator=operator.add, default_value=0) Signals.post_update.send(sender=self.__class__, map_name=self.name, key=key, action='put')
def remove_values(self, key, values): Signals.pre_update.send(sender=self.__class__, map_name=self.name, key=key, action='put') cmp_key = LPCMKey(self.name, key) self.cache.atomic_update(cmp_key.cache_key, values, update_operator=set.difference, default_value=set()) Signals.post_update.send(sender=self.__class__, map_name=self.name, key=key, action='put')
def __getitem__(self, key): cmp_key = LPCMKey(self.name, key) v = self.cache.get(cmp_key.cache_key) if v is not None: return v return self._get_from_dynamodb_and_save_in_cache(cmp_key)
def __setitem__(self, key, value): cmp_key = LPCMKey(self.name, key) value = self._preprocess_value_before_ddb_save(value) item = DynamoDB.create_item(cmp_key) item['value'] = value item.put()