-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
357 lines (292 loc) · 8.77 KB
/
test.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
import sys
sys.path.insert(0, 'lib')
from threading import Thread, current_thread
from Queue import Queue
import time
from lisa.schema import Schema, Attribute
from lisa.data_source import CSVFile, DBTable, Rtree
from lisa.access_methods import FindIdentities, FindRange
from lisa.types import Interval, Geometry
from lisa.mini_engines import ArrayStreamer, DataAccessor, ResultStack, \
Select, Mux, Group, Sort
from lisa.info import ThreadInfo
from lisa.stream import Demux
import signal, os
import sqlite3
from shapely import wkt
# some type definitions
IntInterval = Interval(int)
class NameAgeCombiner(object):
def __init__(self, input_schema):
self._schema = Schema()
self._schema.append(Attribute('name_age', str))
self._input_schema = input_schema
self._indices = {
'name': input_schema.index(Attribute('name', str)),
'age': input_schema.index(Attribute('age', int))
}
def schema(self):
return self._schema
def accepts(self, other_schema):
return self._input_schema == other_schema
def __call__(self, r):
return ('%s: %d' % (
r[self._indices['name']],
r[self._indices['age']]
), )
class NameAgeCombinerReverse(object):
def __init__(self, input_schema):
self._schema = Schema()
self._schema.append(Attribute('name_age', str))
self._input_schema = input_schema
self._indices = {
'name': input_schema.index(Attribute('name', str)),
'age': input_schema.index(Attribute('age', int))
}
def schema(self):
return self._schema
def accepts(self, other_schema):
return self._input_schema == other_schema
def __call__(self, r):
return ('%d: %s' % (
r[self._indices['age']],
r[self._indices['name']]
), )
#############################################################
#
# TEST 1
#
#############################################################
# schema definition of the query stream
query_schema = Schema()
query_schema.append(Attribute('age', IntInterval))
# query stream generator from array
query_streamer = ArrayStreamer(query_schema, [
(IntInterval(1, 3),),
(IntInterval(2, 5),),
(IntInterval(1, 3),),
(IntInterval(1, 3),),
(IntInterval(2, 5),),
(IntInterval(2, 5),),
(IntInterval(1, 3),),
(IntInterval(2, 5),),
])
demux = Demux(query_streamer.output())
# schema definition of the data stream
data_schema = Schema()
data_schema.append(Attribute('name', str))
data_schema.append(Attribute('age', int))
data_schema.append(Attribute('rowid', int, True))
data_source = DBTable('test.db', 'person', data_schema)
# definition of the data source
#data_source = CSVFile('test.csv', data_schema)
data_accessors = []
selects = []
for i in range(0, 1):
# create a data accessor
data_accessor = DataAccessor(
demux,
data_source,
FindRange
)
name_age_combiner = NameAgeCombiner(data_accessor.output().schema())
selects.append(Select(data_accessor.output(), name_age_combiner))
data_accessors.append(data_accessor)
mux = Mux(*[s.output() for s in selects])
#name_age_combiner_reverse = NameAgeCombinerReverse(demux.schema())
#select2 = Select(demux, name_age_combiner_reverse)
#name_age_combiner = NameAgeCombiner(data_accessor.output().schema())
#select = Select(data_accessor.output(), name_age_combiner)
#name_age_combiner_reverse = NameAgeCombinerReverse(data_accessor.output().schema())
#select2 = Select(data_accessor.output(), name_age_combiner_reverse)
result_stack = ResultStack(
# query_streamer.output(),
mux.output(),
# data_accessor.output(),
)
info_queue = Queue()
def manage(task):
print 'Running: ' + str(task)
task.run()
info_queue.put(ThreadInfo())
tasks = []
tasks += [('Select', s) for s in selects]
tasks += [('Data Accessor', da) for da in data_accessors]
tasks += [
('Query Streamer', query_streamer),
('Result Stack', result_stack),
('Mux', mux),
]
threads = []
for t in tasks:
threads.append(
Thread(
target = manage,
name = t[0],
args = (t[1],)
)
)
for t in threads:
t.start()
for t in threads:
t.join()
while not info_queue.empty():
i = info_queue.get()
print i
info_queue.task_done()
#############################################################
#
# TEST 1
#
#############################################################
class SubSchema(object):
def __init__(self, input_schema, output_attributes):
self._input_schema = input_schema
self._output_schema = Schema()
self._indices = []
for name in output_attributes.keys():
i = self._input_schema.index(name)
self._indices.append(i)
self._output_schema.append(Attribute(
output_attributes[name],
self._input_schema[i].type()
))
def schema(self):
return self._output_schema
def accepts(self, other_schema):
return self._input_schema == other_schema
def __call__(self, r):
return tuple(r[i] for i in self._indices)
# schema definition of the query stream
query_schema = Schema()
query_schema.append(Attribute('oid', IntInterval))
# query stream generator from array
query_streamer = ArrayStreamer(query_schema, [
(IntInterval(13, 65),),
(IntInterval(2, 5),),
(IntInterval(13, 65),),
(IntInterval(1, 3),),
(IntInterval(1, 20),),
(IntInterval(2, 5),),
(IntInterval(20, 5000),),
(IntInterval(1, 3),),
(IntInterval(2, 5),),
(IntInterval(2, 5),),
(IntInterval(13, 65),),
(IntInterval(1, 3),),
(IntInterval(1, 20),),
(IntInterval(2, 5),),
(IntInterval(20, 50),),
(IntInterval(1, 3),),
(IntInterval(2, 5),),
])
county_source = Rtree('data/counties', 'county')
zip_source = Rtree('data/zip5', 'zip')
data_accessors = []
county_selects = []
zip_selects = []
groups = []
sorts = []
# create a data accessor
county_accessor = DataAccessor(
query_streamer.output(),
county_source,
FindRange
)
data_accessors.append(county_accessor)
demux = Demux(county_accessor.output())
sub_schema = SubSchema(demux.schema(), {'county': 'zip'})
for i in range(0, 2):
select = Select(demux, sub_schema)
county_selects.append(select)
zip_accessor = DataAccessor(
select.output(),
zip_source,
FindRange
)
sub_schema2 = SubSchema(zip_accessor.output().schema(), {'oid': 'zip'})
zip_select = Select(zip_accessor.output(), sub_schema2)
zip_selects.append(zip_select)
sort = Sort(
zip_select.output(),
{'zip': lambda a, b: cmp(a / 100, b / 100)},
# {'zip': None },
# True
)
sorts.append(sort)
group = Group(
sort.output(),
{'zip': lambda a, b: (a / 1000) == (b / 1000)}
# {'zip': None }
)
groups.append(group)
data_accessors.append(zip_accessor)
mux = Mux(*[s.output() for s in groups])
result_stack = ResultStack(
# query_streamer.output(),
mux.output(),
# data_accessor.output(),
)
tasks = []
tasks += [
('Select %02d' % (i), s)
for i, s in enumerate(county_selects + zip_selects)
]
tasks += [
('Data Accessor %02d' % (i), da)
for i, da in enumerate(data_accessors)
]
tasks += [ ('Group', o) for o in groups ]
tasks += [ ('Sort', o) for o in sorts ]
tasks += [
('Query Streamer', query_streamer),
('Result Stack', result_stack),
('Mux', mux),
]
threads = []
for t in tasks:
threads.append(
Thread(
target = manage,
name = t[0],
args = (t[1],)
)
)
print 'Number of threads: %d' % (len(threads))
for t in threads:
t.start()
for t in threads:
t.join()
while not info_queue.empty():
i = info_queue.get()
print i
info_queue.task_done()
#find_identities = FindIdentities(rtree_source)
#find_range = FindRange(rtree_source)
#querys = Schema()
#querys.append(Attribute('oid', int))
#print find_identities.query(querys, (10000,))
#
#querys = Schema()
#querys.append(Attribute('oid', IntInterval))
#print [x for x in find_range.query(querys, (IntInterval(1, 3),))]
#
#querys = Schema()
#querys.append(Attribute('zip', Geometry))
#print len([x for x in find_range.query(querys, (
# Geometry(wkt.loads(
# 'POLYGON((-86.2 40, -86.0 40, -86.0 39.9, -86.2 39.9, -86.2 40))'
# ).wkb)
#,))])
#
## print rtree_source[999999]
#
#print len([x for x in rtree_source.intersect(
# {
## 'oid': (12345, 12360)
# 'zip': Geometry(wkt.loads(
# 'POLYGON((-86.2 40, -86.0 40, -86.0 39.9, -86.2 39.9, -86.2 40))'
# ).wkb)
# }
# )])
#