def hdel(self, key, *fields): result = 0 key_id, hash_length = self._get_hash_key_id_and_length(key) # safe guard if hash_length == 0: return result batch = self._db.write_batch() for field in fields: if self._db.get(KEY_CODEC.encode_hash_field(key_id, field)) is not None: result += 1 hash_length -= 1 batch.delete(KEY_CODEC.encode_hash_field(key_id, field)) if hash_length == 0: # remove empty hashes from keyspace self.delete(key) else: batch.put( KEY_CODEC.encode_hash(key), KEY_CODEC.encode_key_id_and_length(key, key_id, hash_length)) batch.write() return result
def zrem(self, key, *members): """ see zadd() for information about score and value structures """ result = 0 key_id, zset_length = self._get_zset_key_id_and_length(key) # safe guard if zset_length == 0: return result batch = self._db.write_batch() for member in members: score = self._db.get(KEY_CODEC.encode_zset_value(key_id, member)) if score is None: continue result += 1 zset_length -= 1 batch.delete(KEY_CODEC.encode_zset_value(key_id, member)) batch.delete(KEY_CODEC.encode_zset_score(key_id, member, score)) # empty zset should be removed from keyspace if zset_length == 0: self.delete(key) else: batch.put( KEY_CODEC.encode_zset(key), KEY_CODEC.encode_key_id_and_length(key, key_id, zset_length)) batch.write() return result
def zadd(self, key, score, value, nx=False, xx=False): key_id, zset_length = self._get_zset_key_id_and_length(key) batch = self._db.write_batch() db_score = self._db.get(KEY_CODEC.encode_zset_value(key_id, value)) if db_score is not None: if nx: return 0 result = 0 previous_score = db_score if float(previous_score) == float(score): return result else: batch.delete( KEY_CODEC.encode_zset_score(key_id, value, previous_score)) else: if xx: return 0 result = 1 zset_length += 1 batch.put( KEY_CODEC.encode_zset(key), KEY_CODEC.encode_key_id_and_length(key, key_id, zset_length)) batch.put(KEY_CODEC.encode_zset_value(key_id, value), to_float_string(score)) batch.put(KEY_CODEC.encode_zset_score(key_id, value, score), bytes('')) batch.write() return result
def hset(self, key, field, value): result = 0 key_id, hash_length = self._get_hash_key_id_and_length(key) if self._db.get(KEY_CODEC.encode_hash_field(key_id, field)) is None: result = 1 with self._db.write_batch() as batch: batch.put( KEY_CODEC.encode_hash(key), KEY_CODEC.encode_key_id_and_length(key, key_id, hash_length + result)) batch.put(KEY_CODEC.encode_hash_field(key_id, field), value) return result
def hsetnx(self, key, field, value): key_id, hash_length = self._get_hash_key_id_and_length(key) # only set if not set before if self._db.get(KEY_CODEC.encode_hash_field(key_id, field)) is None: with self._db.write_batch() as batch: batch.put( KEY_CODEC.encode_hash(key), KEY_CODEC.encode_key_id_and_length(key, key_id, hash_length + 1)) batch.put(KEY_CODEC.encode_hash_field(key_id, field), value) return 1 else: return 0
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