Skip to content

Commit

Permalink
more indexStartOptimize test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstruck committed Jan 5, 2019
1 parent 1a4712d commit db8bd49
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
46 changes: 20 additions & 26 deletions engine/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor {
optimized := []gdbi.Processor{}

var lookupV *LookupVerts
hasIdIdx := []int{}
hasIDIdx := []int{}
hasLabelIdx := []int{}
for i, step := range pipe {
if i == 0 {
Expand All @@ -283,15 +283,15 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor {
}
switch s := step.(type) {
case *HasID:
hasIdIdx = append(hasIdIdx, i)
hasIDIdx = append(hasIDIdx, i)
case *HasLabel:
hasLabelIdx = append(hasLabelIdx, i)
case *Has:
if cond := s.stmt.GetCondition(); cond != nil {
path := jsonpath.GetJSONPath(cond.Key)
switch path {
case "$.gid":
hasIdIdx = append(hasIdIdx, i)
hasIDIdx = append(hasIDIdx, i)
case "$.label":
hasLabelIdx = append(hasLabelIdx, i)
default:
Expand All @@ -304,16 +304,15 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor {
}

idOpt := false
if len(hasIdIdx) > 0 {
if len(hasIDIdx) > 0 {
idOpt = true
ids := []string{}
for _, idx := range hasIdIdx {
if has, ok := pipe[idx].(*Has); ok {
ids = append(ids, extractHasVals(has)...)
}
if has, ok := pipe[idx].(*HasID); ok {
ids = append(ids, has.ids...)
}
idx := hasIDIdx[0]
if has, ok := pipe[idx].(*Has); ok {
ids = append(ids, extractHasVals(has)...)
}
if has, ok := pipe[idx].(*HasID); ok {
ids = append(ids, has.ids...)
}
hIdx := &LookupVerts{ids: ids, db: lookupV.db}
optimized = append(optimized, hIdx)
Expand All @@ -323,13 +322,12 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor {
if len(hasLabelIdx) > 0 && !idOpt {
labelOpt = true
labels := []string{}
for _, idx := range hasLabelIdx {
if has, ok := pipe[idx].(*Has); ok {
labels = append(labels, extractHasVals(has)...)
}
if has, ok := pipe[idx].(*HasLabel); ok {
labels = append(labels, has.labels...)
}
idx := hasLabelIdx[0]
if has, ok := pipe[idx].(*Has); ok {
labels = append(labels, extractHasVals(has)...)
}
if has, ok := pipe[idx].(*HasLabel); ok {
labels = append(labels, has.labels...)
}
hIdx := &LookupVertsIndex{labels: labels, db: lookupV.db}
optimized = append(optimized, hIdx)
Expand All @@ -342,17 +340,13 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor {
}
}
if idOpt {
for _, j := range hasIdIdx {
if i != j {
optimized = append(optimized, step)
}
if i != hasIDIdx[0] {
optimized = append(optimized, step)
}
}
if labelOpt {
for _, j := range hasLabelIdx {
if i != j {
optimized = append(optimized, step)
}
if i != hasLabelIdx[0] {
optimized = append(optimized, step)
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions engine/core/optimizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ func TestIndexStartOptimize(t *testing.T) {
t.Error("indexStartOptimize returned an unexpected result")
}

// only use the first statement
expected = []gdbi.Processor{
&LookupVerts{ids: []string{"1", "2", "3"}},
&Has{stmt: gripql.Within("_gid", "4", "5")},
&LookupVertexAdjOut{},
}
original = []gdbi.Processor{
&LookupVerts{},
&HasID{ids: []string{"1", "2", "3"}},
&Has{stmt: gripql.Within("_gid", "4", "5")},
&LookupVertexAdjOut{},
}
optimized = indexStartOptimize(original)
if !reflect.DeepEqual(optimized, expected) {
t.Log("actual", spew.Sdump(optimized))
t.Log("expected:", spew.Sdump(expected))
t.Error("indexStartOptimize returned an unexpected result")
}

expected = []gdbi.Processor{
&LookupVertsIndex{labels: []string{"foo", "bar"}},
&LookupVertexAdjOut{},
Expand Down Expand Up @@ -111,6 +130,25 @@ func TestIndexStartOptimize(t *testing.T) {
t.Error("indexStartOptimize returned an unexpected result")
}

expected = []gdbi.Processor{
&LookupVertsIndex{labels: []string{"foo", "bar"}},
&Has{stmt: gripql.Eq("_label", "baz")},
&LookupVertexAdjOut{},
}
original = []gdbi.Processor{
&LookupVerts{},
&HasLabel{labels: []string{"foo", "bar"}},
&Has{stmt: gripql.Eq("_label", "baz")},
&LookupVertexAdjOut{},
}
optimized = indexStartOptimize(original)
if !reflect.DeepEqual(optimized, expected) {
t.Log("actual", spew.Sdump(optimized))
t.Log("expected:", spew.Sdump(expected))
t.Error("indexStartOptimize returned an unexpected result")
}

// use gid over label to optimize queries
expected = []gdbi.Processor{
&LookupVerts{ids: []string{"1", "2", "3"}},
&Has{stmt: gripql.Eq("$.data.foo", "bar")},
Expand Down

0 comments on commit db8bd49

Please sign in to comment.