forked from HouzuoGuo/tiedot
/
example.go
125 lines (103 loc) · 2.75 KB
/
example.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
package main
import (
"encoding/json"
"fmt"
"loveoneanother.at/tiedot/db"
"os"
)
func embeddedExample() {
dir := "/tmp/MyDatabase"
os.RemoveAll(dir)
defer os.RemoveAll(dir)
// Open database
myDB, err := db.OpenDB(dir)
if err != nil {
panic(err)
}
// Create collection
if err := myDB.Create("A"); err != nil {
panic(err)
}
if err := myDB.Create("B"); err != nil {
panic(err)
}
// Rename collection
if err := myDB.Rename("B", "C"); err != nil {
panic(err)
}
// Which collections do I have?
for name := range myDB.StrCol {
fmt.Printf("I have a collection called %s\n", name)
}
// Drop collection
if err := myDB.Drop("C"); err != nil {
panic(err)
}
// Start using collection
A := myDB.Use("A")
/*
Insert document.
You may insert/update any interface{} to collection, for example:
var doc interface{}
json.Unmarshal([]byte(`{"a": 1, "b": 2}`), &doc)
A.Insert(doc)
And here is an example using struct:
*/
type Document struct {
Url, Owner string
}
docID, err := A.Insert(Document{"http://google.com", "Google Inc."})
if err != nil {
panic(err)
}
fmt.Printf("Inserted document at %d (document ID)\n", docID)
// Update document (you can still use struct, but this example uses generic interface{})
var doc interface{}
json.Unmarshal([]byte(`{"Url": "http://www.google.com.au", "Owner": "Google Inc."}`), &doc)
newID, err := A.Update(docID, doc) // newID may or may not be the same!
if err != nil {
panic(err)
}
fmt.Printf("Updated document %d to %v, new ID is %d\n", docID, doc, newID)
// Read document
var readback Document
if err := A.Read(newID, &readback); err != nil {
panic(err)
}
fmt.Printf("Read document ID %d: %v\n", newID, readback)
// Delete document
A.Delete(123) // passing invalid ID to it will not harm your data
/*
Collection insert/update/delete have their dedicated "durable" calls:
- durableInsert
- durableUpdate
- durableDelete
Those operations ensure a disk flush after each call to guarantee data durability on disk.
However - those operations are 10000x more expensive than ordinary insert/update/delete!
*/
// Create index
if err := A.Index([]string{"a", "b", "c"}); err != nil {
panic(err)
}
// Which indexes do I have on collection A?
for path := range A.StrHT {
fmt.Printf("I have an index on path %s\n", path)
}
// Remove index
if err := A.Unindex([]string{"a", "b", "c"}); err != nil {
panic(err)
}
// Execute query
result := make(map[uint64]struct{})
var query interface{}
json.Unmarshal([]byte(`["all"]`), &query)
if err := db.EvalQuery(query, A, &result); err != nil {
panic(err)
}
for id := range result {
// query results are in map keys
fmt.Printf("Query returned document ID %d\n", id)
}
// Gracefully close database
myDB.Close()
}