forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_hash.go
269 lines (215 loc) · 6.19 KB
/
db_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
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
package rosedb
import (
"bytes"
"github.com/roseduan/rosedb/ds/hash"
"github.com/roseduan/rosedb/storage"
"sync"
"time"
)
// HashIdx hash index.
type HashIdx struct {
mu sync.RWMutex
indexes *hash.Hash
}
// create a new hash index.
func newHashIdx() *HashIdx {
return &HashIdx{indexes: hash.New()}
}
// HSet sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created.
// If field already exists in the hash, it is overwritten.
// Return num of elements in hash of the specified key.
func (db *RoseDB) HSet(key []byte, field []byte, value []byte) (res int, err error) {
if err = db.checkKeyValue(key, value); err != nil {
return
}
// If the existed value is the same as the set value, nothing will be done.
oldVal := db.HGet(key, field)
if bytes.Compare(oldVal, value) == 0 {
return
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
e := storage.NewEntry(key, value, field, Hash, HashHSet)
if err = db.store(e); err != nil {
return
}
res = db.hashIndex.indexes.HSet(string(key), string(field), value)
return
}
// HSetNx Sets field in the hash stored at key to value, only if field does not yet exist.
// If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
// Return if the operation is successful.
func (db *RoseDB) HSetNx(key, field, value []byte) (res int, err error) {
if err = db.checkKeyValue(key, value); err != nil {
return
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
if res = db.hashIndex.indexes.HSetNx(string(key), string(field), value); res == 1 {
e := storage.NewEntry(key, value, field, Hash, HashHSet)
if err = db.store(e); err != nil {
return
}
}
return
}
// HGet returns the value associated with field in the hash stored at key.
func (db *RoseDB) HGet(key, field []byte) []byte {
if err := db.checkKeyValue(key, nil); err != nil {
return nil
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return nil
}
return db.hashIndex.indexes.HGet(string(key), string(field))
}
// HGetAll returns all fields and values of the hash stored at key.
// In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
func (db *RoseDB) HGetAll(key []byte) [][]byte {
if err := db.checkKeyValue(key, nil); err != nil {
return nil
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return nil
}
return db.hashIndex.indexes.HGetAll(string(key))
}
// HDel removes the specified fields from the hash stored at key.
// Specified fields that do not exist within this hash are ignored.
// If key does not exist, it is treated as an empty hash and this command returns false.
func (db *RoseDB) HDel(key []byte, field ...[]byte) (res int, err error) {
if err = db.checkKeyValue(key, nil); err != nil {
return
}
if field == nil || len(field) == 0 {
return
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
for _, f := range field {
if ok := db.hashIndex.indexes.HDel(string(key), string(f)); ok == 1 {
e := storage.NewEntry(key, nil, f, Hash, HashHDel)
if err = db.store(e); err != nil {
return
}
res++
}
}
return
}
// HKeyExists returns if the key is existed in hash.
func (db *RoseDB) HKeyExists(key []byte) (ok bool) {
if err := db.checkKeyValue(key, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return
}
return db.hashIndex.indexes.HKeyExists(string(key))
}
// HExists returns if field is an existing field in the hash stored at key.
func (db *RoseDB) HExists(key, field []byte) int {
if err := db.checkKeyValue(key, nil); err != nil {
return 0
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return 0
}
return db.hashIndex.indexes.HExists(string(key), string(field))
}
// HLen returns the number of fields contained in the hash stored at key.
func (db *RoseDB) HLen(key []byte) int {
if err := db.checkKeyValue(key, nil); err != nil {
return 0
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return 0
}
return db.hashIndex.indexes.HLen(string(key))
}
// HKeys returns all field names in the hash stored at key.
func (db *RoseDB) HKeys(key []byte) (val []string) {
if err := db.checkKeyValue(key, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return nil
}
return db.hashIndex.indexes.HKeys(string(key))
}
// HVals returns all values in the hash stored at key.
func (db *RoseDB) HVals(key []byte) (val [][]byte) {
if err := db.checkKeyValue(key, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return nil
}
return db.hashIndex.indexes.HVals(string(key))
}
// HClear clear the key in hash.
func (db *RoseDB) HClear(key []byte) (err error) {
if err = db.checkKeyValue(key, nil); err != nil {
return
}
if !db.HKeyExists(key) {
return ErrKeyNotExist
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
e := storage.NewEntryNoExtra(key, nil, Hash, HashHClear)
if err := db.store(e); err != nil {
return err
}
db.hashIndex.indexes.HClear(string(key))
delete(db.expires[Hash], string(key))
return
}
// HExpire set expired time for a hash key.
func (db *RoseDB) HExpire(key []byte, duration int64) (err error) {
if duration <= 0 {
return ErrInvalidTTL
}
if err = db.checkKeyValue(key, nil); err != nil {
return
}
if !db.HKeyExists(key) {
return ErrKeyNotExist
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
deadline := time.Now().Unix() + duration
e := storage.NewEntryWithExpire(key, nil, deadline, Hash, HashHExpire)
if err := db.store(e); err != nil {
return err
}
db.expires[Hash][string(key)] = deadline
return
}
// HTTL return time to live for the key.
func (db *RoseDB) HTTL(key []byte) (ttl int64) {
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(key, Hash) {
return
}
deadline, exist := db.expires[Hash][string(key)]
if !exist {
return
}
return deadline - time.Now().Unix()
}