-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubjectview.go
381 lines (348 loc) · 12.5 KB
/
subjectview.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package store4
import (
"bytes"
"fmt"
"sort"
)
// TupleCallbackFn is the function signature used to implement
// callback functions that receive a tuple.
//
// Used with calls to SubjectView's ForEach and ForEachWith.
type TupleCallbackFn func(p string, o interface{})
// TupleTestFn is the function signature used to implement
// callback functions performing tuple tests.
// A response of true means that the test has been passed.
//
// Used with calls to SubjectView's Every, EveryWith, Some and SomeWith.
type TupleTestFn func(p string, o interface{}) bool
// SubjectView provides a subject-centric API
// for working with predicate-object (property/value) tuples.
//
// SubjectView is a convenience façade that simply
// proxies calls to its associated QuadStore.
//
// Returned by calls to Query, SubjectView and
// SubjectViews on both QuadStore and GraphView.
type SubjectView struct {
Subject string
Graph string
QuadStore *QuadStore
}
// SubjectView returns a SubjectView for the given subject and graph.
func (s *QuadStore) SubjectView(subject, graph string) *SubjectView {
p := &SubjectView{
Subject: subject,
Graph: graph,
QuadStore: s,
}
return p
}
// SubjectViews returns a list of SubjectViews for subjects that
// match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (s *QuadStore) SubjectViews(predicate string, object interface{}, graph string) []*SubjectView {
var out []*SubjectView
s.ForSubjects(predicate, object, graph, func(subject string) {
p := &SubjectView{
Subject: subject,
Graph: graph,
QuadStore: s,
}
out = append(out, p)
})
return out
}
// SubjectView returns a SubjectView for the given subject.
func (g *GraphView) SubjectView(subject string) *SubjectView {
return g.QuadStore.SubjectView(subject, g.Graph)
}
// SubjectViews returns a list of SubjectViews for subjects that
// match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (g *GraphView) SubjectViews(predicate string, object interface{}) []*SubjectView {
return g.QuadStore.SubjectViews(predicate, object, g.Graph)
}
type tuple struct {
p string
o interface{}
}
// Query returns a list of SubjectViews for subjects in the store
// having predicate-object terms that match the given pattern.
//
// Pattern is a collection of predicate-object tuples,
// expressed using any of the following types:
// map[string][]interface{}
// map[string][]string
// map[string]interface{}
// map[string]string
// [][2]string
// [2]string
func (s *QuadStore) Query(pattern interface{}, graph string) []*SubjectView {
poList := predicateObjectList(pattern)
// Nothing to do?
if len(poList) == 0 {
return nil
}
haltFn := func(s, p string, o interface{}, g string) bool {
return true
}
var out []*SubjectView
s.ForSubjects(poList[0].p, poList[0].o, graph, func(subject string) {
// Got a match for first q entry,
// check if it also satisfies all other q entries.
for _, po := range poList[1:] {
if !s.SomeWith(subject, po.p, po.o, graph, haltFn) {
// No match, try next subject.
return
}
}
// Matches all po tuples, add to results list.
p := &SubjectView{
Subject: subject,
Graph: graph,
QuadStore: s,
}
out = append(out, p)
})
return out
}
func predicateObjectList(pattern interface{}) []tuple {
// Convert given pattern into query list.
var poList []tuple
switch pattern := pattern.(type) {
default:
panic(fmt.Sprintf("unexpected type %T\n", pattern))
case map[string]string:
for p, o := range pattern {
poList = append(poList, tuple{p, o})
}
case map[string]interface{}:
for p, o := range pattern {
poList = append(poList, tuple{p, o})
}
case map[string][]string:
for p, objects := range pattern {
for _, o := range objects {
poList = append(poList, tuple{p, o})
}
}
case map[string][]interface{}:
for p, objects := range pattern {
for _, o := range objects {
poList = append(poList, tuple{p, o})
}
}
case [][2]string:
for _, po := range pattern {
poList = append(poList, tuple{po[0], po[1]})
}
case [2]string:
poList = append(poList, tuple{pattern[0], pattern[1]})
}
return poList
}
// Query returns a list of SubjectViews for subjects in the graph
// having predicate-object terms that match the given pattern.
//
// Pattern is a collection of predicate-object tuples,
// expressed using any of the following types:
// map[string][]interface{}
// map[string][]string
// map[string]interface{}
// map[string]string
// [][2]string
// [2]string
func (g *GraphView) Query(pattern interface{}) []*SubjectView {
return g.QuadStore.Query(pattern, g.Graph)
}
// Map returns a 'property/value' map containing the predicate terms for
// the SubjectView's subject, mapped to their corresponding object terms.
func (v *SubjectView) Map() map[string][]interface{} {
m := make(map[string][]interface{})
v.ForPredicates("*", func(predicate string) {
m[predicate] = v.FindObjects(predicate)
})
return m
}
// Add a quad to the underlying QuadStore,
// with the given predicate and object values
// and this SubjectView's Subject and Graph values.
// Returns true if the quad was a new quad,
// or false if the quad already existed.
//
// If any of the given terms are "*" (an asterisk),
// then this method will panic. (The asterisk is reserved
// for wildcard operations throughout the API).
func (v *SubjectView) Add(predicate string, object interface{}) bool {
return v.QuadStore.Add(v.Subject, predicate, object, v.Graph)
}
// Count returns a count of tuples in the SubjectView that match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) Count(predicate string, object interface{}) uint64 {
return v.QuadStore.Count(v.Subject, predicate, object, v.Graph)
}
// Empty returns true if the SubjectView has no contents.
func (v *SubjectView) Empty() bool {
haltFn := func(s, p string, o interface{}, g string) bool {
return true
}
return !v.QuadStore.SomeWith(v.Subject, "*", "*", v.Graph, haltFn)
}
// Every tests whether all tuples in the SubjectView pass the test
// implemented by the given function.
//
// The given callback is
// executed once for each tuple present in the SubjectView until
// Every finds one where the callback returns false. If such
// an element is found, iteration is immediately halted and
// Every returns false. Otherwise, if the callback returns
// true for all tuples, then Every returns true.
//
// If no tuples match the given terms, or the SubjectView is empty,
// then Every returns false. Note that this differs from
// the interpretation of 'every' in some other languages,
// which may return true for an empty iteration set.
func (v *SubjectView) Every(fn TupleTestFn) bool {
return v.QuadStore.EveryWith(v.Subject, "*", "*", v.Graph, adaptTupleTestFn(fn))
}
// EveryWith tests whether all tuples in the SubjectView that match the
// given terms pass the test implemented by the given function.
//
// The given callback is
// executed once for each matching tuple in the SubjectView until
// EveryWith finds one where the callback returns false. If such
// an element is found, iteration is immediately halted and
// EveryWith returns false. Otherwise, if the callback returns
// true for all tuples, then EveryWith returns true.
//
// If no tuples match the given terms, or the SubjectView is empty,
// then EveryWith returns false. Note that this differs from
// the interpretation of 'every' in some other languages,
// which may return true for an empty iteration set.
func (v *SubjectView) EveryWith(predicate string, object interface{}, fn TupleTestFn) bool {
return v.QuadStore.EveryWith(v.Subject, predicate, object, v.Graph, adaptTupleTestFn(fn))
}
// FindObjects returns a list of distinct object terms for all
// tuples in the SubjectView that match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) FindObjects(predicate string) []interface{} {
return v.QuadStore.FindObjects(v.Subject, predicate, v.Graph)
}
// FindPredicates returns a list of distinct predicate terms for all
// tuples in the SubjectView that match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) FindPredicates(object interface{}) []string {
return v.QuadStore.FindPredicates(v.Subject, object, v.Graph)
}
// ForEach executes the given callback once for each tuple in the SubjectView.
func (v *SubjectView) ForEach(fn TupleCallbackFn) {
v.QuadStore.ForEachWith(v.Subject, "*", "*", v.Graph, adaptTupleCallbackFn(fn))
}
// ForEachWith executes the given callback once for each tuple in the SubjectView
// that matches the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) ForEachWith(predicate string, object interface{}, fn TupleCallbackFn) {
v.QuadStore.ForEachWith(v.Subject, predicate, object, v.Graph, adaptTupleCallbackFn(fn))
}
// ForObjects executes the given callback once for each distinct object term
// for all tuples in the SubjectView that match the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) ForObjects(predicate string, fn ObjectCallbackFn) {
v.QuadStore.ForObjects(v.Subject, predicate, v.Graph, fn)
}
// ForPredicates executes the given callback once for each distinct predicate term
// for all tuples in the SubjectView that graph the given pattern.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) ForPredicates(object interface{}, fn StringCallbackFn) {
v.QuadStore.ForPredicates(v.Subject, object, v.Graph, fn)
}
// Remove quads from the underlying QuadStore,
// with the given predicate and object values
// and this SubjectView's Subject and Graph values.
// Returns the number of quads removed.
//
// Passing "*" (an asterisk) for any parameter acts as a
// match-everything wildcard for that term.
func (v *SubjectView) Remove(predicate string, object interface{}) uint64 {
return v.QuadStore.Remove(v.Subject, predicate, object, v.Graph)
}
// Size returns the total count of tuples in the SubjectView.
func (v *SubjectView) Size() uint64 {
return v.QuadStore.Count(v.Subject, "*", "*", v.Graph)
}
// Some tests whether some tuple in the SubjectView passes the test
// implemented by the given function.
//
// The given callback is
// executed once for each tuple present in the SubjectView until
// Some finds one where the callback returns true. If such
// an element is found, iteration is immediately halted and
// Some returns true. Otherwise, if the callback returns
// false for all tuples, then Some returns false.
func (v *SubjectView) Some(fn TupleTestFn) bool {
return v.QuadStore.SomeWith(v.Subject, "*", "*", v.Graph, adaptTupleTestFn(fn))
}
// SomeWith tests whether some tuple matching the given pattern
// passes the test implemented by the given function.
//
// The given callback is
// executed once for each tuple matching the given pattern until
// SomeWith finds one where the callback returns true. If such
// an element is found, iteration is immediately halted and
// SomeWith returns true. Otherwise, if the callback returns
// false for all tuples, then SomeWith returns false.
func (v *SubjectView) SomeWith(predicate string, object interface{}, fn TupleTestFn) bool {
return v.QuadStore.SomeWith(v.Subject, predicate, object, v.Graph, adaptTupleTestFn(fn))
}
// String returns the contents of the SubjectView in a human-readable format.
func (v *SubjectView) String() string {
var buf bytes.Buffer
graph := v.Graph
if len(graph) > 0 {
buf.WriteString(graph)
buf.WriteByte('\n')
}
buf.WriteString(v.Subject)
buf.WriteByte('\n')
predicates := v.FindPredicates("*")
sort.Strings(predicates)
for _, predicate := range predicates {
objects := v.FindObjects(predicate)
sortObjects(objects)
for _, object := range objects {
buf.WriteByte('[')
buf.WriteString(predicate)
buf.WriteByte(' ')
buf.WriteString(fmt.Sprint(object))
buf.WriteByte(']')
buf.WriteByte('\n')
}
}
return buf.String()
}
func adaptTupleCallbackFn(fn TupleCallbackFn) QuadCallbackFn {
return func(s, p string, o interface{}, g string) {
fn(p, o)
}
}
func adaptTupleTestFn(fn TupleTestFn) QuadTestFn {
return func(s, p string, o interface{}, g string) bool {
return fn(p, o)
}
}