-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_query_simple.go
More file actions
109 lines (82 loc) · 2.59 KB
/
Copy pathdataset_query_simple.go
File metadata and controls
109 lines (82 loc) · 2.59 KB
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
package inmemory
import (
"context"
"github.com/dpb587/rdfkit-go/rdf"
"github.com/dpb587/rdfkit-go/x/storage/inmemory/simplequery"
)
func (s *Dataset) QuerySimple(ctx context.Context, q simplequery.Query, opts simplequery.QueryOptions) (simplequery.QueryResult, error) {
var selectBindings []simplequery.QueryResultBinding
first, remaining := q.Where.SortStaticEfficiency().Shift()
err := s.querySimpleWhere(
ctx,
q,
&selectBindings,
simplequery.NewQueryResultBinding(map[string]rdf.Term{}),
first,
remaining,
)
if err != nil {
return nil, err
}
return simplequery.NewQueryResult(selectBindings), nil
}
func (s *Dataset) querySimpleWhere(ctx context.Context, q simplequery.Query, selectBindings *[]simplequery.QueryResultBinding, currentBindings simplequery.QueryResultBinding, whereStatement simplequery.WhereTriple, remaining simplequery.WhereTripleList) error {
iter, err := s.NewQuadIterator(ctx, whereStatement)
if err != nil {
return err
}
var oValues rdf.TermList
var oValuesFound bool
if oVar, ok := whereStatement.Object.(simplequery.Var); ok {
vs := q.Values.GetByVar(oVar)
oValues = vs.Terms
oValuesFound = len(vs.Var) > 0
}
var found int
for _, edge := range iter.(*QuadIterator).edges {
if oValuesFound {
var matched bool
for _, oValueTerm := range oValues {
if oValueTerm.TermEquals(edge.GetQuad().Triple.Object) {
matched = true
break
}
}
if !matched {
continue
}
}
found++
nextBindings := whereStatement.UpdateBindings(currentBindings, edge.GetQuad())
if len(remaining) == 0 {
finalTermsByVar := map[string]rdf.Term{}
for _, v := range q.Select {
finalTermsByVar[string(v)] = nextBindings.Get(string(v))
}
*selectBindings = append(*selectBindings, simplequery.NewQueryResultBinding(finalTermsByVar))
continue
} else {
nextStatement, nextRemaining := remaining.ResolveBindings(nextBindings).SortStaticEfficiency().Shift()
err := s.querySimpleWhere(ctx, q, selectBindings, nextBindings, nextStatement, nextRemaining)
if err != nil {
return err
}
}
}
if found == 0 && whereStatement.Optional {
if len(remaining) == 0 {
finalTermsByVar := map[string]rdf.Term{}
for _, v := range q.Select {
finalTermsByVar[string(v)] = currentBindings.Get(string(v))
}
*selectBindings = append(*selectBindings, simplequery.NewQueryResultBinding(finalTermsByVar))
} else {
nextStatement, nextRemaining := remaining.Shift()
err := s.querySimpleWhere(ctx, q, selectBindings, currentBindings, nextStatement, nextRemaining)
if err != nil {
return err
}
}
}
return nil
}