Example #1
0
func (api *API) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
	h, err := api.GetHandleForRequest(r)
	var ses query.HTTP
	switch params.ByName("query_lang") {
	case "gremlin":
		ses = gremlin.NewSession(h.QuadStore, api.config.Timeout, false)
	case "mql":
		ses = mql.NewSession(h.QuadStore)
	default:
		return jsonResponse(w, 400, "Need a query language.")
	}
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return jsonResponse(w, 400, err)
	}
	code := string(bodyBytes)
	result, err := ses.Parse(code)
	switch result {
	case query.Parsed:
		var output []byte
		var err error
		output, err = GetQueryShape(code, ses)
		if err != nil {
			return jsonResponse(w, 400, err)
		}
		fmt.Fprint(w, string(output))
		return 200
	case query.ParseFail:
		return jsonResponse(w, 400, err)
	default:
		return jsonResponse(w, 500, "Incomplete data?")
	}
}
Example #2
0
func checkQueries(t *testing.T) {
	for _, test := range benchmarkQueries {
		if testing.Short() && test.long {
			continue
		}
		if test.skip {
			continue
		}
		tInit := time.Now()
		t.Logf("Now testing %s ", test.message)
		ses := gremlin.NewSession(handle.QuadStore, cfg.Timeout, true)
		_, err := ses.Parse(test.query)
		if err != nil {
			t.Fatalf("Failed to parse benchmark gremlin %s: %v", test.message, err)
		}
		c := make(chan interface{}, 5)
		go ses.Execute(test.query, c, 100)
		var (
			got      []interface{}
			timedOut bool
		)
		for r := range c {
			ses.Collate(r)
			j, err := ses.Results()
			if j == nil && err == nil {
				continue
			}
			if err == gremlin.ErrKillTimeout {
				timedOut = true
				continue
			}
			got = append(got, j.([]interface{})...)
		}

		if timedOut {
			t.Error("Query timed out: skipping validation.")
			continue
		}
		t.Logf("(%v)\n", time.Since(tInit))

		if len(got) != len(test.expect) {
			t.Errorf("Unexpected number of results, got:%d expect:%d on %s.", len(got), len(test.expect), test.message)
			continue
		}
		if unsortedEqual(got, test.expect) {
			continue
		}
		t.Errorf("Unexpected results for %s:\n", test.message)
		for i := range got {
			t.Errorf("\n\tgot:%#v\n\texpect:%#v\n", got[i], test.expect[i])
		}
	}
}
Example #3
0
func runBench(n int, b *testing.B) {
	if testing.Short() && benchmarkQueries[n].long {
		b.Skip()
	}
	prepare(b)
	b.StopTimer()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		c := make(chan interface{}, 5)
		ses := gremlin.NewSession(handle.QuadStore, cfg.Timeout, true)
		// Do the parsing we know works.
		ses.Parse(benchmarkQueries[n].query)
		b.StartTimer()
		go ses.Execute(benchmarkQueries[n].query, c, 100)
		for _ = range c {
		}
		b.StopTimer()
	}
}
Example #4
0
File: repl.go Project: oren/cayley
func Repl(h *graph.Handle, queryLanguage string, cfg *config.Config) error {
	var ses query.Session
	switch queryLanguage {
	case "sexp":
		ses = sexp.NewSession(h.QuadStore)
	case "mql":
		ses = mql.NewSession(h.QuadStore)
	case "gremlin":
		fallthrough
	default:
		ses = gremlin.NewSession(h.QuadStore, cfg.Timeout, true)
	}

	term, err := terminal(history)
	if os.IsNotExist(err) {
		fmt.Printf("creating new history file: %q\n", history)
	}
	defer persist(term, history)

	var (
		prompt = ps1

		code string
	)

	for {
		if len(code) == 0 {
			prompt = ps1
		} else {
			prompt = ps2
		}
		line, err := term.Prompt(prompt)
		if err != nil {
			if err == io.EOF {
				fmt.Println()
				return nil
			}
			return err
		}

		term.AppendHistory(line)

		line = strings.TrimSpace(line)
		if len(line) == 0 || line[0] == '#' {
			continue
		}

		if code == "" {
			cmd, args := splitLine(line)

			switch cmd {
			case ":debug":
				args = strings.TrimSpace(args)
				var debug bool
				switch args {
				case "t":
					debug = true
				case "f":
					// Do nothing.
				default:
					debug, err = strconv.ParseBool(args)
					if err != nil {
						fmt.Printf("Error: cannot parse %q as a valid boolean - acceptable values: 't'|'true' or 'f'|'false'\n", args)
						continue
					}
				}
				ses.Debug(debug)
				fmt.Printf("Debug set to %t\n", debug)
				continue

			case ":a":
				quad, err := cquads.Parse(args)
				if err != nil {
					fmt.Printf("Error: not a valid quad: %v\n", err)
					continue
				}

				h.QuadWriter.AddQuad(quad)
				continue

			case ":d":
				quad, err := cquads.Parse(args)
				if err != nil {
					fmt.Printf("Error: not a valid quad: %v\n", err)
					continue
				}
				err = h.QuadWriter.RemoveQuad(quad)
				if err != nil {
					fmt.Printf("error deleting: %v\n", err)
				}
				continue

			case "exit":
				term.Close()
				os.Exit(0)

			default:
				if cmd[0] == ':' {
					fmt.Printf("Unknown command: %q\n", cmd)
					continue
				}
			}
		}

		code += line

		result, err := ses.Parse(code)
		switch result {
		case query.Parsed:
			Run(code, ses)
			code = ""
		case query.ParseFail:
			fmt.Println("Error: ", err)
			code = ""
		case query.ParseMore:
		}
	}
}