/
example.go
105 lines (85 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"time"
_ "gopkg.in/manishrjain/gocrud.v1/drivers/leveldb"
_ "gopkg.in/manishrjain/gocrud.v1/drivers/memsearch"
"gopkg.in/manishrjain/gocrud.v1/indexer"
"gopkg.in/manishrjain/gocrud.v1/req"
"gopkg.in/manishrjain/gocrud.v1/search"
"gopkg.in/manishrjain/gocrud.v1/store"
"gopkg.in/manishrjain/gocrud.v1/x"
)
// Create a new Social Post.
func storeUpdate(ctx *req.Context) {
// Get User entity, set author for update.
u := store.NewUpdate("User", "userid").SetSource("userid")
// New Post child entity for User entity.
p := u.AddChild("Post").
Set("url", "https://xkcd.com/936/").
Set("body", "Password Strength") // Method chaining.
p.AddChild("Like").Set("upvote", true)
// Add attendees to Event entity.
c := p.AddChild("Comment").Set("text", "Hard for humans, easy for computers.")
c.AddChild("Like").Set("upvote", true)
_ = u.Execute(ctx) // Returns error
}
func storeQuery() {
// Collect all Post entities under User userid.
p := store.NewQuery("userid").Collect("Post")
p.Collect("Like") // Collect all Like entities for Post entities.
// Collect all Comment entities, and Likes on those comments.
p.Collect("Comment").Collect("Like")
// Alternatively, collect everything via breadth first iteration.
p = store.NewQuery("userid").UptoDepth(10)
result, _ := p.Run()
js, _ := result.ToJson() // Convert to JSON.
fmt.Println(string(js))
// Optionally write to w http.ResponseWriter.
// result.WriteJsonResponse(w)
}
type SimpleIndexer struct {
}
// OnUpdate returns all entities to be re-indexed when entity x gets updated.
func (si SimpleIndexer) OnUpdate(e x.Entity) (result []x.Entity) {
result = append(result, e) // Add self.
return
}
func (si SimpleIndexer) Regenerate(e x.Entity) (rdoc x.Doc) {
rdoc.Id = e.Id
rdoc.Kind = e.Kind
rdoc.NanoTs = time.Now().UnixNano() // Used for versioning, to support concurrency.
// Query store to get the entity, and all it's properties.
result, _ := store.NewQuery(e.Id).Run()
data := result.ToMap()
rdoc.Data = data // Data field takes interface{}, so set to anything.
return
} // end_simple
func searchQuery() {
q := search.Get().NewQuery("Comment").Order("-ts_millis")
q.NewAndFilter().
AddRegex("text", "[hH]ard.*easy").
AddExact("source", "userid")
docs, _ := q.Run()
j, _ := json.Marshal(docs)
fmt.Println(string(j))
}
func main() {
path, err := ioutil.TempDir("", "gocrudldb_")
if err != nil {
log.Fatal("Opening file")
return
}
store.Get().Init(path) // leveldb
search.Get().Init() // memsearch
ctx := req.NewContextWithUpdates(3, 10) // Number of chars in unique ids.
indexer.Register("Comment", SimpleIndexer{})
indexer.Run(ctx, 2)
storeUpdate(ctx)
storeQuery()
indexer.WaitForDone(ctx)
searchQuery()
}