forked from tadgh/ArgoRevisit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPJsonQueries.py
315 lines (245 loc) · 9.66 KB
/
PJsonQueries.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
import json
import logging
import math
import pickle
import random
import subprocess
from bench_utils import get_random_data_slice
from nobench_gendata import encode_string
from Query import Query
from Global import pjson_db
from Settings import (
FILES_DIR,
PJSON_FILENAME,
PJSON_EXTRA_FILENAME,
PJSON_PICKLE_FILENAME,
DATA_SIZE,
PSQL_USER,
)
__author__ = 'Ahmed'
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
try:
with open(PJSON_PICKLE_FILENAME, 'rb') as infile:
recommended_strings = pickle.load(infile)
except Exception as e:
log.error("Couldn't find pickle file!! (exception: {0})".format(str(e)))
recommended_strings = []
class Query1PJson(Query):
def __init__(self):
super(Query1PJson, self).__init__("Projection Query 1")
def db_command(self):
cur = pjson_db.cursor()
cur.execute("SELECT data -> 'str1' AS str1, data -> 'num' AS num FROM pjson_main;")
return cur
class Query2PJson(Query):
def __init__(self):
super(Query2PJson, self).__init__("Projection Query 2")
def db_command(self):
cur = pjson_db.cursor()
cur.execute("SELECT data #> '{nested_obj,str}' AS nested_str, data #> '{nested_obj,num}' AS nested_num FROM pjson_main;")
return cur
class Query3PJson(Query):
def __init__(self):
super(Query3PJson, self).__init__("Projection Query 3")
def db_command(self):
cur = pjson_db.cursor()
cur.execute("SELECT data -> 'sparse_110' AS sparse_110, data -> 'sparse_119' AS sparse_119 FROM pjson_main WHERE data ?& array['sparse_110', 'sparse_119'];")
return cur
class Query4PJson(Query):
def __init__(self):
super(Query4PJson, self).__init__("Projection Query 4")
def db_command(self):
cur = pjson_db.cursor()
cur.execute("SELECT data -> 'sparse_305' AS sparse_305, data -> 'sparse_991' AS sparse_991 FROM pjson_main WHERE data ?| array['sparse_305', 'sparse_991'];")
return cur
class Query5PJson(Query):
def __init__(self):
super(Query5PJson, self).__init__("Selection Query 5")
def prepare(self):
seed = random.randint(0, DATA_SIZE - 1)
self.arguments = [encode_string(seed)]
def db_command(self):
cur = pjson_db.cursor()
cur.execute(
"SELECT * FROM pjson_main WHERE data ->> 'str1' = '{}';".format(self.arguments[0])
)
return cur
class Query6PJson(Query):
def __init__(self):
super(Query6PJson, self).__init__("Selection Query 6")
def prepare(self):
#getting 0.1 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.001)
def db_command(self):
cur = pjson_db.cursor()
cur.execute(
"SELECT * FROM pjson_main WHERE CAST(data->>'num' AS integer) >= {}"
" AND CAST(data->>'num' AS integer) < {};".format(
self.arguments[0], self.arguments[1]
)
)
return cur
class Query7PJson(Query):
def __init__(self):
super(Query7PJson, self).__init__("Selection Query 7")
def prepare(self):
#getting 0.1 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.001)
def db_command(self):
cur = pjson_db.cursor()
cur.execute(
"SELECT * FROM pjson_main WHERE data->>'dyn1' >= '{}' AND data->>'dyn1' < '{}';".format(
self.arguments[0], self.arguments[1]
)
)
return cur
class Query8PJson(Query):
def __init__(self):
super(Query8PJson, self).__init__("Selection Query 8")
def prepare(self):
global recommended_strings
random.seed()
random.shuffle(recommended_strings)
self.arguments.append(recommended_strings[0])
def db_command(self):
search_term = self.arguments[0]
jsonb_query = "SELECT * from pjson_main WHERE data @> '{0}';".format(
json.dumps({'nested_arr': [search_term]})
)
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
#jsonb = "SELECT * from pjson_main WHERE data @> '{"nested_arr": ["interested"]}';"
class Query9PJson(Query):
def __init__(self):
super(Query9PJson, self).__init__("Selection Query 9")
def prepare(self):
cur = pjson_db.cursor()
cur.execute("SELECT data->>'sparse_500' from pjson_main WHERE data ? 'sparse_500';")
for index, result in enumerate(cur):
if index == 5:
self.arguments.append(result[0])
def db_command(self):
jsonb_query = "SELECT * FROM pjson_main WHERE data ->> 'sparse_500' = '{0}';".format(
self.arguments[0])
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
class Query10PJson(Query):
def __init__(self):
super(Query10PJson, self).__init__("Aggregation Query 10")
def prepare(self):
#getting 10 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.1)
def db_command(self):
jsonb_query = (
"SELECT COUNT(*) FROM pjson_main WHERE CAST(data->>'num' AS integer) >= {}"
" AND CAST(data->>'num' AS integer) < {}"
" GROUP BY data->>'thousandth';".format(
self.arguments[0], self.arguments[1]
)
)
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
class Query11PJson(Query):
def __init__(self):
super(Query11PJson, self).__init__("Join Query 11")
def prepare(self):
#getting 0.1 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.001)
def db_command(self):
jsonb_query = (
"SELECT * FROM pjson_main a INNER JOIN pjson_main b"
" ON (a.data ->> 'str1' = b.data #>> '{{nested_obj,str}}')"
" WHERE CAST(a.data->>'num' AS integer) >= {}"
" AND CAST(a.data->>'num' AS integer) < {};".format(
self.arguments[0], self.arguments[1]
)
)
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
class Query12PJson(Query):
def __init__(self):
super(Query12PJson, self).__init__("Data Addition Query 12")
def db_command(self):
pjson_load_cmd = "COPY pjson_main FROM '{0}';".format(
FILES_DIR + PJSON_EXTRA_FILENAME)
load_pjson = subprocess.Popen(["psql", "-w", "-U", PSQL_USER, "-d", "pjson", "-c", pjson_load_cmd],
stdout=subprocess.PIPE)
load_pjson.communicate()
class Query13PJson(Query):
def __init__(self):
super(Query13PJson, self).__init__("Deep Select Query 13")
def prepare(self):
seed = random.randint(0, DATA_SIZE - 1)
self.arguments = [encode_string(seed)]
def db_command(self):
jsonb_query = (
"SELECT * FROM pjson_main"
" WHERE data #>> '{{deep_nested_obj,level_2,level_3,level_4"
",level_5,level_6,level_7,level_8,deep_str_single}}' = '{0}';".format(
self.arguments[0]
)
)
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
class Query14PJson(Query):
def __init__(self):
super(Query14PJson, self).__init__("Deep Select Query 14")
def prepare(self):
seed = random.randint(0, 9)
self.arguments = [encode_string(seed)]
def db_command(self):
jsonb_query = (
"SELECT data #>> '{{deep_nested_obj,level_2,level_3,level_4"
",level_5,level_6,level_7,level_8,deep_str_agg}}' FROM pjson_main"
" WHERE data #>> '{{deep_nested_obj,level_2,level_3,level_4"
",level_5,level_6,level_7,level_8,deep_str_agg}}' = '{0}';".format(
self.arguments[0]
)
)
cur = pjson_db.cursor()
cur.execute(jsonb_query)
return cur
class Query15PJson(Query):
def __init__(self):
super(Query15PJson, self).__init__("Data Update Query 15")
def prepare(self):
# Update 20 data entries
self.update_range = get_random_data_slice(DATA_SIZE, 20.0 / DATA_SIZE)
self.arguments = [(i, random.random()) for i in xrange(*self.update_range)]
def db_command(self):
cur = pjson_db.cursor()
for tup in self.arguments:
jsonb_query = (
"SELECT jsonb_set(data, '{{dyn2}}', '{0}') FROM pjson_main"
" WHERE data ->> 'num' = '{1}';".format(tup[1], tup[0])
)
cur.execute(jsonb_query)
class DropCollectionPJson(Query):
def __init__(self):
super(DropCollectionPJson, self).__init__("Dropping Data from PJson")
def db_command(self):
pjson_drop_cmd = "DROP TABLE pjson_main;"
drop_pjson = subprocess.Popen(["psql", "-w", "-U", PSQL_USER, "-d", "pjson", "-c", pjson_drop_cmd],
stdout=subprocess.PIPE)
drop_pjson.communicate()
class InitialLoadPJson(Query):
def __init__(self):
super(InitialLoadPJson, self).__init__("Loading Initial Data into PJson")
def db_command(self):
pjson_load_cmd = "CREATE TABLE pjson_main(data jsonb);"
pjson_load_cmd += "COPY pjson_main FROM '{0}';".format(
FILES_DIR + PJSON_FILENAME)
pjson_load_cmd += "CREATE INDEX on pjson_main USING GIN (data);"
load_pjson = subprocess.Popen(["psql", "-w", "-U", PSQL_USER, "-d", "pjson", "-c", pjson_load_cmd],
stdout=subprocess.PIPE)
load_pjson.communicate()
def generate_data_pjson(items):
global recommended_strings
with open(PJSON_PICKLE_FILENAME, 'wb') as outfile:
pickle.dump(recommended_strings, outfile)