Skip to content

Commit e4ab526

Browse files
committed
make graph.Value safe again
1 parent 8602aaf commit e4ab526

File tree

18 files changed

+110
-88
lines changed

18 files changed

+110
-88
lines changed

cmd/cayley/command/dedup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (a sortProp) Len() int { return len(a) }
7070
func (a sortProp) Less(i, j int) bool { return valueLess(a[i].Pred, a[j].Pred) }
7171
func (a sortProp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
7272

73-
func hashProperties(h hash.Hash, m map[graph.Value]property) string {
73+
func hashProperties(h hash.Hash, m map[interface{}]property) string {
7474
props := make([]property, 0, len(m))
7575
for _, p := range m {
7676
if len(p.Values) > 1 {
@@ -142,7 +142,7 @@ func dedupProperties(ctx context.Context, h *graph.Handle, pred, typ quad.IRI) e
142142
cnt++
143143
it := qs.QuadIterator(quad.Subject, s)
144144
defer it.Close()
145-
m := make(map[graph.Value]property)
145+
m := make(map[interface{}]property)
146146
for it.Next() {
147147
q := it.Result()
148148
p := qs.QuadDirection(q, quad.Predicate)

graph/bolt/quadstore.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333

3434
func init() {
3535
graph.RegisterQuadStore(QuadStoreType, graph.QuadStoreRegistration{
36-
NewFunc: newQuadStore,
37-
UpgradeFunc: upgradeBolt,
38-
InitFunc: createNewBolt,
39-
IsPersistent: true,
36+
NewFunc: newQuadStore,
37+
UpgradeFunc: upgradeBolt,
38+
InitFunc: createNewBolt,
39+
IsPersistent: true,
4040
})
4141
}
4242

@@ -50,8 +50,6 @@ const (
5050
QuadStoreType = "bolt"
5151
)
5252

53-
var _ graph.Keyer = (*Token)(nil)
54-
5553
type Token struct {
5654
nodes bool
5755
bucket []byte

graph/bolt2/quadstore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,5 @@ func (qs *QuadStore) getPrimitive(val Int64Value) (*proto.Primitive, bool) {
350350
}
351351

352352
type Int64Value uint64
353+
354+
func (v Int64Value) Key() interface{} { return v }

graph/gaedatastore/quadstore.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ type Token struct {
5959
Hash string
6060
}
6161

62-
func (t Token) IsNode() bool { return t.Kind == nodeKind }
62+
func (t Token) IsNode() bool { return t.Kind == nodeKind }
63+
func (t Token) Key() interface{} { return t }
6364

6465
type QuadEntry struct {
6566
Hash string

graph/iterator/all.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ type Int64 struct {
3939

4040
type Int64Node int64
4141

42+
func (v Int64Node) Key() interface{} { return v }
43+
4244
func (Int64Node) IsNode() bool { return true }
4345

4446
type Int64Quad int64
4547

48+
func (v Int64Quad) Key() interface{} { return v }
49+
4650
func (Int64Quad) IsNode() bool { return false }
4751

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

149-
func valToInt64(v graph.Value) int64{
150-
if v, ok := v.(Int64Node); ok{
153+
func valToInt64(v graph.Value) int64 {
154+
if v, ok := v.(Int64Node); ok {
151155
return int64(v)
152156
}
153157
return int64(v.(Int64Quad))

graph/iterator/count.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ import (
55
"github.com/cayleygraph/cayley/quad"
66
)
77

8-
var (
9-
_ graph.Value = fetchedValue{}
10-
_ graph.PreFetchedValue = fetchedValue{}
11-
)
12-
13-
type fetchedValue struct {
14-
Val quad.Value
15-
}
16-
17-
func (v fetchedValue) IsNode() bool { return true }
18-
func (v fetchedValue) NameOf() quad.Value { return v.Val }
19-
208
// Count iterator returns one element with size of underlying iterator.
219
type Count struct {
2210
uid uint64
@@ -96,7 +84,7 @@ func (it *Count) Result() graph.Value {
9684
if it.result == nil {
9785
return nil
9886
}
99-
return fetchedValue{Val: it.result}
87+
return graph.PreFetched(it.result)
10088
}
10189

10290
func (it *Count) Contains(val graph.Value) bool {

graph/iterator/count_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,36 @@ import (
99

1010
func TestCount(t *testing.T) {
1111
fixed := NewFixed(Identity,
12-
fetchedValue{Val: quad.String("a")},
13-
fetchedValue{Val: quad.String("b")},
14-
fetchedValue{Val: quad.String("c")},
15-
fetchedValue{Val: quad.String("d")},
16-
fetchedValue{Val: quad.String("e")},
12+
graph.PreFetched(quad.String("a")),
13+
graph.PreFetched(quad.String("b")),
14+
graph.PreFetched(quad.String("c")),
15+
graph.PreFetched(quad.String("d")),
16+
graph.PreFetched(quad.String("e")),
1717
)
1818
it := NewCount(fixed, nil)
1919
require.True(t, it.Next())
20-
require.Equal(t, fetchedValue{Val: quad.Int(5)}, it.Result())
20+
require.Equal(t, graph.PreFetched(quad.Int(5)), it.Result())
2121
require.False(t, it.Next())
22-
require.True(t, it.Contains(fetchedValue{Val: quad.Int(5)}))
23-
require.False(t, it.Contains(fetchedValue{Val: quad.Int(3)}))
22+
require.True(t, it.Contains(graph.PreFetched(quad.Int(5))))
23+
require.False(t, it.Contains(graph.PreFetched(quad.Int(3))))
2424

2525
fixed.Reset()
2626

2727
fixed2 := NewFixed(Identity,
28-
fetchedValue{Val: quad.String("b")},
29-
fetchedValue{Val: quad.String("d")},
28+
graph.PreFetched(quad.String("b")),
29+
graph.PreFetched(quad.String("d")),
3030
)
3131
it = NewCount(NewAnd(nil, fixed, fixed2), nil)
3232
require.True(t, it.Next())
33-
require.Equal(t, fetchedValue{Val: quad.Int(2)}, it.Result())
33+
require.Equal(t, graph.PreFetched(quad.Int(2)), it.Result())
3434
require.False(t, it.Next())
35-
require.False(t, it.Contains(fetchedValue{Val: quad.Int(5)}))
36-
require.True(t, it.Contains(fetchedValue{Val: quad.Int(2)}))
35+
require.False(t, it.Contains(graph.PreFetched(quad.Int(5))))
36+
require.True(t, it.Contains(graph.PreFetched(quad.Int(2))))
3737

3838
it.Reset()
3939
it.Tagger().Add("count")
4040
require.True(t, it.Next())
4141
m := make(map[string]graph.Value)
4242
it.TagResults(m)
43-
require.Equal(t, map[string]graph.Value{"count": fetchedValue{Val: quad.Int(2)}}, m)
43+
require.Equal(t, map[string]graph.Value{"count": graph.PreFetched(quad.Int(2))}, m)
4444
}

graph/iterator/materialize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type result struct {
3232
type Materialize struct {
3333
uid uint64
3434
tags graph.Tagger
35-
containsMap map[graph.Value]int
35+
containsMap map[interface{}]int
3636
values [][]result
3737
actualSize int64
3838
index int
@@ -47,7 +47,7 @@ type Materialize struct {
4747
func NewMaterialize(sub graph.Iterator) *Materialize {
4848
return &Materialize{
4949
uid: NextUID(),
50-
containsMap: make(map[graph.Value]int),
50+
containsMap: make(map[interface{}]int),
5151
subIt: sub,
5252
index: -1,
5353
}

graph/iterator/mock_ts_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,34 @@ type store struct {
110110
var _ graph.QuadStore = &store{}
111111

112112
func (qs *store) ValueOf(s quad.Value) graph.Value {
113-
return s
113+
return graph.PreFetched(s)
114114
}
115115

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

118-
func (qs *store) Quad(v graph.Value) quad.Quad { return v.(quad.Quad) }
118+
type quadValue struct {
119+
q quad.Quad
120+
}
121+
122+
func (q quadValue) Key() interface{} {
123+
return q.q.String()
124+
}
125+
126+
func (qs *store) Quad(v graph.Value) quad.Quad { return v.(quadValue).q }
119127

120128
func (qs *store) NameOf(v graph.Value) quad.Value {
121129
if v == nil {
122130
return nil
123131
}
124-
return v.(quad.Value)
132+
return v.(graph.PreFetchedValue).NameOf()
125133
}
126134

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

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

131139
func (qs *store) QuadDirection(v graph.Value, d quad.Direction) graph.Value {
132-
return qs.Quad(v).Get(d)
140+
return graph.PreFetched(qs.Quad(v).Get(d))
133141
}
134142

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

149157
func (qs *store) QuadIterator(d quad.Direction, i graph.Value) graph.Iterator {
150158
fixed := qs.FixedIterator()
159+
v := i.(graph.PreFetchedValue).NameOf()
151160
for _, q := range qs.data {
152-
if q.Get(d) == i {
153-
fixed.Add(q)
161+
if q.Get(d) == v {
162+
fixed.Add(quadValue{q})
154163
}
155164
}
156165
return fixed
@@ -160,23 +169,23 @@ func (qs *store) NodesAllIterator() graph.Iterator {
160169
set := make(map[string]bool)
161170
for _, q := range qs.data {
162171
for _, d := range quad.Directions {
163-
n := qs.NameOf(q.Get(d))
172+
n := qs.NameOf(graph.PreFetched(q.Get(d)))
164173
if n != nil {
165174
set[n.String()] = true
166175
}
167176
}
168177
}
169178
fixed := qs.FixedIterator()
170179
for k, _ := range set {
171-
fixed.Add(quad.Raw(k))
180+
fixed.Add(graph.PreFetched(quad.Raw(k)))
172181
}
173182
return fixed
174183
}
175184

176185
func (qs *store) QuadsAllIterator() graph.Iterator {
177186
fixed := qs.FixedIterator()
178187
for _, q := range qs.data {
179-
fixed.Add(q)
188+
fixed.Add(quadValue{q})
180189
}
181190
return fixed
182191
}

graph/iterator/recursive.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type Recursive struct {
1818

1919
qs graph.QuadStore
2020
morphism graph.ApplyMorphism
21-
seen map[graph.Value]seenAt
21+
seen map[interface{}]seenAt
2222
nextIt graph.Iterator
2323
depth int
24-
pathMap map[graph.Value][]map[string]graph.Value
24+
pathMap map[interface{}][]map[string]graph.Value
2525
pathIndex int
2626
containsValue graph.Value
2727
depthTags graph.Tagger
@@ -45,10 +45,10 @@ func NewRecursive(qs graph.QuadStore, it graph.Iterator, morphism graph.ApplyMor
4545

4646
qs: qs,
4747
morphism: morphism,
48-
seen: make(map[graph.Value]seenAt),
48+
seen: make(map[interface{}]seenAt),
4949
nextIt: &Null{},
5050
baseIt: qs.FixedIterator(),
51-
pathMap: make(map[graph.Value][]map[string]graph.Value),
51+
pathMap: make(map[interface{}][]map[string]graph.Value),
5252
containsValue: nil,
5353
}
5454
}
@@ -62,8 +62,8 @@ func (it *Recursive) Reset() {
6262
it.result.depth = 0
6363
it.err = nil
6464
it.subIt.Reset()
65-
it.seen = make(map[graph.Value]seenAt)
66-
it.pathMap = make(map[graph.Value][]map[string]graph.Value)
65+
it.seen = make(map[interface{}]seenAt)
66+
it.pathMap = make(map[interface{}][]map[string]graph.Value)
6767
it.containsValue = nil
6868
it.pathIndex = 0
6969
it.nextIt = &Null{}

0 commit comments

Comments
 (0)