-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
100 lines (88 loc) · 1.97 KB
/
examples_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
package flockd_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/iovation/flockd"
)
func tmpdir(name string) string {
dir, err := ioutil.TempDir("", name)
if err != nil {
log.Fatal("TempDir", err)
}
return dir
}
func ExampleNew() {
// Open the database with an access timeout of 10 ms.
db, err := flockd.New("my.db", 10*time.Millisecond)
if err != nil {
log.Fatal("flockd.New", err)
}
// Optionally remove the database.
defer os.RemoveAll(db.Path())
}
func ExampleDB_ForEach() {
// Open the database
path := tmpdir("foreach.db")
db, err := flockd.New(path, time.Millisecond)
if err != nil {
log.Fatal("flockd.New", err)
}
defer os.RemoveAll(path)
// Load some data into the root table.
for k, v := range map[string]string{
"thing 1": "thing 2",
"wilma": "fred",
"michelle": "barrack",
} {
if err := db.Set(k, []byte(v)); err != nil {
log.Fatal("flockd.Set", err)
}
}
// Iterate over the records in the root table.
db.ForEach(func(key string, val []byte) error {
fmt.Printf("%v: %s\n", key, val)
return nil
})
// Unordered output:
// thing 1: thing 2
// wilma: fred
// michelle: barrack
}
func ExampleTable_ForEach() {
// Open the database
path := tmpdir("foreach.db")
db, err := flockd.New(path, time.Millisecond)
if err != nil {
log.Fatal("flockd.New", err)
}
defer os.RemoveAll(path)
// Create a table.
tbl, err := db.Table("simpsons.db")
if err != nil {
log.Fatal("flockd.DB.Table", err)
}
// Add some data.
for k, v := range map[string]string{
"Marge": "Homer",
"Selma": "Sideshow Bob",
"Manjula": "Apu",
"Maude": "Ned",
} {
if err := tbl.Set(k, []byte(v)); err != nil {
log.Fatal("flockd.Table.Set", err)
}
}
// Iterate over the records in the root table.
tbl.ForEach(func(key string, val []byte) error {
fmt.Printf("%v ❤️ %s\n", key, val)
return nil
})
// Unordered output:
// Marge ❤️ Homer
// Maude ❤️ Ned
// Manjula ❤️ Apu
// Selma ❤️ Sideshow Bob
}