-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
326 lines (304 loc) · 8.03 KB
/
tree.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
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
package redigotree
import (
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
)
type TreeNode struct {
Node string `json:"node"`
HasChild bool `json:"hasChild"`
Children []TreeNode `json:"children"`
}
var treeCommands map[string]string
var head = strings.Join(Filter(strings.Split(loadScript("_head"), "\n"), isNotCommend), " ") + " "
var deleteReference = strings.Join(Filter(strings.Split(loadScript("_delete_reference"), "\n"), isNotCommend), " ") + " "
var getPath = strings.Join(Filter(strings.Split(loadScript("_get_path"), "\n"), isNotCommend), " ") + " "
func loadScript(name string) string {
_, file, _, _ := runtime.Caller(1)
path := filepath.Join(filepath.Dir(file), "lua")
b, err := ioutil.ReadFile(path + "/" + name + ".lua")
if err != nil {
panic(fmt.Sprintf("Load Lua Script Failed: %s", err))
}
str := string(b)
str = strings.Join(Filter(strings.Split(str, "\n"), isNotCommend), " ") + " "
return str
}
func isNotCommend(line string) bool {
if line == "" {
return false
}
return strings.TrimSpace(line)[0:2] != "--"
}
func getLuaScript(command string) string {
lua := loadScript(command)
if command == "trem" || command == "tdestroy" || command == "tmrem" || command == "tprune" {
lua = deleteReference + lua
} else if command == "tpath" || command == "tinsert" || command == "tmovechildren" {
lua = getPath + lua
}
return head + lua
}
var commands = []string{
"tinsert", "tchildren", "tparents", "tpath", "trem", "tmrem",
"tdestroy", "texists", "trename", "tprune", "tmovechildren",
}
func init() {
treeCommands = func(vs []string, f func(string) string) map[string]string {
vsm := make(map[string]string, len(vs))
for _, v := range vs {
vsm[v] = f(v)
}
return vsm
}(commands, getLuaScript)
}
func convertNode(t interface{}) TreeNode {
var ret TreeNode
items, ok := t.([]interface{})
if !ok {
return TreeNode{}
}
ret = TreeNode{
B2S(items[0].([]uint8)),
items[1].(int64) > 0,
nil,
}
if len(items) > 2 {
ret.Children = []TreeNode{}
for i := 2; i < len(items); i++ {
child := convertNode(items[i])
if child.Node != "" {
ret.Children = append(ret.Children, child)
}
}
}
return ret
}
func callTreeApi(command string, args []interface{}) (reply interface{}, err error) {
if lua, ok := treeCommands[command]; ok {
redisClient := redisClient.Get()
defer redisClient.Close()
var getScript = redis.NewScript(1, lua)
return getScript.Do(redisClient, args...)
}
// log.Error("RedisTree Command Not Exist")
return nil, errors.New("RedisTree Command Not Exist")
}
func TInsert(key string, parent string, node string, options map[string]string) int {
var reply interface{}
var err error
args := []interface{}{key, parent, node}
if option, ok := options["index"]; ok {
args = append(args, "INDEX", option) //Index, Returned by Insert
} else if option, ok := options["before"]; ok {
args = append(args, "BEFORE", option) //Insert Before Node
} else if option, ok := options["after"]; ok {
args = append(args, "AFTER", option) //Insert After Node
} else {
args = append(args, "INDEX", -1) //Default: Insert At Last Pos
}
reply, err = callTreeApi("tinsert", args)
if err != nil {
log.Error("RedisTree TInsert Error: ", err)
return -1
}
/* log.WithFields(log.Fields{
"key": key,
"parent": parent,
"node": node,
"options": options,
}).Debug("RedisTree TInsert")
*/
insertIndex, _ := redis.Int(reply, err)
return insertIndex
}
func TChildren(key string, node string, options map[string]string) []TreeNode {
var reply interface{}
var err error
args := []interface{}{key, node}
if option, ok := options["level"]; ok {
args = append(args, "LEVEL", option)
}
reply, err = callTreeApi("tchildren", args)
if err != nil {
log.Error("RedisTree TChildren Error: ", err)
return nil
}
/*log.WithFields(log.Fields{
"key": key,
"node": node,
"options": options,
}).Debug("RedisTree TChildren")
*/
items, err := redis.Values(reply, err)
return func(vs []interface{}, f func(interface{}) TreeNode) []TreeNode {
vsm := make([]TreeNode, len(vs))
for i, v := range vs {
result := f(v)
if result.Node != "" {
vsm[i] = f(v)
}
}
// log.Debug("RedisTree TChildren: returned")
return vsm
}(items, convertNode)
}
func TParents(key string, node string) []string {
var reply interface{}
var err error
args := []interface{}{key, node}
reply, err = callTreeApi("tparents", args)
if err != nil {
log.Error("RedisTree TParents Error: ", err)
return nil
}
/*log.WithFields(log.Fields{
"key": key,
"node": node,
}).Debug("RedisTree TParents")
*/
parents, _ := redis.Strings(reply, err)
return parents
}
func TPath(key string, node string, newNode string) []string {
var reply interface{}
var err error
args := []interface{}{key, node, newNode}
reply, err = callTreeApi("tpath", args)
if err != nil {
log.Error("RedisTree TPath Error: ", err)
return []string{}
}
/*log.WithFields(log.Fields{
"key": key,
"node": node,
"newnode": newNode,
}).Debug("RedisTree TPath")
*/
path, _ := redis.Strings(reply, err)
return path
}
func TRem(key string, parent string, count int, node string) int {
var reply interface{}
var err error
args := []interface{}{key, parent, count, node}
reply, err = callTreeApi("trem", args)
if err != nil {
log.Error("RedisTree TRem Error: ", err)
return -1
}
num, _ := redis.Int(reply, err)
// TREM returns count of remaining nodes in the parent.
return num
}
func TMrem(key string, node string, options map[string]string) interface{} {
var reply interface{}
var err error
args := []interface{}{key, node}
if option, ok := options["not"]; ok {
args = append(args, "NOT", option) //Exclude a parent
}
reply, err = callTreeApi("tmrem", args)
if err != nil {
log.Error("RedisTree TMrem Error: ", err)
return nil
}
/* log.WithFields(log.Fields{
"key": key,
"node": node,
"options": options,
}).Debug("RedisTree TMrem")
*/
return reply
}
func TDestroy(key string, node string) int {
var reply interface{}
var err error
args := []interface{}{key, node}
reply, err = callTreeApi("tdestroy", args)
if err != nil {
log.Error("RedisTree TDestroy Error: ", err)
return -1
}
/*
log.WithFields(log.Fields{
"key": key,
"node": node,
}).Debug("RedisTree TDestroy")
*/
numberOfNodesDestroyed, _ := redis.Int(reply, err)
return numberOfNodesDestroyed
}
func TExists(key string, node string) bool {
var reply interface{}
var err error
args := []interface{}{key, node}
reply, err = callTreeApi("texists", args)
if err != nil {
log.Error("RedisTree TExists Error: ", err)
return false
}
/*log.WithFields(log.Fields{
"key": key,
"node": node,
}).Debug("RedisTree TExists")*/
exists, _ := redis.Bool(reply, err)
return exists
}
func TRename(key string, node string, newNode string) bool {
var reply interface{}
var err error
args := []interface{}{key, node, newNode}
reply, err = callTreeApi("trename", args)
if err != nil {
log.Error("RedisTree TRename Error: ", err)
return false
}
/*log.WithFields(log.Fields{
"key": key,
"node": node,
"newnode": newNode,
}).Debug("RedisTree TRename")*/
exists, _ := redis.Bool(reply, err)
return exists
}
func TPrune(key string, node string) bool {
var reply interface{}
var err error
args := []interface{}{key, node}
reply, err = callTreeApi("tprune", args)
if err != nil {
log.Error("RedisTree TPrune Error: ", err)
return false
}
pruned, _ := redis.String(reply, err)
return pruned == "OK"
}
func TMoveChildren(key string, source string, target string, op string) int {
if op != "PREPEND" && op != "APPEND" {
op = "APPEND"
}
var reply interface{}
var err error
args := []interface{}{key, source, target, op}
reply, err = callTreeApi("tmovechildren", args)
if err != nil {
log.Error("RedisTree TMoveChildren Error: ", err)
return -1
}
/*
log.WithFields(log.Fields{
"key": key,
"source": source,
"target": target,
"op": op,
}).Debug("RedisTree TMoveChildren")
*/
sourceListSize, _ := redis.Int(reply, err)
return sourceListSize
}