Skip to content

Commit

Permalink
iterator: replace Describe method with a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Dec 16, 2017
1 parent 633828c commit b491f41
Show file tree
Hide file tree
Showing 35 changed files with 153 additions and 360 deletions.
15 changes: 4 additions & 11 deletions graph/bolt/all_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/cayleygraph/cayley/quad"
)

var _ graph.Iterator = &AllIterator{}

type AllIterator struct {
nodes bool
uid uint64
Expand Down Expand Up @@ -176,15 +178,8 @@ func (it *AllIterator) Size() (int64, bool) {
return it.qs.Size(), true
}

func (it *AllIterator) Describe() graph.Description {
size, _ := it.Size()
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.tags.Tags(),
Size: size,
Direction: it.dir,
}
func (it *AllIterator) String() string {
return fmt.Sprintf("BoltAll(%q)", it.bucket)
}

func (it *AllIterator) Type() graph.Type { return graph.All }
Expand All @@ -203,5 +198,3 @@ func (it *AllIterator) Stats() graph.IteratorStats {
ExactSize: exact,
}
}

var _ graph.Iterator = &AllIterator{}
20 changes: 4 additions & 16 deletions graph/bolt/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/cayleygraph/cayley/quad"
)

var _ graph.Iterator = &Iterator{}

