-
Notifications
You must be signed in to change notification settings - Fork 7
/
quad.go
299 lines (269 loc) · 7.15 KB
/
quad.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
// 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 quad defines quad and triple handling.
package quad
// Defines the struct which makes the QuadStore possible -- the quad.
//
// At its heart, it consists of three fields -- Subject, Predicate, and Object.
// Three IDs that relate to each other. That's all there is to it. The quads
// are the links in the graph, and the existence of node IDs is defined by the
// fact that some quad in the graph mentions them.
//
// This means that a complete representation of the graph is equivalent to a
// list of quads. The rest is just indexing for speed.
//
// Adding fields to the quad is not to be taken lightly. You'll see I mention
// label, but don't as yet use it in any backing store. In general, there
// can be features that can be turned on or off for any store, but I haven't
// decided how to allow/disallow them yet. Another such example would be to add
// a forward and reverse index field -- forward being "order the list of
// objects pointed at by this subject with this predicate" such as first and
// second children, top billing, what have you.
//
// There will never be that much in this file except for the definition, but
// the consequences are not to be taken lightly. But do suggest cool features!
import (
"encoding/json"
"errors"
"fmt"
)
var (
ErrInvalid = errors.New("invalid N-Quad")
ErrIncomplete = errors.New("incomplete N-Quad")
)
// Make creates a quad with provided values.
func Make(subject, predicate, object, label interface{}) (q Quad) {
var ok bool
if q.Subject, ok = AsValue(subject); !ok {
q.Subject = String(fmt.Sprint(subject))
}
if q.Predicate, ok = AsValue(predicate); !ok {
q.Predicate = String(fmt.Sprint(predicate))
}
if q.Object, ok = AsValue(object); !ok {
q.Object = String(fmt.Sprint(object))
}
if q.Label, ok = AsValue(label); !ok {
q.Label = String(fmt.Sprint(label))
}
return
}
// MakeRaw creates a quad with provided raw values (nquads-escaped).
//
// Deprecated: use Make pr MakeIRI instead.
func MakeRaw(subject, predicate, object, label string) (q Quad) {
if subject != "" {
q.Subject = Raw(subject)
}
if predicate != "" {
q.Predicate = Raw(predicate)
}
if object != "" {
q.Object = Raw(object)
}
if label != "" {
q.Label = Raw(label)
}
return
}
// MakeIRI creates a quad with provided IRI values.
func MakeIRI(subject, predicate, object, label string) (q Quad) {
if subject != "" {
q.Subject = IRI(subject)
}
if predicate != "" {
q.Predicate = IRI(predicate)
}
if object != "" {
q.Object = IRI(object)
}
if label != "" {
q.Label = IRI(label)
}
return
}
var (
_ json.Marshaler = Quad{}
_ json.Unmarshaler = (*Quad)(nil)
)
// Our quad struct, used throughout.
type Quad struct {
Subject Value `json:"subject"`
Predicate Value `json:"predicate"`
Object Value `json:"object"`
Label Value `json:"label,omitempty"`
}
type rawQuad struct {
Subject string `json:"subject"`
Predicate string `json:"predicate"`
Object string `json:"object"`
Label string `json:"label,omitempty"`
}
func (q Quad) MarshalJSON() ([]byte, error) {
rq := rawQuad{
Subject: ToString(q.Subject),
Predicate: ToString(q.Predicate),
Object: ToString(q.Object),
}
if q.Label != nil {
rq.Label = ToString(q.Label)
}
return json.Marshal(rq)
}
func (q *Quad) UnmarshalJSON(data []byte) error {
var rq rawQuad
if err := json.Unmarshal(data, &rq); err != nil {
return err
}
// TODO(dennwc): parse nquads? or use StringToValue hack?
*q = MakeRaw(rq.Subject, rq.Predicate, rq.Object, rq.Label)
return nil
}
// Direction specifies an edge's type.
type Direction byte
// List of the valid directions of a quad.
const (
Any Direction = iota
Subject
Predicate
Object
Label
)
var Directions = []Direction{Subject, Predicate, Object, Label}
func (d Direction) Prefix() byte {
switch d {
case Any:
return 'a'
case Subject:
return 's'
case Predicate:
return 'p'
case Label:
return 'c'
case Object:
return 'o'
default:
return '\x00'
}
}
func (d Direction) String() string {
switch d {
case Any:
return "any"
case Subject:
return "subject"
case Predicate:
return "predicate"
case Label:
return "label"
case Object:
return "object"
default:
return fmt.Sprint("illegal direction:", byte(d))
}
}
func (d Direction) GoString() string {
switch d {
case Any:
return "quad.Any"
case Subject:
return "quad.Subject"
case Predicate:
return "quad.Predicate"
case Label:
return "quad.Label"
case Object:
return "quad.Object"
default:
return fmt.Sprintf("%x", byte(d))
}
}
// Per-field accessor for quads.
func (q Quad) Get(d Direction) Value {
switch d {
case Subject:
return q.Subject
case Predicate:
return q.Predicate
case Label:
return q.Label
case Object:
return q.Object
default:
panic(d.String())
}
}
func (q *Quad) Set(d Direction, v Value) {
switch d {
case Subject:
q.Subject = v
case Predicate:
q.Predicate = v
case Label:
q.Label = v
case Object:
q.Object = v
default:
panic(d.String())
}
}
// Per-field accessor for quads that returns strings instead of values.
func (q Quad) GetString(d Direction) string {
switch d {
case Subject:
return StringOf(q.Subject)
case Predicate:
return StringOf(q.Predicate)
case Object:
return StringOf(q.Object)
case Label:
return StringOf(q.Label)
default:
panic(d.String())
}
}
// Pretty-prints a quad.
func (q Quad) String() string {
return fmt.Sprintf("%v -- %v -> %v", q.Subject, q.Predicate, q.Object)
}
func (q Quad) IsValid() bool {
return IsValidValue(q.Subject) && IsValidValue(q.Predicate) && IsValidValue(q.Object)
}
// Prints a quad in N-Quad format.
func (q Quad) NQuad() string {
if q.Label == nil || q.Label.String() == "" {
return fmt.Sprintf("%s %s %s .", q.Subject, q.Predicate, q.Object)
}
return fmt.Sprintf("%s %s %s %s .", q.Subject, q.Predicate, q.Object, q.Label)
}
type ByQuadString []Quad
func (o ByQuadString) Len() int { return len(o) }
func (o ByQuadString) Less(i, j int) bool {
switch { // TODO: optimize
case StringOf(o[i].Subject) < StringOf(o[j].Subject),
StringOf(o[i].Subject) == StringOf(o[j].Subject) &&
StringOf(o[i].Predicate) < StringOf(o[j].Predicate),
StringOf(o[i].Subject) == StringOf(o[j].Subject) &&
StringOf(o[i].Predicate) == StringOf(o[j].Predicate) &&
StringOf(o[i].Object) < StringOf(o[j].Object),
StringOf(o[i].Subject) == StringOf(o[j].Subject) &&
StringOf(o[i].Predicate) == StringOf(o[j].Predicate) &&
StringOf(o[i].Object) == StringOf(o[j].Object) &&
StringOf(o[i].Label) < StringOf(o[j].Label):
return true
default:
return false
}
}
func (o ByQuadString) Swap(i, j int) { o[i], o[j] = o[j], o[i] }