Skip to content

Commit

Permalink
iterator: remove unused equality function for fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Dec 23, 2017
1 parent d994e2a commit b63faea
Show file tree
Hide file tree
Showing 38 changed files with 85 additions and 163 deletions.
3 changes: 1 addition & 2 deletions graph/bolt/bolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func TestOptimize(t *testing.T) {
testutil.MakeWriter(t, qs, opts, graphtest.MakeQuadSet()...)

// With an linksto-fixed pair
fixed := qs.FixedIterator()
fixed.Add(qs.ValueOf(quad.Raw("F")))
fixed := iterator.NewFixed(qs.ValueOf(quad.Raw("F")))
fixed.Tagger().Add("internal")
lto := iterator.NewLinksTo(qs, fixed, quad.Object)

Expand Down
15 changes: 2 additions & 13 deletions graph/bolt/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
"time"

"github.com/boltdb/bolt"
"github.com/cayleygraph/cayley/clog"

"github.com/cayleygraph/cayley/clog"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/graph/proto"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/quad/pquads"
Expand Down Expand Up @@ -60,7 +59,7 @@ type Token struct {
func (t *Token) IsNode() bool { return t.nodes }

func (t *Token) Key() interface{} {
return fmt.Sprint(t.bucket, t.key)
return string(bytes.Join([][]byte{t.bucket, t.key}, []byte{0}))
}

func clone(b []byte) []byte {
Expand Down Expand Up @@ -575,13 +574,3 @@ func (qs *QuadStore) QuadDirection(val graph.Value, d quad.Direction) graph.Valu
}
return qs.ValueOf(qs.Quad(v).Get(d))
}

func compareTokens(a, b graph.Value) bool {
atok := a.(*Token)
btok := b.(*Token)
return bytes.Equal(atok.key, btok.key) && bytes.Equal(atok.bucket, btok.bucket)
}

func (qs *QuadStore) FixedIterator() graph.FixedIterator {
return iterator.NewFixed(compareTokens)
}
11 changes: 0 additions & 11 deletions graph/gaedatastore/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

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

Expand Down Expand Up @@ -500,16 +499,6 @@ func (qs *QuadStore) NodeSize() int64 {
return foundMetadata.NodeCount
}

func compareTokens(a, b graph.Value) bool {
atok := a.(*Token)
btok := b.(*Token)
return atok.Kind == btok.Kind && atok.Hash == btok.Hash
}

func (qs *QuadStore) FixedIterator() graph.FixedIterator {
return iterator.NewFixed(compareTokens)
}

func (qs *QuadStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
return it, false
}
Expand Down
16 changes: 4 additions & 12 deletions graph/graphmock/graphmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ func (qs *Oldstore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
return &iterator.Null{}, false
}

func (qs *Oldstore) FixedIterator() graph.FixedIterator {
return iterator.NewFixed(iterator.Identity)
}

func (qs *Oldstore) Close() error { return nil }

func (qs *Oldstore) QuadDirection(graph.Value, quad.Direction) graph.Value {
Expand Down Expand Up @@ -144,16 +140,12 @@ func (qs *Store) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
return &iterator.Null{}, false
}

func (qs *Store) FixedIterator() graph.FixedIterator {
return iterator.NewFixed(iterator.Identity)
}

func (qs *Store) Close() error { return nil }

func (qs *Store) DebugPrint() {}

func (qs *Store) QuadIterator(d quad.Direction, i graph.Value) graph.Iterator {
fixed := qs.FixedIterator()
fixed := iterator.NewFixed()
v := i.(graph.PreFetchedValue).NameOf()
for _, q := range qs.Data {
if q.Get(d) == v {
Expand All @@ -173,15 +165,15 @@ func (qs *Store) NodesAllIterator() graph.Iterator {
}
}
}
fixed := qs.FixedIterator()
for k, _ := range set {
fixed := iterator.NewFixed()
for k := range set {
fixed.Add(graph.PreFetched(quad.Raw(k)))
}
return fixed
}

func (qs *Store) QuadsAllIterator() graph.Iterator {
fixed := qs.FixedIterator()
fixed := iterator.NewFixed()
for _, q := range qs.Data {
fixed.Add(quadValue{q})
}
Expand Down
6 changes: 2 additions & 4 deletions graph/graphtest/graphtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,9 @@ func TestIteratorsAndNextResultOrderA(t testing.TB, gen testutil.DatabaseFunc, c
}
require.Equal(t, sz, qs.Size(), "Incorrect number of quads")

fixed := qs.FixedIterator()
fixed.Add(qs.ValueOf(quad.Raw("C")))
fixed := iterator.NewFixed(qs.ValueOf(quad.Raw("C")))

fixed2 := qs.FixedIterator()
fixed2.Add(qs.ValueOf(quad.Raw("follows")))
fixed2 := iterator.NewFixed(qs.ValueOf(quad.Raw("follows")))

all := qs.NodesAllIterator()

Expand Down
16 changes: 8 additions & 8 deletions graph/iterator/and_optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
func TestIteratorPromotion(t *testing.T) {
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all := NewInt64(1, 3, true)
fixed := NewFixed(Identity, Int64Node(3))
fixed := NewFixed(Int64Node(3))
a := NewAnd(qs, all, fixed)
all.Tagger().Add("a")
fixed.Tagger().Add("b")
Expand All @@ -56,7 +56,7 @@ func TestIteratorPromotion(t *testing.T) {
func TestNullIteratorAnd(t *testing.T) {
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all := NewInt64(1, 3, true)
null := NewNull()
Expand All @@ -73,7 +73,7 @@ func TestNullIteratorAnd(t *testing.T) {
func TestAllPromotion(t *testing.T) {
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all := NewInt64(100, 300, true)
all.Tagger().Add("good")
Expand Down Expand Up @@ -105,11 +105,11 @@ func TestAllPromotion(t *testing.T) {
func TestReorderWithTag(t *testing.T) {
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all := NewFixed(Identity, Int64Node(3))
all := NewFixed(Int64Node(3))
all.Tagger().Add("good")
all2 := NewFixed(Identity,
all2 := NewFixed(
Int64Node(3),
Int64Node(4),
Int64Node(5),
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestReorderWithTag(t *testing.T) {
func TestAndStatistics(t *testing.T) {
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all := NewInt64(100, 300, true)
all.Tagger().Add("good")
Expand Down
20 changes: 10 additions & 10 deletions graph/iterator/and_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func TestTag(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
fix1 := NewFixed(Identity, Int64Node(234))
fix1 := NewFixed(Int64Node(234))
fix1.Tagger().Add("foo")
and := NewAnd(qs, fix1)
and.Tagger().Add("bar")
Expand Down Expand Up @@ -65,15 +65,15 @@ func TestAndAndFixedIterators(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
fix1 := NewFixed(Identity,
fix1 := NewFixed(
Int64Node(1),
Int64Node(2),
Int64Node(3),
Int64Node(4),
)
fix2 := NewFixed(Identity,
fix2 := NewFixed(
Int64Node(3),
Int64Node(4),
Int64Node(5),
Expand Down Expand Up @@ -108,15 +108,15 @@ func TestNonOverlappingFixedIterators(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
fix1 := NewFixed(Identity,
fix1 := NewFixed(
Int64Node(1),
Int64Node(2),
Int64Node(3),
Int64Node(4),
)
fix2 := NewFixed(Identity,
fix2 := NewFixed(
Int64Node(5),
Int64Node(6),
Int64Node(7),
Expand All @@ -141,7 +141,7 @@ func TestAllIterators(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
all1 := NewInt64(1, 5, true)
all2 := NewInt64(4, 10, true)
Expand All @@ -164,7 +164,7 @@ func TestAndIteratorErr(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
wantErr := errors.New("unique")
allErr := newTestIterator(false, wantErr)
Expand Down
4 changes: 2 additions & 2 deletions graph/iterator/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestCount(t *testing.T) {
ctx := context.TODO()
fixed := NewFixed(Identity,
fixed := NewFixed(
graph.PreFetched(quad.String("a")),
graph.PreFetched(quad.String("b")),
graph.PreFetched(quad.String("c")),
Expand All @@ -27,7 +27,7 @@ func TestCount(t *testing.T) {

fixed.Reset()

fixed2 := NewFixed(Identity,
fixed2 := NewFixed(
graph.PreFetched(quad.String("b")),
graph.PreFetched(quad.String("d")),
)
Expand Down
17 changes: 4 additions & 13 deletions graph/iterator/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,14 @@ type Fixed struct {
tags graph.Tagger
values []graph.Value
lastIndex int
cmp Equality
result graph.Value
}

// Define the signature of an equality function.
type Equality func(a, b graph.Value) bool

// Define an equality function of purely ==, which works for native types.
func Identity(a, b graph.Value) bool {
return a == b
}

// Creates a new Fixed iterator with a custom comparator.
func NewFixed(cmp Equality, vals ...graph.Value) *Fixed {
func NewFixed(vals ...graph.Value) *Fixed {
it := &Fixed{
uid: NextUID(),
values: make([]graph.Value, 0, 20),
cmp: cmp,
}
for _, v := range vals {
it.Add(v)
Expand Down Expand Up @@ -84,7 +74,7 @@ func (it *Fixed) TagResults(dst map[string]graph.Value) {
func (it *Fixed) Clone() graph.Iterator {
vals := make([]graph.Value, len(it.values))
copy(vals, it.values)
out := NewFixed(it.cmp, vals...)
out := NewFixed(vals...)
out.tags.CopyFrom(it)
return out
}
Expand All @@ -108,8 +98,9 @@ func (it *Fixed) Contains(ctx context.Context, v graph.Value) bool {
// However, for fixed iterators, which are by definition kind of tiny, this
// isn't a big issue.
graph.ContainsLogIn(it, v)
vk := graph.ToKey(v)
for _, x := range it.values {
if it.cmp(x, v) {
if graph.ToKey(x) == vk {
it.result = x
return graph.ContainsLogOut(it, v, true)
}
Expand Down
2 changes: 1 addition & 1 deletion graph/iterator/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type testIterator struct {

func newTestIterator(next bool, err error) graph.Iterator {
return &testIterator{
Fixed: NewFixed(Identity),
Fixed: NewFixed(),
NextVal: next,
ErrVal: err,
}
Expand Down
2 changes: 1 addition & 1 deletion graph/iterator/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestLimitIteratorBasics(t *testing.T) {
ctx := context.TODO()
allIt := NewFixed(Identity,
allIt := NewFixed(
Int64Node(1),
Int64Node(2),
Int64Node(3),
Expand Down
4 changes: 2 additions & 2 deletions graph/iterator/linksto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func TestLinksTo(t *testing.T) {
ctx := context.TODO()
qs := &graphmock.Oldstore{
Data: []string{1: "cool"},
Iter: NewFixed(Identity),
Iter: NewFixed(),
}
qs.Iter.(*Fixed).Add(Int64Quad(2))
fixed := NewFixed(Identity)
fixed := NewFixed()
val := qs.ValueOf(quad.Raw("cool"))
if val.(Int64Node) != 1 {
t.Fatalf("Failed to return correct value, got:%v expect:1", val)
Expand Down
6 changes: 3 additions & 3 deletions graph/iterator/not_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

func TestNotIteratorBasics(t *testing.T) {
ctx := context.TODO()
allIt := NewFixed(Identity,
allIt := NewFixed(
Int64Node(1),
Int64Node(2),
Int64Node(3),
Int64Node(4),
)

toComplementIt := NewFixed(Identity,
toComplementIt := NewFixed(
Int64Node(2),
Int64Node(4),
)
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestNotIteratorErr(t *testing.T) {
wantErr := errors.New("unique")
allIt := newTestIterator(false, wantErr)

toComplementIt := NewFixed(Identity)
toComplementIt := NewFixed()

not := NewNot(toComplementIt, allIt)

Expand Down
Loading

0 comments on commit b63faea

Please sign in to comment.