Merge pull request #46 from kortschak/reduction

Reduce the complexity of names and types in graph.{Iterator,TripleStore}
This commit is contained in:
Barak Michener 2014-07-02 13:34:28 -04:00
commit 5b207947ce
44 changed files with 442 additions and 423 deletions

View file

@ -32,15 +32,18 @@ type Iterator interface {
// Tag Accessors. // Tag Accessors.
AddTag(string) AddTag(string)
Tags() []string Tags() []string
AddFixedTag(string, TSVal) AddFixedTag(string, Value)
FixedTags() map[string]TSVal FixedTags() map[string]Value
CopyTagsFrom(Iterator) CopyTagsFrom(Iterator)
// Fills a tag-to-result-value map. // Fills a tag-to-result-value map.
TagResults(*map[string]TSVal) TagResults(map[string]Value)
// Returns the current result. // Returns the current result.
LastResult() TSVal Result() Value
// DEPRECATED -- Fills a ResultTree struct with Result(). // DEPRECATED -- Fills a ResultTree struct with Result().
GetResultTree() *ResultTree ResultTree() *ResultTree
// These methods are the heart and soul of the iterator, as they constitute // These methods are the heart and soul of the iterator, as they constitute
// the iteration interface. // the iteration interface.
@ -56,54 +59,65 @@ type Iterator interface {
// //
// Next() advances the iterator and returns the next valid result. Returns // Next() advances the iterator and returns the next valid result. Returns
// (<value>, true) or (nil, false) // (<value>, true) or (nil, false)
Next() (TSVal, bool) Next() (Value, bool)
// NextResult() advances iterators that may have more than one valid result, // NextResult() advances iterators that may have more than one valid result,
// from the bottom up. // from the bottom up.
NextResult() bool NextResult() bool
// Return whether this iterator is reliably nextable. Most iterators are.
// However, some iterators, like "not" are, by definition, the whole database
// except themselves. Next() on these is unproductive, if impossible.
CanNext() bool
// Check(), given a value, returns whether or not that value is within the set // Check(), given a value, returns whether or not that value is within the set
// held by this iterator. // held by this iterator.
Check(TSVal) bool Check(Value) bool
// Start iteration from the beginning // Start iteration from the beginning
Reset() Reset()
// Create a new iterator just like this one // Create a new iterator just like this one
Clone() Iterator Clone() Iterator
// These methods relate to choosing the right iterator, or optimizing an // These methods relate to choosing the right iterator, or optimizing an
// iterator tree // iterator tree
// //
// GetStats() returns the relative costs of calling the iteration methods for // Stats() returns the relative costs of calling the iteration methods for
// this iterator, as well as the size. Roughly, it will take NextCost * Size // this iterator, as well as the size. Roughly, it will take NextCost * Size
// "cost units" to get everything out of the iterator. This is a wibbly-wobbly // "cost units" to get everything out of the iterator. This is a wibbly-wobbly
// thing, and not exact, but a useful heuristic. // thing, and not exact, but a useful heuristic.
GetStats() *IteratorStats Stats() IteratorStats
// Helpful accessor for the number of things in the iterator. The first return // Helpful accessor for the number of things in the iterator. The first return
// value is the size, and the second return value is whether that number is exact, // value is the size, and the second return value is whether that number is exact,
// or a conservative estimate. // or a conservative estimate.
Size() (int64, bool) Size() (int64, bool)
// Returns a string relating to what the function of the iterator is. By // Returns a string relating to what the function of the iterator is. By
// knowing the names of the iterators, we can devise optimization strategies. // knowing the names of the iterators, we can devise optimization strategies.
Type() string Type() string
// Optimizes an iterator. Can replace the iterator, or merely move things // Optimizes an iterator. Can replace the iterator, or merely move things
// around internally. if it chooses to replace it with a better iterator, // around internally. if it chooses to replace it with a better iterator,
// returns (the new iterator, true), if not, it returns (self, false). // returns (the new iterator, true), if not, it returns (self, false).
Optimize() (Iterator, bool) Optimize() (Iterator, bool)
// Return a slice of the subiterators for this iterator. // Return a slice of the subiterators for this iterator.
GetSubIterators() []Iterator SubIterators() []Iterator
// Return a string representation of the iterator, indented by the given amount. // Return a string representation of the iterator, indented by the given amount.
DebugString(int) string DebugString(int) string
// Return whether this iterator is relaiably nextable. Most iterators are.
// However, some iterators, like "not" are, by definition, the whole database
// except themselves. Next() on these is unproductive, if impossible.
Nextable() bool
// Close the iterator and do internal cleanup. // Close the iterator and do internal cleanup.
Close() Close()
GetUid() int
UID() uintptr
} }
type FixedIterator interface { type FixedIterator interface {
Iterator Iterator
AddValue(TSVal) Add(Value)
} }
type IteratorStats struct { type IteratorStats struct {
@ -114,18 +128,18 @@ type IteratorStats struct {
// Utility logging functions for when an iterator gets called Next upon, or Check upon, as // Utility logging functions for when an iterator gets called Next upon, or Check upon, as
// well as what they return. Highly useful for tracing the execution path of a query. // well as what they return. Highly useful for tracing the execution path of a query.
func CheckLogIn(it Iterator, val TSVal) { func CheckLogIn(it Iterator, val Value) {
if glog.V(4) { if glog.V(4) {
glog.V(4).Infof("%s %d CHECK %d", strings.ToUpper(it.Type()), it.GetUid(), val) glog.V(4).Infof("%s %d CHECK %d", strings.ToUpper(it.Type()), it.UID(), val)
} }
} }
func CheckLogOut(it Iterator, val TSVal, good bool) bool { func CheckLogOut(it Iterator, val Value, good bool) bool {
if glog.V(4) { if glog.V(4) {
if good { if good {
glog.V(4).Infof("%s %d CHECK %d GOOD", strings.ToUpper(it.Type()), it.GetUid(), val) glog.V(4).Infof("%s %d CHECK %d GOOD", strings.ToUpper(it.Type()), it.UID(), val)
} else { } else {
glog.V(4).Infof("%s %d CHECK %d BAD", strings.ToUpper(it.Type()), it.GetUid(), val) glog.V(4).Infof("%s %d CHECK %d BAD", strings.ToUpper(it.Type()), it.UID(), val)
} }
} }
return good return good
@ -133,16 +147,16 @@ func CheckLogOut(it Iterator, val TSVal, good bool) bool {
func NextLogIn(it Iterator) { func NextLogIn(it Iterator) {
if glog.V(4) { if glog.V(4) {
glog.V(4).Infof("%s %d NEXT", strings.ToUpper(it.Type()), it.GetUid()) glog.V(4).Infof("%s %d NEXT", strings.ToUpper(it.Type()), it.UID())
} }
} }
func NextLogOut(it Iterator, val TSVal, ok bool) (TSVal, bool) { func NextLogOut(it Iterator, val Value, ok bool) (Value, bool) {
if glog.V(4) { if glog.V(4) {
if ok { if ok {
glog.V(4).Infof("%s %d NEXT IS %d", strings.ToUpper(it.Type()), it.GetUid(), val) glog.V(4).Infof("%s %d NEXT IS %d", strings.ToUpper(it.Type()), it.UID(), val)
} else { } else {
glog.V(4).Infof("%s %d NEXT DONE", strings.ToUpper(it.Type()), it.GetUid()) glog.V(4).Infof("%s %d NEXT DONE", strings.ToUpper(it.Type()), it.UID())
} }
} }
return val, ok return val, ok

View file

@ -66,7 +66,7 @@ func (it *Int64) DebugString(indent int) string {
// Next() on an Int64 all iterator is a simple incrementing counter. // Next() on an Int64 all iterator is a simple incrementing counter.
// Return the next integer, and mark it as the result. // Return the next integer, and mark it as the result.
func (it *Int64) Next() (graph.TSVal, bool) { func (it *Int64) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
if it.at == -1 { if it.at == -1 {
return graph.NextLogOut(it, nil, false) return graph.NextLogOut(it, nil, false)
@ -89,7 +89,7 @@ func (it *Int64) Size() (int64, bool) {
// Check() for an Int64 is merely seeing if the passed value is // Check() for an Int64 is merely seeing if the passed value is
// withing the range, assuming the value is an int64. // withing the range, assuming the value is an int64.
func (it *Int64) Check(tsv graph.TSVal) bool { func (it *Int64) Check(tsv graph.Value) bool {
graph.CheckLogIn(it, tsv) graph.CheckLogIn(it, tsv)
v := tsv.(int64) v := tsv.(int64)
if it.min <= v && v <= it.max { if it.min <= v && v <= it.max {
@ -108,9 +108,9 @@ func (it *Int64) Optimize() (graph.Iterator, bool) { return it, false }
// Stats for an Int64 are simple. Super cheap to do any operation, // Stats for an Int64 are simple. Super cheap to do any operation,
// and as big as the range. // and as big as the range.
func (it *Int64) GetStats() *graph.IteratorStats { func (it *Int64) Stats() graph.IteratorStats {
s, _ := it.Size() s, _ := it.Size()
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 1, NextCost: 1,
Size: s, Size: s,

View file

@ -64,7 +64,7 @@ func (it *And) Clone() graph.Iterator {
} }
// Returns a slice of the subiterators, in order (primary iterator first). // Returns a slice of the subiterators, in order (primary iterator first).
func (it *And) GetSubIterators() []graph.Iterator { func (it *And) SubIterators() []graph.Iterator {
iters := make([]graph.Iterator, len(it.internalIterators)+1) iters := make([]graph.Iterator, len(it.internalIterators)+1)
iters[0] = it.primaryIt iters[0] = it.primaryIt
copy(iters[1:], it.internalIterators) copy(iters[1:], it.internalIterators)
@ -73,22 +73,22 @@ func (it *And) GetSubIterators() []graph.Iterator {
// Overrides Base TagResults, as it needs to add it's own results and // Overrides Base TagResults, as it needs to add it's own results and
// recurse down it's subiterators. // recurse down it's subiterators.
func (it *And) TagResults(out *map[string]graph.TSVal) { func (it *And) TagResults(dst map[string]graph.Value) {
it.Base.TagResults(out) it.Base.TagResults(dst)
if it.primaryIt != nil { if it.primaryIt != nil {
it.primaryIt.TagResults(out) it.primaryIt.TagResults(dst)
} }
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
sub.TagResults(out) sub.TagResults(dst)
} }
} }
// DEPRECATED Returns the ResultTree for this iterator, recurses to it's subiterators. // DEPRECATED Returns the ResultTree for this iterator, recurses to it's subiterators.
func (it *And) GetResultTree() *graph.ResultTree { func (it *And) ResultTree() *graph.ResultTree {
tree := graph.NewResultTree(it.LastResult()) tree := graph.NewResultTree(it.Result())
tree.AddSubtree(it.primaryIt.GetResultTree()) tree.AddSubtree(it.primaryIt.ResultTree())
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
tree.AddSubtree(sub.GetResultTree()) tree.AddSubtree(sub.ResultTree())
} }
return tree return tree
} }
@ -109,13 +109,14 @@ func (it *And) DebugString(indent int) string {
return fmt.Sprintf("%s(%s %d\n%stags:%s\n%sprimary_it:\n%s\n%sother_its:\n%s)", return fmt.Sprintf("%s(%s %d\n%stags:%s\n%sprimary_it:\n%s\n%sother_its:\n%s)",
strings.Repeat(" ", indent), strings.Repeat(" ", indent),
it.Type(), it.Type(),
it.GetUid(), it.UID(),
spaces, spaces,
tags, tags,
spaces, spaces,
it.primaryIt.DebugString(indent+4), it.primaryIt.DebugString(indent+4),
spaces, spaces,
total) total,
)
} }
// Add a subiterator to this And iterator. // Add a subiterator to this And iterator.
@ -138,9 +139,9 @@ func (it *And) AddSubIterator(sub graph.Iterator) {
// intersection of its subiterators, it must choose one subiterator to produce a // intersection of its subiterators, it must choose one subiterator to produce a
// candidate, and check this value against the subiterators. A productive choice // candidate, and check this value against the subiterators. A productive choice
// of primary iterator is therefore very important. // of primary iterator is therefore very important.
func (it *And) Next() (graph.TSVal, bool) { func (it *And) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
var curr graph.TSVal var curr graph.Value
var exists bool var exists bool
for { for {
curr, exists = it.primaryIt.Next() curr, exists = it.primaryIt.Next()
@ -156,7 +157,7 @@ func (it *And) Next() (graph.TSVal, bool) {
} }
// Checks a value against the non-primary iterators, in order. // Checks a value against the non-primary iterators, in order.
func (it *And) checkSubIts(val graph.TSVal) bool { func (it *And) checkSubIts(val graph.Value) bool {
var subIsGood = true var subIsGood = true
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
subIsGood = sub.Check(val) subIsGood = sub.Check(val)
@ -167,7 +168,7 @@ func (it *And) checkSubIts(val graph.TSVal) bool {
return subIsGood return subIsGood
} }
func (it *And) checkCheckList(val graph.TSVal) bool { func (it *And) checkCheckList(val graph.Value) bool {
ok := true ok := true
for _, c := range it.checkList { for _, c := range it.checkList {
ok = c.Check(val) ok = c.Check(val)
@ -182,7 +183,7 @@ func (it *And) checkCheckList(val graph.TSVal) bool {
} }
// Check a value against the entire iterator, in order. // Check a value against the entire iterator, in order.
func (it *And) Check(val graph.TSVal) bool { func (it *And) Check(val graph.Value) bool {
graph.CheckLogIn(it, val) graph.CheckLogIn(it, val)
if it.checkList != nil { if it.checkList != nil {
return it.checkCheckList(val) return it.checkCheckList(val)

View file

@ -42,7 +42,7 @@ import (
func (it *And) Optimize() (graph.Iterator, bool) { func (it *And) Optimize() (graph.Iterator, bool) {
// First, let's get the slice of iterators, in order (first one is Next()ed, // First, let's get the slice of iterators, in order (first one is Next()ed,
// the rest are Check()ed) // the rest are Check()ed)
old := it.GetSubIterators() old := it.SubIterators()
// And call Optimize() on our subtree, replacing each one in the order we // And call Optimize() on our subtree, replacing each one in the order we
// found them. it_list is the newly optimized versions of these, and changed // found them. it_list is the newly optimized versions of these, and changed
@ -145,20 +145,20 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
// all of it's contents, and to Check() each of those against everyone // all of it's contents, and to Check() each of those against everyone
// else. // else.
for _, it := range its { for _, it := range its {
if !it.Nextable() { if !it.CanNext() {
bad = append(bad, it) bad = append(bad, it)
continue continue
} }
rootStats := it.GetStats() rootStats := it.Stats()
cost := rootStats.NextCost cost := rootStats.NextCost
for _, f := range its { for _, f := range its {
if !f.Nextable() { if !f.CanNext() {
continue continue
} }
if f == it { if f == it {
continue continue
} }
stats := f.GetStats() stats := f.Stats()
cost += stats.CheckCost cost += stats.CheckCost
} }
cost *= rootStats.Size cost *= rootStats.Size
@ -177,7 +177,7 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
// ... push everyone else after... // ... push everyone else after...
for _, it := range its { for _, it := range its {
if !it.Nextable() { if !it.CanNext() {
continue continue
} }
if it != best { if it != best {
@ -192,7 +192,7 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
type byCost []graph.Iterator type byCost []graph.Iterator
func (c byCost) Len() int { return len(c) } func (c byCost) Len() int { return len(c) }
func (c byCost) Less(i, j int) bool { return c[i].GetStats().CheckCost < c[j].GetStats().CheckCost } func (c byCost) Less(i, j int) bool { return c[i].Stats().CheckCost < c[j].Stats().CheckCost }
func (c byCost) Swap(i, j int) { c[i], c[j] = c[j], c[i] } func (c byCost) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// optimizeCheck(l) creates an alternate check list, containing the same contents // optimizeCheck(l) creates an alternate check list, containing the same contents
@ -202,7 +202,7 @@ func (it *And) optimizeCheck() {
// TODO(kortschak) Reuse it.checkList if possible. // TODO(kortschak) Reuse it.checkList if possible.
// This involves providing GetSubIterators with a slice to fill. // This involves providing GetSubIterators with a slice to fill.
// Generally this is a worthwhile thing to do in other places as well. // Generally this is a worthwhile thing to do in other places as well.
it.checkList = it.GetSubIterators() it.checkList = it.SubIterators()
sort.Sort(byCost(it.checkList)) sort.Sort(byCost(it.checkList))
} }
@ -212,7 +212,7 @@ func (it *And) optimizeCheck() {
// getSubTags() returns a map of the tags for all the subiterators. // getSubTags() returns a map of the tags for all the subiterators.
func (it *And) getSubTags() map[string]struct{} { func (it *And) getSubTags() map[string]struct{} {
tags := make(map[string]struct{}) tags := make(map[string]struct{})
for _, sub := range it.GetSubIterators() { for _, sub := range it.SubIterators() {
for _, tag := range sub.Tags() { for _, tag := range sub.Tags() {
tags[tag] = struct{}{} tags[tag] = struct{}{}
} }
@ -292,23 +292,23 @@ func hasOneUsefulIterator(its []graph.Iterator) graph.Iterator {
return nil return nil
} }
// and.GetStats() lives here in and-iterator-optimize.go because it may // and.Stats() lives here in and-iterator-optimize.go because it may
// in the future return different statistics based on how it is optimized. // in the future return different statistics based on how it is optimized.
// For now, however, it's pretty static. // For now, however, it's pretty static.
func (it *And) GetStats() *graph.IteratorStats { func (it *And) Stats() graph.IteratorStats {
primaryStats := it.primaryIt.GetStats() primaryStats := it.primaryIt.Stats()
CheckCost := primaryStats.CheckCost CheckCost := primaryStats.CheckCost
NextCost := primaryStats.NextCost NextCost := primaryStats.NextCost
Size := primaryStats.Size Size := primaryStats.Size
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
stats := sub.GetStats() stats := sub.Stats()
NextCost += stats.CheckCost NextCost += stats.CheckCost
CheckCost += stats.CheckCost CheckCost += stats.CheckCost
if Size > stats.Size { if Size > stats.Size {
Size = stats.Size Size = stats.Size
} }
} }
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: CheckCost, CheckCost: CheckCost,
NextCost: NextCost, NextCost: NextCost,
Size: Size, Size: Size,

View file

@ -26,7 +26,7 @@ import (
func TestIteratorPromotion(t *testing.T) { func TestIteratorPromotion(t *testing.T) {
all := NewInt64(1, 3) all := NewInt64(1, 3)
fixed := newFixed() fixed := newFixed()
fixed.AddValue(3) fixed.Add(3)
a := NewAnd() a := NewAnd()
a.AddSubIterator(all) a.AddSubIterator(all)
a.AddSubIterator(fixed) a.AddSubIterator(fixed)
@ -79,7 +79,7 @@ func TestReorderWithTag(t *testing.T) {
} }
expectedTags := []string{"good", "slow"} expectedTags := []string{"good", "slow"}
tagsOut := make([]string, 0) tagsOut := make([]string, 0)
for _, sub := range newIt.GetSubIterators() { for _, sub := range newIt.SubIterators() {
for _, x := range sub.Tags() { for _, x := range sub.Tags() {
tagsOut = append(tagsOut, x) tagsOut = append(tagsOut, x)
} }
@ -98,12 +98,12 @@ func TestAndStatistics(t *testing.T) {
// Make all2 the default iterator // Make all2 the default iterator
a.AddSubIterator(all2) a.AddSubIterator(all2)
a.AddSubIterator(all) a.AddSubIterator(all)
stats1 := a.GetStats() stats1 := a.Stats()
newIt, changed := a.Optimize() newIt, changed := a.Optimize()
if !changed { if !changed {
t.Error("Didn't optimize") t.Error("Didn't optimize")
} }
stats2 := newIt.GetStats() stats2 := newIt.Stats()
if stats2.NextCost > stats1.NextCost { if stats2.NextCost > stats1.NextCost {
t.Error("And didn't optimize. Next cost old ", stats1.NextCost, "and new ", stats2.NextCost) t.Error("And didn't optimize. Next cost old ", stats1.NextCost, "and new ", stats2.NextCost)
} }

View file

@ -23,7 +23,7 @@ import (
// Make sure that tags work on the And. // Make sure that tags work on the And.
func TestTag(t *testing.T) { func TestTag(t *testing.T) {
fix1 := newFixed() fix1 := newFixed()
fix1.AddValue(234) fix1.Add(234)
fix1.AddTag("foo") fix1.AddTag("foo")
and := NewAnd() and := NewAnd()
and.AddSubIterator(fix1) and.AddSubIterator(fix1)
@ -43,8 +43,8 @@ func TestTag(t *testing.T) {
if val != 234 { if val != 234 {
t.Errorf("Unexpected value") t.Errorf("Unexpected value")
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
and.TagResults(&tags) and.TagResults(tags)
if tags["bar"] != 234 { if tags["bar"] != 234 {
t.Errorf("no bar tag") t.Errorf("no bar tag")
} }
@ -56,14 +56,14 @@ func TestTag(t *testing.T) {
// Do a simple itersection of fixed values. // Do a simple itersection of fixed values.
func TestAndAndFixedIterators(t *testing.T) { func TestAndAndFixedIterators(t *testing.T) {
fix1 := newFixed() fix1 := newFixed()
fix1.AddValue(1) fix1.Add(1)
fix1.AddValue(2) fix1.Add(2)
fix1.AddValue(3) fix1.Add(3)
fix1.AddValue(4) fix1.Add(4)
fix2 := newFixed() fix2 := newFixed()
fix2.AddValue(3) fix2.Add(3)
fix2.AddValue(4) fix2.Add(4)
fix2.AddValue(5) fix2.Add(5)
and := NewAnd() and := NewAnd()
and.AddSubIterator(fix1) and.AddSubIterator(fix1)
and.AddSubIterator(fix2) and.AddSubIterator(fix2)
@ -97,14 +97,14 @@ func TestAndAndFixedIterators(t *testing.T) {
// but there should be nothing to Next() // but there should be nothing to Next()
func TestNonOverlappingFixedIterators(t *testing.T) { func TestNonOverlappingFixedIterators(t *testing.T) {
fix1 := newFixed() fix1 := newFixed()
fix1.AddValue(1) fix1.Add(1)
fix1.AddValue(2) fix1.Add(2)
fix1.AddValue(3) fix1.Add(3)
fix1.AddValue(4) fix1.Add(4)
fix2 := newFixed() fix2 := newFixed()
fix2.AddValue(5) fix2.Add(5)
fix2.AddValue(6) fix2.Add(6)
fix2.AddValue(7) fix2.Add(7)
and := NewAnd() and := NewAnd()
and.AddSubIterator(fix1) and.AddSubIterator(fix1)
and.AddSubIterator(fix2) and.AddSubIterator(fix2)

View file

@ -17,7 +17,7 @@ package iterator
// Defines one of the base iterators, the Fixed iterator. A fixed iterator is quite simple; it // Defines one of the base iterators, the Fixed iterator. A fixed iterator is quite simple; it
// contains an explicit fixed array of values. // contains an explicit fixed array of values.
// //
// A fixed iterator requires an Equality function to be passed to it, by reason that graph.TSVal, the // A fixed iterator requires an Equality function to be passed to it, by reason that graph.Value, the
// opaque Triple store value, may not answer to ==. // opaque Triple store value, may not answer to ==.
import ( import (
@ -31,16 +31,16 @@ import (
// an equality function. // an equality function.
type Fixed struct { type Fixed struct {
Base Base
values []graph.TSVal values []graph.Value
lastIndex int lastIndex int
cmp Equality cmp Equality
} }
// Define the signature of an equality function. // Define the signature of an equality function.
type Equality func(a, b graph.TSVal) bool type Equality func(a, b graph.Value) bool
// Define an equality function of purely ==, which works for native types. // Define an equality function of purely ==, which works for native types.
func BasicEquality(a, b graph.TSVal) bool { func BasicEquality(a, b graph.Value) bool {
if a == b { if a == b {
return true return true
} }
@ -56,7 +56,7 @@ func newFixed() *Fixed {
func NewFixedIteratorWithCompare(compareFn Equality) *Fixed { func NewFixedIteratorWithCompare(compareFn Equality) *Fixed {
var it Fixed var it Fixed
BaseInit(&it.Base) BaseInit(&it.Base)
it.values = make([]graph.TSVal, 0, 20) it.values = make([]graph.Value, 0, 20)
it.lastIndex = 0 it.lastIndex = 0
it.cmp = compareFn it.cmp = compareFn
return &it return &it
@ -71,7 +71,7 @@ func (it *Fixed) Close() {}
func (it *Fixed) Clone() graph.Iterator { func (it *Fixed) Clone() graph.Iterator {
out := NewFixedIteratorWithCompare(it.cmp) out := NewFixedIteratorWithCompare(it.cmp)
for _, val := range it.values { for _, val := range it.values {
out.AddValue(val) out.Add(val)
} }
out.CopyTagsFrom(it) out.CopyTagsFrom(it)
return out return out
@ -79,7 +79,7 @@ func (it *Fixed) Clone() graph.Iterator {
// Add a value to the iterator. The array now contains this value. // Add a value to the iterator. The array now contains this value.
// TODO(barakmich): This ought to be a set someday, disallowing repeated values. // TODO(barakmich): This ought to be a set someday, disallowing repeated values.
func (it *Fixed) AddValue(v graph.TSVal) { func (it *Fixed) Add(v graph.Value) {
it.values = append(it.values, v) it.values = append(it.values, v)
} }
@ -104,7 +104,7 @@ func (it *Fixed) Type() string {
} }
// Check if the passed value is equal to one of the values stored in the iterator. // Check if the passed value is equal to one of the values stored in the iterator.
func (it *Fixed) Check(v graph.TSVal) bool { func (it *Fixed) Check(v graph.Value) bool {
// Could be optimized by keeping it sorted or using a better datastructure. // Could be optimized by keeping it sorted or using a better datastructure.
// However, for fixed iterators, which are by definition kind of tiny, this // However, for fixed iterators, which are by definition kind of tiny, this
// isn't a big issue. // isn't a big issue.
@ -119,7 +119,7 @@ func (it *Fixed) Check(v graph.TSVal) bool {
} }
// Return the next stored value from the iterator. // Return the next stored value from the iterator.
func (it *Fixed) Next() (graph.TSVal, bool) { func (it *Fixed) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
if it.lastIndex == len(it.values) { if it.lastIndex == len(it.values) {
return graph.NextLogOut(it, nil, false) return graph.NextLogOut(it, nil, false)
@ -148,8 +148,8 @@ func (it *Fixed) Size() (int64, bool) {
// As we right now have to scan the entire list, Next and Check are linear with the // As we right now have to scan the entire list, Next and Check are linear with the
// size. However, a better data structure could remove these limits. // size. However, a better data structure could remove these limits.
func (it *Fixed) GetStats() *graph.IteratorStats { func (it *Fixed) Stats() graph.IteratorStats {
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: int64(len(it.values)), CheckCost: int64(len(it.values)),
NextCost: int64(len(it.values)), NextCost: int64(len(it.values)),
Size: int64(len(it.values)), Size: int64(len(it.values)),

View file

@ -65,7 +65,7 @@ func NewHasA(ts graph.TripleStore, subIt graph.Iterator, d graph.Direction) *Has
} }
// Return our sole subiterator. // Return our sole subiterator.
func (it *HasA) GetSubIterators() []graph.Iterator { func (it *HasA) SubIterators() []graph.Iterator {
return []graph.Iterator{it.primaryIt} return []graph.Iterator{it.primaryIt}
} }
@ -99,15 +99,15 @@ func (it *HasA) Optimize() (graph.Iterator, bool) {
} }
// Pass the TagResults down the chain. // Pass the TagResults down the chain.
func (it *HasA) TagResults(out *map[string]graph.TSVal) { func (it *HasA) TagResults(dst map[string]graph.Value) {
it.Base.TagResults(out) it.Base.TagResults(dst)
it.primaryIt.TagResults(out) it.primaryIt.TagResults(dst)
} }
// DEPRECATED Return results in a ResultTree. // DEPRECATED Return results in a ResultTree.
func (it *HasA) GetResultTree() *graph.ResultTree { func (it *HasA) ResultTree() *graph.ResultTree {
tree := graph.NewResultTree(it.LastResult()) tree := graph.NewResultTree(it.Result())
tree.AddSubtree(it.primaryIt.GetResultTree()) tree.AddSubtree(it.primaryIt.ResultTree())
return tree return tree
} }
@ -117,22 +117,22 @@ func (it *HasA) DebugString(indent int) string {
for _, k := range it.Tags() { for _, k := range it.Tags() {
tags += fmt.Sprintf("%s;", k) tags += fmt.Sprintf("%s;", k)
} }
return fmt.Sprintf("%s(%s %d tags:%s direction:%s\n%s)", strings.Repeat(" ", indent), it.Type(), it.GetUid(), tags, it.dir, it.primaryIt.DebugString(indent+4)) 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 // Check a value against our internal iterator. In order to do this, we must first open a new
// iterator of "triples that have `val` in our direction", given to us by the triple store, // iterator of "triples that have `val` in our direction", given to us by the triple store,
// and then Next() values out of that iterator and Check() them against our subiterator. // and then Next() values out of that iterator and Check() them against our subiterator.
func (it *HasA) Check(val graph.TSVal) bool { func (it *HasA) Check(val graph.Value) bool {
graph.CheckLogIn(it, val) graph.CheckLogIn(it, val)
if glog.V(4) { if glog.V(4) {
glog.V(4).Infoln("Id is", it.ts.GetNameFor(val)) glog.V(4).Infoln("Id is", it.ts.NameOf(val))
} }
// TODO(barakmich): Optimize this // TODO(barakmich): Optimize this
if it.resultIt != nil { if it.resultIt != nil {
it.resultIt.Close() it.resultIt.Close()
} }
it.resultIt = it.ts.GetTripleIterator(it.dir, val) it.resultIt = it.ts.TripleIterator(it.dir, val)
return graph.CheckLogOut(it, val, it.GetCheckResult()) return graph.CheckLogOut(it, val, it.GetCheckResult())
} }
@ -146,10 +146,10 @@ func (it *HasA) GetCheckResult() bool {
break break
} }
if glog.V(4) { if glog.V(4) {
glog.V(4).Infoln("Triple is", it.ts.GetTriple(linkVal)) glog.V(4).Infoln("Triple is", it.ts.Triple(linkVal))
} }
if it.primaryIt.Check(linkVal) { if it.primaryIt.Check(linkVal) {
it.Last = it.ts.GetTripleDirection(linkVal, it.dir) it.Last = it.ts.TripleDirection(linkVal, it.dir)
return true return true
} }
} }
@ -173,7 +173,7 @@ func (it *HasA) NextResult() bool {
// Get the next result from this iterator. This is simpler than Check. We have a // Get the next result from this iterator. This is simpler than Check. We have a
// subiterator we can get a value from, and we can take that resultant triple, // subiterator we can get a value from, and we can take that resultant triple,
// pull our direction out of it, and return that. // pull our direction out of it, and return that.
func (it *HasA) Next() (graph.TSVal, bool) { func (it *HasA) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
if it.resultIt != nil { if it.resultIt != nil {
it.resultIt.Close() it.resultIt.Close()
@ -184,8 +184,8 @@ func (it *HasA) Next() (graph.TSVal, bool) {
if !ok { if !ok {
return graph.NextLogOut(it, 0, false) return graph.NextLogOut(it, 0, false)
} }
name := it.ts.GetTriple(tID).Get(it.dir) name := it.ts.Triple(tID).Get(it.dir)
val := it.ts.GetIdFor(name) val := it.ts.ValueOf(name)
it.Last = val it.Last = val
return graph.NextLogOut(it, val, true) return graph.NextLogOut(it, val, true)
} }
@ -196,15 +196,15 @@ func (it *HasA) Next() (graph.TSVal, bool) {
// one sticks -- potentially expensive, depending on fanout. Size, however, is // one sticks -- potentially expensive, depending on fanout. Size, however, is
// potentially smaller. we know at worst it's the size of the subiterator, but // potentially smaller. we know at worst it's the size of the subiterator, but
// if there are many repeated values, it could be much smaller in totality. // if there are many repeated values, it could be much smaller in totality.
func (it *HasA) GetStats() *graph.IteratorStats { func (it *HasA) Stats() graph.IteratorStats {
subitStats := it.primaryIt.GetStats() subitStats := it.primaryIt.Stats()
// TODO(barakmich): These should really come from the triplestore itself // TODO(barakmich): These should really come from the triplestore itself
// and be optimized. // and be optimized.
faninFactor := int64(1) faninFactor := int64(1)
fanoutFactor := int64(30) fanoutFactor := int64(30)
nextConstant := int64(2) nextConstant := int64(2)
tripleConstant := int64(1) tripleConstant := int64(1)
return &graph.IteratorStats{ return graph.IteratorStats{
NextCost: tripleConstant + subitStats.NextCost, NextCost: tripleConstant + subitStats.NextCost,
CheckCost: (fanoutFactor * nextConstant) * subitStats.CheckCost, CheckCost: (fanoutFactor * nextConstant) * subitStats.CheckCost,
Size: faninFactor * subitStats.Size, Size: faninFactor * subitStats.Size,

View file

@ -20,35 +20,39 @@ package iterator
import ( import (
"fmt" "fmt"
"strings" "strings"
"sync/atomic"
"github.com/barakmich/glog" "github.com/barakmich/glog"
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
var iterator_n int = 0 var nextIteratorID uintptr
func nextID() uintptr {
return atomic.AddUintptr(&nextIteratorID, 1) - 1
}
// The Base iterator is the iterator other iterators inherit from to get some // The Base iterator is the iterator other iterators inherit from to get some
// default functionality. // default functionality.
type Base struct { type Base struct {
Last graph.TSVal Last graph.Value
tags []string tags []string
fixedTags map[string]graph.TSVal fixedTags map[string]graph.Value
nextable bool canNext bool
uid int uid uintptr
} }
// Called by subclases. // Called by subclases.
func BaseInit(it *Base) { func BaseInit(it *Base) {
// Your basic iterator is nextable // Your basic iterator is nextable
it.nextable = true it.canNext = true
it.uid = iterator_n
if glog.V(2) { if glog.V(2) {
iterator_n++ it.uid = nextID()
} }
} }
func (it *Base) GetUid() int { func (it *Base) UID() uintptr {
return it.uid return it.uid
} }
@ -60,9 +64,9 @@ func (it *Base) AddTag(tag string) {
it.tags = append(it.tags, tag) it.tags = append(it.tags, tag)
} }
func (it *Base) AddFixedTag(tag string, value graph.TSVal) { func (it *Base) AddFixedTag(tag string, value graph.Value) {
if it.fixedTags == nil { if it.fixedTags == nil {
it.fixedTags = make(map[string]graph.TSVal) it.fixedTags = make(map[string]graph.Value)
} }
it.fixedTags[tag] = value it.fixedTags[tag] = value
} }
@ -72,7 +76,7 @@ func (it *Base) Tags() []string {
return it.tags return it.tags
} }
func (it *Base) FixedTags() map[string]graph.TSVal { func (it *Base) FixedTags() map[string]graph.Value {
return it.fixedTags return it.fixedTags
} }
@ -93,24 +97,24 @@ func (it *Base) DebugString(indent int) string {
} }
// Nothing in a base iterator. // Nothing in a base iterator.
func (it *Base) Check(v graph.TSVal) bool { func (it *Base) Check(v graph.Value) bool {
return false return false
} }
// Base iterators should never appear in a tree if they are, select against // Base iterators should never appear in a tree if they are, select against
// them. // them.
func (it *Base) GetStats() *graph.IteratorStats { func (it *Base) Stats() graph.IteratorStats {
return &graph.IteratorStats{100000, 100000, 100000} return graph.IteratorStats{100000, 100000, 100000}
} }
// DEPRECATED // DEPRECATED
func (it *Base) GetResultTree() *graph.ResultTree { func (it *Base) ResultTree() *graph.ResultTree {
tree := graph.NewResultTree(it.LastResult()) tree := graph.NewResultTree(it.Result())
return tree return tree
} }
// Nothing in a base iterator. // Nothing in a base iterator.
func (it *Base) Next() (graph.TSVal, bool) { func (it *Base) Next() (graph.Value, bool) {
return nil, false return nil, false
} }
@ -119,7 +123,7 @@ func (it *Base) NextResult() bool {
} }
// Returns the last result of an iterator. // Returns the last result of an iterator.
func (it *Base) LastResult() graph.TSVal { func (it *Base) Result() graph.Value {
return it.Last return it.Last
} }
@ -129,22 +133,22 @@ func (it *Base) Size() (int64, bool) {
} }
// No subiterators. Only those with subiterators need to do anything here. // No subiterators. Only those with subiterators need to do anything here.
func (it *Base) GetSubIterators() []graph.Iterator { func (it *Base) SubIterators() []graph.Iterator {
return nil return nil
} }
// Accessor // Accessor
func (it *Base) Nextable() bool { return it.nextable } func (it *Base) CanNext() bool { return it.canNext }
// Fill the map based on the tags assigned to this iterator. Default // Fill the map based on the tags assigned to this iterator. Default
// functionality works well for most iterators. // functionality works well for most iterators.
func (it *Base) TagResults(out_map *map[string]graph.TSVal) { func (it *Base) TagResults(dst map[string]graph.Value) {
for _, tag := range it.Tags() { for _, tag := range it.Tags() {
(*out_map)[tag] = it.LastResult() dst[tag] = it.Result()
} }
for tag, value := range it.FixedTags() { for tag, value := range it.FixedTags() {
(*out_map)[tag] = value dst[tag] = value
} }
} }
@ -182,6 +186,6 @@ func (it *Null) DebugString(indent int) string {
} }
// A null iterator costs nothing. Use it! // A null iterator costs nothing. Use it!
func (it *Null) GetStats() *graph.IteratorStats { func (it *Null) Stats() graph.IteratorStats {
return &graph.IteratorStats{} return graph.IteratorStats{}
} }

View file

@ -77,15 +77,15 @@ func (it *LinksTo) Clone() graph.Iterator {
func (it *LinksTo) Direction() graph.Direction { return it.dir } func (it *LinksTo) Direction() graph.Direction { return it.dir }
// Tag these results, and our subiterator's results. // Tag these results, and our subiterator's results.
func (it *LinksTo) TagResults(out *map[string]graph.TSVal) { func (it *LinksTo) TagResults(dst map[string]graph.Value) {
it.Base.TagResults(out) it.Base.TagResults(dst)
it.primaryIt.TagResults(out) it.primaryIt.TagResults(dst)
} }
// DEPRECATED // DEPRECATED
func (it *LinksTo) GetResultTree() *graph.ResultTree { func (it *LinksTo) ResultTree() *graph.ResultTree {
tree := graph.NewResultTree(it.LastResult()) tree := graph.NewResultTree(it.Result())
tree.AddSubtree(it.primaryIt.GetResultTree()) tree.AddSubtree(it.primaryIt.ResultTree())
return tree return tree
} }
@ -93,14 +93,14 @@ func (it *LinksTo) GetResultTree() *graph.ResultTree {
func (it *LinksTo) DebugString(indent int) string { func (it *LinksTo) DebugString(indent int) string {
return fmt.Sprintf("%s(%s %d direction:%s\n%s)", return fmt.Sprintf("%s(%s %d direction:%s\n%s)",
strings.Repeat(" ", indent), strings.Repeat(" ", indent),
it.Type(), it.GetUid(), it.dir, it.primaryIt.DebugString(indent+4)) it.Type(), it.UID(), it.dir, it.primaryIt.DebugString(indent+4))
} }
// If it checks in the right direction for the subiterator, it is a valid link // If it checks in the right direction for the subiterator, it is a valid link
// for the LinksTo. // for the LinksTo.
func (it *LinksTo) Check(val graph.TSVal) bool { func (it *LinksTo) Check(val graph.Value) bool {
graph.CheckLogIn(it, val) graph.CheckLogIn(it, val)
node := it.ts.GetTripleDirection(val, it.dir) node := it.ts.TripleDirection(val, it.dir)
if it.primaryIt.Check(node) { if it.primaryIt.Check(node) {
it.Last = val it.Last = val
return graph.CheckLogOut(it, val, true) return graph.CheckLogOut(it, val, true)
@ -109,7 +109,7 @@ func (it *LinksTo) Check(val graph.TSVal) bool {
} }
// Return a list containing only our subiterator. // Return a list containing only our subiterator.
func (it *LinksTo) GetSubIterators() []graph.Iterator { func (it *LinksTo) SubIterators() []graph.Iterator {
return []graph.Iterator{it.primaryIt} return []graph.Iterator{it.primaryIt}
} }
@ -135,7 +135,7 @@ func (it *LinksTo) Optimize() (graph.Iterator, bool) {
} }
// Next()ing a LinksTo operates as described above. // Next()ing a LinksTo operates as described above.
func (it *LinksTo) Next() (graph.TSVal, bool) { func (it *LinksTo) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
val, ok := it.nextIt.Next() val, ok := it.nextIt.Next()
if !ok { if !ok {
@ -146,7 +146,7 @@ func (it *LinksTo) Next() (graph.TSVal, bool) {
return graph.NextLogOut(it, 0, false) return graph.NextLogOut(it, 0, false)
} }
it.nextIt.Close() it.nextIt.Close()
it.nextIt = it.ts.GetTripleIterator(it.dir, candidate) it.nextIt = it.ts.TripleIterator(it.dir, candidate)
// Recurse -- return the first in the next set. // Recurse -- return the first in the next set.
return it.Next() return it.Next()
} }
@ -169,13 +169,13 @@ func (it *LinksTo) NextResult() bool {
func (it *LinksTo) Type() string { return "linksto" } func (it *LinksTo) Type() string { return "linksto" }
// Return a guess as to how big or costly it is to next the iterator. // Return a guess as to how big or costly it is to next the iterator.
func (it *LinksTo) GetStats() *graph.IteratorStats { func (it *LinksTo) Stats() graph.IteratorStats {
subitStats := it.primaryIt.GetStats() subitStats := it.primaryIt.Stats()
// TODO(barakmich): These should really come from the triplestore itself // TODO(barakmich): These should really come from the triplestore itself
fanoutFactor := int64(20) fanoutFactor := int64(20)
checkConstant := int64(1) checkConstant := int64(1)
nextConstant := int64(2) nextConstant := int64(2)
return &graph.IteratorStats{ return graph.IteratorStats{
NextCost: nextConstant + subitStats.NextCost, NextCost: nextConstant + subitStats.NextCost,
CheckCost: checkConstant + subitStats.CheckCost, CheckCost: checkConstant + subitStats.CheckCost,
Size: fanoutFactor * subitStats.Size, Size: fanoutFactor * subitStats.Size,

View file

@ -23,17 +23,17 @@ import (
func TestLinksTo(t *testing.T) { func TestLinksTo(t *testing.T) {
ts := new(TestTripleStore) ts := new(TestTripleStore)
tsFixed := newFixed() tsFixed := newFixed()
tsFixed.AddValue(2) tsFixed.Add(2)
ts.On("GetIdFor", "cool").Return(1) ts.On("ValueOf", "cool").Return(1)
ts.On("GetTripleIterator", graph.Object, 1).Return(tsFixed) ts.On("TripleIterator", graph.Object, 1).Return(tsFixed)
fixed := newFixed() fixed := newFixed()
fixed.AddValue(ts.GetIdFor("cool")) fixed.Add(ts.ValueOf("cool"))
lto := NewLinksTo(ts, fixed, graph.Object) lto := NewLinksTo(ts, fixed, graph.Object)
val, ok := lto.Next() val, ok := lto.Next()
if !ok { if !ok {
t.Error("At least one triple matches the fixed object") t.Error("At least one triple matches the fixed object")
} }
if val != 2 { if val != 2 {
t.Errorf("Triple index 2, such as %s, should match %s", ts.GetTriple(2), ts.GetTriple(val)) t.Errorf("Triple index 2, such as %s, should match %s", ts.Triple(2), ts.Triple(val))
} }
} }

View file

@ -27,23 +27,23 @@ type TestTripleStore struct {
mock.Mock mock.Mock
} }
func (ts *TestTripleStore) GetIdFor(s string) graph.TSVal { func (ts *TestTripleStore) ValueOf(s string) graph.Value {
args := ts.Mock.Called(s) args := ts.Mock.Called(s)
return args.Get(0) return args.Get(0)
} }
func (ts *TestTripleStore) AddTriple(*graph.Triple) {} func (ts *TestTripleStore) AddTriple(*graph.Triple) {}
func (ts *TestTripleStore) AddTripleSet([]*graph.Triple) {} func (ts *TestTripleStore) AddTripleSet([]*graph.Triple) {}
func (ts *TestTripleStore) GetTriple(graph.TSVal) *graph.Triple { return &graph.Triple{} } func (ts *TestTripleStore) Triple(graph.Value) *graph.Triple { return &graph.Triple{} }
func (ts *TestTripleStore) GetTripleIterator(d graph.Direction, i graph.TSVal) graph.Iterator { func (ts *TestTripleStore) TripleIterator(d graph.Direction, i graph.Value) graph.Iterator {
args := ts.Mock.Called(d, i) args := ts.Mock.Called(d, i)
return args.Get(0).(graph.Iterator) return args.Get(0).(graph.Iterator)
} }
func (ts *TestTripleStore) GetNodesAllIterator() graph.Iterator { return &Null{} } func (ts *TestTripleStore) NodesAllIterator() graph.Iterator { return &Null{} }
func (ts *TestTripleStore) GetTriplesAllIterator() graph.Iterator { return &Null{} } func (ts *TestTripleStore) TriplesAllIterator() graph.Iterator { return &Null{} }
func (ts *TestTripleStore) GetIteratorByString(string, string, string) graph.Iterator { func (ts *TestTripleStore) GetIteratorByString(string, string, string) graph.Iterator {
return &Null{} return &Null{}
} }
func (ts *TestTripleStore) GetNameFor(v graph.TSVal) string { func (ts *TestTripleStore) NameOf(v graph.Value) string {
args := ts.Mock.Called(v) args := ts.Mock.Called(v)
return args.Get(0).(string) return args.Get(0).(string)
} }
@ -55,6 +55,6 @@ func (ts *TestTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator,
func (ts *TestTripleStore) FixedIterator() graph.FixedIterator { func (ts *TestTripleStore) FixedIterator() graph.FixedIterator {
return NewFixedIteratorWithCompare(BasicEquality) return NewFixedIteratorWithCompare(BasicEquality)
} }
func (ts *TestTripleStore) Close() {} func (ts *TestTripleStore) Close() {}
func (ts *TestTripleStore) GetTripleDirection(graph.TSVal, graph.Direction) graph.TSVal { return 0 } func (ts *TestTripleStore) TripleDirection(graph.Value, graph.Direction) graph.Value { return 0 }
func (ts *TestTripleStore) RemoveTriple(t *graph.Triple) {} func (ts *TestTripleStore) RemoveTriple(t *graph.Triple) {}

View file

@ -47,7 +47,7 @@ type Optional struct {
func NewOptional(it graph.Iterator) *Optional { func NewOptional(it graph.Iterator) *Optional {
var o Optional var o Optional
BaseInit(&o.Base) BaseInit(&o.Base)
o.nextable = false o.canNext = false
o.subIt = it o.subIt = it
return &o return &o
} }
@ -69,7 +69,7 @@ func (it *Optional) Clone() graph.Iterator {
// Nexting the iterator is unsupported -- error and return an empty set. // Nexting the iterator is unsupported -- error and return an empty set.
// (As above, a reasonable alternative would be to Next() an all iterator) // (As above, a reasonable alternative would be to Next() an all iterator)
func (it *Optional) Next() (graph.TSVal, bool) { func (it *Optional) Next() (graph.Value, bool) {
glog.Errorln("Nexting an un-nextable iterator") glog.Errorln("Nexting an un-nextable iterator")
return nil, false return nil, false
} }
@ -87,7 +87,7 @@ func (it *Optional) NextResult() bool {
// Check() is the real hack of this iterator. It always returns true, regardless // Check() is the real hack of this iterator. It always returns true, regardless
// of whether the subiterator matched. But we keep track of whether the subiterator // of whether the subiterator matched. But we keep track of whether the subiterator
// matched for results purposes. // matched for results purposes.
func (it *Optional) Check(val graph.TSVal) bool { func (it *Optional) Check(val graph.Value) bool {
checked := it.subIt.Check(val) checked := it.subIt.Check(val)
it.lastCheck = checked it.lastCheck = checked
it.Last = val it.Last = val
@ -96,11 +96,11 @@ func (it *Optional) Check(val graph.TSVal) bool {
// If we failed the check, then the subiterator should not contribute to the result // If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it. // set. Otherwise, go ahead and tag it.
func (it *Optional) TagResults(out *map[string]graph.TSVal) { func (it *Optional) TagResults(dst map[string]graph.Value) {
if it.lastCheck == false { if it.lastCheck == false {
return return
} }
it.subIt.TagResults(out) it.subIt.TagResults(dst)
} }
// Registers the optional iterator. // Registers the optional iterator.
@ -127,9 +127,9 @@ func (it *Optional) Optimize() (graph.Iterator, bool) {
} }
// We're only as expensive as our subiterator. Except, we can't be nexted. // We're only as expensive as our subiterator. Except, we can't be nexted.
func (it *Optional) GetStats() *graph.IteratorStats { func (it *Optional) Stats() graph.IteratorStats {
subStats := it.subIt.GetStats() subStats := it.subIt.Stats()
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: subStats.CheckCost, CheckCost: subStats.CheckCost,
NextCost: int64(1 << 62), NextCost: int64(1 << 62),
Size: subStats.Size, Size: subStats.Size,

View file

@ -77,22 +77,22 @@ func (it *Or) Clone() graph.Iterator {
} }
// Returns a list.List of the subiterators, in order. The returned slice must not be modified. // Returns a list.List of the subiterators, in order. The returned slice must not be modified.
func (it *Or) GetSubIterators() []graph.Iterator { func (it *Or) SubIterators() []graph.Iterator {
return it.internalIterators return it.internalIterators
} }
// Overrides BaseIterator TagResults, as it needs to add it's own results and // Overrides BaseIterator TagResults, as it needs to add it's own results and
// recurse down it's subiterators. // recurse down it's subiterators.
func (it *Or) TagResults(out *map[string]graph.TSVal) { func (it *Or) TagResults(dst map[string]graph.Value) {
it.Base.TagResults(out) it.Base.TagResults(dst)
it.internalIterators[it.currentIterator].TagResults(out) it.internalIterators[it.currentIterator].TagResults(dst)
} }
// DEPRECATED Returns the ResultTree for this graph.iterator, recurses to it's subiterators. // DEPRECATED Returns the ResultTree for this graph.iterator, recurses to it's subiterators.
func (it *Or) GetResultTree() *graph.ResultTree { func (it *Or) ResultTree() *graph.ResultTree {
tree := graph.NewResultTree(it.LastResult()) tree := graph.NewResultTree(it.Result())
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
tree.AddSubtree(sub.GetResultTree()) tree.AddSubtree(sub.ResultTree())
} }
return tree return tree
} }
@ -128,9 +128,9 @@ func (it *Or) AddSubIterator(sub graph.Iterator) {
// Returns the Next value from the Or graph.iterator. Because the Or is the // Returns the Next value from the Or graph.iterator. Because the Or is the
// union of its subiterators, it must produce from all subiterators -- unless // union of its subiterators, it must produce from all subiterators -- unless
// it's shortcircuiting, in which case, it's the first one that returns anything. // it's shortcircuiting, in which case, it's the first one that returns anything.
func (it *Or) Next() (graph.TSVal, bool) { func (it *Or) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
var curr graph.TSVal var curr graph.Value
var exists bool var exists bool
firstTime := false firstTime := false
for { for {
@ -157,7 +157,7 @@ func (it *Or) Next() (graph.TSVal, bool) {
} }
// Checks a value against the iterators, in order. // Checks a value against the iterators, in order.
func (it *Or) checkSubIts(val graph.TSVal) bool { func (it *Or) checkSubIts(val graph.Value) bool {
var subIsGood = false var subIsGood = false
for i, sub := range it.internalIterators { for i, sub := range it.internalIterators {
subIsGood = sub.Check(val) subIsGood = sub.Check(val)
@ -170,7 +170,7 @@ func (it *Or) checkSubIts(val graph.TSVal) bool {
} }
// Check a value against the entire graph.iterator, in order. // Check a value against the entire graph.iterator, in order.
func (it *Or) Check(val graph.TSVal) bool { func (it *Or) Check(val graph.Value) bool {
graph.CheckLogIn(it, val) graph.CheckLogIn(it, val)
anyGood := it.checkSubIts(val) anyGood := it.checkSubIts(val)
if !anyGood { if !anyGood {
@ -233,7 +233,7 @@ func (it *Or) Close() {
} }
func (it *Or) Optimize() (graph.Iterator, bool) { func (it *Or) Optimize() (graph.Iterator, bool) {
old := it.GetSubIterators() old := it.SubIterators()
optIts := optimizeSubIterators(old) optIts := optimizeSubIterators(old)
// Close the replaced iterators (they ought to close themselves, but Close() // Close the replaced iterators (they ought to close themselves, but Close()
// is idempotent, so this just protects against any machinations). // is idempotent, so this just protects against any machinations).
@ -256,12 +256,12 @@ func (it *Or) Optimize() (graph.Iterator, bool) {
return newOr, true return newOr, true
} }
func (it *Or) GetStats() *graph.IteratorStats { func (it *Or) Stats() graph.IteratorStats {
CheckCost := int64(0) CheckCost := int64(0)
NextCost := int64(0) NextCost := int64(0)
Size := int64(0) Size := int64(0)
for _, sub := range it.internalIterators { for _, sub := range it.internalIterators {
stats := sub.GetStats() stats := sub.Stats()
NextCost += stats.NextCost NextCost += stats.NextCost
CheckCost += stats.CheckCost CheckCost += stats.CheckCost
if it.isShortCircuiting { if it.isShortCircuiting {
@ -272,7 +272,7 @@ func (it *Or) GetStats() *graph.IteratorStats {
Size += stats.Size Size += stats.Size
} }
} }
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: CheckCost, CheckCost: CheckCost,
NextCost: NextCost, NextCost: NextCost,
Size: Size, Size: Size,

View file

@ -40,14 +40,14 @@ func TestOrIteratorBasics(t *testing.T) {
Convey("Given an Or Iterator of two fixed iterators", t, func() { Convey("Given an Or Iterator of two fixed iterators", t, func() {
orIt = NewOr() orIt = NewOr()
fixed1 := newFixed() fixed1 := newFixed()
fixed1.AddValue(1) fixed1.Add(1)
fixed1.AddValue(2) fixed1.Add(2)
fixed1.AddValue(3) fixed1.Add(3)
fixed2 := newFixed() fixed2 := newFixed()
fixed2.AddValue(3) fixed2.Add(3)
fixed2.AddValue(9) fixed2.Add(9)
fixed2.AddValue(20) fixed2.Add(20)
fixed2.AddValue(21) fixed2.Add(21)
orIt.AddSubIterator(fixed1) orIt.AddSubIterator(fixed1)
orIt.AddSubIterator(fixed2) orIt.AddSubIterator(fixed2)
@ -88,14 +88,14 @@ func TestShortCircuitingOrBasics(t *testing.T) {
Convey("Given a short-circuiting Or of two fixed iterators", t, func() { Convey("Given a short-circuiting Or of two fixed iterators", t, func() {
orIt = NewShortCircuitOr() orIt = NewShortCircuitOr()
fixed1 := newFixed() fixed1 := newFixed()
fixed1.AddValue(1) fixed1.Add(1)
fixed1.AddValue(2) fixed1.Add(2)
fixed1.AddValue(3) fixed1.Add(3)
fixed2 := newFixed() fixed2 := newFixed()
fixed2.AddValue(3) fixed2.Add(3)
fixed2.AddValue(9) fixed2.Add(9)
fixed2.AddValue(20) fixed2.Add(20)
fixed2.AddValue(21) fixed2.Add(21)
Convey("It should guess its size.", func() { Convey("It should guess its size.", func() {
orIt.AddSubIterator(fixed1) orIt.AddSubIterator(fixed1)

View file

@ -116,7 +116,7 @@ func (qs *queryShape) MakeNode(it graph.Iterator) *Node {
switch it.Type() { switch it.Type() {
case "and": case "and":
for _, sub := range it.GetSubIterators() { for _, sub := range it.SubIterators() {
qs.nodeId++ qs.nodeId++
newNode := qs.MakeNode(sub) newNode := qs.MakeNode(sub)
if sub.Type() != "or" { if sub.Type() != "or" {
@ -133,7 +133,7 @@ func (qs *queryShape) MakeNode(it graph.Iterator) *Node {
if !more { if !more {
break break
} }
n.Values = append(n.Values, qs.ts.GetNameFor(val)) n.Values = append(n.Values, qs.ts.NameOf(val))
} }
case "hasa": case "hasa":
hasa := it.(*HasA) hasa := it.(*HasA)
@ -143,7 +143,7 @@ func (qs *queryShape) MakeNode(it graph.Iterator) *Node {
qs.AddNode(newNode) qs.AddNode(newNode)
qs.RemoveHasa() qs.RemoveHasa()
case "or": case "or":
for _, sub := range it.GetSubIterators() { for _, sub := range it.SubIterators() {
qs.nodeId++ qs.nodeId++
newNode := qs.MakeNode(sub) newNode := qs.MakeNode(sub)
if sub.Type() == "or" { if sub.Type() == "or" {

View file

@ -25,8 +25,8 @@ import (
func buildHasaWithTag(ts graph.TripleStore, tag string, target string) *HasA { func buildHasaWithTag(ts graph.TripleStore, tag string, target string) *HasA {
fixed_obj := ts.FixedIterator() fixed_obj := ts.FixedIterator()
fixed_pred := ts.FixedIterator() fixed_pred := ts.FixedIterator()
fixed_obj.AddValue(ts.GetIdFor(target)) fixed_obj.Add(ts.ValueOf(target))
fixed_pred.AddValue(ts.GetIdFor("status")) fixed_pred.Add(ts.ValueOf("status"))
fixed_obj.AddTag(tag) fixed_obj.AddTag(tag)
lto1 := NewLinksTo(ts, fixed_obj, graph.Object) lto1 := NewLinksTo(ts, fixed_obj, graph.Object)
lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate) lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate)
@ -40,14 +40,14 @@ func buildHasaWithTag(ts graph.TripleStore, tag string, target string) *HasA {
func TestQueryShape(t *testing.T) { func TestQueryShape(t *testing.T) {
var queryShape map[string]interface{} var queryShape map[string]interface{}
ts := new(TestTripleStore) ts := new(TestTripleStore)
ts.On("GetIdFor", "cool").Return(1) ts.On("ValueOf", "cool").Return(1)
ts.On("GetNameFor", 1).Return("cool") ts.On("NameOf", 1).Return("cool")
ts.On("GetIdFor", "status").Return(2) ts.On("ValueOf", "status").Return(2)
ts.On("GetNameFor", 2).Return("status") ts.On("NameOf", 2).Return("status")
ts.On("GetIdFor", "fun").Return(3) ts.On("ValueOf", "fun").Return(3)
ts.On("GetNameFor", 3).Return("fun") ts.On("NameOf", 3).Return("fun")
ts.On("GetIdFor", "name").Return(4) ts.On("ValueOf", "name").Return(4)
ts.On("GetNameFor", 4).Return("name") ts.On("NameOf", 4).Return("name")
Convey("Given a single linkage iterator's shape", t, func() { Convey("Given a single linkage iterator's shape", t, func() {
queryShape = make(map[string]interface{}) queryShape = make(map[string]interface{})
@ -92,7 +92,7 @@ func TestQueryShape(t *testing.T) {
andInternal.AddSubIterator(hasa1) andInternal.AddSubIterator(hasa1)
andInternal.AddSubIterator(hasa2) andInternal.AddSubIterator(hasa2)
fixed_pred := ts.FixedIterator() fixed_pred := ts.FixedIterator()
fixed_pred.AddValue(ts.GetIdFor("name")) fixed_pred.Add(ts.ValueOf("name"))
lto1 := NewLinksTo(ts, andInternal, graph.Subject) lto1 := NewLinksTo(ts, andInternal, graph.Subject)
lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate) lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate)
and := NewAnd() and := NewAnd()

View file

@ -65,9 +65,9 @@ func NewComparison(sub graph.Iterator, op Operator, val interface{}, ts graph.Tr
// Here's the non-boilerplate part of the ValueComparison iterator. Given a value // Here's the non-boilerplate part of the ValueComparison iterator. Given a value
// and our operator, determine whether or not we meet the requirement. // and our operator, determine whether or not we meet the requirement.
func (it *Comparison) doComparison(val graph.TSVal) bool { func (it *Comparison) doComparison(val graph.Value) bool {
//TODO(barakmich): Implement string comparison. //TODO(barakmich): Implement string comparison.
nodeStr := it.ts.GetNameFor(val) nodeStr := it.ts.NameOf(val)
switch cVal := it.val.(type) { switch cVal := it.val.(type) {
case int: case int:
cInt := int64(cVal) cInt := int64(cVal)
@ -117,8 +117,8 @@ func (it *Comparison) Clone() graph.Iterator {
return out return out
} }
func (it *Comparison) Next() (graph.TSVal, bool) { func (it *Comparison) Next() (graph.Value, bool) {
var val graph.TSVal var val graph.Value
var ok bool var ok bool
for { for {
val, ok = it.subIt.Next() val, ok = it.subIt.Next()
@ -139,15 +139,15 @@ func (it *Comparison) NextResult() bool {
if !hasNext { if !hasNext {
return false return false
} }
if it.doComparison(it.subIt.LastResult()) { if it.doComparison(it.subIt.Result()) {
return true return true
} }
} }
it.Last = it.subIt.LastResult() it.Last = it.subIt.Result()
return true return true
} }
func (it *Comparison) Check(val graph.TSVal) bool { func (it *Comparison) Check(val graph.Value) bool {
if !it.doComparison(val) { if !it.doComparison(val) {
return false return false
} }
@ -156,9 +156,9 @@ func (it *Comparison) Check(val graph.TSVal) bool {
// If we failed the check, then the subiterator should not contribute to the result // If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it. // set. Otherwise, go ahead and tag it.
func (it *Comparison) TagResults(out *map[string]graph.TSVal) { func (it *Comparison) TagResults(dst map[string]graph.Value) {
it.Base.TagResults(out) it.Base.TagResults(dst)
it.subIt.TagResults(out) it.subIt.TagResults(dst)
} }
// Registers the value-comparison iterator. // Registers the value-comparison iterator.
@ -185,6 +185,6 @@ func (it *Comparison) Optimize() (graph.Iterator, bool) {
// We're only as expensive as our subiterator. // We're only as expensive as our subiterator.
// Again, optimized value comparison iterators should do better. // Again, optimized value comparison iterators should do better.
func (it *Comparison) GetStats() *graph.IteratorStats { func (it *Comparison) Stats() graph.IteratorStats {
return it.subIt.GetStats() return it.subIt.Stats()
} }

View file

@ -23,8 +23,8 @@ import (
func SetupMockTripleStore(nameMap map[string]int) *TestTripleStore { func SetupMockTripleStore(nameMap map[string]int) *TestTripleStore {
ts := new(TestTripleStore) ts := new(TestTripleStore)
for k, v := range nameMap { for k, v := range nameMap {
ts.On("GetIdFor", k).Return(v) ts.On("ValueOf", k).Return(v)
ts.On("GetNameFor", v).Return(k) ts.On("NameOf", v).Return(k)
} }
return ts return ts
} }
@ -43,11 +43,11 @@ func SimpleValueTripleStore() *TestTripleStore {
func BuildFixedIterator() *Fixed { func BuildFixedIterator() *Fixed {
fixed := newFixed() fixed := newFixed()
fixed.AddValue(0) fixed.Add(0)
fixed.AddValue(1) fixed.Add(1)
fixed.AddValue(2) fixed.Add(2)
fixed.AddValue(3) fixed.Add(3)
fixed.AddValue(4) fixed.Add(4)
return fixed return fixed
} }
@ -59,7 +59,7 @@ func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []s
if !ok { if !ok {
break break
} }
actual = append(actual, ts.GetNameFor(val)) actual = append(actual, ts.NameOf(val))
} }
actualSet := actual[:] actualSet := actual[:]
for _, a := range expected { for _, a := range expected {

View file

@ -72,7 +72,7 @@ func (it *AllIterator) Clone() graph.Iterator {
return out return out
} }
func (it *AllIterator) Next() (graph.TSVal, bool) { func (it *AllIterator) Next() (graph.Value, bool) {
if !it.open { if !it.open {
it.Last = nil it.Last = nil
return nil, false return nil, false
@ -92,7 +92,7 @@ func (it *AllIterator) Next() (graph.TSVal, bool) {
return out, true return out, true
} }
func (it *AllIterator) Check(v graph.TSVal) bool { func (it *AllIterator) Check(v graph.Value) bool {
it.Last = v it.Last = v
return true return true
} }
@ -125,9 +125,9 @@ func (it *AllIterator) Optimize() (graph.Iterator, bool) {
return it, false return it, false
} }
func (it *AllIterator) GetStats() *graph.IteratorStats { func (it *AllIterator) Stats() graph.IteratorStats {
s, _ := it.Size() s, _ := it.Size()
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 2, NextCost: 2,
Size: s, Size: s,

View file

@ -38,7 +38,7 @@ type Iterator struct {
originalPrefix string originalPrefix string
} }
func NewIterator(prefix string, d graph.Direction, value graph.TSVal, ts *TripleStore) *Iterator { func NewIterator(prefix string, d graph.Direction, value graph.Value, ts *TripleStore) *Iterator {
var it Iterator var it Iterator
iterator.BaseInit(&it.Base) iterator.BaseInit(&it.Base)
it.checkId = value.([]byte) it.checkId = value.([]byte)
@ -85,7 +85,7 @@ func (it *Iterator) Close() {
} }
} }
func (it *Iterator) Next() (graph.TSVal, bool) { func (it *Iterator) Next() (graph.Value, bool) {
if it.it == nil { if it.it == nil {
it.Last = nil it.Last = nil
return nil, false return nil, false
@ -166,7 +166,7 @@ func GetPositionFromPrefix(prefix []byte, d graph.Direction, ts *TripleStore) in
panic("unreachable") panic("unreachable")
} }
func (it *Iterator) Check(v graph.TSVal) bool { func (it *Iterator) Check(v graph.Value) bool {
val := v.([]byte) val := v.([]byte)
if val[0] == 'z' { if val[0] == 'z' {
return false return false
@ -177,8 +177,8 @@ func (it *Iterator) Check(v graph.TSVal) bool {
return true return true
} }
} else { } else {
nameForDir := it.ts.GetTriple(v).Get(it.dir) nameForDir := it.ts.Triple(v).Get(it.dir)
hashForDir := it.ts.GetIdFor(nameForDir).([]byte) hashForDir := it.ts.ValueOf(nameForDir).([]byte)
if bytes.Equal(hashForDir, it.checkId) { if bytes.Equal(hashForDir, it.checkId) {
return true return true
} }
@ -192,7 +192,7 @@ func (it *Iterator) Size() (int64, bool) {
func (it *Iterator) DebugString(indent int) string { func (it *Iterator) DebugString(indent int) string {
size, _ := it.Size() size, _ := it.Size()
return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.GetUid(), it.Tags(), it.dir, size, it.ts.GetNameFor(it.checkId)) return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.UID(), it.Tags(), it.dir, size, it.ts.NameOf(it.checkId))
} }
func (it *Iterator) Type() string { return "leveldb" } func (it *Iterator) Type() string { return "leveldb" }
@ -202,9 +202,9 @@ func (it *Iterator) Optimize() (graph.Iterator, bool) {
return it, false return it, false
} }
func (it *Iterator) GetStats() *graph.IteratorStats { func (it *Iterator) Stats() graph.IteratorStats {
s, _ := it.Size() s, _ := it.Size()
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 2, NextCost: 2,
Size: s, Size: s,

View file

@ -50,7 +50,7 @@ func extractTripleFromIterator(ts graph.TripleStore, it graph.Iterator) []string
if !ok { if !ok {
break break
} }
output = append(output, ts.GetTriple(val).String()) output = append(output, ts.Triple(val).String())
} }
return output return output
} }
@ -62,7 +62,7 @@ func extractValuesFromIterator(ts graph.TripleStore, it graph.Iterator) []string
if !ok { if !ok {
break break
} }
output = append(output, ts.GetNameFor(val)) output = append(output, ts.NameOf(val))
} }
return output return output
} }
@ -113,7 +113,7 @@ func TestLoadDatabase(t *testing.T) {
Convey("Can load a single triple", func() { Convey("Can load a single triple", func() {
ts.AddTriple(&graph.Triple{"Something", "points_to", "Something Else", "context"}) ts.AddTriple(&graph.Triple{"Something", "points_to", "Something Else", "context"})
So(ts.GetNameFor(ts.GetIdFor("Something")), ShouldEqual, "Something") So(ts.NameOf(ts.ValueOf("Something")), ShouldEqual, "Something")
So(ts.Size(), ShouldEqual, 1) So(ts.Size(), ShouldEqual, 1)
}) })
@ -121,12 +121,12 @@ func TestLoadDatabase(t *testing.T) {
ts.AddTripleSet(makeTripleSet()) ts.AddTripleSet(makeTripleSet())
So(ts.Size(), ShouldEqual, 11) So(ts.Size(), ShouldEqual, 11)
So(ts.GetSizeFor(ts.GetIdFor("B")), ShouldEqual, 5) So(ts.GetSizeFor(ts.ValueOf("B")), ShouldEqual, 5)
Convey("Can delete triples", func() { Convey("Can delete triples", func() {
ts.RemoveTriple(&graph.Triple{"A", "follows", "B", ""}) ts.RemoveTriple(&graph.Triple{"A", "follows", "B", ""})
So(ts.Size(), ShouldEqual, 10) So(ts.Size(), ShouldEqual, 10)
So(ts.GetSizeFor(ts.GetIdFor("B")), ShouldEqual, 4) So(ts.GetSizeFor(ts.ValueOf("B")), ShouldEqual, 4)
}) })
}) })
@ -153,7 +153,7 @@ func TestIterator(t *testing.T) {
var it graph.Iterator var it graph.Iterator
Convey("Can create an all iterator for nodes", func() { Convey("Can create an all iterator for nodes", func() {
it = ts.GetNodesAllIterator() it = ts.NodesAllIterator()
So(it, ShouldNotBeNil) So(it, ShouldNotBeNil)
Convey("Has basics", func() { Convey("Has basics", func() {
@ -192,9 +192,9 @@ func TestIterator(t *testing.T) {
}) })
Convey("Contains a couple nodes", func() { Convey("Contains a couple nodes", func() {
So(it.Check(ts.GetIdFor("A")), ShouldBeTrue) So(it.Check(ts.ValueOf("A")), ShouldBeTrue)
So(it.Check(ts.GetIdFor("cool")), ShouldBeTrue) So(it.Check(ts.ValueOf("cool")), ShouldBeTrue)
//So(it.Check(ts.GetIdFor("baller")), ShouldBeFalse) //So(it.Check(ts.ValueOf("baller")), ShouldBeFalse)
}) })
Reset(func() { Reset(func() {
@ -203,7 +203,7 @@ func TestIterator(t *testing.T) {
}) })
Convey("Can create an all iterator for edges", func() { Convey("Can create an all iterator for edges", func() {
it := ts.GetTriplesAllIterator() it := ts.TriplesAllIterator()
So(it, ShouldNotBeNil) So(it, ShouldNotBeNil)
Convey("Has basics", func() { Convey("Has basics", func() {
size, accurate := it.Size() size, accurate := it.Size()
@ -217,7 +217,7 @@ func TestIterator(t *testing.T) {
Convey("Iterates an edge", func() { Convey("Iterates an edge", func() {
edge_val, _ := it.Next() edge_val, _ := it.Next()
triple := ts.GetTriple(edge_val) triple := ts.Triple(edge_val)
set := makeTripleSet() set := makeTripleSet()
var string_set []string var string_set []string
for _, t := range set { for _, t := range set {
@ -249,7 +249,7 @@ func TestSetIterator(t *testing.T) {
var it graph.Iterator var it graph.Iterator
Convey("Can create a subject iterator", func() { Convey("Can create a subject iterator", func() {
it = ts.GetTripleIterator(graph.Subject, ts.GetIdFor("C")) it = ts.TripleIterator(graph.Subject, ts.ValueOf("C"))
Convey("Containing the right things", func() { Convey("Containing the right things", func() {
expected := []string{ expected := []string{
@ -264,7 +264,7 @@ func TestSetIterator(t *testing.T) {
Convey("And checkable", func() { Convey("And checkable", func() {
and := iterator.NewAnd() and := iterator.NewAnd()
and.AddSubIterator(ts.GetTriplesAllIterator()) and.AddSubIterator(ts.TriplesAllIterator())
and.AddSubIterator(it) and.AddSubIterator(it)
expected := []string{ expected := []string{
@ -283,7 +283,7 @@ func TestSetIterator(t *testing.T) {
}) })
Convey("Can create an object iterator", func() { Convey("Can create an object iterator", func() {
it = ts.GetTripleIterator(graph.Object, ts.GetIdFor("F")) it = ts.TripleIterator(graph.Object, ts.ValueOf("F"))
Convey("Containing the right things", func() { Convey("Containing the right things", func() {
expected := []string{ expected := []string{
@ -298,7 +298,7 @@ func TestSetIterator(t *testing.T) {
Convey("Mutually and-checkable", func() { Convey("Mutually and-checkable", func() {
and := iterator.NewAnd() and := iterator.NewAnd()
and.AddSubIterator(ts.GetTripleIterator(graph.Subject, ts.GetIdFor("B"))) and.AddSubIterator(ts.TripleIterator(graph.Subject, ts.ValueOf("B")))
and.AddSubIterator(it) and.AddSubIterator(it)
expected := []string{ expected := []string{
@ -313,7 +313,7 @@ func TestSetIterator(t *testing.T) {
}) })
Convey("Can create a predicate iterator", func() { Convey("Can create a predicate iterator", func() {
it = ts.GetTripleIterator(graph.Predicate, ts.GetIdFor("status")) it = ts.TripleIterator(graph.Predicate, ts.ValueOf("status"))
Convey("Containing the right things", func() { Convey("Containing the right things", func() {
expected := []string{ expected := []string{
@ -330,7 +330,7 @@ func TestSetIterator(t *testing.T) {
}) })
Convey("Can create a provenance iterator", func() { Convey("Can create a provenance iterator", func() {
it = ts.GetTripleIterator(graph.Provenance, ts.GetIdFor("status_graph")) it = ts.TripleIterator(graph.Provenance, ts.ValueOf("status_graph"))
Convey("Containing the right things", func() { Convey("Containing the right things", func() {
expected := []string{ expected := []string{
@ -347,7 +347,7 @@ func TestSetIterator(t *testing.T) {
Convey("Can be cross-checked", func() { Convey("Can be cross-checked", func() {
and := iterator.NewAnd() and := iterator.NewAnd()
// Order is important // Order is important
and.AddSubIterator(ts.GetTripleIterator(graph.Subject, ts.GetIdFor("B"))) and.AddSubIterator(ts.TripleIterator(graph.Subject, ts.ValueOf("B")))
and.AddSubIterator(it) and.AddSubIterator(it)
expected := []string{ expected := []string{
@ -361,7 +361,7 @@ func TestSetIterator(t *testing.T) {
and := iterator.NewAnd() and := iterator.NewAnd()
// Order is important // Order is important
and.AddSubIterator(it) and.AddSubIterator(it)
and.AddSubIterator(ts.GetTripleIterator(graph.Subject, ts.GetIdFor("B"))) and.AddSubIterator(ts.TripleIterator(graph.Subject, ts.ValueOf("B")))
expected := []string{ expected := []string{
(&graph.Triple{"B", "status", "cool", "status_graph"}).String(), (&graph.Triple{"B", "status", "cool", "status_graph"}).String(),
@ -399,7 +399,7 @@ func TestOptimize(t *testing.T) {
Convey("With an linksto-fixed pair", func() { Convey("With an linksto-fixed pair", func() {
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("F")) fixed.Add(ts.ValueOf("F"))
fixed.AddTag("internal") fixed.AddTag("internal")
lto = iterator.NewLinksTo(ts, fixed, graph.Object) lto = iterator.NewLinksTo(ts, fixed, graph.Object)
@ -420,10 +420,10 @@ func TestOptimize(t *testing.T) {
Convey("With the correct tags", func() { Convey("With the correct tags", func() {
oldIt.Next() oldIt.Next()
newIt.Next() newIt.Next()
oldResults := make(map[string]graph.TSVal) oldResults := make(map[string]graph.Value)
oldIt.TagResults(&oldResults) oldIt.TagResults(oldResults)
newResults := make(map[string]graph.TSVal) newResults := make(map[string]graph.Value)
oldIt.TagResults(&newResults) oldIt.TagResults(newResults)
So(newResults, ShouldResemble, oldResults) So(newResults, ShouldResemble, oldResults)
}) })

View file

@ -65,11 +65,11 @@ func CreateNewLevelDB(path string) bool {
return true return true
} }
func NewTripleStore(path string, options graph.OptionsDict) *TripleStore { func NewTripleStore(path string, options graph.Options) *TripleStore {
var ts TripleStore var ts TripleStore
ts.path = path ts.path = path
cache_size := DefaultCacheSize cache_size := DefaultCacheSize
if val, ok := options.GetIntKey("cache_size_mb"); ok { if val, ok := options.IntKey("cache_size_mb"); ok {
cache_size = val cache_size = val
} }
ts.dbOpts = &opt.Options{ ts.dbOpts = &opt.Options{
@ -78,7 +78,7 @@ func NewTripleStore(path string, options graph.OptionsDict) *TripleStore {
ts.dbOpts.ErrorIfMissing = true ts.dbOpts.ErrorIfMissing = true
write_buffer_mb := DefaultWriteBufferSize write_buffer_mb := DefaultWriteBufferSize
if val, ok := options.GetIntKey("write_buffer_mb"); ok { if val, ok := options.IntKey("write_buffer_mb"); ok {
write_buffer_mb = val write_buffer_mb = val
} }
ts.dbOpts.WriteBuffer = write_buffer_mb * opt.MiB ts.dbOpts.WriteBuffer = write_buffer_mb * opt.MiB
@ -301,7 +301,7 @@ func (ts *TripleStore) Close() {
ts.open = false ts.open = false
} }
func (ts *TripleStore) GetTriple(k graph.TSVal) *graph.Triple { func (ts *TripleStore) Triple(k graph.Value) *graph.Triple {
var triple graph.Triple var triple graph.Triple
b, err := ts.db.Get(k.([]byte), ts.readopts) b, err := ts.db.Get(k.([]byte), ts.readopts)
if err != nil && err != leveldb.ErrNotFound { if err != nil && err != leveldb.ErrNotFound {
@ -328,7 +328,7 @@ func (ts *TripleStore) convertStringToByteHash(s string) []byte {
return key return key
} }
func (ts *TripleStore) GetIdFor(s string) graph.TSVal { func (ts *TripleStore) ValueOf(s string) graph.Value {
return ts.createValueKeyFor(s) return ts.createValueKeyFor(s)
} }
@ -352,7 +352,7 @@ func (ts *TripleStore) getValueData(value_key []byte) ValueData {
return out return out
} }
func (ts *TripleStore) GetNameFor(k graph.TSVal) string { func (ts *TripleStore) NameOf(k graph.Value) string {
if k == nil { if k == nil {
glog.V(2).Infoln("k was nil") glog.V(2).Infoln("k was nil")
return "" return ""
@ -360,7 +360,7 @@ func (ts *TripleStore) GetNameFor(k graph.TSVal) string {
return ts.getValueData(k.([]byte)).Name return ts.getValueData(k.([]byte)).Name
} }
func (ts *TripleStore) GetSizeFor(k graph.TSVal) int64 { func (ts *TripleStore) GetSizeFor(k graph.Value) int64 {
if k == nil { if k == nil {
return 0 return 0
} }
@ -401,7 +401,7 @@ func (ts *TripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
return 0, nil return 0, nil
} }
func (ts *TripleStore) GetTripleIterator(d graph.Direction, val graph.TSVal) graph.Iterator { func (ts *TripleStore) TripleIterator(d graph.Direction, val graph.Value) graph.Iterator {
var prefix string var prefix string
switch d { switch d {
case graph.Subject: case graph.Subject:
@ -418,25 +418,25 @@ func (ts *TripleStore) GetTripleIterator(d graph.Direction, val graph.TSVal) gra
return NewIterator(prefix, d, val, ts) return NewIterator(prefix, d, val, ts)
} }
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator { func (ts *TripleStore) NodesAllIterator() graph.Iterator {
return NewAllIterator("z", graph.Any, ts) return NewAllIterator("z", graph.Any, ts)
} }
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator { func (ts *TripleStore) TriplesAllIterator() graph.Iterator {
return NewAllIterator("po", graph.Predicate, ts) return NewAllIterator("po", graph.Predicate, ts)
} }
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, d graph.Direction) graph.TSVal { func (ts *TripleStore) TripleDirection(val graph.Value, d graph.Direction) graph.Value {
v := val.([]uint8) v := val.([]uint8)
offset := GetPositionFromPrefix(v[0:2], d, ts) offset := GetPositionFromPrefix(v[0:2], d, ts)
if offset != -1 { if offset != -1 {
return append([]byte("z"), v[offset:offset+ts.hasher.Size()]...) return append([]byte("z"), v[offset:offset+ts.hasher.Size()]...)
} else { } else {
return ts.GetTriple(val).Get(d) return ts.Triple(val).Get(d)
} }
} }
func compareBytes(a, b graph.TSVal) bool { func compareBytes(a, b graph.Value) bool {
return bytes.Equal(a.([]uint8), b.([]uint8)) return bytes.Equal(a.([]uint8), b.([]uint8))
} }

View file

@ -29,7 +29,7 @@ func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool
} }
func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) { func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) {
subs := it.GetSubIterators() subs := it.SubIterators()
if len(subs) != 1 { if len(subs) != 1 {
return it, false return it, false
} }
@ -41,7 +41,7 @@ func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bo
if !ok { if !ok {
panic("Sizes lie") panic("Sizes lie")
} }
newIt := ts.GetTripleIterator(it.Direction(), val) newIt := ts.TripleIterator(it.Direction(), val)
newIt.CopyTagsFrom(it) newIt.CopyTagsFrom(it)
for _, tag := range primary.Tags() { for _, tag := range primary.Tags() {
newIt.AddFixedTag(tag, val) newIt.AddFixedTag(tag, val)

View file

@ -31,7 +31,7 @@ func NewMemstoreAllIterator(ts *TripleStore) *AllIterator {
return &out return &out
} }
func (it *AllIterator) Next() (graph.TSVal, bool) { func (it *AllIterator) Next() (graph.Value, bool) {
next, out := it.Int64.Next() next, out := it.Int64.Next()
if !out { if !out {
return next, out return next, out

View file

@ -73,7 +73,7 @@ func (it *Iterator) Clone() graph.Iterator {
func (it *Iterator) Close() {} func (it *Iterator) Close() {}
func (it *Iterator) Next() (graph.TSVal, bool) { func (it *Iterator) Next() (graph.Value, bool) {
graph.NextLogIn(it) graph.NextLogIn(it)
if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) { if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) {
return graph.NextLogOut(it, nil, false) return graph.NextLogOut(it, nil, false)
@ -87,7 +87,7 @@ func (it *Iterator) Size() (int64, bool) {
return int64(it.tree.Len()), true return int64(it.tree.Len()), true
} }
func (it *Iterator) Check(v graph.TSVal) bool { func (it *Iterator) Check(v graph.Value) bool {
graph.CheckLogIn(it, v) graph.CheckLogIn(it, v)
if it.tree.Has(Int64(v.(int64))) { if it.tree.Has(Int64(v.(int64))) {
it.Last = v it.Last = v
@ -111,8 +111,8 @@ func (it *Iterator) Optimize() (graph.Iterator, bool) {
return it, false return it, false
} }
func (it *Iterator) GetStats() *graph.IteratorStats { func (it *Iterator) Stats() graph.IteratorStats {
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: int64(math.Log(float64(it.tree.Len()))) + 1, CheckCost: int64(math.Log(float64(it.tree.Len()))) + 1,
NextCost: 1, NextCost: 1,
Size: int64(it.tree.Len()), Size: int64(it.tree.Len()),

View file

@ -217,11 +217,11 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
} }
} }
func (ts *TripleStore) GetTriple(index graph.TSVal) *graph.Triple { func (ts *TripleStore) Triple(index graph.Value) *graph.Triple {
return &ts.triples[index.(int64)] return &ts.triples[index.(int64)]
} }
func (ts *TripleStore) GetTripleIterator(d graph.Direction, value graph.TSVal) graph.Iterator { func (ts *TripleStore) TripleIterator(d graph.Direction, value graph.Value) graph.Iterator {
index, ok := ts.index.Get(d, value.(int64)) index, ok := ts.index.Get(d, value.(int64))
data := fmt.Sprintf("dir:%s val:%d", d, value.(int64)) data := fmt.Sprintf("dir:%s val:%d", d, value.(int64))
if ok { if ok {
@ -243,15 +243,15 @@ func (ts *TripleStore) DebugPrint() {
} }
} }
func (ts *TripleStore) GetIdFor(name string) graph.TSVal { func (ts *TripleStore) ValueOf(name string) graph.Value {
return ts.idMap[name] return ts.idMap[name]
} }
func (ts *TripleStore) GetNameFor(id graph.TSVal) string { func (ts *TripleStore) NameOf(id graph.Value) string {
return ts.revIdMap[id.(int64)] return ts.revIdMap[id.(int64)]
} }
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator { func (ts *TripleStore) TriplesAllIterator() graph.Iterator {
return iterator.NewInt64(0, ts.Size()) return iterator.NewInt64(0, ts.Size())
} }
@ -259,12 +259,12 @@ func (ts *TripleStore) FixedIterator() graph.FixedIterator {
return iterator.NewFixedIteratorWithCompare(iterator.BasicEquality) return iterator.NewFixedIteratorWithCompare(iterator.BasicEquality)
} }
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, d graph.Direction) graph.TSVal { func (ts *TripleStore) TripleDirection(val graph.Value, d graph.Direction) graph.Value {
name := ts.GetTriple(val).Get(d) name := ts.Triple(val).Get(d)
return ts.GetIdFor(name) return ts.ValueOf(name)
} }
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator { func (ts *TripleStore) NodesAllIterator() graph.Iterator {
return NewMemstoreAllIterator(ts) return NewMemstoreAllIterator(ts)
} }
func (ts *TripleStore) Close() {} func (ts *TripleStore) Close() {}

View file

@ -29,7 +29,7 @@ func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool
} }
func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) { func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) {
subs := it.GetSubIterators() subs := it.SubIterators()
if len(subs) != 1 { if len(subs) != 1 {
return it, false return it, false
} }
@ -41,7 +41,7 @@ func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bo
if !ok { if !ok {
panic("Sizes lie") panic("Sizes lie")
} }
newIt := ts.GetTripleIterator(it.Direction(), val) newIt := ts.TripleIterator(it.Direction(), val)
newIt.CopyTagsFrom(it) newIt.CopyTagsFrom(it)
for _, tag := range primary.Tags() { for _, tag := range primary.Tags() {
newIt.AddFixedTag(tag, val) newIt.AddFixedTag(tag, val)

View file

@ -31,7 +31,7 @@ func TestMemstore(t *testing.T) {
So(ts.Size(), ShouldEqual, 11) So(ts.Size(), ShouldEqual, 11)
}) })
Convey("It should have an Id Space that makes sense", func() { Convey("It should have an Id Space that makes sense", func() {
v := ts.GetIdFor("C") v := ts.ValueOf("C")
So(v.(int64), ShouldEqual, 4) So(v.(int64), ShouldEqual, 4)
}) })
}) })
@ -40,13 +40,13 @@ func TestMemstore(t *testing.T) {
func TestIteratorsAndNextResultOrderA(t *testing.T) { func TestIteratorsAndNextResultOrderA(t *testing.T) {
ts := MakeTestingMemstore() ts := MakeTestingMemstore()
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("C")) fixed.Add(ts.ValueOf("C"))
all := ts.GetNodesAllIterator() all := ts.NodesAllIterator()
lto := iterator.NewLinksTo(ts, all, graph.Object) lto := iterator.NewLinksTo(ts, all, graph.Object)
innerAnd := iterator.NewAnd() innerAnd := iterator.NewAnd()
fixed2 := ts.FixedIterator() fixed2 := ts.FixedIterator()
fixed2.AddValue(ts.GetIdFor("follows")) fixed2.Add(ts.ValueOf("follows"))
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate) lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd.AddSubIterator(lto2) innerAnd.AddSubIterator(lto2)
innerAnd.AddSubIterator(lto) innerAnd.AddSubIterator(lto)
@ -58,19 +58,19 @@ func TestIteratorsAndNextResultOrderA(t *testing.T) {
if !ok { if !ok {
t.Error("Expected one matching subtree") t.Error("Expected one matching subtree")
} }
if ts.GetNameFor(val) != "C" { if ts.NameOf(val) != "C" {
t.Errorf("Matching subtree should be %s, got %s", "barak", ts.GetNameFor(val)) t.Errorf("Matching subtree should be %s, got %s", "barak", ts.NameOf(val))
} }
expected := make([]string, 2) expected := make([]string, 2)
expected[0] = "B" expected[0] = "B"
expected[1] = "D" expected[1] = "D"
actualOut := make([]string, 2) actualOut := make([]string, 2)
actualOut[0] = ts.GetNameFor(all.LastResult()) actualOut[0] = ts.NameOf(all.Result())
nresultOk := outerAnd.NextResult() nresultOk := outerAnd.NextResult()
if !nresultOk { if !nresultOk {
t.Error("Expected two results got one") t.Error("Expected two results got one")
} }
actualOut[1] = ts.GetNameFor(all.LastResult()) actualOut[1] = ts.NameOf(all.Result())
nresultOk = outerAnd.NextResult() nresultOk = outerAnd.NextResult()
if nresultOk { if nresultOk {
t.Error("Expected two results got three") t.Error("Expected two results got three")
@ -98,7 +98,7 @@ func CompareStringSlices(t *testing.T, expected []string, actual []string) {
func TestLinksToOptimization(t *testing.T) { func TestLinksToOptimization(t *testing.T) {
ts := MakeTestingMemstore() ts := MakeTestingMemstore()
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("cool")) fixed.Add(ts.ValueOf("cool"))
lto := iterator.NewLinksTo(ts, fixed, graph.Object) lto := iterator.NewLinksTo(ts, fixed, graph.Object)
lto.AddTag("foo") lto.AddTag("foo")
newIt, changed := lto.Optimize() newIt, changed := lto.Optimize()
@ -122,10 +122,10 @@ func TestRemoveTriple(t *testing.T) {
ts := MakeTestingMemstore() ts := MakeTestingMemstore()
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""}) ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("E")) fixed.Add(ts.ValueOf("E"))
lto := iterator.NewLinksTo(ts, fixed, graph.Subject) lto := iterator.NewLinksTo(ts, fixed, graph.Subject)
fixed2 := ts.FixedIterator() fixed2 := ts.FixedIterator()
fixed2.AddValue(ts.GetIdFor("follows")) fixed2.Add(ts.ValueOf("follows"))
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate) lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd := iterator.NewAnd() innerAnd := iterator.NewAnd()
innerAnd.AddSubIterator(lto2) innerAnd.AddSubIterator(lto2)

View file

@ -39,11 +39,11 @@ type Iterator struct {
collection string collection string
} }
func NewIterator(ts *TripleStore, collection string, d graph.Direction, val graph.TSVal) *Iterator { func NewIterator(ts *TripleStore, collection string, d graph.Direction, val graph.Value) *Iterator {
var m Iterator var m Iterator
iterator.BaseInit(&m.Base) iterator.BaseInit(&m.Base)
m.name = ts.GetNameFor(val) m.name = ts.NameOf(val)
m.collection = collection m.collection = collection
switch d { switch d {
case graph.Subject: case graph.Subject:
@ -109,7 +109,7 @@ func (it *Iterator) Clone() graph.Iterator {
return newM return newM
} }
func (it *Iterator) Next() (graph.TSVal, bool) { func (it *Iterator) Next() (graph.Value, bool) {
var result struct { var result struct {
Id string "_id" Id string "_id"
//Sub string "Sub" //Sub string "Sub"
@ -128,7 +128,7 @@ func (it *Iterator) Next() (graph.TSVal, bool) {
return result.Id, true return result.Id, true
} }
func (it *Iterator) Check(v graph.TSVal) bool { func (it *Iterator) Check(v graph.Value) bool {
graph.CheckLogIn(it, v) graph.CheckLogIn(it, v)
if it.isAll { if it.isAll {
it.Last = v it.Last = v
@ -171,9 +171,9 @@ func (it *Iterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s size:%d %s %s)", strings.Repeat(" ", indent), it.Type(), size, it.hash, it.name) return fmt.Sprintf("%s(%s size:%d %s %s)", strings.Repeat(" ", indent), it.Type(), size, it.hash, it.name)
} }
func (it *Iterator) GetStats() *graph.IteratorStats { func (it *Iterator) Stats() graph.IteratorStats {
size, _ := it.Size() size, _ := it.Size()
return &graph.IteratorStats{ return graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 5, NextCost: 5,
Size: size, Size: size,

View file

@ -37,7 +37,7 @@ type TripleStore struct {
idCache *IDLru idCache *IDLru
} }
func CreateNewMongoGraph(addr string, options graph.OptionsDict) bool { func CreateNewMongoGraph(addr string, options graph.Options) bool {
conn, err := mgo.Dial(addr) conn, err := mgo.Dial(addr)
if err != nil { if err != nil {
glog.Fatal("Error connecting: ", err) glog.Fatal("Error connecting: ", err)
@ -45,7 +45,7 @@ func CreateNewMongoGraph(addr string, options graph.OptionsDict) bool {
} }
conn.SetSafe(&mgo.Safe{}) conn.SetSafe(&mgo.Safe{})
dbName := DefaultDBName dbName := DefaultDBName
if val, ok := options.GetStringKey("database_name"); ok { if val, ok := options.StringKey("database_name"); ok {
dbName = val dbName = val
} }
db := conn.DB(dbName) db := conn.DB(dbName)
@ -66,7 +66,7 @@ func CreateNewMongoGraph(addr string, options graph.OptionsDict) bool {
return true return true
} }
func NewTripleStore(addr string, options graph.OptionsDict) *TripleStore { func NewTripleStore(addr string, options graph.Options) *TripleStore {
var ts TripleStore var ts TripleStore
conn, err := mgo.Dial(addr) conn, err := mgo.Dial(addr)
if err != nil { if err != nil {
@ -74,7 +74,7 @@ func NewTripleStore(addr string, options graph.OptionsDict) *TripleStore {
} }
conn.SetSafe(&mgo.Safe{}) conn.SetSafe(&mgo.Safe{})
dbName := DefaultDBName dbName := DefaultDBName
if val, ok := options.GetStringKey("database_name"); ok { if val, ok := options.StringKey("database_name"); ok {
dbName = val dbName = val
} }
ts.db = conn.DB(dbName) ts.db = conn.DB(dbName)
@ -108,7 +108,7 @@ type MongoNode struct {
func (ts *TripleStore) updateNodeBy(node_name string, inc int) { func (ts *TripleStore) updateNodeBy(node_name string, inc int) {
var size MongoNode var size MongoNode
node := ts.GetIdFor(node_name) node := ts.ValueOf(node_name)
err := ts.db.C("nodes").FindId(node).One(&size) err := ts.db.C("nodes").FindId(node).One(&size)
if err != nil { if err != nil {
if err.Error() == "not found" { if err.Error() == "not found" {
@ -209,7 +209,7 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
} }
} }
func (ts *TripleStore) GetTriple(val graph.TSVal) *graph.Triple { func (ts *TripleStore) Triple(val graph.Value) *graph.Triple {
var bsonDoc bson.M var bsonDoc bson.M
err := ts.db.C("triples").FindId(val.(string)).One(&bsonDoc) err := ts.db.C("triples").FindId(val.(string)).One(&bsonDoc)
if err != nil { if err != nil {
@ -223,23 +223,23 @@ func (ts *TripleStore) GetTriple(val graph.TSVal) *graph.Triple {
} }
} }
func (ts *TripleStore) GetTripleIterator(d graph.Direction, val graph.TSVal) graph.Iterator { func (ts *TripleStore) TripleIterator(d graph.Direction, val graph.Value) graph.Iterator {
return NewIterator(ts, "triples", d, val) return NewIterator(ts, "triples", d, val)
} }
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator { func (ts *TripleStore) NodesAllIterator() graph.Iterator {
return NewAllIterator(ts, "nodes") return NewAllIterator(ts, "nodes")
} }
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator { func (ts *TripleStore) TriplesAllIterator() graph.Iterator {
return NewAllIterator(ts, "triples") return NewAllIterator(ts, "triples")
} }
func (ts *TripleStore) GetIdFor(s string) graph.TSVal { func (ts *TripleStore) ValueOf(s string) graph.Value {
return ts.ConvertStringToByteHash(s) return ts.ConvertStringToByteHash(s)
} }
func (ts *TripleStore) GetNameFor(v graph.TSVal) string { func (ts *TripleStore) NameOf(v graph.Value) string {
val, ok := ts.idCache.Get(v.(string)) val, ok := ts.idCache.Get(v.(string))
if ok { if ok {
return val return val
@ -262,7 +262,7 @@ func (ts *TripleStore) Size() int64 {
return int64(count) return int64(count)
} }
func compareStrings(a, b graph.TSVal) bool { func compareStrings(a, b graph.Value) bool {
return a.(string) == b.(string) return a.(string) == b.(string)
} }
@ -274,7 +274,7 @@ func (ts *TripleStore) Close() {
ts.db.Session.Close() ts.db.Session.Close()
} }
func (ts *TripleStore) GetTripleDirection(in graph.TSVal, d graph.Direction) graph.TSVal { func (ts *TripleStore) TripleDirection(in graph.Value, d graph.Direction) graph.Value {
// Maybe do the trick here // Maybe do the trick here
var offset int var offset int
switch d { switch d {

View file

@ -29,7 +29,7 @@ func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool
} }
func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) { func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) {
subs := it.GetSubIterators() subs := it.SubIterators()
if len(subs) != 1 { if len(subs) != 1 {
return it, false return it, false
} }
@ -41,7 +41,7 @@ func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bo
if !ok { if !ok {
panic("Sizes lie") panic("Sizes lie")
} }
newIt := ts.GetTripleIterator(it.Direction(), val) newIt := ts.TripleIterator(it.Direction(), val)
newIt.CopyTagsFrom(it) newIt.CopyTagsFrom(it)
for _, tag := range primary.Tags() { for _, tag := range primary.Tags() {
newIt.AddFixedTag(tag, val) newIt.AddFixedTag(tag, val)

View file

@ -17,11 +17,11 @@ package graph
import "fmt" import "fmt"
type ResultTree struct { type ResultTree struct {
result TSVal result Value
subtrees []*ResultTree subtrees []*ResultTree
} }
func NewResultTree(result TSVal) *ResultTree { func NewResultTree(result Value) *ResultTree {
return &ResultTree{result: result} return &ResultTree{result: result}
} }
@ -48,11 +48,11 @@ func StringResultTreeEvaluator(it Iterator) string {
if !ok { if !ok {
break break
} }
out += it.GetResultTree().String() out += it.ResultTree().String()
out += "\n" out += "\n"
for it.NextResult() == true { for it.NextResult() == true {
out += " " out += " "
out += it.GetResultTree().String() out += it.ResultTree().String()
out += "\n" out += "\n"
} }
} }

View file

@ -188,7 +188,7 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
var out graph.Iterator var out graph.Iterator
nodeID := getIdentString(tree) nodeID := getIdentString(tree)
if tree.Children[0].Name == "Variable" { if tree.Children[0].Name == "Variable" {
allIt := ts.GetNodesAllIterator() allIt := ts.NodesAllIterator()
allIt.AddTag(nodeID) allIt.AddTag(nodeID)
out = allIt out = allIt
} else { } else {
@ -197,7 +197,7 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
n = nodeID[1:] n = nodeID[1:]
} }
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor(n)) fixed.Add(ts.ValueOf(n))
out = fixed out = fixed
} }
return out return out

View file

@ -47,7 +47,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
So(it.Type(), ShouldEqual, "and") So(it.Type(), ShouldEqual, "and")
out, ok := it.Next() out, ok := it.Next()
So(ok, ShouldBeTrue) So(ok, ShouldBeTrue)
So(out, ShouldEqual, ts.GetIdFor("i")) So(out, ShouldEqual, ts.ValueOf("i"))
}) })
Convey("It can get an internal linkage", func() { Convey("It can get an internal linkage", func() {
@ -57,7 +57,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
So(it.Type(), ShouldEqual, "and") So(it.Type(), ShouldEqual, "and")
out, ok := it.Next() out, ok := it.Next()
So(ok, ShouldBeTrue) So(ok, ShouldBeTrue)
So(out, ShouldEqual, ts.GetIdFor("i")) So(out, ShouldEqual, ts.ValueOf("i"))
}) })
}) })
@ -78,8 +78,8 @@ func TestTreeConstraintParse(t *testing.T) {
if !ok { if !ok {
t.Error("Got no results") t.Error("Got no results")
} }
if out != ts.GetIdFor("i") { if out != ts.ValueOf("i") {
t.Errorf("Got %d, expected %d", out, ts.GetIdFor("i")) t.Errorf("Got %d, expected %d", out, ts.ValueOf("i"))
} }
} }
@ -95,10 +95,10 @@ func TestTreeConstraintTagParse(t *testing.T) {
if !ok { if !ok {
t.Error("Got no results") t.Error("Got no results")
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
if ts.GetNameFor(tags["$a"]) != "food" { if ts.NameOf(tags["$a"]) != "food" {
t.Errorf("Got %s, expected food", ts.GetNameFor(tags["$a"])) t.Errorf("Got %s, expected food", ts.NameOf(tags["$a"]))
} }
} }
@ -119,8 +119,8 @@ func TestMultipleConstraintParse(t *testing.T) {
if !ok { if !ok {
t.Error("Got no results") t.Error("Got no results")
} }
if out != ts.GetIdFor("i") { if out != ts.ValueOf("i") {
t.Errorf("Got %d, expected %d", out, ts.GetIdFor("i")) t.Errorf("Got %d, expected %d", out, ts.ValueOf("i"))
} }
_, ok = it.Next() _, ok = it.Next()
if ok { if ok {

View file

@ -81,16 +81,16 @@ func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
if !ok { if !ok {
break break
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
out <- &tags out <- &tags
nResults++ nResults++
if nResults > limit && limit != -1 { if nResults > limit && limit != -1 {
break break
} }
for it.NextResult() == true { for it.NextResult() == true {
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
out <- &tags out <- &tags
nResults++ nResults++
if nResults > limit && limit != -1 { if nResults > limit && limit != -1 {
@ -103,10 +103,10 @@ func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
func (s *Session) ToText(result interface{}) string { func (s *Session) ToText(result interface{}) string {
out := fmt.Sprintln("****") out := fmt.Sprintln("****")
tags := result.(*map[string]graph.TSVal) tags := result.(map[string]graph.Value)
tagKeys := make([]string, len(*tags)) tagKeys := make([]string, len(tags))
i := 0 i := 0
for k, _ := range *tags { for k := range tags {
tagKeys[i] = k tagKeys[i] = k
i++ i++
} }
@ -115,7 +115,7 @@ func (s *Session) ToText(result interface{}) string {
if k == "$_" { if k == "$_" {
continue continue
} }
out += fmt.Sprintf("%s : %s\n", k, s.ts.GetNameFor((*tags)[k])) out += fmt.Sprintf("%s : %s\n", k, s.ts.NameOf(tags[k]))
} }
return out return out
} }

View file

@ -26,14 +26,14 @@ import (
) )
// Defines an opaque "triple store value" type. However the backend wishes to // Defines an opaque "triple store value" type. However the backend wishes to
// implement it, a TSVal is merely a token to a triple or a node that the backing // implement it, a Value is merely a token to a triple or a node that the backing
// store itself understands, and the base iterators pass around. // store itself understands, and the base iterators pass around.
// //
// For example, in a very traditional, graphd-style graph, these are int64s // For example, in a very traditional, graphd-style graph, these are int64s
// (guids of the primitives). In a very direct sort of graph, these could be // (guids of the primitives). In a very direct sort of graph, these could be
// pointers to structs, or merely triples, or whatever works best for the // pointers to structs, or merely triples, or whatever works best for the
// backing store. // backing store.
type TSVal interface{} type Value interface{}
type TripleStore interface { type TripleStore interface {
// Add a triple to the store. // Add a triple to the store.
@ -47,29 +47,29 @@ type TripleStore interface {
RemoveTriple(*Triple) RemoveTriple(*Triple)
// Given an opaque token, returns the triple for that token from the store. // Given an opaque token, returns the triple for that token from the store.
GetTriple(TSVal) *Triple Triple(Value) *Triple
// Given a direction and a token, creates an iterator of links which have // Given a direction and a token, creates an iterator of links which have
// that node token in that directional field. // that node token in that directional field.
GetTripleIterator(Direction, TSVal) Iterator TripleIterator(Direction, Value) Iterator
// Returns an iterator enumerating all nodes in the graph. // Returns an iterator enumerating all nodes in the graph.
GetNodesAllIterator() Iterator NodesAllIterator() Iterator
// Returns an iterator enumerating all links in the graph. // Returns an iterator enumerating all links in the graph.
GetTriplesAllIterator() Iterator TriplesAllIterator() Iterator
// Given a node ID, return the opaque token used by the TripleStore // Given a node ID, return the opaque token used by the TripleStore
// to represent that id. // to represent that id.
GetIdFor(string) TSVal ValueOf(string) Value
// Given an opaque token, return the node that it represents. // Given an opaque token, return the node that it represents.
GetNameFor(TSVal) string NameOf(Value) string
// Returns the number of triples currently stored. // Returns the number of triples currently stored.
Size() int64 Size() int64
// Creates a fixed iterator which can compare TSVals // Creates a fixed iterator which can compare Values
FixedIterator() FixedIterator FixedIterator() FixedIterator
// Optimize an iterator in the context of the triple store. // Optimize an iterator in the context of the triple store.
@ -88,13 +88,13 @@ type TripleStore interface {
// gives the TripleStore the opportunity to make this optimization. // gives the TripleStore the opportunity to make this optimization.
// //
// Iterators will call this. At worst, a valid implementation is // Iterators will call this. At worst, a valid implementation is
// self.GetIdFor(self.GetTriple(triple_id).Get(dir)) // ts.IdFor(ts.Triple(triple_id).Get(dir))
GetTripleDirection(triple_id TSVal, d Direction) TSVal TripleDirection(triple_id Value, d Direction) Value
} }
type OptionsDict map[string]interface{} type Options map[string]interface{}
func (d OptionsDict) GetIntKey(key string) (int, bool) { func (d Options) IntKey(key string) (int, bool) {
if val, ok := d[key]; ok { if val, ok := d[key]; ok {
switch vv := val.(type) { switch vv := val.(type) {
case float64: case float64:
@ -106,7 +106,7 @@ func (d OptionsDict) GetIntKey(key string) (int, bool) {
return 0, false return 0, false
} }
func (d OptionsDict) GetStringKey(key string) (string, bool) { func (d Options) StringKey(key string) (string, bool) {
if val, ok := d[key]; ok { if val, ok := d[key]; ok {
switch vv := val.(type) { switch vv := val.(type) {
case string: case string:

View file

@ -68,14 +68,14 @@ func makeListOfStringsFromArrayValue(obj *otto.Object) []string {
func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator { func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator {
if val.IsNull() || val.IsUndefined() { if val.IsNull() || val.IsUndefined() {
return ts.GetNodesAllIterator() return ts.NodesAllIterator()
} }
if val.IsPrimitive() { if val.IsPrimitive() {
thing, _ := val.Export() thing, _ := val.Export()
switch v := thing.(type) { switch v := thing.(type) {
case string: case string:
it := ts.FixedIterator() it := ts.FixedIterator()
it.AddValue(ts.GetIdFor(v)) it.Add(ts.ValueOf(v))
return it return it
default: default:
glog.Errorln("Trying to build unknown primitive value.") glog.Errorln("Trying to build unknown primitive value.")
@ -89,7 +89,7 @@ func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator
strings := makeListOfStringsFromArrayValue(val.Object()) strings := makeListOfStringsFromArrayValue(val.Object())
it := ts.FixedIterator() it := ts.FixedIterator()
for _, x := range strings { for _, x := range strings {
it.AddValue(ts.GetIdFor(x)) it.Add(ts.ValueOf(x))
} }
return it return it
case "Number": case "Number":
@ -101,7 +101,7 @@ func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator
case "String": case "String":
it := ts.FixedIterator() it := ts.FixedIterator()
str, _ := val.ToString() str, _ := val.ToString()
it.AddValue(ts.GetIdFor(str)) it.Add(ts.ValueOf(str))
return it return it
default: default:
glog.Errorln("Trying to handle unsupported Javascript value.") glog.Errorln("Trying to handle unsupported Javascript value.")
@ -120,7 +120,7 @@ func buildInOutIterator(obj *otto.Object, ts graph.TripleStore, base graph.Itera
length, _ := lengthVal.ToInteger() length, _ := lengthVal.ToInteger()
var predicateNodeIterator graph.Iterator var predicateNodeIterator graph.Iterator
if length == 0 { if length == 0 {
predicateNodeIterator = ts.GetNodesAllIterator() predicateNodeIterator = ts.NodesAllIterator()
} else { } else {
zero, _ := argArray.Get("0") zero, _ := argArray.Get("0")
predicateNodeIterator = buildIteratorFromValue(zero, ts) predicateNodeIterator = buildIteratorFromValue(zero, ts)
@ -168,11 +168,11 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
switch kind { switch kind {
case "vertex": case "vertex":
if len(stringArgs) == 0 { if len(stringArgs) == 0 {
it = ts.GetNodesAllIterator() it = ts.NodesAllIterator()
} else { } else {
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
for _, name := range stringArgs { for _, name := range stringArgs {
fixed.AddValue(ts.GetIdFor(name)) fixed.Add(ts.ValueOf(name))
} }
it = fixed it = fixed
} }
@ -182,7 +182,7 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
it.AddTag(tag) it.AddTag(tag)
} }
case "save": case "save":
all := ts.GetNodesAllIterator() all := ts.NodesAllIterator()
if len(stringArgs) > 2 || len(stringArgs) == 0 { if len(stringArgs) > 2 || len(stringArgs) == 0 {
return iterator.NewNull() return iterator.NewNull()
} }
@ -192,7 +192,7 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
all.AddTag(stringArgs[0]) all.AddTag(stringArgs[0])
} }
predFixed := ts.FixedIterator() predFixed := ts.FixedIterator()
predFixed.AddValue(ts.GetIdFor(stringArgs[0])) predFixed.Add(ts.ValueOf(stringArgs[0]))
subAnd := iterator.NewAnd() subAnd := iterator.NewAnd()
subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate))
subAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Object)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Object))
@ -202,7 +202,7 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
and.AddSubIterator(subIt) and.AddSubIterator(subIt)
it = and it = and
case "saver": case "saver":
all := ts.GetNodesAllIterator() all := ts.NodesAllIterator()
if len(stringArgs) > 2 || len(stringArgs) == 0 { if len(stringArgs) > 2 || len(stringArgs) == 0 {
return iterator.NewNull() return iterator.NewNull()
} }
@ -212,7 +212,7 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
all.AddTag(stringArgs[0]) all.AddTag(stringArgs[0])
} }
predFixed := ts.FixedIterator() predFixed := ts.FixedIterator()
predFixed.AddValue(ts.GetIdFor(stringArgs[0])) predFixed.Add(ts.ValueOf(stringArgs[0]))
subAnd := iterator.NewAnd() subAnd := iterator.NewAnd()
subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate))
subAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Subject)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Subject))
@ -227,10 +227,10 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
return iterator.NewNull() return iterator.NewNull()
} }
for _, name := range stringArgs[1:] { for _, name := range stringArgs[1:] {
fixed.AddValue(ts.GetIdFor(name)) fixed.Add(ts.ValueOf(name))
} }
predFixed := ts.FixedIterator() predFixed := ts.FixedIterator()
predFixed.AddValue(ts.GetIdFor(stringArgs[0])) predFixed.Add(ts.ValueOf(stringArgs[0]))
subAnd := iterator.NewAnd() subAnd := iterator.NewAnd()
subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, predFixed, graph.Predicate))
subAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, graph.Object)) subAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, graph.Object))
@ -263,7 +263,7 @@ func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.
case "is": case "is":
fixed := ts.FixedIterator() fixed := ts.FixedIterator()
for _, name := range stringArgs { for _, name := range stringArgs {
fixed.AddValue(ts.GetIdFor(name)) fixed.Add(ts.ValueOf(name))
} }
and := iterator.NewAnd() and := iterator.NewAnd()
and.AddSubIterator(fixed) and.AddSubIterator(fixed)

View file

@ -135,10 +135,10 @@ func mapFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionC
} }
} }
func tagsToValueMap(m map[string]graph.TSVal, ses *Session) map[string]string { func tagsToValueMap(m map[string]graph.Value, ses *Session) map[string]string {
outputMap := make(map[string]string) outputMap := make(map[string]string)
for k, v := range m { for k, v := range m {
outputMap[k] = ses.ts.GetNameFor(v) outputMap[k] = ses.ts.NameOf(v)
} }
return outputMap return outputMap
} }
@ -155,8 +155,8 @@ func runIteratorToArray(it graph.Iterator, ses *Session, limit int) []map[string
if !ok { if !ok {
break break
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
output = append(output, tagsToValueMap(tags, ses)) output = append(output, tagsToValueMap(tags, ses))
count++ count++
if limit >= 0 && count >= limit { if limit >= 0 && count >= limit {
@ -166,8 +166,8 @@ func runIteratorToArray(it graph.Iterator, ses *Session, limit int) []map[string
if ses.doHalt { if ses.doHalt {
return nil return nil
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
output = append(output, tagsToValueMap(tags, ses)) output = append(output, tagsToValueMap(tags, ses))
count++ count++
if limit >= 0 && count >= limit { if limit >= 0 && count >= limit {
@ -191,7 +191,7 @@ func runIteratorToArrayNoTags(it graph.Iterator, ses *Session, limit int) []stri
if !ok { if !ok {
break break
} }
output = append(output, ses.ts.GetNameFor(val)) output = append(output, ses.ts.NameOf(val))
count++ count++
if limit >= 0 && count >= limit { if limit >= 0 && count >= limit {
break break
@ -212,8 +212,8 @@ func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Valu
if !ok { if !ok {
break break
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses))
val, _ = callback.Call(this.This, val) val, _ = callback.Call(this.This, val)
count++ count++
@ -224,8 +224,8 @@ func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Valu
if ses.doHalt { if ses.doHalt {
return return
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses))
val, _ = callback.Call(this.This, val) val, _ = callback.Call(this.This, val)
count++ count++
@ -253,8 +253,8 @@ func runIteratorOnSession(it graph.Iterator, ses *Session) {
if !ok { if !ok {
break break
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags}) cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags})
if !cont { if !cont {
break break
@ -263,8 +263,8 @@ func runIteratorOnSession(it graph.Iterator, ses *Session) {
if ses.doHalt { if ses.doHalt {
return return
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags}) cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags})
if !cont { if !cont {
break break

View file

@ -63,7 +63,7 @@ func runQueryGetTag(query string, tag string) ([]string, int) {
if data.val == nil { if data.val == nil {
val := (*data.actualResults)[tag] val := (*data.actualResults)[tag]
if val != nil { if val != nil {
output = append(output, js.ts.GetNameFor(val)) output = append(output, js.ts.NameOf(val))
} }
} }
} }

View file

@ -65,7 +65,7 @@ type GremlinResult struct {
metaresult bool metaresult bool
err string err string
val *otto.Value val *otto.Value
actualResults *map[string]graph.TSVal actualResults *map[string]graph.Value
} }
func (s *Session) ToggleDebug() { func (s *Session) ToggleDebug() {
@ -202,7 +202,7 @@ func (s *Session) ToText(result interface{}) string {
if k == "$_" { if k == "$_" {
continue continue
} }
out += fmt.Sprintf("%s : %s\n", k, s.ts.GetNameFor((*tags)[k])) out += fmt.Sprintf("%s : %s\n", k, s.ts.NameOf((*tags)[k]))
} }
} else { } else {
if data.val.IsObject() { if data.val.IsObject() {
@ -234,7 +234,7 @@ func (ses *Session) BuildJson(result interface{}) {
} }
sort.Strings(tagKeys) sort.Strings(tagKeys)
for _, k := range tagKeys { for _, k := range tagKeys {
obj[k] = ses.ts.GetNameFor((*tags)[k]) obj[k] = ses.ts.NameOf((*tags)[k])
} }
ses.dataOutput = append(ses.dataOutput, obj) ses.dataOutput = append(ses.dataOutput, obj)
} else { } else {

View file

@ -27,12 +27,12 @@ import (
func (q *Query) buildFixed(s string) graph.Iterator { func (q *Query) buildFixed(s string) graph.Iterator {
f := q.ses.ts.FixedIterator() f := q.ses.ts.FixedIterator()
f.AddValue(q.ses.ts.GetIdFor(s)) f.Add(q.ses.ts.ValueOf(s))
return f return f
} }
func (q *Query) buildResultIterator(path Path) graph.Iterator { func (q *Query) buildResultIterator(path Path) graph.Iterator {
all := q.ses.ts.GetNodesAllIterator() all := q.ses.ts.NodesAllIterator()
all.AddTag(string(path)) all.AddTag(string(path))
return all return all
} }
@ -103,7 +103,7 @@ func (q *Query) buildIteratorTreeInternal(query interface{}, path Path) (it grap
func (q *Query) buildIteratorTreeMapInternal(query map[string]interface{}, path Path) (graph.Iterator, error) { func (q *Query) buildIteratorTreeMapInternal(query map[string]interface{}, path Path) (graph.Iterator, error) {
it := iterator.NewAnd() it := iterator.NewAnd()
it.AddSubIterator(q.ses.ts.GetNodesAllIterator()) it.AddSubIterator(q.ses.ts.NodesAllIterator())
var err error var err error
err = nil err = nil
outputStructure := make(map[string]interface{}) outputStructure := make(map[string]interface{})
@ -138,7 +138,7 @@ func (q *Query) buildIteratorTreeMapInternal(query map[string]interface{}, path
} }
subAnd := iterator.NewAnd() subAnd := iterator.NewAnd()
predFixed := q.ses.ts.FixedIterator() predFixed := q.ses.ts.FixedIterator()
predFixed.AddValue(q.ses.ts.GetIdFor(pred)) predFixed.Add(q.ses.ts.ValueOf(pred))
subAnd.AddSubIterator(iterator.NewLinksTo(q.ses.ts, predFixed, graph.Predicate)) subAnd.AddSubIterator(iterator.NewLinksTo(q.ses.ts, predFixed, graph.Predicate))
if reverse { if reverse {
lto := iterator.NewLinksTo(q.ses.ts, builtIt, graph.Subject) lto := iterator.NewLinksTo(q.ses.ts, builtIt, graph.Subject)

View file

@ -20,14 +20,14 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
func (q *Query) treeifyResult(tags map[string]graph.TSVal) map[ResultPath]string { func (q *Query) treeifyResult(tags map[string]graph.Value) map[ResultPath]string {
// Transform the map into something a little more interesting. // Transform the map into something a little more interesting.
results := make(map[Path]string) results := make(map[Path]string)
for k, v := range tags { for k, v := range tags {
if v == nil { if v == nil {
continue continue
} }
results[Path(k)] = q.ses.ts.GetNameFor(v) results[Path(k)] = q.ses.ts.NameOf(v)
} }
resultPaths := make(map[ResultPath]string) resultPaths := make(map[ResultPath]string)
for k, v := range results { for k, v := range results {

View file

@ -92,19 +92,19 @@ func (s *Session) ExecInput(input string, c chan interface{}, limit int) {
if !ok { if !ok {
break break
} }
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
c <- &tags c <- tags
for it.NextResult() == true { for it.NextResult() == true {
tags := make(map[string]graph.TSVal) tags := make(map[string]graph.Value)
it.TagResults(&tags) it.TagResults(tags)
c <- &tags c <- tags
} }
} }
} }
func (s *Session) ToText(result interface{}) string { func (s *Session) ToText(result interface{}) string {
tags := *(result.(*map[string]graph.TSVal)) tags := result.(map[string]graph.Value)
out := fmt.Sprintln("****") out := fmt.Sprintln("****")
tagKeys := make([]string, len(tags)) tagKeys := make([]string, len(tags))
s.currentQuery.treeifyResult(tags) s.currentQuery.treeifyResult(tags)
@ -121,13 +121,13 @@ func (s *Session) ToText(result interface{}) string {
if k == "$_" { if k == "$_" {
continue continue
} }
out += fmt.Sprintf("%s : %s\n", k, s.ts.GetNameFor(tags[k])) out += fmt.Sprintf("%s : %s\n", k, s.ts.NameOf(tags[k]))
} }
return out return out
} }
func (s *Session) BuildJson(result interface{}) { func (s *Session) BuildJson(result interface{}) {
s.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal))) s.currentQuery.treeifyResult(result.(map[string]graph.Value))
} }
func (s *Session) GetJson() (interface{}, error) { func (s *Session) GetJson() (interface{}, error) {