forked from szferi/gomdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
68 lines (60 loc) · 1.33 KB
/
example_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
package mdb
import (
"fmt"
"io/ioutil"
"os"
)
// Most mdb functions/methods can return errors. This example ignores errors
// for brevity. Real code should check all return values.
func Example() {
// create a directory to hold the database
path, _ := ioutil.TempDir("", "mdb_test")
defer os.RemoveAll(path)
// open the db
env, _ := NewEnv()
env.SetMapSize(1 << 20) // max file size
env.Open(path, 0, 0664)
defer env.Close()
txn, _ := env.BeginTxn(nil, 0)
dbi, _ := txn.DBIOpen(nil, 0)
defer env.DBIClose(dbi)
txn.Commit()
// write some data
txn, _ = env.BeginTxn(nil, 0)
num_entries := 5
for i := 0; i < num_entries; i++ {
key := fmt.Sprintf("Key-%d", i)
val := fmt.Sprintf("Val-%d", i)
txn.Put(dbi, []byte(key), []byte(val), 0)
}
txn.Commit()
// inspect the database
stat, _ := env.Stat()
fmt.Println(stat.Entries)
// scan the database
txn, _ = env.BeginTxn(nil, RDONLY)
defer txn.Abort()
cursor, _ := txn.CursorOpen(dbi)
defer cursor.Close()
for {
bkey, bval, err := cursor.Get(nil, nil, NEXT)
if err == NotFound {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%s: %s\n", bkey, bval)
}
// random access
bval, _ := txn.Get(dbi, []byte("Key-3"))
fmt.Println(string(bval))
// Output:
// 5
// Key-0: Val-0
// Key-1: Val-1
// Key-2: Val-2
// Key-3: Val-3
// Key-4: Val-4
// Val-3
}