-
Notifications
You must be signed in to change notification settings - Fork 1
/
wcache.py
452 lines (379 loc) · 14.9 KB
/
wcache.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import copy
import setpath
import functions
import apsw
import sys
import operator
import json
from lib import argsparse
from collections import OrderedDict, deque
from lib import schemaUtils
import time
registered=True
constraints={
2:'SQLITE_INDEX_CONSTRAINT_EQ',
32:'SQLITE_INDEX_CONSTRAINT_GE',
4:'SQLITE_INDEX_CONSTRAINT_GT',
8:'SQLITE_INDEX_CONSTRAINT_LE',
16:'SQLITE_INDEX_CONSTRAINT_LT',
64:'SQLITE_INDEX_CONSTRAINT_MATCH'
}
# Decorator to extended a function by calling first another function with no arguments
def echocall(func):
def wrapper(*args, **kw):
obj=args[0]
Extra=""
if 'tablename' in obj.__dict__:
Extra=obj.tablename
if functions.settings['vtdebug']:
print "Table %s:Before Calling %s.%s(%s)" %(Extra+str(obj),obj.__class__.__name__,func.__name__,','.join([repr(l) for l in args[1:]]+["%s=%s" %(k,repr(v)) for k,v in kw.items()]))
aftermsg="Table %s:After Calling %s.%s(%s)" %(Extra,obj.__class__.__name__,func.__name__,','.join([repr(l) for l in args[1:]]+["%s=%s" %(k,repr(v)) for k,v in kw.items()]))
a=func(*args, **kw)
if functions.settings['vtdebug']:
pass
#print aftermsg
return a
#return func(*args, **kw)
return wrapper
class Source:
def __init__(self,boolargs=[],nonstringargs=dict(),needsescape=[]):
self.tableObjs=dict()
self.boolargs=boolargs
self.nonstringargs=nonstringargs
self.needsescape=needsescape
@echocall
def Create(self, db, modulename, dbname, tablename,*args):
dictargs={'tablename':tablename,'db':db,'dbname':dbname,'modulename':modulename}
self.tableObjs[tablename]=LTable(self.tableObjs,self.boolargs,self.nonstringargs,self.needsescape,*args,**dictargs)
return [self.tableObjs[tablename].getschema(),self.tableObjs[tablename]]
@echocall
def Connect(self, db, modulename, dbname, tablename,*args):
if tablename not in self.tableObjs:
return Create(self, db, modulename, dbname, tablename,*args)
return [self.tableObjs[tablename].getschema(),self.tableObjs[tablename]]
class Cache:
def __init__(self, querycursor, query):
# print Cache
self.query = query
self.cursor = querycursor
self.ordercaches = {}
self.orderbys = []
self.maxlen = 1000
self.stopiteration = False
self.cachetotalwindownumber = 0
self.tempcachelist = None
self.lastvalue = None
self.stopiteration = True
def addOrderBy(self, orderbys):
if orderbys in self.orderbys:
return
else:
if len(orderbys) != 0 and len([item for item in orderbys if item[0] == 0]) == 0:
raise functions.OperatorError(__name__.rsplit('.')[-1], "Orders are not in time column ")
self.orderbys.append(orderbys)
self.ordercaches[orderbys] = (OrderedDict(), deque())
for k, v in self.ordercaches[self.orderbys[0]][1]:
data = self.orderByData(orderbys, copy.copy(v))
self.ordercaches[orderbys][0][k] = data
self.ordercaches[orderbys][1].append([k, data])
def orderByData(self, orderbys, data):
for o in reversed(orderbys):
data.sort(key=operator.itemgetter(o[0]),reverse=o[1])
return data
def nextGroup(self):
while not self.nextValue():
pass
# return True if close a group, else return False
def nextValue(self):
currentrow = self.cursor.next()
if self.tempcachelist is None:
self.tempcachelist = [currentrow[0], [currentrow]]
self.stopiteration = False
return False
if self.tempcachelist[0] != currentrow[0]:
for orderbys in self.orderbys:
data = self.orderByData(orderbys, copy.copy(self.tempcachelist[1]))
self.ordercaches[orderbys][1].appendleft([self.tempcachelist[0], data])
self.ordercaches[orderbys][0][self.tempcachelist[0]] = data
firstcachelist = self.ordercaches[self.orderbys[0]][1]
if len(firstcachelist) > self.maxlen:
for cache in self.ordercaches.values():
cache[0].popitem(last=False)
cache[1].pop()
self.cachetotalwindownumber += 1
self.lastvalue = self.tempcachelist[0]
self.tempcachelist = [currentrow[0], [currentrow]]
return True
else:
self.tempcachelist[1].append(currentrow)
return False
def scan(self, orderbys):
cachelist = self.ordercaches[orderbys][1]
# If order needed
if len(orderbys) > 1:
try:
if len(cachelist) == 0:
self.nextGroup()
totalwindownumber = self.cachetotalwindownumber - (len(cachelist) - 1)
while True:
if (self.cachetotalwindownumber - totalwindownumber) >= len(cachelist):
totalwindownumber = self.cachetotalwindownumber - (len(cachelist) - 1)
while (self.cachetotalwindownumber - totalwindownumber) < 0:
self.nextGroup()
idx = self.cachetotalwindownumber - totalwindownumber
for v in cachelist[idx][1]:
yield v
totalwindownumber += 1
except StopIteration:
for v in self.tempcachelist[1]:
yield v
raise StopIteration
else:
if self.tempcachelist is None:
self.nextValue()
totalwindownumber = self.cachetotalwindownumber - len(cachelist)
tuplenumber = 0
while True:
if (self.cachetotalwindownumber-totalwindownumber) == -1 and (len(self.tempcachelist[1])-1) < tuplenumber:
self.nextValue()
idx = self.cachetotalwindownumber - totalwindownumber
if idx >= len(cachelist):
totalwindownumber = self.cachetotalwindownumber - (len(cachelist) - 1)
tuplenumber = 0
elif idx >= 0 and (len(cachelist[idx][1]) - 1) < tuplenumber:
totalwindownumber += 1
tuplenumber = 0
idx = self.cachetotalwindownumber - totalwindownumber
if idx == -1:
yield self.tempcachelist[1][tuplenumber]
else:
yield cachelist[idx][1][tuplenumber]
tuplenumber += 1
# def innerJoin(self, key, constraints, constraintargs, orderbys):
# # print 'constraints, constraintargs, orderbys', constraints, constraintargs, orderbys
# try:adp@
# while self.lastvalue is None or key > self.lastvalue:
# self.nextGroup()
# except StopIteration:
# pass
#
# try:
# return self.ordercaches[orderbys][0][key]
# except KeyError:
# return []
def innerJoin(self, key, orderbys):
while self.tempcachelist is None or key > self.tempcachelist[0]:
self.nextValue()
if self.tempcachelist[0] == key:
totalwindownumber = self.cachetotalwindownumber + 1
tuplenumber = 0
while True:
idx = self.cachetotalwindownumber - totalwindownumber
if idx > -1:
cachelist = self.ordercaches[orderbys][1][idx]
for v in cachelist[1][tuplenumber:]:
yield v
break
else:
if (len(self.tempcachelist[1])-1) < tuplenumber:
if self.nextValue():
break
yield self.tempcachelist[1][tuplenumber]
tuplenumber += 1
else:
try:
for v in self.ordercaches[orderbys][0][key]:
yield v
except KeyError:
raise StopIteration
class LTable: ####Init means setschema and execstatus
autostring='automatic_vtable'
@echocall
def __init__(self,tblist,boolargs,nonstringargs,needsescape,*args,**envars): # envars tablename, auto , OPTIONAL []
self.delayedexception=None
self.tblist=tblist
self.auto=False
self.first=True
self.schema="create table %s('Error')" % (envars['tablename'])
self.tablename=envars['tablename']
self.description=None
self.consdict={}
self.coldata=[]
self.rowids=[]
self.kdindex=None
self.lastcalculatedidx=None
self.ordered=False
self.envarsdb=envars['db']
self.innerjoin=True
self.query = None
self.keepcursor = True
largs, kargs = [] ,dict()
try:
largs, kargs = argsparse.parse(args,boolargs,nonstringargs,needsescape)
except Exception,e:
raise functions.MadisError(e)
if 'fullouterjoin' in kargs:
if str(kargs['fullouterjoin']).lower() == 't' or str(kargs['fullouterjoin']).lower() == 'true':
self.innerjoin=False
try:
self.query=kargs['query']
self.q=envars['db'].cursor().execute(kargs['query'])
self.description=self.q.getdescription()
except apsw.ExecutionCompleteError:
raise functions.DynamicSchemaWithEmptyResultError(__name__.rsplit('.')[-1])
self._setschema()
self.cache=Cache(self.q, self.query)
@echocall
def _setschema(self):
descr=self.description ### get list of tuples columnname, type
self.schema=schemaUtils.CreateStatement(descr, self.tablename)
@echocall
def getschema(self):
if functions.settings['tracing']:
print "VT schema:%s" %(self.schema)
return self.schema
def getnewcursor(self):
if self.keepcursor:
self.keepcursor = False
return self.q
try:
return self.envarsdb.cursor().execute(self.query)
except apsw.ExecutionCompleteError:
raise functions.DynamicSchemaWithEmptyResultError(__name__.rsplit('.')[-1])
@echocall
def BestIndex(self, constraint_param, orderbys):
indexes = []
newcons = []
# print 'constraint_param, orderbys', constraint_param, orderbys
timecolseen = False
for c in constraint_param:
if c[1] == apsw.SQLITE_INDEX_CONSTRAINT_MATCH or c[0] != 0:
indexes.append(None)
else:
if c[1] != apsw.SQLITE_INDEX_CONSTRAINT_EQ or timecolseen:
indexes.append(None)
else:
indexes.append((0, True))
newcons.append(c)
timecolseen = True
consname = json.dumps(newcons, separators=(',', ':')) + json.dumps(orderbys, separators=(',', ':'))
self.consdict[consname] = (newcons, orderbys)
cost=0
# if newcons == [] and ((0, False),) != orderbys:
if newcons == []:
cost=1000000000
ordered = True
if len(orderbys) != 0 and len([item for item in orderbys if item[0] == 0]) == 0:
ordered = False
# print 'Bestndex', indexes, 0, consname, ordered, cost
return indexes, 0, consname, ordered, cost
@echocall
def Open(self):
return Cursor(self)
@echocall
def Disconnect(self):
pass
@echocall
def Destroy(self):
"""
This method is called when the table is no longer used
"""
del self.cache
del(self.tblist[self.tablename])
# Represents a cursor
class Cursor:
def __init__(self, table):
# print Cursor
self.table=table
self.query=table.query
self.eof=False
self.pos=0
self.timeargpos = None
self.resultrows = []
self.timecolumn=0
self.data=list()
self.unreadValue=None
self.lasttime=None
self.firsttime=True
def getTimeConstraintArg(self, constraints, constraintsargs):
for i, c in enumerate(constraints):
if c[1] == apsw.SQLITE_INDEX_CONSTRAINT_EQ and c[0] == 0:
return constraintsargs[i]
raise functions.OperatorError(__name__.rsplit('.')[-1], "Not Defined Equal Constraint in Timecolumn")
def Filter(self, indexnum, indexname, constraintargs):
self.eof=False
constraints, orderbys=self.table.consdict[indexname]
self.table.cache.addOrderBy(orderbys)
# print 'constraints, orderbys, constraintargs', constraints, orderbys, constraintargs
if len(constraintargs) == 0:
self.resultrows = self.table.cache.scan(orderbys)
else:
key = self.getTimeConstraintArg(constraints, constraintargs)
self.resultrows=self.table.cache.innerJoin(key, orderbys)
try:
self.row=self.resultrows.next()
except StopIteration:
self.eof=True
# @echocall #-- Commented out for speed reasons
def Eof(self):
return self.eof
# @echocall #-- Commented out for speed reasons
def Rowid(self):
return self.pos
# @echocall #-- Commented out for speed reasons
def Column(self, col):
return self.row[col]
# @echocall #-- Commented out for speed reasons
def Next(self):
try:
self.row=self.resultrows.next()
except StopIteration:
self.eof=True
@echocall
def Close(self):
del(self.resultrows)
import re
onlyalphnum=re.compile(ur'[a-zA-Z]\w*$')
def schemastr(tablename,colnames,typenames=None):
stripedcolnames=[el if onlyalphnum.match(el) else '"'+el.replace('"','""')+'"' for el in colnames]
if not typenames:
return "create table %s(%s)" %(tablename,','.join(['"'+str(c)+'"' for c in unify(stripedcolnames)]))
else:
stripedtypenames=['' if el=="None" else el if onlyalphnum.match(el) else '"'+el.replace('"','""')+'"' for el in typenames]
return "create table %s(%s)" %(tablename,','.join([str(c)+' '+str(t) for c,t in zip(unify(stripedcolnames),stripedtypenames)]))
def unify(slist):
if len(set(slist))==len(slist):
return slist
eldict={}
for s in slist:
if s in eldict:
eldict[s]+=1
else:
eldict[s]=1
for val,fr in eldict.items():
if fr==1:
del eldict[val]
for val in eldict:
eldict[val]=1
uniquelist=[]
for s in slist:
if s in eldict:
uniquelist+=[s+str(eldict[s])]
eldict[s]+=1
else:
uniquelist+=[s]
return uniquelist
if not ('.' in __name__):
"""
This is needed to be able to test the function, put it at the end of every
new function you create
"""
import sys
import setpath
from functions import *
testfunction()
if __name__ == "__main__":
reload(sys)
sys.setdefaultencoding('utf-8')
import doctest
doctest.testmod()