List all quads from store

List all quads from store #

Problem #

How can I retrieve and print all quads from the store?

Solution #

Retrieve and list all quads from the store: #

package main

import (
    "fmt"
    "log"
    "reflect"
    "context"

    "github.com/cayleygraph/cayley"
)

func main() {

    // Create a brand new graph
    store, err := cayley.NewMemoryGraph()
    if err != nil {
        log.Fatalln(err)
    }

    // Create Quads about some people using Blank Nodes as references
    quads := makeQuads()

    // Add quads to the store
    for _, q := range(quads) {
        store.AddQuad(q)
    }

    printAllQuads(store)
}

func printAllQuads(store *cayley.Handle) {
    // Get the iterator that enumerates all nodes in the graph.
    it := store.QuadsAllIterator()
    //fmt.Printf("%v %v\n", it, reflect.TypeOf(it))
    for it.Next(context.Background()) {
        ref := it.Result()
        key := ref.Key()
        value := store.NameOf(ref)
        quad := store.Quad(ref)
        fmt.Printf("Ref: %v %v\n", ref, reflect.TypeOf(ref))
        fmt.Printf("Key: %v %v\n", key, reflect.TypeOf(key))
        fmt.Printf("Value: %v %v\n", value, reflect.TypeOf(value))
        fmt.Printf("Quad: %v %v\n", quad, reflect.TypeOf(quad))
        fmt.Println()
        /*
        v := it.Result()
        fmt.Printf("%v %v\n", v, reflect.TypeOf(v))
        fmt.Printf("%v\n", v.Key())

        name := store.NameOf(v)
        fmt.Printf("%v %v\n", name, reflect.TypeOf(name))
        q := store.Quad(v)
        fmt.Println(q)
        fmt.Println()
        */
    }
}

Results:

Ref: {0xc00007a1c0} memstore.qprim
Key: 5 int64
Value: _:memnode5 quad.BNode
Quad: _:n7250714951529471378 -- <rdf:type> -> <foaf:Person> quad.Quad

Ref: {0xc00007a3c0} memstore.qprim
Key: 8 int64
Value: _:memnode8 quad.BNode
Quad: _:n7250714951529471378 -- <foaf:givenName> -> "Luke" quad.Quad

Ref: {0xc00007a500} memstore.qprim
Key: 11 int64
Value: _:memnode11 quad.BNode
Quad: _:n7250714951529471378 -- <foaf:familyName> -> "Skywalker" quad.Quad

Ref: {0xc00007a640} memstore.qprim
Key: 14 int64
Value: _:memnode14 quad.BNode
Quad: _:n7250714951529471378 -- <foaf:age> -> "23"^^<xsd:integer> quad.Quad

Ref: {0xc00007a740} memstore.qprim
Key: 16 int64
Value: _:memnode16 quad.BNode
Quad: <starwars:leia_organa> -- <rdf:type> -> <foaf:Person> quad.Quad

Ref: {0xc00007a800} memstore.qprim
Key: 18 int64
Value: _:memnode18 quad.BNode
Quad: <starwars:leia_organa> -- <foaf:knows> -> _:n7250714951529471378 quad.Quad

Ref: {0xc00007a900} memstore.qprim
Key: 20 int64
Value: _:memnode20 quad.BNode
Quad: <starwars:leia_organa> -- <foaf:givenName> -> "Leia" quad.Quad

Ref: {0xc00007a9c0} memstore.qprim
Key: 22 int64
Value: _:memnode22 quad.BNode
Quad: <starwars:leia_organa> -- <foaf:familyName> -> "Organa" quad.Quad