/
hash.go
170 lines (151 loc) · 3.75 KB
/
hash.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
package nodis
import (
"time"
"github.com/diiyw/nodis/pb"
"github.com/diiyw/nodis/ds"
"github.com/diiyw/nodis/ds/hash"
)
func (n *Nodis) newHash() ds.DataStruct {
return hash.NewHashMap()
}
func (n *Nodis) HSet(key string, field string, value []byte) {
meta := n.store.writeKey(key, n.newHash)
meta.ds.(*hash.HashMap).HSet(field, value)
meta.commit()
n.notify(pb.NewOp(pb.OpType_HSet, key).Fields(field).Value(value))
}
func (n *Nodis) HGet(key string, field string) []byte {
meta := n.store.readKey(key)
defer meta.commit()
if !meta.isOk() {
return nil
}
return meta.ds.(*hash.HashMap).HGet(field)
}
func (n *Nodis) HDel(key string, fields ...string) int64 {
meta := n.store.writeKey(key, nil)
if !meta.isOk() {
meta.commit()
return 0
}
meta.ds.(*hash.HashMap).HDel(fields...)
if meta.ds.(*hash.HashMap).HLen() == 0 {
n.store.delKey(key)
}
meta.commit()
n.notify(pb.NewOp(pb.OpType_HDel, key).Fields(fields...))
return int64(len(fields))
}
func (n *Nodis) HLen(key string) int64 {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return 0
}
v := meta.ds.(*hash.HashMap).HLen()
meta.commit()
return v
}
func (n *Nodis) HKeys(key string) []string {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return nil
}
v := meta.ds.(*hash.HashMap).HKeys()
meta.commit()
return v
}
func (n *Nodis) HExists(key string, field string) bool {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return false
}
v := meta.ds.(*hash.HashMap).HExists(field)
meta.commit()
return v
}
func (n *Nodis) HGetAll(key string) map[string][]byte {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return nil
}
v := meta.ds.(*hash.HashMap).HGetAll()
meta.commit()
return v
}
func (n *Nodis) HIncrBy(key string, field string, value int64) int64 {
meta := n.store.writeKey(key, n.newHash)
v := meta.ds.(*hash.HashMap).HIncrBy(field, value)
meta.commit()
n.notify(pb.NewOp(pb.OpType_HIncrBy, key).Fields(field).IncrInt(value))
return v
}
func (n *Nodis) HIncrByFloat(key string, field string, value float64) float64 {
meta := n.store.writeKey(key, n.newHash)
v := meta.ds.(*hash.HashMap).HIncrByFloat(field, value)
meta.commit()
n.notify(pb.NewOp(pb.OpType_HIncrByFloat, key).Fields(field).IncrFloat(value))
return v
}
func (n *Nodis) HSetNX(key string, field string, value []byte) bool {
meta := n.store.writeKey(key, nil)
if meta.isOk() && meta.ds.(*hash.HashMap).HExists(field) {
meta.commit()
return false
}
h := n.newHash()
n.store.values.Set(key, h)
k := newKey()
k.lastUse = time.Now().Unix()
n.store.keys.Set(key, k)
h.(*hash.HashMap).HSet(field, value)
meta.commit()
return true
}
func (n *Nodis) HMSet(key string, fields map[string][]byte) {
meta := n.store.writeKey(key, n.newHash)
var ops = make([]*pb.Op, 0, len(fields))
for field, value := range fields {
meta.ds.(*hash.HashMap).HSet(field, value)
ops = append(ops, pb.NewOp(pb.OpType_HSet, key).Fields(field).Value(value))
}
n.notify(ops...)
meta.ds.(*hash.HashMap).HMSet(fields)
meta.commit()
}
func (n *Nodis) HMGet(key string, fields ...string) [][]byte {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return nil
}
v := meta.ds.(*hash.HashMap).HMGet(fields...)
meta.commit()
return v
}
func (n *Nodis) HClear(key string) {
n.Del(key)
}
func (n *Nodis) HScan(key string, cursor int64, match string, count int64) (int64, map[string][]byte) {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return 0, nil
}
c, v := meta.ds.(*hash.HashMap).HScan(cursor, match, count)
meta.commit()
return c, v
}
func (n *Nodis) HVals(key string) [][]byte {
meta := n.store.readKey(key)
if !meta.isOk() {
meta.commit()
return nil
}
v := meta.ds.(*hash.HashMap).HVals()
meta.commit()
return v
}