aboutsummaryrefslogtreecommitdiff
path: root/example_test.go
blob: 542109c0847c85f52cccec544ea9956e9e0260c5 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package bolt

import (
	"fmt"
	"os"
)

func init() {
	os.RemoveAll("/tmp/bolt")
	os.MkdirAll("/tmp/bolt", 0777)
}

func ExampleDB_Put() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/db_put.db", 0666)
	defer db.Close()

	// Create a bucket.
	db.CreateBucket("widgets")

	// Set the value "bar" for the key "foo".
	db.Put("widgets", []byte("foo"), []byte("bar"))

	// Retrieve the key back from the database and verify it.
	value, _ := db.Get("widgets", []byte("foo"))
	fmt.Printf("The value of 'foo' is: %s\n", string(value))

	// Output:
	// The value of 'foo' is: bar
}

func ExampleDB_Delete() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/db_delete.db", 0666)
	defer db.Close()

	// Create a bucket.
	db.CreateBucket("widgets")

	// Set the value "bar" for the key "foo".
	db.Put("widgets", []byte("foo"), []byte("bar"))

	// Retrieve the key back from the database and verify it.
	value, _ := db.Get("widgets", []byte("foo"))
	fmt.Printf("The value of 'foo' was: %s\n", string(value))

	// Delete the "foo" key.
	db.Delete("widgets", []byte("foo"))

	// Retrieve the key again.
	value, _ = db.Get("widgets", []byte("foo"))
	if value == nil {
		fmt.Printf("The value of 'foo' is now: nil\n")
	}

	// Output:
	// The value of 'foo' was: bar
	// The value of 'foo' is now: nil
}

func ExampleDB_Do() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/db_do.db", 0666)
	defer db.Close()

	// Execute several commands within a write transaction.
	err := db.Do(func(t *RWTransaction) error {
		if err := t.CreateBucket("widgets"); err != nil {
			return err
		}
		if err := t.Put("widgets", []byte("foo"), []byte("bar")); err != nil {
			return err
		}
		return nil
	})

	// If our transactional block didn't return an error then our data is saved.
	if err == nil {
		value, _ := db.Get("widgets", []byte("foo"))
		fmt.Printf("The value of 'foo' is: %s\n", string(value))
	}

	// Output:
	// The value of 'foo' is: bar
}

func ExampleDB_ForEach() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/db_foreach.db", 0666)
	defer db.Close()

	// Insert data into a bucket.
	db.CreateBucket("animals")
	db.Put("animals", []byte("dog"), []byte("fun"))
	db.Put("animals", []byte("cat"), []byte("lame"))
	db.Put("animals", []byte("liger"), []byte("awesome"))

	// Iterate over items in sorted key order.
	db.ForEach("animals", func(k, v []byte) error {
		fmt.Printf("A %s is %s.\n", string(k), string(v))
		return nil
	})

	// Output:
	// A cat is lame.
	// A dog is fun.
	// A liger is awesome.
}

func ExampleRWTransaction() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/rwtransaction.db", 0666)
	defer db.Close()

	// Create a bucket.
	db.CreateBucket("widgets")

	// Create several keys in a transaction.
	rwtxn, _ := db.RWTransaction()
	rwtxn.Put("widgets", []byte("john"), []byte("blue"))
	rwtxn.Put("widgets", []byte("abby"), []byte("red"))
	rwtxn.Put("widgets", []byte("zephyr"), []byte("purple"))
	rwtxn.Commit()

	// Iterate over the values in sorted key order.
	txn, _ := db.Transaction()
	c, _ := txn.Cursor("widgets")
	for k, v := c.First(); k != nil; k, v = c.Next() {
		fmt.Printf("%s likes %s\n", string(k), string(v))
	}
	txn.Close()

	// Output:
	// abby likes red
	// john likes blue
	// zephyr likes purple
}

func ExampleRWTransaction_rollback() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/rwtransaction_rollback.db", 0666)
	defer db.Close()

	// Create a bucket.
	db.CreateBucket("widgets")

	// Set a value for a key.
	db.Put("widgets", []byte("foo"), []byte("bar"))

	// Update the key but rollback the transaction so it never saves.
	rwtxn, _ := db.RWTransaction()
	rwtxn.Put("widgets", []byte("foo"), []byte("baz"))
	rwtxn.Rollback()

	// Ensure that our original value is still set.
	value, _ := db.Get("widgets", []byte("foo"))
	fmt.Printf("The value for 'foo' is still: %s\n", string(value))

	// Output:
	// The value for 'foo' is still: bar
}

func ExampleDB_CopyFile() {
	// Open the database.
	var db DB
	db.Open("/tmp/bolt/db_copy.db", 0666)
	defer db.Close()

	// Create a bucket and a key.
	db.CreateBucket("widgets")
	db.Put("widgets", []byte("foo"), []byte("bar"))

	// Copy the database to another file.
	db.CopyFile("/tmp/bolt/db_copy_2.db", 0666)

	// Open the cloned database.
	var db2 DB
	db2.Open("/tmp/bolt/db_copy_2.db", 0666)
	defer db2.Close()

	// Ensure that the key exists in the copy.
	value, _ := db2.Get("widgets", []byte("foo"))
	fmt.Printf("The value for 'foo' in the clone is: %s\n", string(value))

	// Output:
	// The value for 'foo' in the clone is: bar
}