Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Resolver iterator #741

Merged
merged 2 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Alexander Peters <info@alexanderpeters.de>
Andrew Dunham <andrew@du.nham.ca>
Barak Michener <barakmich@google.com> <barak@cayley.io> <me@barakmich.com>
Bram Leenders <bcleenders@gmail.com>
Connor Newton <connor@ifthenelse.io>
Denys Smirnov <denis.smirnov.91@gmail.com>
Derek Liang <fr.derekliang@gmail.com>
Jay Graves <jaywgraves@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions graph/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ const (
Regex = Type("regexp")
Count = Type("count")
Recursive = Type("recursive")
Resolver = Type("resolver")
)

// String returns a string representation of the Type.
Expand Down
191 changes: 191 additions & 0 deletions graph/iterator/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright 2018 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package iterator

import (
"context"
"fmt"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
)

var _ graph.Iterator = &Resolver{}

// A Resolver iterator consists of it's order, an index (where it is in the,
// process of iterating) and a store to resolve values from.
type Resolver struct {
qs graph.QuadStore
uid uint64
tags graph.Tagger
order []quad.Value
values map[quad.Value]graph.Value
nodes map[interface{}]quad.Value
cached bool
index int
err error
result graph.Value
}

// Creates a new Resolver iterator.
func NewResolver(qs graph.QuadStore, nodes ...quad.Value) *Resolver {
it := &Resolver{
uid: NextUID(),
qs: qs,
order: make([]quad.Value, len(nodes)),
// Generally there are going to be no/few duplicates given
// so allocate maps large enough to accommodate all
values: make(map[quad.Value]graph.Value, len(nodes)),
nodes: make(map[interface{}]quad.Value, len(nodes)),
}
copy(it.order, nodes)
return it
}

func (it *Resolver) UID() uint64 {
return it.uid
}

func (it *Resolver) Reset() {
it.index = 0
it.err = nil
it.result = nil
}

func (it *Resolver) Close() error {
return nil
}

func (it *Resolver) Tagger() *graph.Tagger {
return &it.tags
}

func (it *Resolver) TagResults(dst map[string]graph.Value) {
it.tags.TagResult(dst, it.Result())
}

func (it *Resolver) Clone() graph.Iterator {
out := NewResolver(it.qs, it.order...)
// Nodes and values maps should contain identical data, so
// just iterate through one
for node, value := range it.values {
out.values[node] = value
out.nodes[value.Key()] = node
}
out.tags.CopyFrom(it)
return out
}

func (it *Resolver) String() string {
return fmt.Sprintf("Resolver(%v)", it.order)
}

// Register this iterator as a Resolver iterator.
func (it *Resolver) Type() graph.Type { return graph.Resolver }

// Resolve nodes to values
func (it *Resolver) resolve(ctx context.Context) error {
values, err := graph.RefsOf(ctx, it.qs, it.order)
if err != nil {
return err
}
for index, value := range values {
node := it.order[index]
it.values[node] = value
it.nodes[value.Key()] = node
}
it.cached = true
return nil
}

// Check if the passed value is equal to one of the order stored in the iterator.
func (it *Resolver) Contains(ctx context.Context, value graph.Value) bool {
graph.ContainsLogIn(it, value)
if !it.cached {
it.err = it.resolve(ctx)
if it.err != nil {
return false
}
}
_, ok := it.nodes[value.Key()]
return graph.ContainsLogOut(it, value, ok)
}

// Next advances the iterator.
func (it *Resolver) Next(ctx context.Context) bool {
graph.NextLogIn(it)
if it.index >= len(it.order) {
it.result = nil
return graph.NextLogOut(it, false)
}
if !it.cached {
it.err = it.resolve(ctx)
if it.err != nil {
return false
}
}
node := it.order[it.index]
value, ok := it.values[node]
if !ok {
it.result = nil
it.err = fmt.Errorf("not found: %v", node)
return graph.NextLogOut(it, false)
}
it.result = value
it.index++
return graph.NextLogOut(it, true)
}

