@@ -16,7 +16,7 @@ type Bolt struct {
16
16
ttl_map * ttlmap.Map
17
17
}
18
18
19
- const GLOBAL = "bolt "
19
+ const GLOBAL = "m41gA7omIWU4s "
20
20
21
21
func Open (path string ) * Bolt {
22
22
b := & Bolt {}
@@ -35,11 +35,10 @@ func Open(path string) *Bolt {
35
35
OnWillEvict : func (key string , item ttlmap.Item ) {
36
36
fmt .Printf ("evicted: [%s=%v]\n " , key , item .Value ())
37
37
b .engine .Update (func (tx * bolt.Tx ) error {
38
- buk := tx .Bucket ([]byte (GLOBAL ))
39
- if buk == nil {
40
- return nil
38
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
39
+ return buk .Delete ([]byte (key ))
41
40
}
42
- return buk . Delete ([] byte ( key ))
41
+ return nil
43
42
})
44
43
},
45
44
}
@@ -48,14 +47,12 @@ func Open(path string) *Bolt {
48
47
}
49
48
50
49
func (b * Bolt ) Get (k []byte ) (v []byte ) {
51
- item , err := b .ttl_map .Get (string (k ))
52
- if err == nil {
50
+ if item , err := b .ttl_map .Get (string (k )); err == nil {
53
51
return []byte (item .Value ().(string ))
54
52
}
55
53
56
54
b .engine .View (func (tx * bolt.Tx ) error {
57
- buk := tx .Bucket ([]byte (GLOBAL ))
58
- if buk != nil {
55
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
59
56
v = buk .Get (k )
60
57
}
61
58
return nil
@@ -80,25 +77,22 @@ func (b *Bolt) Del(k []byte) (err error) {
80
77
go b .ttl_map .Delete (string (k ))
81
78
82
79
err = b .engine .Update (func (tx * bolt.Tx ) error {
83
- buk := tx .Bucket ([]byte (GLOBAL ))
84
- if buk == nil {
85
- return nil
80
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
81
+ return buk .Delete (k )
86
82
}
87
- return buk . Delete ( k )
83
+ return nil
88
84
})
89
85
90
86
return
91
87
}
92
88
93
89
func (b * Bolt ) Prefix (prefix []byte ) (res [][]byte ) {
94
90
b .engine .View (func (tx * bolt.Tx ) error {
95
- buk := tx .Bucket ([]byte (GLOBAL ))
96
- if buk == nil {
97
- return nil
98
- }
99
- c := buk .Cursor ()
100
- for k , v := c .Seek (prefix ); k != nil && bytes .HasPrefix (k , prefix ); k , v = c .Next () {
101
- res = append (res , common .SafeCopy (nil , v ))
91
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
92
+ c := buk .Cursor ()
93
+ for k , v := c .Seek (prefix ); k != nil && bytes .HasPrefix (k , prefix ); k , v = c .Next () {
94
+ res = append (res , common .SafeCopy (nil , v ))
95
+ }
102
96
}
103
97
104
98
return nil
@@ -109,15 +103,12 @@ func (b *Bolt) Prefix(prefix []byte) (res [][]byte) {
109
103
110
104
func (b * Bolt ) Suffix (suffix []byte ) (res [][]byte ) {
111
105
b .engine .View (func (tx * bolt.Tx ) error {
112
- buk := tx .Bucket ([]byte (GLOBAL ))
113
- if buk == nil {
114
- return nil
115
- }
116
-
117
- c := buk .Cursor ()
118
- for k , v := c .First (); k != nil ; k , v = c .Next () {
119
- if bytes .HasSuffix (k , suffix ) {
120
- res = append (res , common .SafeCopy (nil , v ))
106
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
107
+ c := buk .Cursor ()
108
+ for k , v := c .First (); k != nil ; k , v = c .Next () {
109
+ if bytes .HasSuffix (k , suffix ) {
110
+ res = append (res , common .SafeCopy (nil , v ))
111
+ }
121
112
}
122
113
}
123
114
@@ -129,14 +120,12 @@ func (b *Bolt) Suffix(suffix []byte) (res [][]byte) {
129
120
130
121
func (b * Bolt ) Scan () (res [][]byte ) {
131
122
b .engine .View (func (tx * bolt.Tx ) error {
132
- buk := tx .Bucket ([]byte (GLOBAL ))
133
- if buk == nil {
134
- return nil
123
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
124
+ buk .ForEach (func (k , v []byte ) error {
125
+ res = append (res , common .SafeCopy (nil , v ))
126
+ return nil
127
+ })
135
128
}
136
- buk .ForEach (func (k , v []byte ) error {
137
- res = append (res , common .SafeCopy (nil , v ))
138
- return nil
139
- })
140
129
return nil
141
130
})
142
131
@@ -161,16 +150,14 @@ func (b *Bolt) SetTTL(k, v []byte, expire time.Duration) (err error) {
161
150
162
151
func (b * Bolt ) Range (start , limit []byte ) (res [][]byte ) {
163
152
b .engine .View (func (tx * bolt.Tx ) error {
164
- buk := tx .Bucket ([]byte (GLOBAL ))
165
- if buk == nil {
166
- return nil
167
- }
168
- c := buk .Cursor ()
169
- for k , v := c .Seek (start ); k != nil && bytes .Compare (start , k ) <= 0 ; k , v = c .Next () {
170
- if bytes .Compare (limit , k ) > 0 {
171
- res = append (res , common .SafeCopy (nil , v ))
172
- } else {
173
- break
153
+ if buk := tx .Bucket ([]byte (GLOBAL )); buk != nil {
154
+ c := buk .Cursor ()
155
+ for k , v := c .Seek (start ); k != nil && bytes .Compare (start , k ) <= 0 ; k , v = c .Next () {
156
+ if bytes .Compare (limit , k ) > 0 {
157
+ res = append (res , common .SafeCopy (nil , v ))
158
+ } else {
159
+ break
160
+ }
174
161
}
175
162
}
176
163
0 commit comments