diff --git a/JumpScale9RecordChain/clients/zdb/ZDBClientNS.py b/JumpScale9RecordChain/clients/zdb/ZDBClientNS.py index b3cf5f8..358cf92 100644 --- a/JumpScale9RecordChain/clients/zdb/ZDBClientNS.py +++ b/JumpScale9RecordChain/clients/zdb/ZDBClientNS.py @@ -8,9 +8,10 @@ JSBASE = j.application.jsbase_get_class() + class ZDBClientNS(JSBASE): - def __init__(self,zdbclient,nsname ): + def __init__(self, zdbclient, nsname): """ is connection to ZDB @@ -27,26 +28,25 @@ def __init__(self,zdbclient,nsname ): self.zdbclient = zdbclient self.redis = j.clients.redis.get(ipaddr=zdbclient.config.data['addr'], - port=zdbclient.config.data['port'], - fromcache=False) + port=zdbclient.config.data['port'], + fromcache=False) self.redis = self._patch_redis_client(self.redis) - self.nsname = nsname.lower().strip() self.mode = self.zdbclient.mode if self.adminsecret is not "": self.redis.execute_command("AUTH", self.adminsecret) - #put secret on namespace & select namespace + # put secret on namespace & select namespace if self.secret is "": self.redis.execute_command("SELECT", self.nsname) else: self.redis.execute_command("SELECT", self.nsname, self.secret) @property - def adminsecret(self): + def adminsecret(self): return self.zdbclient.adminsecret @property @@ -60,28 +60,30 @@ def _patch_redis_client(self, redis): # don't auto parse response for set, cause it's not 100% redis compatible # 0-db does return a key after in set del redis.response_callbacks['SET'] + # don't auto parse response for del, 0-db returns OK for success instead of the number of key deleted + del redis.response_callbacks['DEL'] return redis - def _key_get(self,key,set=True): - - if self.mode=="seq": + def _key_get(self, key, set=True): + + if self.mode == "seq": if key is None: - key="" + key = "" else: - key = struct.pack(" will consider to be json if binary -> will consider data for capnp @@ -69,9 +68,9 @@ def set(self,data,id=None,hook=None): if ddict will put inside JSOBJ @RETURN JSOBJ - + """ - if j.data.types.string.check(data): + if j.data.types.string.check(data): data = j.data.serializer.json.loads(data) obj = self.schema.get(data) elif j.data.types.bytes.check(data): @@ -79,90 +78,85 @@ def set(self,data,id=None,hook=None): elif "_JSOBJ" in data.__dict__: obj = data if id is None and obj.id is not None: - id=obj.id + id = obj.id elif j.data.types.dict.check(data): obj = self.schema.get(data) else: raise RuntimeError("Cannot find data type, str,bin,obj or ddict is only supported") bdata = obj.data - #we store data in list - l=[] - index={} + # we store data in list + l = [] + index = {} for item in self.schema.index_list: - r=eval("obj.%s"%item) + r = eval("obj.%s" % item) if r: - index[item]=r + index[item] = r - #later: + # later: acl = b"" crc = b"" signature = b"" - l=[index,bdata] + l = [index, bdata] data = msgpack.packb(l) if hook: - obj = hook(obj,index) + obj = hook(obj, index) - - if id==None: - #means a new one - id = self.db.set(data) - else: - id2 = self.db.set(data,id=id) + id = self.db.set(data, key=id) obj.id = id obj.index = index - self._index(index,id) - + self._index(index, id) + return obj - def _index(self,index,id): - - for key,item in index.items(): + def _index(self, index, id): + + for key, item in index.items(): if j.data.types.bytes.check(key): - key=key.decode() - key2=self._index_key+":%s"%key - res = self.index.hget(key2,item) - if res==None: - res = struct.pack("