This repository has been archived by the owner on Jul 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BCDBTable.py
216 lines (166 loc) · 5.64 KB
/
BCDBTable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from js9 import j
import msgpack
from functools import reduce
import struct
JSBASE = j.application.jsbase_get_class()
class BCDBTable(JSBASE):
def __init__(self,bcdb,schema,name=""):
"""
"""
JSBASE.__init__(self)
self.bcdb = bcdb
s=bcdb.server
self.name = name
if j.data.types.string.check(schema):
schema = j.data.schema.schema_from_text(schema)
elif "_SCHEMA" in schema.__dict__:
pass
else:
raise RuntimeError("input needs to be schema in text or obj format")
if self.name=="":
self.name = schema.name
if not schema.name:
raise RuntimeError("schema name cannot be empty")
self.db = self.bcdb.server.client_get(namespace=self.name, secret=s.config.data["adminsecret_"])
self.schema = schema
self.index = j.core.db #only for now
self._index_key = "index:%s:%s"%(self.db.instance,self.name)
self.index_load()
def index_delete(self):
for key in self.index.keys(self._index_key+"*"):
self.index.delete(key)
def index_load(self):
"""
"""
self.index_delete()
self.logger.debug("build index for: %s"%self.name)
def iload(id,data,result):
if data:
index,bdata = msgpack.unpackb(data)
self._index(index,id)
self.db.iterate(iload)
self.logger.debug("build index done")
def destroy(self):
raise RuntimeError("not implemented yet, need to go to db and remove namespace")
def set(self,data,id=None,hook=None):
"""
if string -> will consider to be json
if binary -> will consider data for capnp
if obj -> will check of JSOBJ
if ddict will put inside JSOBJ
@RETURN JSOBJ
"""
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):
obj = self.schema.get(capnpbin=data)
elif "_JSOBJ" in data.__dict__:
obj = data
if id is None and obj.id is not None:
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={}
for item in self.schema.index_list:
r=eval("obj.%s"%item)
if r:
index[item]=r
#later:
acl = b""
crc = b""
signature = b""
l=[index,bdata]
data = msgpack.packb(l)
if hook:
obj = hook(obj,index)
if id==None:
#means a new one
id = self.db.set(data)
else:
id2 = self.db.set(data,id=id)
obj.id = id
obj.index = index
self._index(index,id)
return obj
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("<I",id)
self.index.hset(key2,item,res)
else:
res=self._index_get(res)
if id not in res:
res.append(id)
res = msgpack.packb(res)
self.index.hset(key2,item,res)
def _index_get(self,res):
if len(res)==4:
res =[struct.unpack("<I",res)[0]]
else:
res=msgpack.unpackb(res) #is already list
return res
def new(self):
return self.schema.get()
def get(self,id,capnp=False,hook=None):
"""
@PARAM capnp if true will return data as capnp binary object, no hook will be done !
@RETURN obj (.index is in obj)
"""
if id==None:
raise RuntimeError("id cannot be None")
data = self.db.get(id)
if data==None:
return None
res = msgpack.unpackb(data)
if len(res)==2:
index, bdata = res
else:
raise RuntimeError("not supported format in table yet")
if capnp:
return bdata
else:
obj = self.schema.get(capnpbin=bdata)
obj.id = id
obj.index = index
if hook:
obj = hook(obj)
return obj
def find(self,hook=None,**args):
res=[]
for key,val in args.items():
indexkey=self._index_key+":%s"%key
res2=[]
if val == '*' or val == b'*':
for item in self.index.hkeys(indexkey):
id=self.index.hget(indexkey,item)
id=struct.unpack("<I",id)[0]
if id not in res2:
res2.append(id)
else:
id=self.index.hget(indexkey,val)
if id is not None:
id=struct.unpack("<I",id)[0]
if id not in res2:
res2.append(id)
res.append(res2)
res = [set(item) for item in res]
if not res:
return []
res3 = reduce(set.intersection, res)
res4=[]
for item in res3:
obj=self.get(item)
if hook:
obj = hook(obj)
res4.append(obj)
return res4