Skip to content

Commit

Permalink
make graph.Value safe again
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed May 14, 2017
1 parent 8602aaf commit e4ab526
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 88 deletions.
4 changes: 2 additions & 2 deletions cmd/cayley/command/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a sortProp) Len() int { return len(a) }
func (a sortProp) Less(i, j int) bool { return valueLess(a[i].Pred, a[j].Pred) }
func (a sortProp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func hashProperties(h hash.Hash, m map[graph.Value]property) string {
func hashProperties(h hash.Hash, m map[interface{}]property) string {
props := make([]property, 0, len(m))
for _, p := range m {
if len(p.Values) > 1 {
Expand Down Expand Up @@ -142,7 +142,7 @@ func dedupProperties(ctx context.Context, h *graph.Handle, pred, typ quad.IRI) e
cnt++
it := qs.QuadIterator(quad.Subject, s)
defer it.Close()
m := make(map[graph.Value]property)
m := make(map[interface{}]property)
for it.Next() {
q := it.Result()
p := qs.QuadDirection(q, quad.Predicate)
Expand Down
10 changes: 4 additions & 6 deletions graph/bolt/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (

func init() {
graph.RegisterQuadStore(QuadStoreType, graph.QuadStoreRegistration{
NewFunc: newQuadStore,
UpgradeFunc: upgradeBolt,
InitFunc: createNewBolt,
IsPersistent: true,
NewFunc: newQuadStore,
UpgradeFunc: upgradeBolt,
InitFunc: createNewBolt,
IsPersistent: true,
})
}

Expand All @@ -50,8 +50,6 @@ const (
QuadStoreType = "bolt"
)

var _ graph.Keyer = (*Token)(nil)

type Token struct {
nodes bool
bucket []byte
Expand Down
2 changes: 2 additions & 0 deletions graph/bolt2/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,5 @@ func (qs *QuadStore) getPrimitive(val Int64Value) (*proto.Primitive, bool) {
}

type Int64Value uint64

func (v Int64Value) Key() interface{} { return v }
3 changes: 2 additions & 1 deletion graph/gaedatastore/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type Token struct {
Hash string
}

func (t Token) IsNode() bool { return t.Kind == nodeKind }
func (t Token) IsNode() bool { return t.Kind == nodeKind }
func (t Token) Key() interface{} { return t }

type QuadEntry struct {
Hash string
Expand Down
8 changes: 6 additions & 2 deletions graph/iterator/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ type Int64 struct {

type Int64Node int64

func (v Int64Node) Key() interface{} { return v }

func (Int64Node) IsNode() bool { return true }

type Int64Quad int64

func (v Int64Quad) Key() interface{} { return v }

func (Int64Quad) IsNode() bool { return false }

// Creates a new Int64 with the given range.
Expand Down Expand Up @@ -146,8 +150,8 @@ func (it *Int64) Size() (int64, bool) {
return Size, true
}

func valToInt64(v graph.Value) int64{
if v, ok := v.(Int64Node); ok{
func valToInt64(v graph.Value) int64 {
if v, ok := v.(Int64Node); ok {
return int64(v)
}
return int64(v.(Int64Quad))
Expand Down
14 changes: 1 addition & 13 deletions graph/iterator/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ import (
"github.com/cayleygraph/cayley/quad"
)

var (
_ graph.Value = fetchedValue{}
_ graph.PreFetchedValue = fetchedValue{}
)

type fetchedValue struct {
Val quad.Value
}

func (v fetchedValue) IsNode() bool { return true }
func (v fetchedValue) NameOf() quad.Value { return v.Val }

// Count iterator returns one element with size of underlying iterator.
type Count struct {
uid uint64
Expand Down Expand Up @@ -96,7 +84,7 @@ func (it *Count) Result() graph.Value {
if it.result == nil {
return nil
}
return fetchedValue{Val: it.result}
return graph.PreFetched(it.result)
}

func (it *Count) Contains(val graph.Value) bool {
Expand Down
28 changes: 14 additions & 14 deletions graph/iterator/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@ import (

func TestCount(t *testing.T) {
fixed := NewFixed(Identity,
fetchedValue{Val: quad.String("a")},
fetchedValue{Val: quad.String("b")},
fetchedValue{Val: quad.String("c")},
fetchedValue{Val: quad.String("d")},
fetchedValue{Val: quad.String("e")},
graph.PreFetched(quad.String("a")),
graph.PreFetched(quad.String("b")),
graph.PreFetched(quad.String("c")),
graph.PreFetched(quad.String("d")),
graph.PreFetched(quad.String("e")),
)
it := NewCount(fixed, nil)
require.True(t, it.Next())
require.Equal(t, fetchedValue{Val: quad.Int(5)}, it.Result())
require.Equal(t, graph.PreFetched(quad.Int(5)), it.Result())
require.False(t, it.Next())
require.True(t, it.Contains(fetchedValue{Val: quad.Int(5)}))
require.False(t, it.Contains(fetchedValue{Val: quad.Int(3)}))
require.True(t, it.Contains(graph.PreFetched(quad.Int(5))))
require.False(t, it.Contains(graph.PreFetched(quad.Int(3))))

fixed.Reset()

fixed2 := NewFixed(Identity,
fetchedValue{Val: quad.String("b")},
fetchedValue{Val: quad.String("d")},
graph.PreFetched(quad.String("b")),
graph.PreFetched(quad.String("d")),
)
it = NewCount(NewAnd(nil, fixed, fixed2), nil)
require.True(t, it.Next())
require.Equal(t, fetchedValue{Val: quad.Int(2)}, it.Result())
require.Equal(t, graph.PreFetched(quad.Int(2)), it.Result())
require.False(t, it.Next())
require.False(t, it.Contains(fetchedValue{Val: quad.Int(5)}))
require.True(t, it.Contains(fetchedValue{Val: quad.Int(2)}))
require.False(t, it.Contains(graph.PreFetched(quad.Int(5))))
require.True(t, it.Contains(graph.PreFetched(quad.Int(2))))

it.Reset()
it.Tagger().Add("count")
require.True(t, it.Next())
m := make(map[string]graph.Value)
it.TagResults(m)
require.Equal(t, map[string]graph.Value{"count": fetchedValue{Val: quad.Int(2)}}, m)
require.Equal(t, map[string]graph.Value{"count": graph.PreFetched(quad.Int(2))}, m)
}
4 changes: 2 additions & 2 deletions graph/iterator/materialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type result struct {
type Materialize struct {
uid uint64
tags graph.Tagger
containsMap map[graph.Value]int
containsMap map[interface{}]int
values [][]result
actualSize int64
index int
Expand All @@ -47,7 +47,7 @@ type Materialize struct {
func NewMaterialize(sub graph.Iterator) *Materialize {
return &Materialize{
uid: NextUID(),
containsMap: make(map[graph.Value]int),
containsMap: make(map[interface{}]int),
subIt: sub,
index: -1,
}
Expand Down
27 changes: 18 additions & 9 deletions graph/iterator/mock_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,34 @@ type store struct {
var _ graph.QuadStore = &store{}

func (qs *store) ValueOf(s quad.Value) graph.Value {
return s
return graph.PreFetched(s)
}

func (qs *store) ApplyDeltas([]graph.Delta, graph.IgnoreOpts) error { return nil }

func (qs *store) Quad(v graph.Value) quad.Quad { return v.(quad.Quad) }
type quadValue struct {
q quad.Quad
}

func (q quadValue) Key() interface{} {
return q.q.String()
}

func (qs *store) Quad(v graph.Value) quad.Quad { return v.(quadValue).q }

func (qs *store) NameOf(v graph.Value) quad.Value {
if v == nil {
return nil
}
return v.(quad.Value)
return v.(graph.PreFetchedValue).NameOf()
}

func (qs *store) RemoveQuad(t quad.Quad) {}

func (qs *store) Type() string { return "mockstore" }

func (qs *store) QuadDirection(v graph.Value, d quad.Direction) graph.Value {
return qs.Quad(v).Get(d)
return graph.PreFetched(qs.Quad(v).Get(d))
}

func (qs *store) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
Expand All @@ -148,9 +156,10 @@ func (qs *store) DebugPrint() {}

func (qs *store) QuadIterator(d quad.Direction, i graph.Value) graph.Iterator {
fixed := qs.FixedIterator()
v := i.(graph.PreFetchedValue).NameOf()
for _, q := range qs.data {
if q.Get(d) == i {
fixed.Add(q)
if q.Get(d) == v {
fixed.Add(quadValue{q})
}
}
return fixed
Expand All @@ -160,23 +169,23 @@ func (qs *store) NodesAllIterator() graph.Iterator {
set := make(map[string]bool)
for _, q := range qs.data {
for _, d := range quad.Directions {
n := qs.NameOf(q.Get(d))
n := qs.NameOf(graph.PreFetched(q.Get(d)))
if n != nil {
set[n.String()] = true
}
}
}
fixed := qs.FixedIterator()
for k, _ := range set {
fixed.Add(quad.Raw(k))
fixed.Add(graph.PreFetched(quad.Raw(k)))
}
return fixed
}

func (qs *store) QuadsAllIterator() graph.Iterator {
fixed := qs.FixedIterator()
for _, q := range qs.data {
fixed.Add(q)
fixed.Add(quadValue{q})
}
return fixed
}
Expand Down
12 changes: 6 additions & 6 deletions graph/iterator/recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type Recursive struct {

qs graph.QuadStore
morphism graph.ApplyMorphism
seen map[graph.Value]seenAt
seen map[interface{}]seenAt
nextIt graph.Iterator
depth int
pathMap map[graph.Value][]map[string]graph.Value
pathMap map[interface{}][]map[string]graph.Value
pathIndex int
containsValue graph.Value
depthTags graph.Tagger
Expand All @@ -45,10 +45,10 @@ func NewRecursive(qs graph.QuadStore, it graph.Iterator, morphism graph.ApplyMor

qs: qs,
morphism: morphism,
seen: make(map[graph.Value]seenAt),
seen: make(map[interface{}]seenAt),
nextIt: &Null{},
baseIt: qs.FixedIterator(),
pathMap: make(map[graph.Value][]map[string]graph.Value),
pathMap: make(map[interface{}][]map[string]graph.Value),
containsValue: nil,
}
}
Expand All @@ -62,8 +62,8 @@ func (it *Recursive) Reset() {
it.result.depth = 0
it.err = nil
it.subIt.Reset()
it.seen = make(map[graph.Value]seenAt)
it.pathMap = make(map[graph.Value][]map[string]graph.Value)
it.seen = make(map[interface{}]seenAt)
it.pathMap = make(map[interface{}][]map[string]graph.Value)
it.containsValue = nil
it.pathIndex = 0
it.nextIt = &Null{}
Expand Down
8 changes: 4 additions & 4 deletions graph/iterator/recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
func singleHop(pred string) graph.ApplyMorphism {
return func(qs graph.QuadStore, it graph.Iterator) graph.Iterator {
fixed := qs.FixedIterator()
fixed.Add(quad.Raw(pred))
fixed.Add(graph.PreFetched(quad.Raw(pred)))
predlto := NewLinksTo(qs, fixed, quad.Predicate)
lto := NewLinksTo(qs, it.Clone(), quad.Subject)
and := NewAnd(qs)
Expand All @@ -51,7 +51,7 @@ var rec_test_qs = &store{
func TestRecursiveNext(t *testing.T) {
qs := rec_test_qs
start := qs.FixedIterator()
start.Add(quad.Raw("alice"))
start.Add(graph.PreFetched(quad.Raw("alice")))
r := NewRecursive(qs, start, singleHop("parent"))
expected := []string{"bob", "charlie", "dani", "emily"}

Expand All @@ -69,7 +69,7 @@ func TestRecursiveNext(t *testing.T) {
func TestRecursiveContains(t *testing.T) {
qs := rec_test_qs
start := qs.FixedIterator()
start.Add(quad.Raw("alice"))
start.Add(graph.PreFetched(quad.Raw("alice")))
r := NewRecursive(qs, start, singleHop("parent"))
values := []string{"charlie", "bob", "not"}
expected := []bool{true, true, false}
Expand All @@ -90,7 +90,7 @@ func TestRecursiveNextPath(t *testing.T) {
and := NewAnd(qs)
and.AddSubIterator(it)
fixed := qs.FixedIterator()
fixed.Add(quad.Raw("alice"))
fixed.Add(graph.PreFetched(quad.Raw("alice")))
and.AddSubIterator(fixed)
r := NewRecursive(qs, and, singleHop("parent"))

Expand Down
5 changes: 1 addition & 4 deletions graph/iterator/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ func (it *Unique) Next() bool {

for it.subIt.Next() {
curr := it.subIt.Result()
var key interface{} = curr
if v, ok := curr.(graph.Keyer); ok {
key = v.Key()
}
key := graph.ToKey(curr)
if ok := it.seen[key]; !ok {
it.result = curr
it.seen[key] = true
Expand Down
1 change: 1 addition & 0 deletions graph/iterator/value_comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func mixedFixedIterator() *Fixed {
type stringNode string

func (stringNode) IsNode() bool { return true }
func (s stringNode) Key() interface{} { return s }

var comparisonTests = []struct {
message string
Expand Down
10 changes: 4 additions & 6 deletions graph/leveldb/quadstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (

func init() {
graph.RegisterQuadStore(QuadStoreType, graph.QuadStoreRegistration{
NewFunc: newQuadStore,
UpgradeFunc: upgradeLevelDB,
InitFunc: createNewLevelDB,
IsPersistent: true,
NewFunc: newQuadStore,
UpgradeFunc: upgradeLevelDB,
InitFunc: createNewLevelDB,
IsPersistent: true,
})
}

Expand All @@ -51,8 +51,6 @@ const (

var order = binary.LittleEndian

var _ graph.Keyer = (Token)(nil)

type Token []byte

func (t Token) IsNode() bool { return len(t) > 0 && t[0] == 'z' }
Expand Down
Loading

0 comments on commit e4ab526

Please sign in to comment.