func (it *Resolver) Err() error {
return it.err
}

func (it *Resolver) Result() graph.Value {
return it.result
}

func (it *Resolver) NextPath(ctx context.Context) bool {
return false
}

func (it *Resolver) SubIterators() []graph.Iterator {
return nil
}

// Returns a Null iterator if it's empty so that upstream iterators can optimize it
// away, otherwise there is no optimization.
func (it *Resolver) Optimize() (graph.Iterator, bool) {
if len(it.order) == 0 {
return NewNull(), true
}
return it, false
}

// Size is the number of m stored.
func (it *Resolver) Size() (int64, bool) {
return int64(len(it.order)), true
}

func (it *Resolver) Stats() graph.IteratorStats {
s, exact := it.Size()
return graph.IteratorStats{
// Lookup cost is size of set
ContainsCost: s,
// Next is (presumably) O(1) from store
NextCost: 1,
Size: s,
ExactSize: exact,
}
}
133 changes: 133 additions & 0 deletions graph/iterator/resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package iterator_test

import (
"testing"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running goimports over it will be nice (analog of gofmt)

"context"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/graph/graphmock"
)

func TestResolverIteratorIterate(t *testing.T) {
var ctx context.Context
nodes := []quad.Value{
quad.String("1"),
quad.String("2"),
quad.String("3"),
quad.String("4"),
quad.String("5"),
phyrwork marked this conversation as resolved.
Show resolved Hide resolved
quad.String("3"), // Assert iterator can handle duplicate values
}
data := make([]quad.Quad, 0, len(nodes))
for _, node := range nodes {
data = append(data, quad.Make(quad.String("0"), "has", node, nil))
}
qs := &graphmock.Store{
Data: data,
}
expected := make(map[quad.Value]graph.Value)
for _, node := range nodes {
expected[node] = qs.ValueOf(node)
}
it := iterator.NewResolver(qs, nodes...)
for _, node := range nodes {
if it.Next(ctx) != true {
t.Fatal("unexpected end of iterator")
}
if err := it.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if value := it.Result(); value != expected[node] {
t.Fatalf("unexpected quad value: expected %v, got %v", expected[node], value)
}
}
if it.Next(ctx) != false {
t.Fatal("expected end of iterator")
}
if it.Result() != nil {
t.Fatal("expected nil result")
}
}

func TestResolverIteratorNotFoundError(t *testing.T) {
var ctx context.Context
nodes := []quad.Value{
quad.String("1"),
quad.String("2"),
quad.String("3"),
quad.String("4"),
quad.String("5"),
}
data := make([]quad.Quad, 0)
skip := 3
for i, node := range nodes {
// Simulate a missing subject
if i == skip {
continue
}
data = append(data, quad.Make(quad.String("0"), "has", node, nil))
}
qs := &graphmock.Store{
Data: data,
}
count := 0
it := iterator.NewResolver(qs, nodes...)
for it.Next(ctx) {
count++
}
if count != 0 {
t.Fatal("expected end of iterator")
}
if it.Err() == nil {
t.Fatal("expected not found error")
}
if it.Result() != nil {
t.Fatal("expected nil result")
}
}

func TestResolverIteratorContains(t *testing.T) {
tests := []struct {
name string
nodes []quad.Value
subject quad.Value
contains bool
}{
{
"contains",
[]quad.Value{
quad.String("1"),
quad.String("2"),
quad.String("3"),
},
quad.String("2"),
true,
},
{
"not contains",
[]quad.Value{
quad.String("1"),
quad.String("3"),
},
quad.String("2"),
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var ctx context.Context
data := make([]quad.Quad, 0, len(test.nodes))
for _, node := range test.nodes {
data = append(data, quad.Make(quad.String("0"), "has", node, nil))
}
qs := &graphmock.Store{
Data: data,
}
it := iterator.NewResolver(qs, test.nodes...)
if it.Contains(ctx, graph.PreFetched(test.subject)) != test.contains {
t.Fatal("unexpected result")
}
})
}
}