-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
110 lines (94 loc) · 2.62 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
_ "github.com/cayleygraph/cayley/graph/bolt"
"github.com/cayleygraph/cayley/quad"
"github.com/satori/go.uuid"
)
func main() {
dbFile := flag.String("db", "data/pokemon.boltdb", "BoltDB file")
csvFile := flag.String("csv", "data/pokemon.csv", "csv file with pokemon")
flag.Parse()
// Initialize the database
graph.InitQuadStore("bolt", *dbFile, nil)
// Open and use the database
store, err := cayley.NewGraph("bolt", *dbFile, nil)
if err != nil {
log.Fatalln(err)
}
file, err := os.Open(*csvFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
id, err := strconv.Atoi(s[0])
if err != nil {
log.Fatal(err)
}
speciesId, err := strconv.Atoi(s[2])
if err != nil {
log.Fatal(err)
}
height, err := strconv.Atoi(s[3])
if err != nil {
log.Fatal(err)
}
baseExperience, err := strconv.Atoi(s[4])
if err != nil {
log.Fatal(err)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
uuid := uuid.NewV1()
store.AddQuad(quad.Make(uuid, "id", id, nil))
store.AddQuad(quad.Make(uuid, "type", "pokemon", nil))
store.AddQuad(quad.Make(uuid, "name", s[1], nil))
store.AddQuad(quad.Make(uuid, "species_id", speciesId, nil))
store.AddQuad(quad.Make(uuid, "height", height, nil))
store.AddQuad(quad.Make(uuid, "base_experience", baseExperience, nil))
}
// find uuid of pikacho
p := cayley.StartPath(store).Has("name", quad.String("pikacho"))
vals, err := p.Iterate(nil).AllValues(nil)
if err != nil {
log.Fatalln(err)
} else if len(vals) == 0 {
log.Fatalln("pikacho not found")
}
uuid := vals[0].Native().(string)
// change pikacho to pikachu
t := cayley.NewTransaction()
t.RemoveQuad(quad.Make(uuid, "name", "pikacho", nil))
t.AddQuad(quad.Make(uuid, "name", "pikachu", nil))
err = store.ApplyTransaction(t)
if err != nil {
log.Fatalln(err)
}
// Now we create the path, to get to our data
p = cayley.StartPath(store).Has("type", quad.String("pokemon")).Out(quad.String("name"))
it, _ := p.BuildIterator().Optimize()
it, _ = store.OptimizeIterator(it)
defer it.Close()
// While we have items
for it.Next() {
token := it.Result() // get a ref to a node
value := store.NameOf(token) // get the value in the node
nativeValue := quad.NativeOf(value) // this converts nquad values to normal Go type
fmt.Println(nativeValue) // print it!
}
if err := it.Err(); err != nil {
log.Fatalln(err)
}
}