Exemplo n.º 1
0
func (s *Session) ExecInput(input string, c chan interface{}, _ int) {
	defer close(c)
	var mqlQuery interface{}
	err := json.Unmarshal([]byte(input), &mqlQuery)
	if err != nil {
		return
	}
	s.currentQuery = NewQuery(s)
	s.currentQuery.BuildIteratorTree(mqlQuery)
	if s.currentQuery.isError() {
		return
	}
	it, _ := s.currentQuery.it.Optimize()
	if glog.V(2) {
		b, err := json.MarshalIndent(it.Describe(), "", "  ")
		if err != nil {
			glog.Infof("failed to format description: %v", err)
		} else {
			glog.Infof("%s", b)
		}
	}
	for graph.Next(it) {
		tags := make(map[string]graph.Value)
		it.TagResults(tags)
		c <- tags
		for it.NextPath() == true {
			tags := make(map[string]graph.Value)
			it.TagResults(tags)
			c <- tags
		}
	}
}
Exemplo n.º 2
0
func LogRequest(handler ResponseHandler) httprouter.Handle {
	return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		start := time.Now()
		addr := req.Header.Get("X-Real-IP")
		if addr == "" {
			addr = req.Header.Get("X-Forwarded-For")
			if addr == "" {
				addr = req.RemoteAddr
			}
		}
		glog.Infof("Started %s %s for %s", req.Method, req.URL.Path, addr)
		code := handler(w, req, params)
		glog.Infof("Completed %v %s %s in %v", code, http.StatusText(code), req.URL.Path, time.Since(start))

	}
}
Exemplo n.º 3
0
func (it *Materialize) materializeSet() {
	i := 0
	for graph.Next(it.subIt) {
		i++
		if i > abortMaterializeAt {
			it.aborted = true
			break
		}
		id := it.subIt.Result()
		val := id
		if h, ok := id.(Keyer); ok {
			val = h.Key()
		}
		if _, ok := it.containsMap[val]; !ok {
			it.containsMap[val] = len(it.values)
			it.values = append(it.values, nil)
		}
		index := it.containsMap[val]
		tags := make(map[string]graph.Value)
		it.subIt.TagResults(tags)
		it.values[index] = append(it.values[index], result{id: id, tags: tags})
		for it.subIt.NextPath() {
			tags := make(map[string]graph.Value)
			it.subIt.TagResults(tags)
			it.values[index] = append(it.values[index], result{id: id, tags: tags})
		}
	}
	if it.aborted {
		it.values = nil
		it.containsMap = nil
		it.subIt.Reset()
	}
	glog.Infof("Materialization List %d: %#v", it.values)
	it.hasRun = true
}
Exemplo n.º 4
0
func (wk *worker) runIterator(it graph.Iterator) {
	if wk.wantShape() {
		iterator.OutputQueryShapeForIterator(it, wk.qs, wk.shape)
		return
	}
	it, _ = it.Optimize()
	if glog.V(2) {
		b, err := json.MarshalIndent(it.Describe(), "", "  ")
		if err != nil {
			glog.Infof("failed to format description: %v", err)
		} else {
			glog.Infof("%s", b)
		}
	}
	for {
		select {
		case <-wk.kill:
			return
		default:
		}
		if !graph.Next(it) {
			break
		}
		tags := make(map[string]graph.Value)
		it.TagResults(tags)
		if !wk.send(&Result{actualResults: tags}) {
			break
		}
		for it.NextPath() {
			select {
			case <-wk.kill:
				return
			default:
			}
			tags := make(map[string]graph.Value)
			it.TagResults(tags)
			if !wk.send(&Result{actualResults: tags}) {
				break
			}
		}
	}
	if glog.V(2) {
		bytes, _ := json.MarshalIndent(graph.DumpStats(it), "", "  ")
		glog.V(2).Infoln(string(bytes))
	}
	it.Close()
}
Exemplo n.º 5
0
func Serve(handle *graph.Handle, cfg *config.Config) {
	SetupRoutes(handle, cfg)
	glog.Infof("Cayley now listening on %s:%s\n", cfg.ListenHost, cfg.ListenPort)
	fmt.Printf("Cayley now listening on http://%s:%s\n", cfg.ListenHost, cfg.ListenPort)
	err := http.ListenAndServe(fmt.Sprintf("%s:%s", cfg.ListenHost, cfg.ListenPort), nil)
	if err != nil {
		glog.Fatal("ListenAndServe: ", err)
	}
}
Exemplo n.º 6
0
func OpenQuadWriter(qs graph.QuadStore, cfg *config.Config) (graph.QuadWriter, error) {
	glog.Infof("Opening replication method %q", cfg.ReplicationType)
	w, err := graph.NewQuadWriter(cfg.ReplicationType, qs, cfg.ReplicationOptions)
	if err != nil {
		return nil, err
	}

	return w, nil
}
Exemplo n.º 7
0
func OpenQuadStore(cfg *config.Config) (graph.QuadStore, error) {
	glog.Infof("Opening quad store %q at %s", cfg.DatabaseType, cfg.DatabasePath)
	qs, err := graph.NewQuadStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
	if err != nil {
		return nil, err
	}

	return qs, nil
}
Exemplo n.º 8
0
func Open(cfg *config.Config) (graph.TripleStore, error) {
	glog.Infof("Opening database %q at %s", cfg.DatabaseType, cfg.DatabasePath)
	ts, err := graph.NewTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
	if err != nil {
		return nil, err
	}

	return ts, nil
}
Exemplo n.º 9
0
func CayleyHTTP(ts graph.TripleStore, cfg *config.CayleyConfig) {
	SetupRoutes(ts, cfg)
	glog.Infof("Cayley now listening on %s:%s\n", cfg.ListenHost, cfg.ListenPort)
	fmt.Printf("Cayley now listening on %s:%s\n", cfg.ListenHost, cfg.ListenPort)
	err := http.ListenAndServe(fmt.Sprintf("%s:%s", cfg.ListenHost, cfg.ListenPort), nil)
	if err != nil {
		glog.Fatal("ListenAndServe: ", err)
	}
}
Exemplo n.º 10
0
func Open(cfg *config.Config) graph.TripleStore {
	glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
	switch cfg.DatabaseType {
	case "mongo", "mongodb":
		return mongo.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
	case "leveldb":
		return leveldb.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
	case "mem":
		ts := memstore.NewTripleStore()
		Load(ts, cfg, cfg.DatabasePath, true)
		return ts
	}
	panic("Unsupported database backend " + cfg.DatabaseType)
}
Exemplo n.º 11
0
func OpenTSFromConfig(config *cfg.CayleyConfig) graph.TripleStore {
	glog.Infof("Opening database \"%s\" at %s", config.DatabaseType, config.DatabasePath)
	switch config.DatabaseType {
	case "mongo", "mongodb":
		return graph_mongo.NewMongoTripleStore(config.DatabasePath, config.DatabaseOptions)
	case "leveldb":
		return graph_leveldb.NewDefaultLevelDBTripleStore(config.DatabasePath, config.DatabaseOptions)
	case "mem":
		ts := graph_memstore.NewMemTripleStore()
		CayleyLoad(ts, config, config.DatabasePath, true)
		return ts
	}
	panic("Unsupported database backend " + config.DatabaseType)
}
Exemplo n.º 12
0
func Open(cfg *config.Config) (graph.TripleStore, error) {
	glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
	ts, err := graph.NewTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
	if err != nil {
		return nil, err
	}

	// Memstore is not persistent, so it MUST be loaded.
	if cfg.DatabaseType == "memstore" {
		err = Load(ts, cfg, cfg.DatabasePath)
		if err != nil {
			return nil, err
		}
	}

	return ts, nil
}
Exemplo n.º 13
0
func printIterator(qs graph.QuadStore, it graph.Iterator) {
	for graph.Next(it) {
		glog.Infof("%v", qs.Quad(it.Result()))
	}
}