-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
89 lines (76 loc) · 2.06 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
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"
)
func main() {
dbFile := flag.String("db", "db", "BoltDB file")
csvFile := flag.String("csv", "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)
}
store.AddQuad(quad.Make(id, "name", s[1], "."))
store.AddQuad(quad.Make(id, "species_id", speciesId, "."))
store.AddQuad(quad.Make(id, "height", height, "."))
store.AddQuad(quad.Make(id, "base_experience", baseExperience, "."))
}
// Now we create the path, to get to our data
p := cayley.StartPath(store).In(quad.String("name"))
// the same results
// p := cayley.StartPath(store).Has(quad.String("name"))
// show that id 134 is connected to 2 names
// p := cayley.StartPath(store, quad.Int(134)).Out(quad.String("name"))
it, _ := p.BuildIterator().Optimize()
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)
}
}