forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_test.go
134 lines (115 loc) · 3.41 KB
/
doc_test.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
/*
* Copyright 2017 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package badger_test
import (
"fmt"
"io/ioutil"
"sync"
"github.com/dgraph-io/badger"
)
var d string = "doc"
func Example() {
opt := badger.DefaultOptions
dir, _ := ioutil.TempDir("", "badger")
opt.Dir = dir
opt.ValueDir = dir
kv, _ := badger.NewKV(&opt)
key := []byte("hello")
kv.Set(key, []byte("world"), 0x00)
fmt.Printf("SET %s world\n", key)
var item badger.KVItem
if err := kv.Get(key, &item); err != nil {
fmt.Printf("Error while getting key: %q", key)
}
fmt.Printf("GET %s %s\n", key, item.Value())
if err := kv.CompareAndSet(key, []byte("venus"), 100); err != nil {
fmt.Println("CAS counter mismatch")
} else {
if err = kv.Get(key, &item); err != nil {
fmt.Printf("Error while getting key: %q", key)
}
fmt.Printf("Set to %s\n", item.Value())
}
if err := kv.CompareAndSet(key, []byte("mars"), item.Counter()); err == nil {
fmt.Println("Set to mars")
} else {
fmt.Printf("Unsuccessful write. Got error: %v\n", err)
}
// Output:
// SET hello world
// GET hello world
// CAS counter mismatch
// Set to mars
}
// func ExampleNewIterator() {
// opt := DefaultOptions
// opt.Dir = "/tmp/badger"
// kv := NewKV(&opt)
// itrOpt := IteratorOptions{
// PrefetchSize: 1000,
// FetchValues: true,
// Reverse: false,
// }
// itr := kv.NewIterator(itrOpt)
// for itr.Rewind(); itr.Valid(); itr.Next() {
// item := itr.Item()
// item.Key()
// item.Value()
// }
// }
func ExampleKV_BatchSetAsync() {
opt := badger.DefaultOptions
dir, _ := ioutil.TempDir("", "badger")
opt.Dir = dir
opt.SyncWrites = true
opt.ValueDir = dir
kv, _ := badger.NewKV(&opt)
wg := new(sync.WaitGroup)
wb := make([]*badger.Entry, 0, 100)
wg.Add(1)
// Async writes would be useful if you want to write some key-value pairs without waiting
// for them to be complete and perform some cleanup when they are written.
// In Dgraph we keep on flushing posting lists periodically to badger. We do it an async
// manner and provide a callback to it which can do the cleanup when the writes are done.
f := func(err error) {
defer wg.Done()
if err != nil {
// At this point you can retry writing keys or send error over a channel to handle
// in some other goroutine.
fmt.Printf("Got error: %+v\n", err)
}
// Check for error in entries which could be non-nil if the user supplies a CasCounter.
for _, e := range wb {
if e.Error != nil {
fmt.Printf("Got error: %+v\n", e.Error)
}
}
// You can do cleanup now. Like deleting keys from cache.
fmt.Println("All async sets complete.")
}
for i := 0; i < 100; i++ {
k := []byte(fmt.Sprintf("%09d", i))
wb = append(wb, &badger.Entry{
Key: k,
Value: k,
})
}
kv.BatchSetAsync(wb, f)
fmt.Println("Finished writing keys to badger.")
wg.Wait()
// Output: Finished writing keys to badger.
// All async sets complete.
}