1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
15 "github.com/syndtr/goleveldb/leveldb/util"
18 type KeyValueEntry struct {
22 type KeyValue struct {
23 entries []KeyValueEntry
27 func (kv *KeyValue) Put(key, value []byte) {
28 if n := len(kv.entries); n > 0 && cmp.Compare(kv.entries[n-1].key, key) >= 0 {
29 panic(fmt.Sprintf("Put: keys are not in increasing order: %q, %q", kv.entries[n-1].key, key))
31 kv.entries = append(kv.entries, KeyValueEntry{key, value})
32 kv.nbytes += len(key) + len(value)
35 func (kv *KeyValue) PutString(key, value string) {
36 kv.Put([]byte(key), []byte(value))
39 func (kv *KeyValue) PutU(key, value []byte) bool {
40 if i, exist := kv.Get(key); !exist {
42 kv.entries = append(kv.entries[:i+1], kv.entries[i:]...)
43 kv.entries[i] = KeyValueEntry{key, value}
45 kv.entries = append(kv.entries, KeyValueEntry{key, value})
47 kv.nbytes += len(key) + len(value)
50 kv.nbytes += len(value) - len(kv.ValueAt(i))
51 kv.entries[i].value = value
56 func (kv *KeyValue) PutUString(key, value string) bool {
57 return kv.PutU([]byte(key), []byte(value))
60 func (kv *KeyValue) Delete(key []byte) (exist bool, value []byte) {
61 i, exist := kv.Get(key)
63 value = kv.entries[i].value
69 func (kv *KeyValue) DeleteIndex(i int) bool {
71 kv.nbytes -= len(kv.KeyAt(i)) + len(kv.ValueAt(i))
72 kv.entries = append(kv.entries[:i], kv.entries[i+1:]...)
78 func (kv KeyValue) Len() int {
79 return len(kv.entries)
82 func (kv *KeyValue) Size() int {
86 func (kv KeyValue) KeyAt(i int) []byte {
87 return kv.entries[i].key
90 func (kv KeyValue) ValueAt(i int) []byte {
91 return kv.entries[i].value
94 func (kv KeyValue) Index(i int) (key, value []byte) {
95 if i < 0 || i >= len(kv.entries) {
96 panic(fmt.Sprintf("Index #%d: out of range", i))
98 return kv.entries[i].key, kv.entries[i].value
101 func (kv KeyValue) IndexInexact(i int) (key_, key, value []byte) {
102 key, value = kv.Index(i)
104 var key1 = kv.KeyAt(i)
106 key0 = kv.KeyAt(i - 1)
108 key_ = BytesSeparator(key0, key1)
112 func (kv KeyValue) IndexOrNil(i int) (key, value []byte) {
113 if i >= 0 && i < len(kv.entries) {
114 return kv.entries[i].key, kv.entries[i].value
119 func (kv KeyValue) IndexString(i int) (key, value string) {
120 key_, _value := kv.Index(i)
121 return string(key_), string(_value)
124 func (kv KeyValue) Search(key []byte) int {
125 return sort.Search(kv.Len(), func(i int) bool {
126 return cmp.Compare(kv.KeyAt(i), key) >= 0
130 func (kv KeyValue) SearchString(key string) int {
131 return kv.Search([]byte(key))
134 func (kv KeyValue) Get(key []byte) (i int, exist bool) {
136 if i < kv.Len() && cmp.Compare(kv.KeyAt(i), key) == 0 {
142 func (kv KeyValue) GetString(key string) (i int, exist bool) {
143 return kv.Get([]byte(key))
146 func (kv KeyValue) Iterate(fn func(i int, key, value []byte)) {
147 for i, x := range kv.entries {
148 fn(i, x.key, x.value)
152 func (kv KeyValue) IterateString(fn func(i int, key, value string)) {
153 kv.Iterate(func(i int, key, value []byte) {
154 fn(i, string(key), string(value))
158 func (kv KeyValue) IterateShuffled(rnd *rand.Rand, fn func(i int, key, value []byte)) {
159 ShuffledIndex(rnd, kv.Len(), 1, func(i int) {
160 fn(i, kv.entries[i].key, kv.entries[i].value)
164 func (kv KeyValue) IterateShuffledString(rnd *rand.Rand, fn func(i int, key, value string)) {
165 kv.IterateShuffled(rnd, func(i int, key, value []byte) {
166 fn(i, string(key), string(value))
170 func (kv KeyValue) IterateInexact(fn func(i int, key_, key, value []byte)) {
171 for i := range kv.entries {
172 key_, key, value := kv.IndexInexact(i)
173 fn(i, key_, key, value)
177 func (kv KeyValue) IterateInexactString(fn func(i int, key_, key, value string)) {
178 kv.IterateInexact(func(i int, key_, key, value []byte) {
179 fn(i, string(key_), string(key), string(value))
183 func (kv KeyValue) Clone() KeyValue {
184 return KeyValue{append([]KeyValueEntry{}, kv.entries...), kv.nbytes}
187 func (kv KeyValue) Slice(start, limit int) KeyValue {
188 if start < 0 || limit > kv.Len() {
189 panic(fmt.Sprintf("Slice %d .. %d: out of range", start, limit))
190 } else if limit < start {
191 panic(fmt.Sprintf("Slice %d .. %d: invalid range", start, limit))
193 return KeyValue{append([]KeyValueEntry{}, kv.entries[start:limit]...), kv.nbytes}
196 func (kv KeyValue) SliceKey(start, limit []byte) KeyValue {
200 start_ = kv.Search(start)
203 limit_ = kv.Search(limit)
205 return kv.Slice(start_, limit_)
208 func (kv KeyValue) SliceKeyString(start, limit string) KeyValue {
209 return kv.SliceKey([]byte(start), []byte(limit))
212 func (kv KeyValue) SliceRange(r *util.Range) KeyValue {
214 return kv.SliceKey(r.Start, r.Limit)
219 func (kv KeyValue) Range(start, limit int) (r util.Range) {
221 if start == kv.Len() {
222 r.Start = BytesAfter(kv.KeyAt(start - 1))
224 r.Start = kv.KeyAt(start)
227 if limit < kv.Len() {
228 r.Limit = kv.KeyAt(limit)
233 func KeyValue_EmptyKey() *KeyValue {
235 kv.PutString("", "v")
239 func KeyValue_EmptyValue() *KeyValue {
241 kv.PutString("abc", "")
242 kv.PutString("abcd", "")
246 func KeyValue_OneKeyValue() *KeyValue {
248 kv.PutString("abc", "v")
252 func KeyValue_BigValue() *KeyValue {
254 kv.PutString("big1", strings.Repeat("1", 200000))
258 func KeyValue_SpecialKey() *KeyValue {
260 kv.PutString("\xff\xff", "v3")
264 func KeyValue_MultipleKeyValue() *KeyValue {
266 kv.PutString("a", "v")
267 kv.PutString("aa", "v1")
268 kv.PutString("aaa", "v2")
269 kv.PutString("aaacccccccccc", "v2")
270 kv.PutString("aaaccccccccccd", "v3")
271 kv.PutString("aaaccccccccccf", "v4")
272 kv.PutString("aaaccccccccccfg", "v5")
273 kv.PutString("ab", "v6")
274 kv.PutString("abc", "v7")
275 kv.PutString("abcd", "v8")
276 kv.PutString("accccccccccccccc", "v9")
277 kv.PutString("b", "v10")
278 kv.PutString("bb", "v11")
279 kv.PutString("bc", "v12")
280 kv.PutString("c", "v13")
281 kv.PutString("c1", "v13")
282 kv.PutString("czzzzzzzzzzzzzz", "v14")
283 kv.PutString("fffffffffffffff", "v15")
284 kv.PutString("g11", "v15")
285 kv.PutString("g111", "v15")
286 kv.PutString("g111\xff", "v15")
287 kv.PutString("zz", "v16")
288 kv.PutString("zzzzzzz", "v16")
289 kv.PutString("zzzzzzzzzzzzzzzz", "v16")
293 var keymap = []byte("012345678ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy")
295 func KeyValue_Generate(rnd *rand.Rand, n, incr, minlen, maxlen, vminlen, vmaxlen int) *KeyValue {
300 panic("max len should >= min len")
303 rrand := func(min, max int) int {
307 return rnd.Intn(max-min) + min
311 endC := byte(len(keymap) - incr)
312 gen := make([]byte, 0, maxlen)
313 for i := 0; i < n; i++ {
314 m := rrand(minlen, maxlen)
318 if k := len(last); m > k {
319 for j := k; j < m; j++ {
323 for j := m - 1; j >= 0; j-- {
328 gen[j] = c + byte(incr)
329 for j++; j < m; j++ {
338 panic(fmt.Sprintf("only able to generate %d keys out of %d keys, try increasing max len", kv.Len(), n))
341 key := make([]byte, m)
342 for j := 0; j < m; j++ {
343 key[j] = keymap[gen[j]]
345 value := make([]byte, rrand(vminlen, vmaxlen))
346 for n := copy(value, []byte(fmt.Sprintf("v%d", i))); n < len(value); n++ {