Move UID handling from Base
Also clean up some of the value creation code.
This commit is contained in:
parent
1604dca737
commit
01b7278c3a
13 changed files with 198 additions and 92 deletions
|
|
@ -32,6 +32,7 @@ import (
|
|||
// An All iterator across a range of int64 values, from `max` to `min`.
|
||||
type Int64 struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
max, min int64
|
||||
at int64
|
||||
|
|
@ -39,14 +40,20 @@ type Int64 struct {
|
|||
|
||||
// Creates a new Int64 with the given range.
|
||||
func NewInt64(min, max int64) *Int64 {
|
||||
var all Int64
|
||||
all := Int64{
|
||||
uid: NextUID(),
|
||||
min: min,
|
||||
max: max,
|
||||
at: min,
|
||||
}
|
||||
BaseInit(&all.Base)
|
||||
all.max = max
|
||||
all.min = min
|
||||
all.at = min
|
||||
return &all
|
||||
}
|
||||
|
||||
func (it *Int64) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Start back at the beginning
|
||||
func (it *Int64) Reset() {
|
||||
it.at = it.min
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
// be Next()ed if next is called.
|
||||
type And struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
internalIterators []graph.Iterator
|
||||
itCount int
|
||||
|
|
@ -35,13 +36,18 @@ type And struct {
|
|||
|
||||
// Creates a new And iterator.
|
||||
func NewAnd() *And {
|
||||
var and And
|
||||
and := And{
|
||||
uid: NextUID(),
|
||||
internalIterators: make([]graph.Iterator, 0, 20),
|
||||
}
|
||||
BaseInit(&and.Base)
|
||||
and.internalIterators = make([]graph.Iterator, 0, 20)
|
||||
and.checkList = nil
|
||||
return &and
|
||||
}
|
||||
|
||||
func (it *And) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Reset all internal iterators
|
||||
func (it *And) Reset() {
|
||||
it.primaryIt.Reset()
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import (
|
|||
// an equality function.
|
||||
type Fixed struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
values []graph.Value
|
||||
lastIndex int
|
||||
|
|
@ -55,14 +56,19 @@ func newFixed() *Fixed {
|
|||
|
||||
// Creates a new Fixed iterator with a custom comparitor.
|
||||
func NewFixedIteratorWithCompare(compareFn Equality) *Fixed {
|
||||
var it Fixed
|
||||
it := Fixed{
|
||||
uid: NextUID(),
|
||||
values: make([]graph.Value, 0, 20),
|
||||
cmp: compareFn,
|
||||
}
|
||||
BaseInit(&it.Base)
|
||||
it.values = make([]graph.Value, 0, 20)
|
||||
it.lastIndex = 0
|
||||
it.cmp = compareFn
|
||||
return &it
|
||||
}
|
||||
|
||||
func (it *Fixed) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
func (it *Fixed) Reset() {
|
||||
it.lastIndex = 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ import (
|
|||
// and a temporary holder for the iterator generated on Check().
|
||||
type HasA struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
ts graph.TripleStore
|
||||
primaryIt graph.Iterator
|
||||
|
|
@ -57,14 +58,20 @@ type HasA struct {
|
|||
// Construct a new HasA iterator, given the triple subiterator, and the triple
|
||||
// direction for which it stands.
|
||||
func NewHasA(ts graph.TripleStore, subIt graph.Iterator, d graph.Direction) *HasA {
|
||||
var hasa HasA
|
||||
hasa := HasA{
|
||||
uid: NextUID(),
|
||||
ts: ts,
|
||||
primaryIt: subIt,
|
||||
dir: d,
|
||||
}
|
||||
BaseInit(&hasa.Base)
|
||||
hasa.ts = ts
|
||||
hasa.primaryIt = subIt
|
||||
hasa.dir = d
|
||||
return &hasa
|
||||
}
|
||||
|
||||
func (it *HasA) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Return our sole subiterator.
|
||||
func (it *HasA) SubIterators() []graph.Iterator {
|
||||
return []graph.Iterator{it.primaryIt}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,6 @@ import (
|
|||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
|
|
@ -38,20 +36,12 @@ func NextUID() uint64 {
|
|||
type Base struct {
|
||||
Last graph.Value
|
||||
canNext bool
|
||||
uid uint64
|
||||
}
|
||||
|
||||
// Called by subclases.
|
||||
func BaseInit(it *Base) {
|
||||
// Your basic iterator is nextable
|
||||
it.canNext = true
|
||||
if glog.V(2) {
|
||||
it.uid = NextUID()
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Base) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Prints a silly debug string. Most classes override.
|
||||
|
|
@ -115,12 +105,17 @@ func (it *Base) Reset() {}
|
|||
// so it's important to give it a special iterator.
|
||||
type Null struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
}
|
||||
|
||||
// Fairly useless New function.
|
||||
func NewNull() *Null {
|
||||
return &Null{}
|
||||
return &Null{uid: NextUID()}
|
||||
}
|
||||
|
||||
func (it *Null) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
func (it *Null) Tagger() *graph.Tagger {
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ import (
|
|||
// `next_it` is the tempoarary iterator held per result in `primary_it`.
|
||||
type LinksTo struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
ts graph.TripleStore
|
||||
primaryIt graph.Iterator
|
||||
|
|
@ -51,15 +52,21 @@ type LinksTo struct {
|
|||
// Construct a new LinksTo iterator around a direction and a subiterator of
|
||||
// nodes.
|
||||
func NewLinksTo(ts graph.TripleStore, it graph.Iterator, d graph.Direction) *LinksTo {
|
||||
var lto LinksTo
|
||||
lto := LinksTo{
|
||||
uid: NextUID(),
|
||||
ts: ts,
|
||||
primaryIt: it,
|
||||
dir: d,
|
||||
nextIt: &Null{},
|
||||
}
|
||||
BaseInit(<o.Base)
|
||||
lto.ts = ts
|
||||
lto.primaryIt = it
|
||||
lto.dir = d
|
||||
lto.nextIt = &Null{}
|
||||
return <o
|
||||
}
|
||||
|
||||
func (it *LinksTo) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
func (it *LinksTo) Reset() {
|
||||
it.primaryIt.Reset()
|
||||
if it.nextIt != nil {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import (
|
|||
// and whether the last check we received was true or false.
|
||||
type Optional struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
subIt graph.Iterator
|
||||
lastCheck bool
|
||||
|
|
@ -50,9 +51,14 @@ func NewOptional(it graph.Iterator) *Optional {
|
|||
BaseInit(&o.Base)
|
||||
o.canNext = false
|
||||
o.subIt = it
|
||||
o.uid = NextUID()
|
||||
return &o
|
||||
}
|
||||
|
||||
func (it *Optional) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
func (it *Optional) Reset() {
|
||||
it.subIt.Reset()
|
||||
it.lastCheck = false
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import (
|
|||
|
||||
type Or struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
isShortCircuiting bool
|
||||
internalIterators []graph.Iterator
|
||||
|
|
@ -38,23 +39,30 @@ type Or struct {
|
|||
}
|
||||
|
||||
func NewOr() *Or {
|
||||
var or Or
|
||||
or := Or{
|
||||
uid: NextUID(),
|
||||
internalIterators: make([]graph.Iterator, 0, 20),
|
||||
currentIterator: -1,
|
||||
}
|
||||
BaseInit(&or.Base)
|
||||
or.internalIterators = make([]graph.Iterator, 0, 20)
|
||||
or.isShortCircuiting = false
|
||||
or.currentIterator = -1
|
||||
return &or
|
||||
}
|
||||
|
||||
func NewShortCircuitOr() *Or {
|
||||
var or Or
|
||||
or := Or{
|
||||
uid: NextUID(),
|
||||
internalIterators: make([]graph.Iterator, 0, 20),
|
||||
isShortCircuiting: true,
|
||||
currentIterator: -1,
|
||||
}
|
||||
BaseInit(&or.Base)
|
||||
or.internalIterators = make([]graph.Iterator, 0, 20)
|
||||
or.isShortCircuiting = true
|
||||
or.currentIterator = -1
|
||||
return &or
|
||||
}
|
||||
|
||||
func (it *Or) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Reset all internal iterators
|
||||
func (it *Or) Reset() {
|
||||
for _, sub := range it.internalIterators {
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ const (
|
|||
|
||||
type Comparison struct {
|
||||
Base
|
||||
uid uint64
|
||||
tags graph.Tagger
|
||||
subIt graph.Iterator
|
||||
op Operator
|
||||
|
|
@ -55,15 +56,21 @@ type Comparison struct {
|
|||
}
|
||||
|
||||
func NewComparison(sub graph.Iterator, op Operator, val interface{}, ts graph.TripleStore) *Comparison {
|
||||
var vc Comparison
|
||||
vc := Comparison{
|
||||
uid: NextUID(),
|
||||
subIt: sub,
|
||||
op: op,
|
||||
val: val,
|
||||
ts: ts,
|
||||
}
|
||||
BaseInit(&vc.Base)
|
||||
vc.subIt = sub
|
||||
vc.op = op
|
||||
vc.val = val
|
||||
vc.ts = ts
|
||||
return &vc
|
||||
}
|
||||
|
||||
func (it *Comparison) UID() uint64 {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
// Here's the non-boilerplate part of the ValueComparison iterator. Given a value
|
||||
// and our operator, determine whether or not we meet the requirement.
|
||||
func (it *Comparison) doComparison(val graph.Value) bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue