-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapping.py
300 lines (264 loc) · 11 KB
/
mapping.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
# Copyright 2016 Parinz Ameri, Haipeng Guan
#
# This file is part of Nowog.
#
# Nowog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Nowog is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Nowog. If not, see <http://www.gnu.org/licenses/>
import random
import string
from bson.son import SON
import values
import logging
"""
This module is used for mapping parse result into MongoDB command.
The core methods of mapping only include unpack(), makeSON() and mapping().
DBCommand class is specifically used for generate a instance of SON, which can be
directly used in db.command().
"""
def unpack(data, parent=[]):
"""unpack parse result in order to unpack nested attributes.
Can be used in generating query for find(), delete() and update()
Examples:
['A1', [['B1', 'num_match']]] -> ['A1.B1', 'num_match']
['A1', [['B1', [['C1', 'arr_read_op', 'Text']]]]] -> ['A1.B1.C1', 'arr_read_op', 'Text']
"""
res = []
for l in data:
new_parent = parent+[l[0]]
if isinstance(l[1], list):
map(res.append, unpack(l[1], new_parent))
else:
res.append(['.'.join(new_parent)]+l[1:])
return res
def makeSON(lst):
"""Convert parsing result list (packed or unpacked) into a instance of SON.
The first element (attribute) is KEY, and use '.' to combine all following
element and use it as VALUE in SON.
Example:
['A1', [['B1', 'num_match']]] -> {'A1': {'B1': 'num_match'}}
['A1.B1.C1', 'arr_read_op', 'Text'] -> {'A1.B1.C1': 'arr_read_op.Text'}
"""
res = SON()
for l in lst:
if isinstance(l[1], list):
res[l[0]] = makeSON(l[1])
else:
res[l[0]] = '.'.join(l[1:])
return res
def mapping(data, dictionary):
"""Mapping a dictionary (SON) into a a MongoDB command.
The idea is: all values are keywords of a lambda functions dictionary,
each function take attribute as the only argument. Each lambda function
will generate a SON file consist of proper form of MongoDB operation
and a random values in designated place.
Note:
please notice that same keyword might represent different lambda
function in different type of operation.
E.g. Array.Num in update and insert represent different operator.
Example:
{'A1.B1': 'num_match'} + {'num_match' : lambda attr: {attr: self.values.randInt()},}
--> return {'A1.B1': 130416}
"""
res = SON()
for attr,cmd in data.items():
if isinstance(cmd, dict): # nested document: {'A1': {'B1': 'num_match'}}
res[attr] = mapping(cmd, dictionary)
else:
new = dictionary[cmd](attr)
assert(len(new) == 1)
k, v = new.items()[0]
if k in res: # duplicate attribute -> operators in update, e.g. $set, $push
res[k].update(v)
else:
res.update(new)
return res
class DBCommand(object):
"""Generate a instance of SON can be directly used by db.command()"""
def __init__(self, seed=None, **kwargs):
self.logger = logging.getLogger('DBCommand')
self.logger.setLevel(logging.INFO)
self.init_values(seed, **kwargs)
self.query_dict = { # used for find() and delete()
'True' : lambda attr: {attr: True},
'False' : lambda attr: {attr: False},
'geo_op': lambda attr: {attr: {'$near': {'$geometry': {'type': 'Point', 'coordinates': [0, 0]},'$maxDistance': 50}}},
'num_match' : lambda attr: {attr: self.values.randInt()},
'text_read' : lambda attr: {attr: self.values.randStr()},
'range_op' : lambda attr: {attr: self.values.randRangeDict()},
'arr_read_op' : lambda attr: {attr: self.values.randIntArray()},
'arr_read_op.Text' : lambda attr: {attr: self.values.randStrArray()},
'arr_read_op.Num' : lambda attr: {attr: self.values.randIntArray()},
'arr_read_op.Bool' : lambda attr: {attr: self.values.randBoolArray()},
'arr_read_op.range_op' : lambda attr: {attr: {'$elemMatch': self.values.randRangeDict()}},
}
self.sort_dict = { # used for find()
'1' : 1,
'-1': -1
}
self.update_dict = { # used for update()
'True' : lambda attr: {'$set': {attr: True}},
'False': lambda attr: {'$set': {attr: False}},
'num_match' : lambda attr: {'$set': {attr: self.values.randInt()}},
'text_write' : lambda attr: {'$set': {attr: self.values.randStr()}},
'Array.Text' : lambda attr: {'$set': {attr: self.values.randStrArray()}},
'Array.Num' : lambda attr: {'$set': {attr: self.values.randIntArray()}},
'Array.Bool' : lambda attr: {'$set': {attr: self.values.randBoolArray()}},
'arr_add_op.Text' : lambda attr: {'$push': {attr: self.values.randStr()}},
'arr_add_op.Num' : lambda attr: {'$push': {attr: self.values.randInt()}},
'arr_add_op.Bool' : lambda attr: {'$push': {attr: self.values.randBool()}},
'arr_remove_op.Text' : lambda attr: {'$push': {attr: self.values.randStrArray()}},
'arr_remove_op.Num' : lambda attr: {'$push': {attr: self.values.randIntArray()}},
'arr_remove_op.Bool' : lambda attr: {'$push': {attr: self.values.randBoolArray()}},
}
self.document_dict = { # used for inert()
'True' : lambda attr: {attr: True},
'False' : lambda attr: {attr: False},
'num_match' : lambda attr: {attr: self.values.randInt()},
'text_write' : lambda attr: {attr: self.values.randStr()},
'Array.Text' : lambda attr: {attr: self.values.randStrArray()},
'Array.Num' : lambda attr: {attr: self.values.randIntArray()},
'Array.Bool' : lambda attr: {attr: self.values.randBoolArray()},
}
def init_values(self, seed=None, **kwargs):
self.values = values.Values(seed, **kwargs)
def makeCommands(self, read, write, sort, size=1, coll_name='undefined'):
if self.isFind(read, write):
return self.makeFindCmds(read, sort, size, coll_name)
elif self.isInsert(read, write):
return self.makeInsertCmds(write, size, coll_name)
elif self.isUpdate(read, write):
return self.makeUpdateCmds(read, write, size, coll_name)
elif self.isDelete(read, write):
return self.makeDeleteCmds(read, size, coll_name)
else:
self.hanlde_err_type(read, write)
# -------------------------------------------------------------------
# | read == [] | read != [] | read==['ALL'] |
# -------------------------------------------------------------------
# write == [] | x | find | x |
# -------------------------------------------------------------------
# write != [] | insert | update | update all |
# -------------------------------------------------------------------
# write==[NULL] | x | delete | x |
# -------------------------------------------------------------------
def isFind(self, read, write):
return read != [] and read[0] != 'ALL' and write == []
def isUpdate(self, read, write):
return read != [] and write != [] and write[0] != 'NULL'
def isInsert(self, read, write):
return read == [] and write != [] and write[0] != 'NULL'
def isDelete(self, read, write):
return read != [] and read[0] != 'ALL' and write[0] == 'NULL'
def hanlde_err_type(self, read, write):
err_str = 'Operation type error: '
if read == [] and write == []:
raise TypeError(err_str + 'at least either <read> or <write> should have one input phrase')
elif read == [] and write[0] == 'NULL':
raise TypeError(err_str + 'for operation [delete], <read> should have at least one input phrase')
elif read[0] == 'ALL' and write == []:
raise TypeError(err_str + 'for operation [update], <write> should have at least one input phrase')
elif read[0] == 'ALL' and write[0] == 'NULL':
raise TypeError(err_str + 'Keyword "ALL" and "NULL" cannot be used together')
def makeFindCmds(self, read, sort, size=1, coll_name='undefined'):
self.logger.info('making %d commands: [find]' % size)
queries = self.makeQuery(read, size)
sort = self.makeSort(sort)
return [self.getFindTemplate(q, sort, coll_name) for q in queries]
def makeUpdateCmds(self, read, write, size=1, coll_name='undefined'):
self.logger.info('making %d commands: [update]' % size)
queries = self.makeQuery(read, size)
updates = self.makeUpdate(write, size)
return [self.getUpdateTemplate(queries[i], updates[i], coll_name) for i in xrange(size)]
def makeInsertCmds(self, write, size=1, coll_name='undefined'):
self.logger.info('making %d commands: [insert]' % size)
documents = self.makeDocuments(write, size)
return [self.getInsertTemplate(d, coll_name) for d in documents]
def makeDeleteCmds(self, read, size=1, coll_name='undefined'):
self.logger.info('making %d commands: [delete]' % size)
queries = self.makeQuery(read, size)
return [self.getDeleteTemplate(q, coll_name) for q in queries]
def makeQuery(self, read, size=1):
if read[0] == 'ALL': return [SON()]*size
read_unpacked = unpack(read)
read_SON = makeSON(read_unpacked)
return [mapping(read_SON, self.query_dict) for _ in xrange(size)]
def makeUpdate(self, write, size=1):
write_unpacked = unpack(write)
write_SON = makeSON(write_unpacked)
return [mapping(write_SON, self.update_dict) for _ in xrange(size)]
def makeDocuments(self,write, size=1):
# NO unpack!!
write_SON = makeSON(write)
return [mapping(write_SON, self.document_dict) for _ in xrange(size)]
def makeSort(self, sort):
if sort == [] or sort[0] == 'NULL': return None
else: return SON([ (lst[0], self.sort_dict[lst[1]]) for lst in sort ])
# https://docs.mongodb.org/manual/reference/command/find/#dbcmd.find
# {
# "find": <string>,
# "filter": <document>,
# "sort": <document>,
# }
def getFindTemplate(self, query, sort, coll_name='undefined'):
find = SON([
('find', coll_name),
('filter', query)])
if sort: find['sort'] = sort
return find
# https://docs.mongodb.org/manual/reference/command/update/#dbcmd.update
# {
# update: <collection>,
# updates:
# [
# { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },
# ...
# ],
# }
def getUpdateTemplate(self, query, update, coll_name):
return SON([
('update', coll_name),
('updates',[
SON([('q', query), ('u', update), ('multi', True)])
])
])
# https://docs.mongodb.org/manual/reference/command/insert/#dbcmd.insert
# {
# insert: <collection>,
# documents: [ <document>, <document>, <document>, ... ],
# }
def getInsertTemplate(self, document, coll_name):
return SON([
('insert', coll_name),
('documents', [document])
])
# https://docs.mongodb.org/manual/reference/command/delete/#dbcmd.delete
# {
# delete: <collection>,
# deletes: [
# { q : <query>, limit : <integer> },
# ...
# ],
# }
def getDeleteTemplate(self, query, coll_name):
return SON([
('delete', coll_name),
('deletes', [SON([('q', query),('limit', 0)])])
])
# if __name__ == '__main__':
# read = [['A1', 'False'], ['A2', 'num_match'], ['A3', [['B1', 'text_read']]]]
# # print make_Queries(read,3)
# db_cmd = DBCommand()
# finds = db_cmd.makeFindCmds(read, ['NULL'], 2)
# import json
# for f in finds:
# print json.dumps(f, indent=4)