forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
210 lines (183 loc) · 4.67 KB
/
regex.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
// Copyright 2014 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iterator
import (
"regexp"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
)
// Regex is a unary operator -- a filter across the values in the relevant
// subiterator. It works similarly to gremlin's filter{it.matches('exp')},
// reducing the iterator set to values whose string representation passes a
// regular expression test.
type Regex struct {
uid uint64
tags graph.Tagger
subIt graph.Iterator
re *regexp.Regexp
qs graph.QuadStore
result graph.Value
err error
allowRefs bool
}
func NewRegex(sub graph.Iterator, re *regexp.Regexp, qs graph.QuadStore) *Regex {
return &Regex{
uid: NextUID(),
subIt: sub,
re: re,
qs: qs,
}
}
// AllowRefs allows regexp iterator to match IRIs and BNodes.
//
// Consider using it carefully. In most cases it's better to reconsider
// your graph structure instead of relying on slow unoptimizable regexp.
//
// An example of incorrect usage is to match IRIs:
// <http://example.org/page>
// <http://example.org/page/foo>
// Via regexp like:
// http://example.org/page.*
//
// The right way is to explicitly link graph nodes and query them by this relation:
// <http://example.org/page/foo> <type> <http://example.org/page>
func (it *Regex) AllowRefs(v bool) {
it.allowRefs = v
}
func (it *Regex) testRegex(val graph.Value) bool {
// Type switch to avoid coercing and testing numeric types
v := it.qs.NameOf(val)
switch v := v.(type) {
case quad.Raw:
return it.re.MatchString(string(v))
case quad.String:
return it.re.MatchString(string(v))
case quad.TypedString:
return it.re.MatchString(string(v.Value))
default:
if it.allowRefs {
switch v := v.(type) {
case quad.BNode:
return it.re.MatchString(string(v))
case quad.IRI:
return it.re.MatchString(string(v))
}
}
}
return false
}
func (it *Regex) UID() uint64 {
return it.uid
}
func (it *Regex) Close() error {
return it.subIt.Close()
}
func (it *Regex) Reset() {
it.subIt.Reset()
it.err = nil
it.result = nil
}
func (it *Regex) Tagger() *graph.Tagger {
return &it.tags
}
func (it *Regex) Clone() graph.Iterator {
out := NewRegex(it.subIt.Clone(), it.re, it.qs)
out.tags.CopyFrom(it)
return out
}
func (it *Regex) Next() bool {
for it.subIt.Next() {
val := it.subIt.Result()
if it.testRegex(val) {
it.result = val
return true
}
}
it.err = it.subIt.Err()
return false
}
func (it *Regex) Err() error {
return it.err
}
func (it *Regex) Result() graph.Value {
return it.result
}
func (it *Regex) NextPath() bool {
for {
hasNext := it.subIt.NextPath()
if !hasNext {
it.err = it.subIt.Err()
return false
}
if it.testRegex(it.subIt.Result()) {
break
}
}
it.result = it.subIt.Result()
return true
}
func (it *Regex) SubIterators() []graph.Iterator {
return []graph.Iterator{it.subIt}
}
func (it *Regex) Contains(val graph.Value) bool {
if !it.testRegex(val) {
return false
}
ok := it.subIt.Contains(val)
if !ok {
it.err = it.subIt.Err()
}
return ok
}
// Registers the Regex iterator.
func (it *Regex) Type() graph.Type {
return graph.Regex
}
func (it *Regex) Describe() graph.Description {
primary := it.subIt.Describe()
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Iterator: &primary,
}
}
// There's nothing to optimize, locally, for a Regex iterator.
// Replace the underlying iterator if need be.
func (it *Regex) Optimize() (graph.Iterator, bool) {
newSub, changed := it.subIt.Optimize()
if changed {
it.subIt.Close()
it.subIt = newSub
}
return it, false
}
// We're only as expensive as our subiterator.
func (it *Regex) Stats() graph.IteratorStats {
return it.subIt.Stats()
}
// If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it.
func (it *Regex) TagResults(dst map[string]graph.Value) {
for _, tag := range it.tags.Tags() {
dst[tag] = it.Result()
}
for tag, value := range it.tags.Fixed() {
dst[tag] = value
}
it.subIt.TagResults(dst)
}
func (it *Regex) Size() (int64, bool) {
return 0, false
}
var _ graph.Iterator = &Regex{}