Use hash's as ids for table relationships

This commit is contained in:
Vikram Rangnekar
2019-05-14 22:32:12 -04:00
parent e1d3bb9055
commit 58408eadc1
7 changed files with 116 additions and 41 deletions

View File

@ -73,3 +73,57 @@ func TestEmptyCompile(t *testing.T) {
t.Fatal(errors.New("expecting an error"))
}
}
func BenchmarkQCompile(b *testing.B) {
qcompile, _ := NewCompiler(Config{})
val := `query {
products(
where: {
and: {
not: { id: { is_null: true } },
price: { gt: 10 }
}}) {
id
name
price
}
}`
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, err := qcompile.CompileQuery(val)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkLex(b *testing.B) {
val := `query {
products(
where: {
and: {
not: { id: { is_null: true } },
price: { gt: 10 }
}}) {
id
name
price
}
}`
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, err := lex(val)
if err != nil {
b.Fatal(err)
}
}
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/cespare/xxhash/v2"
"github.com/dosco/super-graph/util"
"github.com/gobuffalo/flect"
)
@ -30,6 +31,7 @@ type Column struct {
type Select struct {
ID uint16
ParentID uint16
RelID uint64
Args map[string]*Node
AsList bool
Table string
@ -255,6 +257,7 @@ func (com *Compiler) compileQuery(op *Operation) (*Query, error) {
selects := make([]Select, 0, 5)
st := util.NewStack()
h := xxhash.New()
if len(op.Fields) == 0 {
return nil, errors.New("empty query")
@ -294,6 +297,7 @@ func (com *Compiler) compileQuery(op *Operation) (*Query, error) {
if s.ID != 0 {
p := &selects[s.ParentID]
p.Children = append(p.Children, s.ID)
s.RelID = relID(h, tn, p.Table)
}
if fn == tn {
@ -875,3 +879,11 @@ func buildPath(a []string) string {
}
return b.String()
}
func relID(h *xxhash.Digest, child, parent string) uint64 {
h.WriteString(child)
h.WriteString(parent)
v := h.Sum64()
h.Reset()
return v
}