Replace DebugString with Describe
This change makes tree description completely open to mechanical analysis and ensures consistency between description formats for each of the iterator types. Renamed StatsContainer.(Kind -> Type) for consistency.
This commit is contained in:
parent
b5f113203d
commit
62013d3dfc
22 changed files with 186 additions and 162 deletions
|
|
@ -23,9 +23,6 @@ package iterator
|
|||
// the base iterators, and it helps just to see it here.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
|
|
@ -81,9 +78,12 @@ func (it *Int64) TagResults(dst map[string]graph.Value) {
|
|||
}
|
||||
}
|
||||
|
||||
// Prints the All iterator as just an "all".
|
||||
func (it *Int64) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s tags: %v)", strings.Repeat(" ", indent), it.Type(), it.tags.Tags())
|
||||
func (it *Int64) Describe() graph.Description {
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
}
|
||||
}
|
||||
|
||||
// Next() on an Int64 all iterator is a simple incrementing counter.
|
||||
|
|
|
|||
|
|
@ -16,9 +16,6 @@
|
|||
package iterator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
|
|
@ -111,30 +108,19 @@ func (it *And) ResultTree() *graph.ResultTree {
|
|||
return tree
|
||||
}
|
||||
|
||||
// Prints information about this iterator.
|
||||
func (it *And) DebugString(indent int) string {
|
||||
var total string
|
||||
func (it *And) Describe() graph.Description {
|
||||
subIts := make([]graph.Description, len(it.internalIterators))
|
||||
for i, sub := range it.internalIterators {
|
||||
total += strings.Repeat(" ", indent+2)
|
||||
total += fmt.Sprintf("%d:\n%s\n", i, sub.DebugString(indent+4))
|
||||
subIts[i] = sub.Describe()
|
||||
}
|
||||
var tags string
|
||||
for _, k := range it.tags.Tags() {
|
||||
tags += fmt.Sprintf("%s;", k)
|
||||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterator: &primary,
|
||||
Iterators: subIts,
|
||||
}
|
||||
spaces := strings.Repeat(" ", indent+2)
|
||||
|
||||
return fmt.Sprintf("%s(%s %d\n%stags:%s\n%sprimary_it:\n%s\n%sother_its:\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
it.UID(),
|
||||
spaces,
|
||||
tags,
|
||||
spaces,
|
||||
it.primaryIt.DebugString(indent+4),
|
||||
spaces,
|
||||
total,
|
||||
)
|
||||
}
|
||||
|
||||
// Add a subiterator to this And iterator.
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ package iterator
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sort"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
|
@ -94,20 +94,23 @@ func (it *Fixed) Add(v graph.Value) {
|
|||
it.values = append(it.values, v)
|
||||
}
|
||||
|
||||
// Print some information about the iterator.
|
||||
func (it *Fixed) DebugString(indent int) string {
|
||||
value := ""
|
||||
func (it *Fixed) Describe() graph.Description {
|
||||
var value string
|
||||
if len(it.values) > 0 {
|
||||
value = fmt.Sprint(it.values[0])
|
||||
}
|
||||
return fmt.Sprintf("%s(%s %d tags: %s Size: %d id0: %s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
it.UID(),
|
||||
it.tags.Fixed(),
|
||||
len(it.values),
|
||||
value,
|
||||
)
|
||||
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().String(),
|
||||
Tags: fixed,
|
||||
Size: int64(len(it.values)),
|
||||
}
|
||||
}
|
||||
|
||||
// Register this iterator as a Fixed iterator.
|
||||
|
|
|
|||
|
|
@ -34,9 +34,6 @@ package iterator
|
|||
// Alternatively, can be seen as the dual of the LinksTo iterator.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
|
|
@ -130,13 +127,15 @@ func (it *HasA) ResultTree() *graph.ResultTree {
|
|||
return tree
|
||||
}
|
||||
|
||||
// Print some information about this iterator.
|
||||
func (it *HasA) DebugString(indent int) string {
|
||||
var tags string
|
||||
for _, k := range it.tags.Tags() {
|
||||
tags += fmt.Sprintf("%s;", k)
|
||||
func (it *HasA) Describe() graph.Description {
|
||||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
Direction: it.dir,
|
||||
Iterator: &primary,
|
||||
}
|
||||
return fmt.Sprintf("%s(%s %d tags:%s direction:%s\n%s)", strings.Repeat(" ", indent), it.Type(), it.UID(), tags, it.dir, it.primaryIt.DebugString(indent+4))
|
||||
}
|
||||
|
||||
// Check a value against our internal iterator. In order to do this, we must first open a new
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ package iterator
|
|||
// Define the general iterator interface.
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
|
|
@ -78,9 +77,11 @@ func (it *Null) Type() graph.Type { return graph.Null }
|
|||
// Null has nothing it needs to do.
|
||||
func (it *Null) Optimize() (graph.Iterator, bool) { return it, false }
|
||||
|
||||
// Print the null iterator.
|
||||
func (it *Null) DebugString(indent int) string {
|
||||
return strings.Repeat(" ", indent) + "(null)"
|
||||
func (it *Null) Describe() graph.Description {
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Null) Next() bool {
|
||||
|
|
|
|||
|
|
@ -30,9 +30,6 @@ package iterator
|
|||
// Can be seen as the dual of the HasA iterator.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/quad"
|
||||
)
|
||||
|
|
@ -108,11 +105,14 @@ func (it *LinksTo) ResultTree() *graph.ResultTree {
|
|||
return tree
|
||||
}
|
||||
|
||||
// Print the iterator.
|
||||
func (it *LinksTo) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s %d direction:%s\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(), it.UID(), it.dir, it.primaryIt.DebugString(indent+4))
|
||||
func (it *LinksTo) Describe() graph.Description {
|
||||
primary := it.primaryIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Direction: it.dir,
|
||||
Iterator: &primary,
|
||||
}
|
||||
}
|
||||
|
||||
// If it checks in the right direction for the subiterator, it is a valid link
|
||||
|
|
|
|||
|
|
@ -17,9 +17,6 @@ package iterator
|
|||
// A simple iterator that, when first called Contains() or Next() upon, materializes the whole subiterator, stores it locally, and responds. Essentially a cache.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
|
|
@ -118,15 +115,15 @@ func (it *Materialize) Clone() graph.Iterator {
|
|||
return out
|
||||
}
|
||||
|
||||
// Print some information about the iterator.
|
||||
func (it *Materialize) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s tags: %s Size: %d\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
it.tags.Tags(),
|
||||
len(it.values),
|
||||
it.subIt.DebugString(indent+4),
|
||||
)
|
||||
func (it *Materialize) Describe() graph.Description {
|
||||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
Size: int64(len(it.values)),
|
||||
Iterator: &primary,
|
||||
}
|
||||
}
|
||||
|
||||
// Register this iterator as a Materialize iterator.
|
||||
|
|
|
|||
|
|
@ -27,9 +27,6 @@ package iterator
|
|||
// -- all things in the graph. It matches everything (as does the regex "(a)?")
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
|
|
@ -120,13 +117,14 @@ func (it *Optional) TagResults(dst map[string]graph.Value) {
|
|||
// Registers the optional iterator.
|
||||
func (it *Optional) Type() graph.Type { return graph.Optional }
|
||||
|
||||
// Prints the optional and it's subiterator.
|
||||
func (it *Optional) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s tags:%s\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
it.tags.Tags(),
|
||||
it.subIt.DebugString(indent+4))
|
||||
func (it *Optional) Describe() graph.Description {
|
||||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterator: &primary,
|
||||
}
|
||||
}
|
||||
|
||||
// There's nothing to optimize for an optional. Optimize the subiterator and
|
||||
|
|
|
|||
|
|
@ -22,9 +22,6 @@ package iterator
|
|||
// May return the same value twice -- once for each branch.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
|
|
@ -113,26 +110,17 @@ func (it *Or) ResultTree() *graph.ResultTree {
|
|||
return tree
|
||||
}
|
||||
|
||||
// Prints information about this graph.iterator.
|
||||
func (it *Or) DebugString(indent int) string {
|
||||
var total string
|
||||
func (it *Or) Describe() graph.Description {
|
||||
var subIts []graph.Description
|
||||
for i, sub := range it.internalIterators {
|
||||
total += strings.Repeat(" ", indent+2)
|
||||
total += fmt.Sprintf("%d:\n%s\n", i, sub.DebugString(indent+4))
|
||||
subIts[i] = sub.Describe()
|
||||
}
|
||||
var tags string
|
||||
for _, k := range it.tags.Tags() {
|
||||
tags += fmt.Sprintf("%s;", k)
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Tags: it.tags.Tags(),
|
||||
Iterators: subIts,
|
||||
}
|
||||
spaces := strings.Repeat(" ", indent+2)
|
||||
|
||||
return fmt.Sprintf("%s(%s\n%stags:%s\n%sits:\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
spaces,
|
||||
tags,
|
||||
spaces,
|
||||
total)
|
||||
}
|
||||
|
||||
// Add a subiterator to this Or graph.iterator. Order matters.
|
||||
|
|
|
|||
|
|
@ -27,10 +27,8 @@ package iterator
|
|||
// In MQL terms, this is the [{"age>=": 21}] concept.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
|
@ -190,11 +188,13 @@ func (it *Comparison) TagResults(dst map[string]graph.Value) {
|
|||
// Registers the value-comparison iterator.
|
||||
func (it *Comparison) Type() graph.Type { return graph.Comparison }
|
||||
|
||||
// Prints the value-comparison and its subiterator.
|
||||
func (it *Comparison) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(), it.subIt.DebugString(indent+4))
|
||||
func (it *Comparison) Describe() graph.Description {
|
||||
primary := it.subIt.Describe()
|
||||
return graph.Description{
|
||||
UID: it.UID(),
|
||||
Type: it.Type().String(),
|
||||
Iterator: &primary,
|
||||
}
|
||||
}
|
||||
|
||||
// There's nothing to optimize, locally, for a value-comparison iterator.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue