/
main.go
176 lines (156 loc) · 4.15 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/alash3al/go-color"
"github.com/rs/xid"
"github.com/tidwall/redcon"
)
func main() {
fmt.Println(color.CyanString(banner))
fmt.Printf("⇨ uTree started on port %s \n", color.GreenString(*flagListenAddr))
redcon.ListenAndServe(*flagListenAddr, handleProcessCommand, handleAcceptConn, nil)
}
func handleProcessCommand(conn redcon.Conn, cmd redcon.Command) {
action := strings.ToLower(string(cmd.Args[0]))
args := []string{}
for _, v := range cmd.Args[1:] {
args = append(args, string(v))
}
switch action {
default:
conn.WriteError("unknown command: " + action)
// ping
// returns PONG
case "ping":
conn.WriteString("PONG")
// gen
// generates a new unique id
case "gen":
id := xid.New().String()
conn.WriteString(id)
// append
// adds a new node to the tree
case "append":
if len(args) < 1 {
conn.WriteError("you must specify the parent")
break
}
parent, child := args[0], xid.New().String()
redisConn.SAdd("utree:tree:"+parent+":children", child).Val()
redisConn.Set("utree:tree:"+child+":parent", parent, -1).Val()
conn.WriteString(child)
// flatten
// returns the elements of the node [tree]
// as an array of strings
case "flatten":
if len(args) < 1 {
conn.WriteError("you must specify the parent")
break
}
var nest func(string) []string
parent := args[0]
loaded := map[string]bool{}
nest = func(parent string) []string {
ret := []string{}
children := redisConn.SMembers("utree:tree:" + parent + ":children").Val()
for _, id := range children {
if loaded[id] {
continue
}
loaded[id] = true
ret = append(ret, id)
ret = append(ret, nest(id)...)
}
return ret
}
all := nest(parent)
conn.WriteArray(len(all))
for _, v := range all {
conn.WriteBulkString(v)
}
// tree
// returns the node elements
// as a tree as json encoded string
case "tree":
if len(args) < 1 {
conn.WriteError("you must specify the parent")
break
}
var nest func(string) []Node
parent := args[0]
loaded := map[string]*Node{}
nest = func(parent string) []Node {
node := new(Node)
node.Node = parent
node.Tree = []Node{}
children := redisConn.SMembers("utree:tree:" + parent + ":children").Val()
for _, id := range children {
if loaded[id] != nil {
continue
}
loaded[id] = new(Node)
loaded[id].Node = id
loaded[id].Tree = nest(id)
node.Tree = append(node.Tree, *(loaded[id]))
}
return node.Tree
}
d, _ := json.Marshal(nest(parent))
conn.WriteString(string(d))
// returns a flatten array of the parents
// of the specified node
case "parents":
if len(args) < 1 {
conn.WriteError("you must specify a node id")
break
}
var nest func(id string) []string
nest = func(id string) []string {
parent := redisConn.Get("utree:tree:" + id + ":parent").Val()
if parent == "" {
return nil
}
return append([]string{parent}, nest(parent)...)
}
all := nest(args[0])
conn.WriteArray(len(all))
for _, v := range all {
conn.WriteBulkString(v)
}
// move a node to another parent
// 1)- fetch the old parent
// 2)- remove the node from the old parent children
// 3)- add the node to the dst's children
// 4)- set the parent of the node as dst
case "mv":
if len(args) < 2 {
conn.WriteError("you must specify the node and the dst")
break
}
n, dst := args[0], args[1]
oldParentID := redisConn.Get("utree:tree:" + n + ":parent").Val()
redisConn.SRem("utree:tree:"+oldParentID+":children", n).Val()
redisConn.SAdd("utree:tree:"+dst+":children", n).Val()
redisConn.Set("utree:tree:"+n+":parent", dst, -1).Val()
conn.WriteInt(1)
break
// remove a node
// 1)- fetch the old parent
// 2)- remove the node from the old parent children
// 3)- clean the parent from the node
case "rm":
if len(args) < 1 {
conn.WriteError("you must specify the deletable node")
break
}
n := args[0]
oldParentID := redisConn.Get("utree:tree:" + n + ":parent").Val()
redisConn.SRem("utree:tree:"+oldParentID+":children", n).Val()
redisConn.Del("utree:tree:" + n + ":parent").Val()
}
}
func handleAcceptConn(conn redcon.Conn) bool {
return true
}