diff --git a/engine/core/compile.go b/engine/core/compile.go index b0ce4bcd..cdbe7f29 100644 --- a/engine/core/compile.go +++ b/engine/core/compile.go @@ -7,7 +7,7 @@ import ( "github.com/bmeg/grip/gripql" "github.com/bmeg/grip/jsonpath" "github.com/bmeg/grip/protoutil" - // log "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) // DefaultPipeline a set of runnable query operations @@ -260,7 +260,9 @@ func (comp DefaultCompiler) Compile(stmts []*gripql.GraphStatement) (gdbi.Pipeli } } + log.Infof("Before optimizer: %+v", procs) procs = indexStartOptimize(procs) + log.Infof("After optimizer: %+v", procs) return &DefaultPipeline{procs, lastType, markTypes}, nil } @@ -272,7 +274,11 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor { var lookupV *LookupVerts hasIDIdx := []int{} hasLabelIdx := []int{} + isDone := false for i, step := range pipe { + if isDone { + break + } if i == 0 { if lv, ok := step.(*LookupVerts); ok { lookupV = lv @@ -299,7 +305,7 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor { } } default: - break + isDone = true } } @@ -338,6 +344,8 @@ func indexStartOptimize(pipe []gdbi.Processor) []gdbi.Processor { if i == 0 { continue } + } else { + optimized = append(optimized, step) } if idOpt { if i != hasIDIdx[0] { diff --git a/engine/core/optimizer_test.go b/engine/core/optimizer_test.go index 9c0f8b43..3553d1e2 100644 --- a/engine/core/optimizer_test.go +++ b/engine/core/optimizer_test.go @@ -15,11 +15,43 @@ func TestIndexStartOptimize(t *testing.T) { &LookupVertexAdjOut{}, } original := []gdbi.Processor{ + &LookupVerts{ids: []string{"1", "2", "3"}}, + &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{ &LookupVerts{}, + &LookupVertexAdjOut{}, &HasID{ids: []string{"1", "2", "3"}}, + } + original = []gdbi.Processor{ + &LookupVerts{}, &LookupVertexAdjOut{}, + &HasID{ids: []string{"1", "2", "3"}}, } - optimized := indexStartOptimize(original) + 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{ + &LookupVerts{ids: []string{"1", "2", "3"}}, + &LookupVertexAdjOut{}, + } + original = []gdbi.Processor{ + &LookupVerts{}, + &HasID{ids: []string{"1", "2", "3"}}, + &LookupVertexAdjOut{}, + } + optimized = indexStartOptimize(original) if !reflect.DeepEqual(optimized, expected) { t.Log("actual", spew.Sdump(optimized)) t.Log("expected:", spew.Sdump(expected))