-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
102 lines (98 loc) · 2.17 KB
/
generic.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
package main
import (
"github.com/gushitong/rekkles/stor"
"github.com/gushitong/rekkles/ut"
)
func del(db stor.DB, conn aryConnection, cmd aryCommand) {
if len(cmd.Args) == 0 {
conn.WriteError(ErrWrongNumOfArguments.Error())
return
}
err := db.Update(func(txn stor.Transaction) error {
for _, key := range cmd.Args {
// del string
stringKey := NewStringEncoder(key).Encode()
if err := txn.Del(stringKey); err != nil {
return err
}
// del hash
hashPrefix := ut.ConcatBytearray(
[]byte{(byte)(SymbolHash)},
[]byte{uint8(len(key))},
key,
)
if err := delprefix(db, hashPrefix); err != nil {
return err
}
// del set
setPrefix := ut.ConcatBytearray(
[]byte{(byte)(SymbolSet)},
[]byte{uint8(len(key))},
key,
)
if err := delprefix(db, setPrefix); err != nil {
return err
}
// del zset
zsetPrefix := ut.ConcatBytearray(
[]byte{(byte)(SymbolZset)},
[]byte{uint8(len(key))},
key,
)
if err := delprefix(db, zsetPrefix); err != nil {
return err
}
// del list
listPrefix := ut.ConcatBytearray(
[]byte{(byte)(SymbolList)},
[]byte{uint8(len(key))},
key,
)
if err := delprefix(db, listPrefix); err != nil {
return err
}
// del list prefix
listSeqPrefix := ut.ConcatBytearray(
[]byte{(byte)(SymbolListIndex)},
[]byte{uint8(len(key))},
key,
)
if err := delprefix(db, listSeqPrefix); err != nil {
return err
}
// del queue
queueKey := ut.ConcatBytearray(
[]byte{(byte)(SymbolQueue)},
key,
)
if err := txn.Del(queueKey); err != nil {
return err
}
}
return nil
})
if err != nil {
conn.WriteError(err.Error())
return
}
conn.WriteString("OK")
}
func delprefix(db stor.DB, prefix []byte) error {
return db.Update(func(txn stor.Transaction) error {
ops := stor.DefaultIteratorOptions
ops.PrefetchValues = false
it := txn.NewIterator(ops)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
key := it.GetItem().Key()
err := txn.Del(key)
if err != nil {
return err
}
}
return nil
})
}
func ping(db stor.DB, conn aryConnection, cmd aryCommand) {
conn.WriteString("PONG")
}