This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boltron_example_test.go
117 lines (104 loc) · 2.56 KB
/
boltron_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
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
package boltron_test
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"time"
bolt "github.com/dc0d/boltron"
"github.com/tidwall/gjson"
)
func ExampleDB_boltronIndexTimeJSON() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
defer os.Remove(db.Path())
defer db.Close()
// sample struct that holds the data
type data struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Score float64 `json:"score,omitempty"`
At time.Time `json:"at,omitempty"`
}
err = db.AddIndex(bolt.NewIndex("times", func(k, v []byte) [][]byte {
// this is a sample index that indexes the data if
// the key starts with "data:" and
// the value is a valid json and
// it has a field named "at" containing a valid time.Time representation.
// otherwise it will get ignored and not be indexed.
// ignoring any key that has not a 'data:' prefix
// by returning nil. this way, other kinds of data
// would not be indexed by this index.
if !bytes.HasPrefix(k, []byte("data:")) {
return nil
}
// check if v is valid json
js := string(v)
if !gjson.Valid(js) {
return nil
}
// getting the event time
res := gjson.Get(string(v), "at")
at := res.Time()
if at.IsZero() {
return nil
}
return [][]byte{[]byte(at.Format("20060102150405"))}
}))
if err != nil {
log.Fatal(err)
}
err = db.Update(func(tx *bolt.Tx) error {
// inserting a sample record
d := data{
ID: "data:000010",
Name: "Kaveh",
Score: 71,
At: time.Now(),
}
js, err := json.Marshal(&d)
if err != nil {
return err
}
bk, err := tx.CreateBucketIfNotExists([]byte("data"))
if err != nil {
return err
}
return bk.Put([]byte(d.ID), js)
})
if err != nil {
log.Fatal(err)
}
var found *data
err = db.View(func(tx *bolt.Tx) error {
// searching for first records between yesterday (in current hour) and tomorrow
indexBucket := tx.Bucket([]byte("times"))
now := time.Now()
yesterday := []byte(now.Add(time.Hour * 24 * -1).Format("20060102150405"))
tomorrow := []byte(now.Add(time.Hour * 24).Format("20060102150405"))
c := indexBucket.Cursor()
prefix := yesterday
var id []byte
for k, v := c.Seek(prefix); k != nil && bytes.Compare(k, tomorrow) <= 0; k, v = c.Next() {
id = v
break
}
dataBucket := tx.Bucket([]byte("data"))
v := dataBucket.Get(id)
if len(v) == 0 {
log.Fatal("not found")
}
found = new(data)
return json.Unmarshal(v, found)
})
if err != nil {
log.Fatal(err)
}
fmt.Println(found.ID)
// Output:
// data:000010
}