-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
123 lines (117 loc) · 2.67 KB
/
transaction.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
package main
type transaction struct {
id int64
request request
}
func (tr transaction) execute() string {
switch tr.request.fn {
case "create_db":
if len(tr.request.args) <= 0 {
caba_err("ERR GET - NOT ENOUGH ARGS")
return "not ok"
}
v := create_db(tr.request.args[0])
if v != nil {
return v.name
}
return "not ok"
case "choose_db":
if len(tr.request.args) <= 0 {
caba_err("ERR GET - NOT ENOUGH ARGS")
return "not ok"
}
maindb = db{tr.request.args[0]}
cache_.clear()
return tr.request.args[0]
case "get":
if len(tr.request.args) <= 0 {
caba_err("ERR GET - NOT ENOUGH ARGS")
return "not ok"
}
v := db.get(maindb, tr.request.args[0])
if v != _zeroslice {
return v.value
}
return ""
case "multiget":
if len(tr.request.args) <= 0 {
caba_err("ERR GET - NOT ENOUGH ARGS")
return "not ok"
}
v := db.multiget(maindb, tr.request.args)
if v == nil {
return "[]"
}
rv := "["
for _, k := range v {
rv += k.value + ","
}
rv = rv[:len(rv)-1] + "]"
return rv
case "set":
if len(tr.request.args) <= 0 {
caba_err("ERR SET - NOT ENOUGH ARGS")
return "not ok"
}
v := []dbslice{}
for i := 0; i < len(tr.request.args); i += 2 {
v = append(v, dbslice{tr.request.args[i], tr.request.args[i+1]})
}
defer db.set(maindb, v)
return "ok - set"
case "del":
if len(tr.request.args) <= 0 {
caba_err("ERR DEL - NOT ENOUGH ARGS")
return "not ok"
}
db.remove(maindb, tr.request.args[0])
return "ok - del"
case "clearcache":
cache_.clear()
return "ok - cleared"
case "updatecache":
cache_.save_cache()
return "ok - updated"
case "loadcache":
cache_.load_cache()
return "ok - loaded"
case "loadfrom":
if len(tr.request.args) <= 0 {
caba_err("ERR LOADFROM - NOT ENOUGH ARGS")
return "not ok"
}
load_backup_file(tr.request.args[0])
return "ok - loaded from " + tr.request.args[0]
case "save":
return save_backup()
case "asave":
go save_backup_async()
return "ok - async updating backup started"
case "loadcfg":
if len(tr.request.args) <= 0 {
caba_err("ERR LOADFROM - NOT ENOUGH ARGS")
return "not ok"
}
load_cfg(tr.request.args[0])
return "ok - loaded config " + tr.request.args[0]
case "zip":
dname := ""
if len(tr.request.args) <= 0 {
dname = maindb.name
} else {
dname = tr.request.args[0]
}
createZip(dname+".zip", dname)
return dname
case "unzip":
dname := ""
if len(tr.request.args) <= 0 {
dname = maindb.name
} else {
dname = tr.request.args[0]
}
unzip(dname+".zip", dname)
return dname
}
return "ok"
}