func main() {

	store, err := cayley.NewGraph("bolt", dbPath, nil)
	if err != nil {
		fmt.Println("error in creating database", err)
	}

	path := cayley.StartPath(store, "Article").
		In().
		Tag("link").
		Save("has_image", "image").
		Save("has_title", "title").
		Save("has_description", "description")

	it := path.BuildIterator()
	it, _ = it.Optimize()

	for graph.Next(it) {
		tags := make(map[string]graph.Value)
		it.TagResults(tags)
		fmt.Println(store.NameOf(tags["image"]))
		fmt.Println(store.NameOf(tags["title"]))
		fmt.Println(store.NameOf(tags["description"]))
		fmt.Println(store.NameOf(tags["link"]))
	}

}
Example #2
0
// Open opens a Cayley database, creating it if necessary and return its handle
func Open(dbType, dbPath string) error {
	if store != nil {
		log.Errorf("could not open database at %s : a database is already opened", dbPath)
		return ErrCantOpen
	}

	var err error

	// Try to create database if necessary
	if dbType == "bolt" || dbType == "leveldb" {
		if _, err := os.Stat(dbPath); os.IsNotExist(err) {
			// No, initialize it if possible
			log.Infof("database at %s does not exist yet, creating it", dbPath)

			if err = graph.InitQuadStore(dbType, dbPath, nil); err != nil {
				log.Errorf("could not create database at %s : %s", dbPath, err)
				return ErrCantOpen
			}
		}
	} else if dbType == "sql" {
		graph.InitQuadStore(dbType, dbPath, nil)
	}

	store, err = cayley.NewGraph(dbType, dbPath, nil)
	if err != nil {
		log.Errorf("could not open database at %s : %s", dbPath, err)
		return ErrCantOpen
	}

	return nil
}
Example #3
0
File: db.go Project: oren/user
func New(path string) Db {
	db := Db{location: path}
	graph.InitQuadStore("bolt", path, nil)
	store, err := cayley.NewGraph("bolt", path, nil)
	db.Store = *store

	if err != nil {
		log.Fatalln(err)
	}

	return db
}
func main() {
	//graph.InitQuadStore("bolt", dbPath, nil)
	store, err := cayley.NewGraph("bolt", dbPath, nil)
	if err != nil {
		fmt.Println("error in creating database", err)
	}
	err = store.AddQuad(cayley.Quad("food", "is", "good", ""))
	// if err != nil {
	// 	fmt.Println("write error", err)
	// }
	err = store.AddQuad(cayley.Quad("nothing", "is", "good", ""))
}
Example #5
0
func GetStorage() (s *Storage, err error) {
	if storage == nil {
		graph.InitQuadStore("bolt", BoltPath, nil)
		var handle *cayley.Handle
		handle, err = cayley.NewGraph("bolt", BoltPath, nil)
		s = &Storage{handle}
		storage = s
	} else {
		s = storage
	}

	return s, err
}
Example #6
0
// Open opens a Cayley database, creating it if necessary and return its handle
func Open(config *config.DatabaseConfig) error {
	if store != nil {
		log.Errorf("could not open database at %s : a database is already opened", config.Path)
		return ErrCantOpen
	}
	if config.Type != "memstore" && config.Path == "" {
		log.Errorf("could not open database : no path provided.")
		return ErrCantOpen
	}

	var err error
	options := make(graph.Options)

	switch config.Type {
	case "bolt", "leveldb":
		if _, err := os.Stat(config.Path); os.IsNotExist(err) {
			log.Infof("database at %s does not exist yet, creating it", config.Path)

			err = graph.InitQuadStore(config.Type, config.Path, options)
			if err != nil && err != graph.ErrDatabaseExists {
				log.Errorf("could not create database at %s : %s", config.Path, err)
				return ErrCantOpen
			}
		}
	case "sql":
		// Replaces the PostgreSQL's slow COUNT query with a fast estimator.
		// Ref: https://wiki.postgresql.org/wiki/Count_estimate
		options["use_estimates"] = true

		err := graph.InitQuadStore(config.Type, config.Path, options)
		if err != nil && err != graph.ErrDatabaseExists {
			log.Errorf("could not create database at %s : %s", config.Path, err)
			return ErrCantOpen
		}
	}

	store, err = cayley.NewGraph(config.Type, config.Path, options)
	if err != nil {
		log.Errorf("could not open database at %s : %s", config.Path, err)
		return ErrCantOpen
	}

	return nil
}
Example #7
0
func open() {
	path := "./db"
	// Initialize the database
	graph.InitQuadStore("bolt", path, nil)

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

	p := cayley.StartPath(store, "").Out("type").Is("Person")

	it := p.BuildIterator()
	for cayley.RawNext(it) {
		log.Println(store.NameOf(it.Result()))
	}

}
Example #8
0
// Wrap initializes a new MongoDB decorator that wraps the base service
func Wrap(service common.IServiceRegistry, dial string) (common.IServiceRegistry, error) {
	var s = &mgoDecorator{
		base: service,
		dial: dial}

	session, err := createMgoSession(s.dial)

	if err != nil {
		return nil, fmt.Errorf("Could not establish a connection to mgo: %s", err.Error())
	}

	c := session.DB("test").C("serviceDefinitions")
	err = c.EnsureIndex(identifierUnique)
	err = c.EnsureIndex(prefixUnique)

	if err != nil {
		return nil, fmt.Errorf("Could not ensure indexes for collection 'serviceDefinitions': %s", err.Error())
	}

	c = session.DB("test").C("apiDefinitions")
	err = c.EnsureIndex(versionUnique)

	if err != nil {
		return nil, fmt.Errorf("Could not ensure indexes for collection 'apiDefinitions': %s", err.Error())
	}

	c = session.DB("test").C("serviceVersions")
	err = c.EnsureIndex(serviceVersionUnique)

	if err != nil {
		return nil, fmt.Errorf("Could not ensure indexes for collection 'serviceDefinitions': %s", err.Error())
	}

	cstore, err := cayley.NewGraph("mongo", dial, nil)
	if err != nil {
		return nil, fmt.Errorf("Could not connect cayley to mongodb: %s", err.Error())
	}
	s.store = cstore

	return s, nil
}
Example #9
0
func initDb() (*graph.NodeGraph, error) {
	var handle *cayley.Handle
	var err error
	if !debug {
		dbPath := filepath.Join(env.EnvPath(env.DbPath), "db.dat")
		if !env.Exists(dbPath) {
			if err = cgraph.InitQuadStore("bolt", dbPath, nil); err != nil {
				return nil, err
			}
		}
		if handle, err = cayley.NewGraph("bolt", dbPath, nil); err != nil {
			return nil, err
		}
	} else {
		if handle, err = cayley.NewMemoryGraph(); err != nil {
			return nil, err
		}
	}

	return graph.NewGraph(handle)
}
Example #10
0
func open() {
	path := "./db"
	// Initialize the database
	graph.InitQuadStore("bolt", path, nil)

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

	store.AddQuad(cayley.Quad("person:sophie", "type", "Person", ""))
	store.AddQuad(cayley.Quad("person:sophie", "name", "Sophie Grégoire", ""))
	store.AddQuad(cayley.Quad("person:sophie", "born", "1974", ""))
	store.AddQuad(cayley.Quad("person:sophie", "lives in", "country:canada", ""))

	store.AddQuad(cayley.Quad("person:justin", "type", "Person", ""))
	store.AddQuad(cayley.Quad("person:justin", "name", "Justin Trudeau", ""))
	store.AddQuad(cayley.Quad("person:justin", "born", "1972", ""))
	store.AddQuad(cayley.Quad("person:justin", "in love with", "person:sophie", ""))
}
Example #11
0
func main() {

	store, err := cayley.NewGraph("bolt", dbPath, nil)
	if err != nil {
		fmt.Println("error in opening database", err)
		return
	}

	http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("website/images"))))
	http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir("website/fonts"))))
	http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("website/assets"))))
	http.Handle("/searchAssets/", http.StripPrefix("/searchAssets/", http.FileServer(http.Dir("website/Search/assets"))))
	http.Handle("/sass/", http.StripPrefix("/sass/", http.FileServer(http.Dir("website/sass"))))

	http.HandleFunc("/submitPost", func(w http.ResponseWriter, r *http.Request) { submitLink(w, r, store) })
	http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) { mainHandler(w, r, store) })
	http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { searchHandler(w, r, store) })
	http.HandleFunc("/searchResults", func(w http.ResponseWriter, r *http.Request) { searchResultsHandler(w, r, store) })
	fmt.Println("Server started at port 8080")
	http.ListenAndServe(":8080", nil)

}
Example #12
0
// Open opens a Cayley database, creating it if necessary and return its handle
func Open(dbType, dbPath string) error {
	if store != nil {
		log.Errorf("could not open database at %s : a database is already opened", dbPath)
		return ErrCantOpen
	}

	var err error
	options := make(graph.Options)

	switch dbType {
	case "bolt", "leveldb":
		if _, err := os.Stat(dbPath); os.IsNotExist(err) {
			log.Infof("database at %s does not exist yet, creating it", dbPath)

			err = graph.InitQuadStore(dbType, dbPath, options)
			if err != nil {
				log.Errorf("could not create database at %s : %s", dbPath, err)
				return ErrCantOpen
			}
		}
	case "sql":
		// Replaces the PostgreSQL's slow COUNT query with a fast estimator.
		// See:
		// Ref: https://wiki.postgresql.org/wiki/Count_estimate
		options["use_estimates"] = true

		graph.InitQuadStore(dbType, dbPath, options)
	}

	store, err = cayley.NewGraph(dbType, dbPath, options)
	if err != nil {
		log.Errorf("could not open database at %s : %s", dbPath, err)
		return ErrCantOpen
	}

	return nil
}
Example #13
0
func TestInit() (*graph.NodeGraph, string) {
	if dir, err := ioutil.TempDir(os.TempDir(), ".olympus"); err != nil {
		panic(err)
	} else {
		os.Setenv("OLYMPUS_HOME", dir)
		if err = env.InitializeEnvironment(); err != nil {
			panic(err)
		}

		dbPath := filepath.Join(env.EnvPath(env.DbPath), "db.dat")
		if !env.Exists(dbPath) {
			cgraph.InitQuadStore("bolt", dbPath, nil)
			if handle, err := cayley.NewGraph("bolt", dbPath, nil); err != nil {
				panic(err)
			} else if ng, err := graph.NewGraph(handle); err != nil {
				panic(err)
			} else {
				return ng, dir
			}
		} else {
			return nil, ""
		}
	}
}
func (p *mgoPersistenceProvider) LoadFrom(dial string, target common.IServiceRegistry) error {
	var mgoSession, err = createMgoSession(dial)
	if err != nil {
		return err
	}

	graph, err := cayley.NewGraph("mongo", dial, nil)
	if err != nil {
		return fmt.Errorf("Could not connect cayley to mongodb: %s", err.Error())
	}

	// Get all service Definitions
	err = p.loadServiceDefinitions(mgoSession, target, graph)
	if err != nil {
		return err
	}

	err = p.loadAPIRevisions(mgoSession, target, graph)
	if err != nil {
		return err
	}

	return nil
}