Пример #1
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
}
Пример #2
0
func Init(cfg *config.Config) error {
	if !graph.IsPersistent(cfg.DatabaseType) {
		return fmt.Errorf("ignoring unproductive database initialization request: %v", ErrNotPersistent)
	}

	return graph.InitQuadStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
}
Пример #3
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
}
Пример #4
0
Файл: db.go Проект: 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
}
Пример #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
}
Пример #6
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
}
Пример #7
0
func main() {

	path := "/tmp/pc"

	graph.InitQuadStore("bolt", path, nil)

	m := martini.Classic()

	unescapeFuncMap := template.FuncMap{"unescape": unescape}

	m.Use(session.Middleware)

	m.Use(render.Renderer(render.Options{
		Directory:  "templates",                         // Specify what path to load the templates from.
		Layout:     "layout",                            // Specify a layout template. Layouts can call {{ yield }} to render the current template.
		Extensions: []string{".tmpl", ".html"},          // Specify extensions to load for templates.
		Funcs:      []template.FuncMap{unescapeFuncMap}, // Specify helper function maps for templates to access.
		Charset:    "UTF-8",                             // Sets encoding for json and html content-types. Default is "UTF-8".
		IndentJSON: true,                                // Output human readable JSON
	}))

	storage, err := models.GetStorage()
	if err != nil {
		log.Fatalln(err)
	}

	server, err := server.NewServer(storage)
	if err != nil {
		log.Fatalln(err)
	}

	user := models.NewUser("admin")
	user.Iteration()

	staticOptions := martini.StaticOptions{Prefix: "assets"}
	m.Use(martini.Static("assets", staticOptions))
	m.Get("/", routes.IndexHandler)
	m.Get("/login", routes.GetLoginHandler)
	m.Get("/logout", routes.LogoutHandler)
	m.Post("/login", routes.PostLoginHandler)
	m.Get("/view:id", routes.ViewHandler)
	m.Post("/gethtml", routes.GetHtmlHandler)
	m.Get("/socket.io/", func(w http.ResponseWriter, rnd render.Render, r *http.Request, s *session.Session) {
		server.SetSession(s)
		server.ServeHTTP(w, r)
	})
	m.Run()
}
Пример #8
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()))
	}

}
Пример #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)
}
Пример #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", ""))
}
Пример #11
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, ""
		}
	}
}