def sadd(self, key, value): if self._db.get(KEY_CODEC.encode_set_member(key, value)) is None: length = int(self._db.get(KEY_CODEC.encode_set(key)) or b'0') with self._db.write_batch() as batch: batch.put(KEY_CODEC.encode_set(key), bytes(length + 1)) batch.put(KEY_CODEC.encode_set_member(key, value), bytes('')) return 1 else: return 0
def _delete_db_set(self, key): # there are two sets of db keys for sets: # * set # * set members with self._db.write_batch() as batch: batch.delete(KEY_CODEC.encode_set(key)) for db_key, _ in self._get_db_iterator( KEY_CODEC.get_min_set_member(key)): batch.delete(db_key)
def smembers(self, key): result = set() if self._db.get(KEY_CODEC.encode_set(key)): for db_key, _ in self._get_db_iterator( KEY_CODEC.get_min_set_member(key)): _, length, member_key = KEY_CODEC.decode_key(db_key) member_value = member_key[length:] result.add(member_value) return result
def type(self, key): if self._db.get(KEY_CODEC.encode_string(key)) is not None: return 'string' if self._db.get(KEY_CODEC.encode_set(key)) is not None: return 'set' if self._db.get(KEY_CODEC.encode_hash(key)) is not None: return 'hash' if self._db.get(KEY_CODEC.encode_zset(key)) is not None: return 'zset' return 'none'
def _delete_db_set(self, key): # there are two sets of db keys for sets: # * set # * set members # # currently the `set` key is immediately deleted and the other keys # will be collected by gc.KeyGarbageCollector() key_id, _ = self._get_set_key_id_and_length(key) with self._db.write_batch() as batch: batch.delete(KEY_CODEC.encode_set(key)) batch.put(KEY_CODEC.encode_deleted_set(key_id), bytes(''))
def sadd(self, key, value): key_id, length = self._get_set_key_id_and_length(key) if self._db.get(KEY_CODEC.encode_set_member(key_id, value)) is None: with self._db.write_batch() as batch: batch.put( KEY_CODEC.encode_set(key), KEY_CODEC.encode_key_id_and_length(key, key_id, length + 1)) batch.put(KEY_CODEC.encode_set_member(key_id, value), bytes('')) return 1 else: return 0
def rename(self, old_name, new_name): if self.exists(old_name): if old_name == new_name: return # replace the key that holds the key ID and don't touch the rest key_type = self.type(old_name) if key_type == 'zset': old_db_key = KEY_CODEC.encode_zset(old_name) new_db_key = KEY_CODEC.encode_zset(new_name) elif key_type == 'hash': old_db_key = KEY_CODEC.encode_hash(old_name) new_db_key = KEY_CODEC.encode_hash(new_name) elif key_type == 'set': old_db_key = KEY_CODEC.encode_set(old_name) new_db_key = KEY_CODEC.encode_set(new_name) elif key_type == 'string': old_db_key = KEY_CODEC.encode_string(old_name) new_db_key = KEY_CODEC.encode_string(new_name) else: raise DredisError("invalid key type") self._replace_db_key(new_db_key, old_db_key) else: raise NoKeyError()
def delete(self, *keys): result = 0 for key in keys: if self._db.get(KEY_CODEC.encode_string(key)) is not None: self._delete_db_string(key) result += 1 elif self._db.get(KEY_CODEC.encode_set(key)) is not None: self._delete_db_set(key) result += 1 elif self._db.get(KEY_CODEC.encode_hash(key)) is not None: self._delete_db_hash(key) result += 1 elif self._db.get(KEY_CODEC.encode_zset(key)) is not None: self._delete_db_zset(key) result += 1 return result
def scard(self, key): length = self._db.get(KEY_CODEC.encode_set(key)) if length is None: return 0 else: return int(length)
def _get_set_key_id_and_length(self, key): db_value = self._db.get(KEY_CODEC.encode_set(key)) return KEY_CODEC.decode_key_id_and_length(key, db_value)