-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
188 lines (169 loc) · 3.62 KB
/
list.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
package bitcask
import (
"bytes"
"errors"
)
func (b *Bitcask) List(key []byte) *List {
return &List{db: b, key: key}
}
// List ...
// +key,l = ""
// l[key]0 = "a"
// l[key]1 = "b"
// l[key]2 = "c"
type List struct {
db *Bitcask
key []byte
}
func (l *List) Index(i int64) ([]byte, error) {
x, err := l.leftIndex()
if err != nil {
return nil, err
}
return l.db.Get(l.indexKey(x + i))
}
// Range enumerate value by index
// <start> must >= 0
// <stop> should equal to -1 or lager than <start>
func (l *List) Range(start, stop int64, fn func(i int64, value []byte, quit *bool)) error {
if start < 0 || (stop != -1 && start > stop) {
return errors.New("bad start/stop index")
}
x, y, err := l.rangeIndex()
if err != nil {
return err
}
if stop == -1 {
stop = (y - x + 1) - 1 // (size) - 1
}
min := l.indexKey(x + int64(start))
max := l.indexKey(x + int64(stop))
var i int64 // 0
ErrStopIteration := errors.New("err: stop iteration")
err = l.db.Scan(min, func(key []byte) error {
if key != nil && bytes.Compare(key, max) <= 0 {
val, err := l.db.Get(key)
if err != nil {
return err
}
quit := false
if fn(start+i, val, &quit); quit {
return ErrStopIteration
}
i++
return nil
}
return ErrStopIteration
})
if err == ErrStopIteration {
return nil
}
return err
}
// Append ...
func (l *List) Append(vals ...[]byte) error {
x, y, err := l.rangeIndex()
if err != nil {
return err
}
if x == 0 && y == -1 {
if err := l.db.Put(l.rawKey(), nil); err != nil {
return err
}
}
for i, val := range vals {
if err := l.db.Put(l.indexKey(y+int64(i)+1), val); err != nil {
return err
}
}
return nil
}
// Pop ...
func (l *List) Pop() ([]byte, error) {
x, y, err := l.rangeIndex()
if err != nil {
return nil, err
}
size := y - x + 1
if size == 0 {
return nil, nil
} else if size < 0 { // double check
return nil, errors.New("bad list struct")
}
idxkey := l.indexKey(y)
val, err := l.db.Get(idxkey)
if err != nil {
return nil, err
}
if err := l.db.Delete(idxkey); err != nil {
return nil, err
}
if size == 1 { // clean up
return nil, l.db.Delete(l.rawKey())
}
return val, nil
}
// Len ...
func (l *List) Len() (int64, error) {
x, y, err := l.rangeIndex()
return y - x + 1, err
}
func (l *List) rangeIndex() (int64, int64, error) {
left, err := l.leftIndex()
if err != nil {
return 0, -1, err
}
right, err := l.rightIndex()
if err != nil {
return 0, -1, err
}
return left, right, nil
}
func (l *List) leftIndex() (int64, error) {
idx := int64(0) // default 0
prefix := l.keyPrefix()
ErrStopIteration := errors.New("err: stop iteration")
err := l.db.Scan(prefix, func(key []byte) error {
if bytes.HasPrefix(key, prefix) {
idx = l.indexInKey(key)
}
return ErrStopIteration
})
if err == ErrStopIteration {
return idx, nil
}
return idx, err
}
func (l *List) rightIndex() (int64, error) {
idx := int64(-1) // default -1
prefix := l.keyPrefix()
err := l.db.Scan(prefix, func(key []byte) error {
if bytes.HasPrefix(key, prefix) {
idx = l.indexInKey(key)
}
return nil
})
return idx, err
}
// +key,l = ""
func (l *List) rawKey() []byte {
return rawKey(l.key, ElemType(LIST))
}
// l[key]
func (l *List) keyPrefix() []byte {
return bytes.Join([][]byte{[]byte{byte(LIST)}, SOK, l.key, EOK}, nil)
}
// l[key]0 = "a"
func (l *List) indexKey(i int64) []byte {
sign := []byte{0}
if i >= 0 {
sign = []byte{1}
}
b := bytes.Join([][]byte{l.keyPrefix(), sign, itob(i)}, nil)
return b
}
// split l[key]index into index
func (l *List) indexInKey(key []byte) int64 {
idxbuf := bytes.TrimPrefix(key, l.keyPrefix())
return btoi(idxbuf[1:]) // skip sign "0/1"
}