/
save.go
270 lines (219 loc) · 5.65 KB
/
save.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package iterator
import (
"context"
"fmt"
"github.com/cayleygraph/cayley/graph"
)
var (
_ graph.IteratorFuture = (*Save)(nil)
_ graph.Tagger = (*Save)(nil)
)
func Tag(it graph.Iterator, tag string) graph.Iterator {
if s, ok := it.(graph.Tagger); ok {
s.AddTags(tag)
return s
} else if s, ok := graph.AsShape(it).(graph.TaggerShape); ok {
s.AddTags(tag)
return graph.AsLegacy(s)
}
return NewSave(it, tag)
}
func TagShape(it graph.IteratorShape, tag string) graph.IteratorShape {
if s, ok := it.(graph.TaggerShape); ok {
s.AddTags(tag)
return s
} else if s, ok := graph.AsLegacy(it).(graph.Tagger); ok {
s.AddTags(tag)
return graph.AsShape(s)
}
return newSave(it, tag)
}
func NewSave(on graph.Iterator, tags ...string) *Save {
it := &Save{
it: newSave(graph.AsShape(on), tags...),
}
it.Iterator = graph.NewLegacy(it.it, it)
return it
}
type Save struct {
it *save
graph.Iterator
}
func (it *Save) AsShape() graph.IteratorShape {
it.Close()
return it.it
}
// Add a tag to the iterator.
func (it *Save) AddTags(tag ...string) {
it.it.AddTags(tag...)
}
func (it *Save) AddFixedTag(tag string, value graph.Ref) {
it.it.AddFixedTag(tag, value)
}
// Tags returns the tags held in the tagger. The returned value must not be mutated.
func (it *Save) Tags() []string {
return it.it.Tags()
}
// Fixed returns the fixed tags held in the tagger. The returned value must not be mutated.
func (it *Save) FixedTags() map[string]graph.Ref {
return it.it.FixedTags()
}
func (it *Save) CopyFromTagger(st graph.TaggerBase) {
it.it.CopyFromTagger(st)
}
var (
_ graph.IteratorShapeCompat = (*save)(nil)
_ graph.TaggerShape = (*save)(nil)
)
func newSave(on graph.IteratorShape, tags ...string) *save {
s := &save{it: on}
s.AddTags(tags...)
return s
}
type save struct {
it graph.IteratorShape
tags []string
fixedTags map[string]graph.Ref
}
func (it *save) Iterate() graph.Scanner {
return newSaveNext(it.it.Iterate(), it.tags, it.fixedTags)
}
func (it *save) Lookup() graph.Index {
return newSaveContains(it.it.Lookup(), it.tags, it.fixedTags)
}
func (it *save) AsLegacy() graph.Iterator {
it2 := &Save{it: it}
it2.Iterator = graph.NewLegacy(it, it2)
return it2
}
func (it *save) String() string {
return fmt.Sprintf("Save(%v, %v)", it.tags, it.fixedTags)
}
// Add a tag to the iterator.
func (it *save) AddTags(tag ...string) {
it.tags = append(it.tags, tag...)
}
func (it *save) AddFixedTag(tag string, value graph.Ref) {
if it.fixedTags == nil {
it.fixedTags = make(map[string]graph.Ref)
}
it.fixedTags[tag] = value
}
// Tags returns the tags held in the tagger. The returned value must not be mutated.
func (it *save) Tags() []string {
return it.tags
}
// Fixed returns the fixed tags held in the tagger. The returned value must not be mutated.
func (it *save) FixedTags() map[string]graph.Ref {
return it.fixedTags
}
func (it *save) CopyFromTagger(st graph.TaggerBase) {
it.tags = append(it.tags, st.Tags()...)
fixed := st.FixedTags()
if len(fixed) == 0 {
return
}
if it.fixedTags == nil {
it.fixedTags = make(map[string]graph.Ref, len(fixed))
}
for k, v := range fixed {
it.fixedTags[k] = v
}
}
func (it *save) Stats(ctx context.Context) (graph.IteratorCosts, error) {
return it.it.Stats(ctx)
}
func (it *save) Optimize(ctx context.Context) (nit graph.IteratorShape, no bool) {
sub, ok := it.it.Optimize(ctx)
if len(it.tags) == 0 && len(it.fixedTags) == 0 {
return sub, true
}
if st, ok2 := sub.(graph.TaggerShape); ok2 {
st.CopyFromTagger(it)
return st, true
} else if st, ok2 := graph.AsLegacy(sub).(graph.Tagger); ok2 {
st.CopyFromTagger(it)
return graph.AsShape(st), true
}
if !ok {
return it, false
}
s := newSave(sub)
s.CopyFromTagger(it)
return s, true
}
func (it *save) SubIterators() []graph.IteratorShape {
return []graph.IteratorShape{it.it}
}
func newSaveNext(it graph.Scanner, tags []string, fixed map[string]graph.Ref) *saveNext {
return &saveNext{it: it, tags: tags, fixedTags: fixed}
}
type saveNext struct {
it graph.Scanner
tags []string
fixedTags map[string]graph.Ref
}
func (it *saveNext) String() string {
return fmt.Sprintf("Save(%v, %v)", it.tags, it.fixedTags)
}
func (it *saveNext) TagResults(dst map[string]graph.Ref) {
it.it.TagResults(dst)
v := it.Result()
for _, tag := range it.tags {
dst[tag] = v
}
for tag, value := range it.fixedTags {
dst[tag] = value
}
}
func (it *saveNext) Result() graph.Ref {
return it.it.Result()
}
func (it *saveNext) Next(ctx context.Context) bool {
return it.it.Next(ctx)
}
func (it *saveNext) NextPath(ctx context.Context) bool {
return it.it.NextPath(ctx)
}
func (it *saveNext) Err() error {
return it.it.Err()
}
func (it *saveNext) Close() error {
return it.it.Close()
}
func newSaveContains(it graph.Index, tags []string, fixed map[string]graph.Ref) *saveContains {
return &saveContains{it: it, tags: tags, fixed: fixed}
}
type saveContains struct {
it graph.Index
tags []string
fixed map[string]graph.Ref
}
func (it *saveContains) String() string {
return fmt.Sprintf("SaveContains(%v, %v)", it.tags, it.fixed)
}
func (it *saveContains) TagResults(dst map[string]graph.Ref) {
it.it.TagResults(dst)
v := it.Result()
for _, tag := range it.tags {
dst[tag] = v
}
for tag, value := range it.fixed {
dst[tag] = value
}
}
func (it *saveContains) Result() graph.Ref {
return it.it.Result()
}
func (it *saveContains) NextPath(ctx context.Context) bool {
return it.it.NextPath(ctx)
}
func (it *saveContains) Contains(ctx context.Context, v graph.Ref) bool {
return it.it.Contains(ctx, v)
}
func (it *saveContains) Err() error {
return it.it.Err()
}
func (it *saveContains) Close() error {
return it.it.Close()
}