-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrexastore.lua
More file actions
383 lines (346 loc) · 8.76 KB
/
rexastore.lua
File metadata and controls
383 lines (346 loc) · 8.76 KB
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
local _USAGE = {
'KEYS[1] - graph key name',
'KEYS[2] - command. Can be:',
' drop - drops an index',
' get - returns item if exists',
' put - puts new item to graph',
' delete or del - deletes item from graph',
' query - query using graph-query syntax',
}
local function drop()
redis.call('DEL', KEYS[1])
end
local function keys(t)
return {
t['subject'],
t['predicate'],
t['object']
}
end
local function _hexkeys(query)
local subject = query['subject']
local predicate = query['predicate']
local object = query['object']
return {
spo = subject .. ':' .. predicate .. ':' .. object,
sop = subject .. ':' .. object .. ':' .. predicate,
ops = object .. ':' .. predicate .. ':' .. subject,
osp = object .. ':' .. subject .. ':' .. predicate,
pso = predicate .. ':' .. subject .. ':' .. object,
pos = predicate .. ':' .. object .. ':' .. subject
}
end
local function parse(type, spo)
if not spo then
return nil
end
local v = string.gmatch(spo, '([^:]+)')
local t = type
if t == nil then
t = v()
end
local subject
local predicate
local object
if t == 'spo' then
subject = v()
predicate = v()
object = v()
elseif t == 'pos' then
predicate = v()
object = v()
subject = v()
elseif t == 'osp' then
object = v()
subject = v()
predicate = v()
elseif t == 'sop' then
subject = v()
object = v()
predicate = v()
elseif t == 'ops' then
object = v()
predicate = v()
subject = v()
elseif t == 'pso' then
predicate = v()
subject = v()
object = v()
end
local res = {}
if subject then
res['subject'] = subject
end
if predicate then
res['predicate'] = predicate
end
if object then
res['object'] = object
end
return (res)
end
local function put(query)
local data = _hexkeys(query)
redis.call('ZADD', KEYS[1], 0, 'spo:' .. data['spo'])
redis.call('ZADD', KEYS[1], 0, 'sop:' .. data['sop'])
redis.call('ZADD', KEYS[1], 0, 'ops:' .. data['ops'])
redis.call('ZADD', KEYS[1], 0, 'osp:' .. data['osp'])
redis.call('ZADD', KEYS[1], 0, 'pso:' .. data['pso'])
redis.call('ZADD', KEYS[1], 0, 'pos:' .. data['pos'])
end
local function delete(query)
local data = _hexkeys(query)
redis.call('ZREM', KEYS[1], 0, 'spo:' .. data['spo'])
redis.call('ZREM', KEYS[1], 0, 'sop:' .. data['sop'])
redis.call('ZREM', KEYS[1], 0, 'ops:' .. data['ops'])
redis.call('ZREM', KEYS[1], 0, 'osp:' .. data['osp'])
redis.call('ZREM', KEYS[1], 0, 'pso:' .. data['pso'])
redis.call('ZREM', KEYS[1], 0, 'pos:' .. data['pos'])
end
local function save_spo(key, items)
for _, p in pairs(items) do
redis.call('SADD', key, table.concat(keys(p), ':'))
end
end
local function save_items(key, items)
for _, p in pairs(items) do
redis.call('SADD', key, p)
end
end
local function get_items(key)
return redis.call('SMEMBERS', key)
end
local function get(data)
local t = ''
local q = ''
local subject = data['subject']
local predicate = data['predicate']
local object = data['object']
if subject then
t = t .. 's'
q = q .. ':' .. subject
end
if predicate then
t = t .. 'p'
q = q .. ':' .. predicate
end
if object then
t = t .. 'o'
q = q .. ':' .. object
end
local tl = #t
if #t == 1 then
if t == 's' then
t = t .. 'po'
end
if t == 'p' then
t = t .. 'os'
end
if t == 'o' then
t = t .. 'sp'
end
end
if #t == 2 then
if t == 'sp' then
t = t .. 'o'
end
if t == 'po' then
t = t .. 's'
end
if t == 'so' then
t = t .. 'p'
end
end
local s = t .. q
if tl < 3 then
s = s .. ':'
end
local results = redis.call(
'ZRANGEBYLEX', KEYS[1], '[' .. s, '[' .. s .. '\255'
)
local items = {}
for _, p in pairs(results) do
table.insert(items, parse(nil, p))
end
return(items)
end
local function parse_var(v)
if v:match(':') then
return nil
end
local t = v:sub(1, 1)
if t == '<' or t == '>' then
return {
type = t,
name = v:sub(2)
}
else
return false
end
end
local function query(q)
local res = {}
for i, p in pairs(q) do
local GKEY = 'GRAPH_QUERY:ITER_' .. i
local var = parse_var(p)
if var then
res = get_items('GRAPH_QUERY:VAR_' .. var['name'])
break
end
local d = parse('spo', p)
local s_var = parse_var(d['subject'])
local p_var = parse_var(d['predicate'])
local o_var = parse_var(d['object'])
local s_iter = {}
local p_iter = {}
local o_iter = {}
if s_var and s_var['type'] == '<' then
s_iter = get_items('GRAPH_QUERY:VAR_' .. s_var['name'])
else
s_iter = {d['subject']}
end
if p_var and p_var['type'] == '<' then
p_iter = get_items('GRAPH_QUERY:VAR_' .. p_var['name'])
else
p_iter = {d['predicate']}
end
if o_var and o_var['type'] == '<' then
o_iter = get_items('GRAPH_QUERY:VAR_' .. o_var['name'])
else
o_iter = {d['object']}
end
for _, s in pairs(s_iter) do
for _, p in pairs(p_iter) do
for _, o in pairs(o_iter) do
local s_var = parse_var(s)
local p_var = parse_var(p)
local o_var = parse_var(o)
local q = {}
if not s_var then
q['subject'] = s
end
if not p_var then
q['predicate'] = p
end
if not o_var then
q['object'] = o
end
local items = get(q)
local s_items = {}
local p_items = {}
local o_items = {}
for _, p in pairs(items) do
if s_var then
table.insert(s_items, p['subject'])
end
if p_var then
table.insert(p_items, p['predicate'])
end
if o_var then
table.insert(o_items, p['object'])
end
end
if #s_items > 0 then
save_items('GRAPH_QUERY:VAR_' .. s_var['name'], s_items)
redis.call('SADD', 'GRAPH_KEYS_TO_DELETE', 'GRAPH_QUERY:VAR_' .. s_var['name'])
end
if #p_items > 0 then
save_items('GRAPH_QUERY:VAR_' .. p_var['name'], p_items)
redis.call('SADD', 'GRAPH_KEYS_TO_DELETE', 'GRAPH_QUERY:VAR_' .. p_var['name'])
end
if #o_items > 0 then
save_items('GRAPH_QUERY:VAR_' .. o_var['name'], o_items)
redis.call('SADD', 'GRAPH_KEYS_TO_DELETE', 'GRAPH_QUERY:VAR_' .. o_var['name'])
end
end
end
end
redis.call('SADD', 'GRAPH_KEYS_TO_DELETE', GKEY)
redis.call('SADD', 'GRAPH_KEYS_TO_DELETE', 'GRAPH_KEYS_TO_DELETE')
end
local v_a = redis.call('SMEMBERS', 'GRAPH_QUERY:VAR_A')
local v_b = redis.call('SMEMBERS', 'GRAPH_QUERY:VAR_B')
local keys_to_delete = redis.call('SMEMBERS', 'GRAPH_KEYS_TO_DELETE')
for _, k in pairs(keys_to_delete) do
redis.call('DEL', k)
end
return res
end
local function test()
drop()
put({subject = 'Alice', predicate = 'friend', object = 'Bob'})
local len = redis.call('ZCARD', KEYS[1])
if len ~= 6 then
return({err = 'Wrong number of graph items'})
end
put({subject = 'Alice', predicate = 'friend', object = 'Carol'})
put({subject = 'Carol', predicate = 'friend', object = 'Alice'})
local friends = get({subject = 'Alice', predicate = 'friend'})
if #friends ~= 2 then
return({err = 'Wrong number of friends of Alice'})
end
if friends[1]['object'] ~= 'Bob' then
return({err = 'Wrong name of Alice\'s friend'})
end
delete({subject = 'Alice', predicate = 'friend', object = 'Bob'})
local len = redis.call('ZCARD', KEYS[1])
if len ~= 12 then
return({err = 'Graph items was not removed'})
end
put({subject = 'Alice', predicate = 'friend', object = 'Bob'})
local foaf = query({
'Carol:friend:>A',
'<A:friend:>B',
'<B'
})
-- return cjson
if #foaf ~= 2 then
return({err = 'Wrong number of friends of a friend of Carol'})
end
if foaf[1] ~= 'Bob' or foaf[2] ~= 'Carol' then
return({err = 'Wrong friends: ' .. cjson.encode(foaf)})
end
drop()
return('OK')
end
-- parse arguments
if #KEYS ~= 2 then
return(_USAGE)
end
local cmd = KEYS[2]:lower()
if cmd == 'test' then
return test()
end
if cmd == 'get' then
local result = get({
subject = ARGV[1],
predicate = ARGV[2],
object = ARGV[3]
})
local response = {}
for _, h in pairs(result) do
table.insert(response, keys(h))
end
return response
end
if cmd == 'put' then
return put({
subject = ARGV[1],
predicate = ARGV[2],
object = ARGV[3]
})
end
if cmd == 'delete' or cmd == 'del' then
return delete({
subject = ARGV[1],
predicate = ARGV[2],
object = ARGV[3]
})
end
if cmd == 'query' then
return query(ARGV)
end
if cmd == 'drop' then
return drop()
end
return(_USAGE)