forked from sonic-net/sonic-mgmt-framework
-
Notifications
You must be signed in to change notification settings - Fork 16
/
cvl_luascript.go
255 lines (197 loc) · 7.11 KB
/
cvl_luascript.go
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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright 2019 Broadcom. The term Broadcom refers to Broadcom Inc. and/or //
// its subsidiaries. //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// //
////////////////////////////////////////////////////////////////////////////////
package cvl
import (
"github.com/go-redis/redis"
)
//Redis server side script
func loadLuaScript(luaScripts map[string]*redis.Script) {
// Find entry which has given fieldName and value
luaScripts["find_key"] = redis.NewScript(`
local tableName=ARGV[1]
local sep=ARGV[2]
local fieldName=ARGV[3]
local fieldValue=ARGV[4]
-- Check if field value is part of key
local entries=redis.call('KEYS', tableName..sep.."*"..fieldValue.."*")
if (entries[1] ~= nil)
then
return entries[1]
else
-- Search through all keys for fieldName and fieldValue
local entries=redis.call('KEYS', tableName..sep.."*")
local idx = 1
while(entries[idx] ~= nil)
do
local val = redis.call("HGET", entries[idx], fieldName)
if (val == fieldValue)
then
-- Return the key
return entries[idx]
end
idx = idx + 1
end
end
-- Could not find the key
return ""
`)
//Find current number of entries in a table
luaScripts["count_entries"] = redis.NewScript(`
--ARGV[1] => Key patterns
--ARGV[2] => Key names separated by '|'
--ARGV[3] => predicate patterns
--ARGV[4] => Field
if (#ARGV == 1) then
--count of all table entries
return #redis.call('KEYS', ARGV[1])
end
-- count with filter
local keys = redis.call('KEYS', ARGV[1])
if #keys == 0 then return 0 end
local cnt = 0
local sepStart = string.find(keys[1], "|")
if sepStart == nil then return ; end
-- Function to load lua predicate code
local function loadPredicateScript(str)
if (str == nil or str == "") then return nil; end
local f, err = loadstring("return function (k,h) " .. str .. " end")
if f then return f(); else return nil;end
end
local keySetNames = {}
ARGV[2]:gsub("([^|]+)",function(c) table.insert(keySetNames, c) end)
local predicate = loadPredicateScript(ARGV[3])
local field = ""
if (ARGV[4] ~= nil) then field = ARGV[4]; end
for _, key in ipairs(keys) do
local hash = redis.call('HGETALL', key)
local row = {}; local keySet = {}; local keyVal = {}
local keyOnly = string.sub(key, sepStart+1)
for index = 1, #hash, 2 do
row[hash[index]] = hash[index + 1]
end
local incFlag = false
if (predicate == nil) then
incFlag = true
else
--Split key values
keyOnly:gsub("([^|]+)", function(c) table.insert(keyVal, c) end)
for idx = 1, #keySetNames, 1 do
keySet[keySetNames[idx]] = keyVal[idx]
end
if (predicate(keySet, row) == true) then
incFlag = true
end
end
if (incFlag == true) then
if (field ~= "") then
if (row[field] ~= nil) then
cnt = cnt + 1
elseif (row[field.."@"] ~= nil) then
row[field.."@"]:gsub("([^,]+)", function(c) cnt = cnt + 1 end)
elseif (string.match(ARGV[2], field.."[|]?") ~= nil) then
cnt = cnt + 1
end
else
cnt = cnt + 1
end
end
end
return cnt
`)
// Find entry which has given fieldName and value
luaScripts["filter_keys"] = redis.NewScript(`
--ARGV[1] => Key patterns
--ARGV[2] => Key names separated by '|'
--ARGV[3] => predicate patterns
local filterKeys = ""
local keys = redis.call('KEYS', ARGV[1])
if #keys == 0 then return end
local sepStart = string.find(keys[1], "|")
if sepStart == nil then return ; end
-- Function to load lua predicate code
local function loadPredicateScript(str)
if (str == nil or str == "") then return nil; end
local f, err = loadstring("return function (k,h) " .. str .. " end")
if f then return f(); else return nil;end
end
local keySetNames = {}
ARGV[2]:gsub("([^|]+)",function(c) table.insert(keySetNames, c) end)
local predicate = loadPredicateScript(ARGV[3])
for _, key in ipairs(keys) do
local hash = redis.call('HGETALL', key)
local row = {}; local keySet = {}; local keyVal = {}
local keyOnly = string.sub(key, sepStart+1)
for index = 1, #hash, 2 do
row[hash[index]] = hash[index + 1]
end
--Split key values
keyOnly:gsub("([^|]+)", function(c) table.insert(keyVal, c) end)
for idx = 1, #keySetNames, 1 do
keySet[keySetNames[idx]] = keyVal[idx]
end
if (predicate == nil) or (predicate(keySet, row) == true) then
filterKeys = filterKeys .. key .. ","
end
end
return string.sub(filterKeys, 1, #filterKeys - 1)
`)
//Get filtered entries as per given key filters and predicate
luaScripts["filter_entries"] = redis.NewScript(`
--ARGV[1] => Key patterns
--ARGV[2] => Key names separated by '|'
--ARGV[3] => predicate patterns
--ARGV[4] => Fields to return
--ARGV[5] => Count of entries to return
local tableData = {} ; local tbl = {}
local keys = redis.call('KEYS', ARGV[1])
if #keys == 0 then return end
local count = -1
if (ARGV[5] ~= nil and ARGV[5] ~= "") then count=tonumber(ARGV[5]) end
local sepStart = string.find(keys[1], "|")
if sepStart == nil then return ; end
-- Function to load lua predicate code
local function loadPredicateScript(str)
if (str == nil or str == "") then return nil; end
local f, err = loadstring("return function (k,h) " .. str .. " end")
if f then return f(); else return nil;end
end
local keySetNames = {}
ARGV[2]:gsub("([^|]+)",function(c) table.insert(keySetNames, c) end)
local predicate = loadPredicateScript(ARGV[3])
for _, key in ipairs(keys) do
local hash = redis.call('HGETALL', key)
local row = {}; local keySet = {}; local keyVal = {}
local keyOnly = string.sub(key, sepStart+1)
for index = 1, #hash, 2 do
row[hash[index]] = hash[index + 1]
end
--Split key values
keyOnly:gsub("([^|]+)", function(c) table.insert(keyVal, c) end)
for idx = 1, #keySetNames, 1 do
keySet[keySetNames[idx]] = keyVal[idx]
end
if (predicate == nil) or (predicate(keySet, row) == true) then
tbl[keyOnly] = row
end
if (count ~= -1 and #tbl == count) then break end
end
tableData[string.sub(keys[1], 0, sepStart-1)] = tbl
return cjson.encode(tableData)
`)
}