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 hset(self, key, field, value): result = 0 if self._db.get(KEY_CODEC.encode_hash_field(key, field)) is None: result = 1 hash_length = int(self._db.get(KEY_CODEC.encode_hash(key), '0')) with self._db.write_batch() as batch: batch.put(KEY_CODEC.encode_hash(key), bytes(hash_length + 1)) batch.put(KEY_CODEC.encode_hash_field(key, field), value) return result
def hsetnx(self, key, field, value): # only set if not set before if self._db.get(KEY_CODEC.encode_hash_field(key, field)) is None: hash_length = int(self._db.get(KEY_CODEC.encode_hash(key), '0')) with self._db.write_batch() as batch: batch.put(KEY_CODEC.encode_hash(key), bytes(hash_length + 1)) batch.put(KEY_CODEC.encode_hash_field(key, field), value) return 1 else: return 0
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 hget(self, key, field): return self._db.get(KEY_CODEC.encode_hash_field(key, field))
def hget(self, key, field): key_id, _ = self._get_hash_key_id_and_length(key) return self._db.get(KEY_CODEC.encode_hash_field(key_id, field))