forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadwriter.go
342 lines (295 loc) · 8.07 KB
/
quadwriter.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
// 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 graph
// Defines the interface for consistent replication of a graph instance.
//
// Separate from the backend, this dictates how individual quads get
// identified and replicated consistently across (potentially) multiple
// instances. The simplest case is to keep an append-only log of quad
// changes.
import (
"context"
"errors"
"io"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/quad"
)
type Procedure int8
func (p Procedure) String() string {
switch p {
case +1:
return "add"
case -1:
return "delete"
default:
return "invalid"
}
}
// The different types of actions a transaction can do.
const (
Add Procedure = +1
Delete Procedure = -1
)
type Delta struct {
Quad quad.Quad
Action Procedure
}
// Unwrap returns an original QuadStore value if it was wrapped by Handle.
// This prevents shadowing of optional interface implementations.
func Unwrap(qs QuadStore) QuadStore {
if h, ok := qs.(*Handle); ok {
return h.QuadStore
}
return qs
}
type Handle struct {
QuadStore
QuadWriter
}
type IgnoreOpts struct {
IgnoreDup, IgnoreMissing bool
}
func (h *Handle) Close() error {
err := h.QuadWriter.Close()
h.QuadStore.Close()
return err
}
var (
ErrQuadExists = errors.New("quad exists")
ErrQuadNotExist = errors.New("quad does not exist")
ErrInvalidAction = errors.New("invalid action")
ErrNodeNotExists = errors.New("node does not exist")
)
// DeltaError records an error and the delta that caused it.
type DeltaError struct {
Delta Delta
Err error
}
func (e *DeltaError) Error() string {
if !e.Delta.Quad.IsValid() {
return e.Err.Error()
}
return e.Delta.Action.String() + " " + e.Delta.Quad.String() + ": " + e.Err.Error()
}
// IsQuadExist returns whether an error is a DeltaError
// with the Err field equal to ErrQuadExists.
func IsQuadExist(err error) bool {
if err == ErrQuadExists {
return true
}
de, ok := err.(*DeltaError)
return ok && de.Err == ErrQuadExists
}
// IsQuadNotExist returns whether an error is a DeltaError
// with the Err field equal to ErrQuadNotExist.
func IsQuadNotExist(err error) bool {
if err == ErrQuadNotExist {
return true
}
de, ok := err.(*DeltaError)
return ok && de.Err == ErrQuadNotExist
}
// IsInvalidAction returns whether an error is a DeltaError
// with the Err field equal to ErrInvalidAction.
func IsInvalidAction(err error) bool {
if err == ErrInvalidAction {
return true
}
de, ok := err.(*DeltaError)
return ok && de.Err == ErrInvalidAction
}
var (
// IgnoreDuplicates specifies whether duplicate quads
// cause an error during loading or are ignored.
IgnoreDuplicates = true
// IgnoreMissing specifies whether missing quads
// cause an error during deletion or are ignored.
IgnoreMissing = false
)
type QuadWriter interface {
// AddQuad adds a quad to the store.
AddQuad(quad.Quad) error
// TODO(barakmich): Deprecate in favor of transaction.
// AddQuadSet adds a set of quads to the store, atomically if possible.
AddQuadSet([]quad.Quad) error
// RemoveQuad removes a quad matching the given one from the database,
// if it exists. Does nothing otherwise.
RemoveQuad(quad.Quad) error
// ApplyTransaction applies a set of quad changes.
ApplyTransaction(*Transaction) error
// RemoveNode removes all quads which have the given node as subject, predicate, object, or label.
//
// It returns ErrNodeNotExists if node is missing.
RemoveNode(quad.Value) error
// Close cleans up replication and closes the writing aspect of the database.
Close() error
}
type NewQuadWriterFunc func(QuadStore, Options) (QuadWriter, error)
var writerRegistry = make(map[string]NewQuadWriterFunc)
func RegisterWriter(name string, newFunc NewQuadWriterFunc) {
if _, found := writerRegistry[name]; found {
panic("already registered QuadWriter " + name)
}
writerRegistry[name] = newFunc
}
func NewQuadWriter(name string, qs QuadStore, opts Options) (QuadWriter, error) {
newFunc, hasNew := writerRegistry[name]
if !hasNew {
return nil, errors.New("replication: name '" + name + "' is not registered")
}
return newFunc(qs, opts)
}
func WriterMethods() []string {
t := make([]string, 0, len(writerRegistry))
for n := range writerRegistry {
t = append(t, n)
}
return t
}
type BatchWriter interface {
quad.WriteCloser
Flush() error
}
// NewWriter creates a quad writer for a given QuadStore.
//
// Caller must call Flush or Close to flush an internal buffer.
func NewWriter(qs QuadWriter) BatchWriter {
return &batchWriter{qs: qs}
}
type batchWriter struct {
qs QuadWriter
buf []quad.Quad
}
func (w *batchWriter) flushBuffer(force bool) error {
if !force && len(w.buf) < quad.DefaultBatch {
return nil
}
_, err := w.WriteQuads(w.buf)
w.buf = w.buf[:0]
return err
}
func (w *batchWriter) WriteQuad(q quad.Quad) error {
if err := w.flushBuffer(false); err != nil {
return err
}
w.buf = append(w.buf, q)
return nil
}
func (w *batchWriter) WriteQuads(quads []quad.Quad) (int, error) {
if err := w.qs.AddQuadSet(quads); err != nil {
return 0, err
}
return len(quads), nil
}
func (w *batchWriter) Flush() error {
return w.flushBuffer(true)
}
func (w *batchWriter) Close() error {
return w.Flush()
}
// NewTxWriter creates a writer that applies a given procedures for all quads in stream.
// If procedure is zero, Add operation will be used.
func NewTxWriter(tx *Transaction, p Procedure) quad.Writer {
if p == 0 {
p = Add
}
return &txWriter{tx: tx, p: p}
}
type txWriter struct {
tx *Transaction
p Procedure
}
func (w *txWriter) WriteQuad(q quad.Quad) error {
switch w.p {
case Add:
w.tx.AddQuad(q)
case Delete:
w.tx.RemoveQuad(q)
default:
return ErrInvalidAction
}
return nil
}
func (w *txWriter) WriteQuads(buf []quad.Quad) (int, error) {
for i, q := range buf {
if err := w.WriteQuad(q); err != nil {
return i, err
}
}
return len(buf), nil
}
// NewRemover creates a quad writer for a given QuadStore which removes quads instead of adding them.
func NewRemover(qs QuadWriter) BatchWriter {
return &removeWriter{qs: qs}
}
type removeWriter struct {
qs QuadWriter
}
func (w *removeWriter) WriteQuad(q quad.Quad) error {
return w.qs.RemoveQuad(q)
}
func (w *removeWriter) WriteQuads(quads []quad.Quad) (int, error) {
tx := NewTransaction()
for _, q := range quads {
tx.RemoveQuad(q)
}
if err := w.qs.ApplyTransaction(tx); err != nil {
return 0, err
}
return len(quads), nil
}
func (w *removeWriter) Flush() error {
return nil // TODO: batch deletes automatically
}
func (w *removeWriter) Close() error { return nil }
// NewQuadStoreReader creates a quad reader for a given QuadStore.
func NewQuadStoreReader(qs QuadStore) quad.ReadSkipCloser {
return NewResultReader(qs, nil)
}
// NewResultReader creates a quad reader for a given QuadStore and iterator.
// If iterator is nil QuadsAllIterator will be used.
//
// Only quads returned by iterator's Result will be used.
//
// Iterator will be closed with the reader.
func NewResultReader(qs QuadStore, it iterator.Scanner) quad.ReadSkipCloser {
if it == nil {
it = qs.QuadsAllIterator().Iterate()
}
return &quadReader{qs: qs, it: it}
}
type quadReader struct {
qs QuadStore
it iterator.Scanner
}
func (r *quadReader) ReadQuad() (quad.Quad, error) {
if r.it.Next(context.TODO()) {
return r.qs.Quad(r.it.Result()), nil
}
err := r.it.Err()
if err == nil {
err = io.EOF
}
return quad.Quad{}, err
}
func (r *quadReader) SkipQuad() error {
if r.it.Next(context.TODO()) {
return nil
}
if err := r.it.Err(); err != nil {
return err
}
return io.EOF
}
func (r *quadReader) Close() error { return r.it.Close() }