var (
bufferSize = 50
errNotExist = errors.New("quad does not exist")
Expand Down Expand Up @@ -269,20 +271,8 @@ func (it *Iterator) Size() (int64, bool) {
return it.size, true
}

func (it *Iterator) Describe() graph.Description {
nameOf := it.qs.NameOf(&Token{
nodes: true,
bucket: it.bucket,
key: it.checkID,
})
return graph.Description{
UID: it.UID(),
Name: quad.StringOf(nameOf),
Type: it.Type(),
Tags: it.tags.Tags(),
Size: it.size,
Direction: it.dir,
}
func (it *Iterator) String() string {
return fmt.Sprintf("BoltIter(%q)", it.bucket)
}

func (it *Iterator) Type() graph.Type { return "bolt" }
Expand All @@ -301,5 +291,3 @@ func (it *Iterator) Stats() graph.IteratorStats {
ExactSize: exact,
}
}

var _ graph.Iterator = &Iterator{}
14 changes: 4 additions & 10 deletions graph/elastic/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package elastic

import (
"context"
"fmt"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/quad"
elastic "gopkg.in/olivere/elastic.v5"
"gopkg.in/olivere/elastic.v5"
)

// Iterator struct used for elastic backend
Expand Down Expand Up @@ -232,15 +233,8 @@ func (it *Iterator) SubIterators() []graph.Iterator {
return nil
}

// Describe gives the graph description
func (it *Iterator) Describe() graph.Description {
size, _ := it.Size()
return graph.Description{
UID: it.UID(),
Name: string(it.hash),
Type: it.Type(),
Size: size,
}
func (it *Iterator) String() string {
return fmt.Sprintf("Elastic(%v)", it.resultType)
}

// Close closes the iterator
Expand Down
12 changes: 2 additions & 10 deletions graph/gaedatastore/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,8 @@ func (it *Iterator) Type() graph.Type {
}
func (it *Iterator) Sorted() bool { return false }
func (it *Iterator) Optimize() (graph.Iterator, bool) { return it, false }
func (it *Iterator) Describe() graph.Description {
size, _ := it.Size()
return graph.Description{
UID: it.UID(),
Name: fmt.Sprintf("%s/%s", it.name, it.hash),
Type: it.Type(),
Size: size,
Tags: it.tags.Tags(),
Direction: it.dir,
}
func (it *Iterator) String() string {
return fmt.Sprintf("GAE(%s/%s)", it.name, it.hash)
}

// TODO (panamafrancis) calculate costs
Expand Down
4 changes: 2 additions & 2 deletions graph/gaedatastore/quadstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func TestIterators(t *testing.T) {
// Test cloning an iterator
var it2 graph.Iterator
it2 = it.Clone()
x := it2.Describe()
y := it.Describe()
x := graph.DescribeIterator(it2)
y := graph.DescribeIterator(it)

require.Equal(t, y.Name, x.Name, "Iterator Clone was not successful")
}
2 changes: 1 addition & 1 deletion graph/iterate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *IterateChain) start() {
if !clog.V(2) {
return
}
if b, err := json.MarshalIndent(c.it.Describe(), "", " "); err != nil {
if b, err := json.MarshalIndent(DescribeIterator(c.it), "", " "); err != nil {
clog.Infof("failed to format description: %v", err)
} else {
clog.Infof("%s", b)
Expand Down
39 changes: 28 additions & 11 deletions graph/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (t *Tagger) CopyFromTagger(st *Tagger) {
}

type Iterator interface {
// String returns a short textual representation of an iterator.
String() string

Tagger() *Tagger

// Fills a tag-to-result-value map.
Expand Down Expand Up @@ -162,25 +165,39 @@ type Iterator interface {
// Return a slice of the subiterators for this iterator.
SubIterators() []Iterator

// Return a string representation of the iterator.
Describe() Description

// Close the iterator and do internal cleanup.
Close() error

// UID returns the unique identifier of the iterator.
UID() uint64
}

// DescribeIterator returns a description of the iterator tree.
func DescribeIterator(it Iterator) Description {
sz, _ := it.Size()
d := Description{
UID: it.UID(),
Name: it.String(),
Type: it.Type(),
Tags: it.Tagger().Tags(),
Size: sz,
}
if sub := it.SubIterators(); len(sub) != 0 {
d.Iterators = make([]Description, 0, len(sub))
for _, sit := range sub {
d.Iterators = append(d.Iterators, DescribeIterator(sit))
}
}
return d
}

type Description struct {
UID uint64 `json:",omitempty"`
Name string `json:",omitempty"`
Type Type `json:",omitempty"`
Tags []string `json:",omitempty"`
Size int64 `json:",omitempty"`
Direction quad.Direction `json:",omitempty"`
Iterator *Description `json:",omitempty"`
Iterators []Description `json:",omitempty"`
UID uint64 `json:",omitempty"`
Name string `json:",omitempty"`
Type Type `json:",omitempty"`
Tags []string `json:",omitempty"`
Size int64 `json:",omitempty"`
Iterators []Description `json:",omitempty"`
}

// ApplyMorphism is a curried function that can generates a new iterator based on some prior iterator.
Expand Down
17 changes: 7 additions & 10 deletions graph/iterator/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ package iterator
// the base iterators, and it helps just to see it here.

import (
"fmt"
"github.com/cayleygraph/cayley/graph"
)

var _ graph.Iterator = &Int64{}

// An All iterator across a range of int64 values, from `max` to `min`.
type Int64 struct {
node bool
Expand Down Expand Up @@ -88,12 +91,8 @@ func (it *Int64) TagResults(dst map[string]graph.Value) {
it.tags.TagResult(dst, it.Result())
}

func (it *Int64) Describe() graph.Description {
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.tags.Tags(),
}
func (it *Int64) String() string {
return fmt.Sprintf("Int64(%d-%d)", it.min, it.max)
}

// Next() on an Int64 all iterator is a simple incrementing counter.
Expand Down Expand Up @@ -140,8 +139,8 @@ func (it *Int64) SubIterators() []graph.Iterator {
// The number of elements in an Int64 is the size of the range.
// The size is exact.
func (it *Int64) Size() (int64, bool) {
Size := ((it.max - it.min) + 1)
return Size, true
sz := (it.max - it.min) + 1
return sz, true
}

func valToInt64(v graph.Value) int64 {
Expand Down Expand Up @@ -184,5 +183,3 @@ func (it *Int64) Stats() graph.IteratorStats {
Contains: it.runstats.Contains,
}
}

var _ graph.Iterator = &Int64{}
19 changes: 4 additions & 15 deletions graph/iterator/and.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/cayleygraph/cayley/graph"
)

var _ graph.Iterator = &And{}

// The And iterator. Consists of a number of subiterators, the primary of which will
// be Next()ed if next is called.
type And struct {
Expand Down Expand Up @@ -100,19 +102,8 @@ func (it *And) SubIterators() []graph.Iterator {
return iters
}

func (it *And) Describe() graph.Description {
subIts := make([]graph.Description, len(it.internalIterators))
for i, sub := range it.internalIterators {
subIts[i] = sub.Describe()
}
primary := it.primaryIt.Describe()
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.tags.Tags(),
Iterator: &primary,
Iterators: subIts,
}
func (it *And) String() string {
return "And"
}

// Add a subiterator to this And iterator.
Expand Down Expand Up @@ -306,5 +297,3 @@ func (it *And) Close() error {

// Register this as an "and" iterator.
func (it *And) Type() graph.Type { return graph.And }

var _ graph.Iterator = &And{}
16 changes: 3 additions & 13 deletions graph/iterator/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/cayleygraph/cayley/quad"
)

var _ graph.Iterator = &Count{}

// Count iterator returns one element with size of underlying iterator.
type Count struct {
uid uint64
Expand Down Expand Up @@ -128,16 +130,4 @@ func (it *Count) Size() (int64, bool) {
return 1, true
}

func (it *Count) Describe() graph.Description {
subIts := []graph.Description{
it.it.Describe(),
}
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.Tagger().Tags(),
Iterators: subIts,
}
}

var _ graph.Iterator = &Count{}
func (it *Count) String() string { return "Count" }
24 changes: 4 additions & 20 deletions graph/iterator/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ package iterator

import (
"fmt"
"sort"

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

var _ graph.Iterator = &Fixed{}

// A Fixed iterator consists of it's values, an index (where it is in the process of Next()ing) and
// an equality function.
type Fixed struct {
Expand Down Expand Up @@ -93,23 +94,8 @@ func (it *Fixed) Add(v graph.Value) {
it.values = append(it.values, v)
}

func (it *Fixed) Describe() graph.Description {
var value string
if len(it.values) > 0 {
value = fmt.Sprint(it.values[0])
}
fixed := make([]string, 0, len(it.tags.Fixed()))
for k := range it.tags.Fixed() {
fixed = append(fixed, k)
}
sort.Strings(fixed)
return graph.Description{
UID: it.UID(),
Name: value,
Type: it.Type(),
Tags: fixed,
Size: int64(len(it.values)),
}
func (it *Fixed) String() string {
return fmt.Sprintf("Fixed(%v)", it.values)
}

// Register this iterator as a Fixed iterator.
Expand Down Expand Up @@ -186,5 +172,3 @@ func (it *Fixed) Stats() graph.IteratorStats {
ExactSize: exact,
}
}

var _ graph.Iterator = &Fixed{}
16 changes: 5 additions & 11 deletions graph/iterator/hasa.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ package iterator
import (
"github.com/cayleygraph/cayley/clog"

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

var _ graph.Iterator = &HasA{}

// A HasA consists of a reference back to the graph.QuadStore that it references,
// a primary subiterator, a direction in which the quads for that subiterator point,
// and a temporary holder for the iterator generated on Contains().
Expand Down Expand Up @@ -123,15 +126,8 @@ func (it *HasA) TagResults(dst map[string]graph.Value) {
it.primaryIt.TagResults(dst)
}

func (it *HasA) Describe() graph.Description {
primary := it.primaryIt.Describe()
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.tags.Tags(),
Direction: it.dir,
Iterator: &primary,
}
func (it *HasA) String() string {
return fmt.Sprintf("HasA(%v)", it.dir)
}

// Check a value against our internal iterator. In order to do this, we must first open a new
Expand Down Expand Up @@ -281,5 +277,3 @@ func (it *HasA) Size() (int64, bool) {
st := it.Stats()
return st.Size, st.ExactSize
}

var _ graph.Iterator = &HasA{}
Loading

0 comments on commit b491f41

Please sign in to comment.