From b9c7dba92ebdb2cac684633e1b09d6369e770f14 Mon Sep 17 00:00:00 2001 From: Oleg Chernyakhovskiy Date: Thu, 18 Feb 2016 19:37:56 -0800 Subject: [PATCH] Don't decrement Checker counter if anchor no in bounding range --- storage/memory/memory.go | 9 ++--- storage/memory/memory_test.go | 72 ++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/storage/memory/memory.go b/storage/memory/memory.go index 7c8716fe..58634ed0 100644 --- a/storage/memory/memory.go +++ b/storage/memory/memory.go @@ -246,13 +246,11 @@ func newChecker(o *storage.LookupOptions) *checker { // CheckAndUpdate checks if a predicate should be considered and it also updates // the internal state in case counts are needed. func (c *checker) CheckAndUpdate(p *predicate.Predicate) bool { - if c.max { - if c.c <= 0 { - return false - } - c.c-- + if c.max && c.c <= 0 { + return false } if p.Type() == predicate.Immutable { + c.c-- return true } t, _ := p.TimeAnchor() @@ -262,6 +260,7 @@ func (c *checker) CheckAndUpdate(p *predicate.Predicate) bool { if c.o.UpperAnchor != nil && t.After(*c.o.UpperAnchor) { return false } + c.c-- return true } diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go index d7601ac7..648c0039 100644 --- a/storage/memory/memory_test.go +++ b/storage/memory/memory_test.go @@ -162,16 +162,8 @@ func TestTemporalBoundedLookupChecker(t *testing.T) { } } -func getTestTriples(t *testing.T) []*triple.Triple { +func createTriples(t *testing.T, ss []string) []*triple.Triple { ts := []*triple.Triple{} - ss := []string{ - "/u\t\"knows\"@[]\t/u", - "/u\t\"knows\"@[]\t/u", - "/u\t\"knows\"@[]\t/u", - "/u\t\"knows\"@[]\t/u", - "/u\t\"knows\"@[]\t/u", - "/u\t\"knows\"@[]\t/u", - } for _, s := range ss { trpl, err := triple.Parse(s, literal.DefaultBuilder()) if err != nil { @@ -183,6 +175,17 @@ func getTestTriples(t *testing.T) []*triple.Triple { return ts } +func getTestTriples(t *testing.T) []*triple.Triple { + return createTriples(t, []string{ + "/u\t\"knows\"@[]\t/u", + "/u\t\"knows\"@[]\t/u", + "/u\t\"knows\"@[]\t/u", + "/u\t\"knows\"@[]\t/u", + "/u\t\"knows\"@[]\t/u", + "/u\t\"knows\"@[]\t/u", + }) +} + func TestAddRemoveTriples(t *testing.T) { ts, ctx := getTestTriples(t), context.Background() g, _ := NewStore().NewGraph(ctx, "test") @@ -388,6 +391,57 @@ func TestTriplesForObject(t *testing.T) { } } +func mustParse(t string) *time.Time { + r, err := time.Parse(time.RFC3339Nano, t) + if err != nil { + panic(err) + } + return &r +} + +func TestTriplesForObjectWithLimit(t *testing.T) { + ts := createTriples(t, []string{ + "/u\t\"kissed\"@[2015-01-01T00:00:00-09:00]\t/u", + "/u\t\"kissed\"@[2015-02-01T00:00:00-09:00]\t/u", + "/u\t\"kissed\"@[2015-03-01T00:00:00-09:00]\t/u", + "/u\t\"kissed\"@[2015-04-01T00:00:00-09:00]\t/u", + "/u\t\"kissed\"@[2015-05-01T00:00:00-09:00]\t/u", + "/u\t\"kissed\"@[2015-06-01T00:00:00-09:00]\t/u", + }) + ctx := context.Background() + g, _ := NewStore().NewGraph(ctx, "test") + if err := g.AddTriples(ctx, ts); err != nil { + t.Errorf("g.AddTriples(_) failed failed to add test triples with error %v", err) + } + // To avoid blocking on the test. On a real usage of the driver you woul like + // to call the graph operation on a separated goroutine using a sync.WaitGroup + // to collect the error code eventualy. + trpls := make(chan *triple.Triple, 100) + lo := &storage.LookupOptions{ + MaxElements: 2, + LowerAnchor: mustParse("2015-04-01T00:00:00-08:00"), + UpperAnchor: mustParse("2015-06-01T00:00:00-10:00"), + } + if err := g.TriplesForObject(ctx, ts[0].Object(), lo, trpls); err != nil { + t.Errorf("g.TriplesForObject(%s) failed with error %v", ts[0].Object(), err) + } + cnt := 0 + for tr := range trpls { + ta, err := tr.Predicate().TimeAnchor() + if err != nil { + t.Error(err) + continue + } + if ta.Before(*lo.LowerAnchor) || ta.After(*lo.UpperAnchor) { + t.Errorf("g.TriplesForObject(%s) unexpected triple receved: %s", ts[0].Object(), tr) + } + cnt++ + } + if cnt != lo.MaxElements { + t.Errorf("g.TriplesForObject(%s) failed to retrieve 2 triples, got %d instead", ts[0].Object(), cnt) + } +} + func TestTriplesForSubjectAndPredicate(t *testing.T) { ts, ctx := getTestTriples(t), context.Background() g, _ := NewStore().NewGraph(ctx, "test")