Exemplo n.º 1
0
// New creates a new cayley handle.
func New(mongoURL string, ses *mgo.Session) (*cayley.Handle, error) {

	// If the session is not provided, create the Cayley Handle from
	// the mongo URL.
	if ses == nil {

		// Parse the provied mongoURL.
		cfg, err := url.Parse(mongoURL)
		if err != nil {
			return nil, err
		}

		// Form the Cayley connection options.
		opts := parseMongoURL(cfg)

		// Create the cayley handle that maintains a connection to the
		// Cayley graph database in Mongo.
		store, err := cayley.NewGraph("mongo", cfg.Host, opts)
		if err != nil {
			return store, err
		}

		return store, nil
	}

	// If the session is not nil, create the Cayley handle with the
	// provided mongo session.
	opts := make(map[string]interface{})
	opts["session"] = ses

	// Create the cayley handle that maintains a connection to the
	// Cayley graph database in Mongo.
	store, err := cayley.NewGraph("mongo", "", opts)
	if err != nil {
		return store, err
	}

	return store, nil
}
Exemplo n.º 2
0
func loadGraph(path string) (*cayley.Handle, error) {
	*graph.IgnoreDup = true

	err := graph.InitQuadStore("leveldb", path, map[string]interface{}{
		"ignore_duplicate": true,
	})
	if err == graph.ErrDatabaseExists {
		log.Print(err)
	} else if err != nil {
		return nil, err
	}
	return cayley.NewGraph("leveldb", path, nil)
}
Exemplo n.º 3
0
func main() {
	// File for your new BoltDB. Use path to regular file and not temporary in the real world
	tmpfile, err := ioutil.TempFile("", "example")
	if err != nil {
		log.Fatal(err)
	}

	defer os.Remove(tmpfile.Name()) // clean up

	// Initialize the database
	graph.InitQuadStore("bolt", tmpfile.Name(), nil)

	// Open and use the database
	store, err := cayley.NewGraph("bolt", tmpfile.Name(), nil)
	if err != nil {
		log.Fatalln(err)
	}

	store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello BoltDB!", "demo graph"))

	// Now we create the path, to get to our data
	p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course"))

	// This is more advanced example of the query.
	// Simpler equivalent can be found in hello_world example.

	// Now we get an iterator for the path and optimize it.
	// The second return is if it was optimized, but we don't care for now.
	it, _ := p.BuildIterator().Optimize()

	// Optimize iterator on quad store level.
	// After this step iterators will be replaced with backend-specific ones.
	it, _ = store.OptimizeIterator(it)

	// remember to cleanup after yourself
	defer it.Close()

	// While we have items
	for it.Next() {
		token := it.Result()                // get a ref to a node (backend-specific)
		value := store.NameOf(token)        // get the value in the node (RDF)
		nativeValue := quad.NativeOf(value) // convert value to normal Go type

		fmt.Println(nativeValue) // print it!
	}
	if err := it.Err(); err != nil {
		log.Fatalln(err)
	}
}