github.com/cayleygraph/cayley@v0.7.7/examples/hello_bolt/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "os" 9 10 "github.com/cayleygraph/cayley" 11 "github.com/cayleygraph/cayley/graph" 12 _ "github.com/cayleygraph/cayley/graph/kv/bolt" 13 "github.com/cayleygraph/quad" 14 ) 15 16 func main() { 17 // File for your new BoltDB. Use path to regular file and not temporary in the real world 18 tmpdir, err := ioutil.TempDir("", "example") 19 if err != nil { 20 log.Fatal(err) 21 } 22 23 defer os.RemoveAll(tmpdir) // clean up 24 25 // Initialize the database 26 err = graph.InitQuadStore("bolt", tmpdir, nil) 27 if err != nil { 28 log.Fatal(err) 29 } 30 31 // Open and use the database 32 store, err := cayley.NewGraph("bolt", tmpdir, nil) 33 if err != nil { 34 log.Fatalln(err) 35 } 36 37 store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello BoltDB!", "demo graph")) 38 39 // Now we create the path, to get to our data 40 p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course")) 41 42 // This is more advanced example of the query. 43 // Simpler equivalent can be found in hello_world example. 44 45 // Now we get an iterator for the path and optimize it. 46 // The second return is if it was optimized, but we don't care for now. 47 it, _ := p.BuildIterator().Optimize() 48 49 // remember to cleanup after yourself 50 defer it.Close() 51 52 ctx := context.TODO() 53 // While we have items 54 for it.Next(ctx) { 55 token := it.Result() // get a ref to a node (backend-specific) 56 value := store.NameOf(token) // get the value in the node (RDF) 57 nativeValue := quad.NativeOf(value) // convert value to normal Go type 58 59 fmt.Println(nativeValue) // print it! 60 } 61 if err := it.Err(); err != nil { 62 log.Fatalln(err) 63 } 64 }