diff --git a/graph/iterator/not_iterator.go b/graph/iterator/not_iterator.go index f6e7eec..32d8957 100644 --- a/graph/iterator/not_iterator.go +++ b/graph/iterator/not_iterator.go @@ -154,15 +154,12 @@ func (it *Not) Optimize() (graph.Iterator, bool) { } func (it *Not) Stats() graph.IteratorStats { - subitStats := it.primaryIt.Stats() - // TODO(barakmich): These should really come from the triplestore itself - fanoutFactor := int64(20) - checkConstant := int64(1) - nextConstant := int64(2) + primaryStats := it.primaryIt.Stats() + allStats := it.allIt.Stats() return graph.IteratorStats{ - NextCost: nextConstant + subitStats.NextCost, - ContainsCost: checkConstant + subitStats.ContainsCost, - Size: fanoutFactor * subitStats.Size, + NextCost: allStats.NextCost + primaryStats.ContainsCost, + ContainsCost: primaryStats.ContainsCost, + Size: allStats.Size - primaryStats.Size, Next: it.runstats.Next, Contains: it.runstats.Contains, ContainsNext: it.runstats.ContainsNext, diff --git a/graph/iterator/not_iterator_test.go b/graph/iterator/not_iterator_test.go new file mode 100644 index 0000000..3414ca0 --- /dev/null +++ b/graph/iterator/not_iterator_test.go @@ -0,0 +1,44 @@ +package iterator + +import ( + "reflect" + "testing" +) + +func TestNotIteratorBasics(t *testing.T) { + allIt := newFixed() + allIt.Add(1) + allIt.Add(2) + allIt.Add(3) + allIt.Add(4) + + toComplementIt := newFixed() + toComplementIt.Add(2) + toComplementIt.Add(4) + + not := NewNot(toComplementIt, allIt) + + if v, _ := not.Size(); v != 2 { + t.Errorf("Unexpected iterator size: got:%d, expected: %d", v, 2) + } + + expect := []int{1, 3} + for i := 0; i < 2; i++ { + if got := iterated(not); !reflect.DeepEqual(got, expect) { + t.Errorf("Failed to iterate Not correctly on repeat %d: got:%v expected:%v", i, got, expect) + } + not.Reset() + } + + for _, v := range []int{1, 3} { + if !not.Contains(v) { + t.Errorf("Failed to correctly check %d as true", v) + } + } + + for _, v := range []int{2, 4} { + if not.Contains(v) { + t.Errorf("Failed to correctly check %d as false", v) + } + } +}