Skip to content

Commit

Permalink
Merge pull request #37 from defrager/ItterateLocal
Browse files Browse the repository at this point in the history
Don't decrement Checker counter if anchor not in the bounding range
  • Loading branch information
xllora committed Feb 19, 2016
2 parents f93c5d0 + b9c7dba commit a25541a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
9 changes: 4 additions & 5 deletions storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
72 changes: 63 additions & 9 deletions storage/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<john>\t\"knows\"@[]\t/u<mary>",
"/u<john>\t\"knows\"@[]\t/u<peter>",
"/u<john>\t\"knows\"@[]\t/u<alice>",
"/u<mary>\t\"knows\"@[]\t/u<andrew>",
"/u<mary>\t\"knows\"@[]\t/u<kim>",
"/u<mary>\t\"knows\"@[]\t/u<alice>",
}
for _, s := range ss {
trpl, err := triple.Parse(s, literal.DefaultBuilder())
if err != nil {
Expand All @@ -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<john>\t\"knows\"@[]\t/u<mary>",
"/u<john>\t\"knows\"@[]\t/u<peter>",
"/u<john>\t\"knows\"@[]\t/u<alice>",
"/u<mary>\t\"knows\"@[]\t/u<andrew>",
"/u<mary>\t\"knows\"@[]\t/u<kim>",
"/u<mary>\t\"knows\"@[]\t/u<alice>",
})
}

func TestAddRemoveTriples(t *testing.T) {
ts, ctx := getTestTriples(t), context.Background()
g, _ := NewStore().NewGraph(ctx, "test")
Expand Down Expand Up @@ -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<bob>\t\"kissed\"@[2015-01-01T00:00:00-09:00]\t/u<mary>",
"/u<bob>\t\"kissed\"@[2015-02-01T00:00:00-09:00]\t/u<mary>",
"/u<bob>\t\"kissed\"@[2015-03-01T00:00:00-09:00]\t/u<mary>",
"/u<bob>\t\"kissed\"@[2015-04-01T00:00:00-09:00]\t/u<mary>",
"/u<bob>\t\"kissed\"@[2015-05-01T00:00:00-09:00]\t/u<mary>",
"/u<bob>\t\"kissed\"@[2015-06-01T00:00:00-09:00]\t/u<mary>",
})
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")
Expand Down

0 comments on commit a25541a

Please sign in to comment.