-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathdocument.go
594 lines (513 loc) · 14.1 KB
/
document.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package client
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/fxamacker/cbor/v2"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/sourcenetwork/defradb/errors"
)
// This is the main implementation starting point for accessing the internal Document API
// which provides API access to the various operations available for Documents, i.e. CRUD.
//
// Documents in this case refer to the core database type of DefraDB which is a
// "NoSQL Document Datastore".
//
// This section is not concerned with the outer query layer used to interact with the
// Document API, but instead is solely concerned with carrying out the internal API
// operations.
//
// Note: These actions on the outside are deceivingly simple, but require a number
// of complex interactions with the underlying KV Datastore, as well as the
// Merkle CRDT semantics.
// Document is a generalized struct referring to a stored document in the database.
//
// It *can* have a reference to a enforced schema, which is enforced at the time
// of an operation.
//
// Documents are similar to JSON Objects stored in MongoDB, which are collections
// of Fields and Values.
//
// Fields are Key names that point to values.
// Values are literal or complex objects such as strings, integers, or sub documents (objects).
//
// Note: Documents represent the serialized state of the underlying MerkleCRDTs
//
// @todo: Extract Document into a Interface
// @body: A document interface can be implemented by both a TypedDocument and a
// UnTypedDocument, which use a schema and schemaless approach respectively.
type Document struct {
key DocKey
fields map[string]Field
values map[Field]Value
head cid.Cid
mu sync.RWMutex
// marks if document has unsaved changes
isDirty bool
}
func NewDocWithKey(key DocKey) *Document {
doc := newEmptyDoc()
doc.key = key
return doc
}
func newEmptyDoc() *Document {
return &Document{
fields: make(map[string]Field),
values: make(map[Field]Value),
}
}
func NewDocFromMap(data map[string]any) (*Document, error) {
var err error
doc := &Document{
fields: make(map[string]Field),
values: make(map[Field]Value),
}
// check if document contains special _key field
k, hasKey := data["_key"]
if hasKey {
delete(data, "_key") // remove the key so it isn't parsed further
kstr, ok := k.(string)
if !ok {
return nil, errors.New("provided _key in document must be a string type")
}
if doc.key, err = NewDocKeyFromString(kstr); err != nil {
return nil, err
}
}
err = doc.setAndParseObjectType(data)
if err != nil {
return nil, err
}
// if no key was specified, then we assume it doesn't exist and we generate it.
if !hasKey {
pref := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.SHA2_256,
MhLength: -1, // default length
}
buf, err := doc.Bytes()
if err != nil {
return nil, err
}
// And then feed it some data
c, err := pref.Sum(buf)
if err != nil {
return nil, err
}
doc.key = NewDocKeyV0(c)
}
return doc, nil
}
// NewFromJSON creates a new instance of a Document from a raw JSON object byte array
func NewDocFromJSON(obj []byte) (*Document, error) {
data := make(map[string]any)
err := json.Unmarshal(obj, &data)
if err != nil {
return nil, err
}
return NewDocFromMap(data)
}
func (doc *Document) Head() cid.Cid {
doc.mu.RLock()
defer doc.mu.RUnlock()
return doc.head
}
func (doc *Document) SetHead(head cid.Cid) {
doc.mu.Lock()
defer doc.mu.Unlock()
doc.head = head
}
// Key returns the generated DocKey for this document
func (doc *Document) Key() DocKey {
// Reading without a read-lock as we assume the DocKey is immutable
return doc.key
}
// Get returns the raw value for a given field
// Since Documents are objects with potentially sub objects
// a supplied field string can be of the form "A/B/C"
// Where field A is an object containing a object B which has
// a field C
// If no matching field exists then return an empty interface
// and an error.
func (doc *Document) Get(field string) (any, error) {
val, err := doc.GetValue(field)
if err != nil {
return nil, err
}
return val.Value(), nil
}
// GetValue given a field as a string, return the Value type
func (doc *Document) GetValue(field string) (Value, error) {
doc.mu.RLock()
defer doc.mu.RUnlock()
path, subPaths, hasSubPaths := parseFieldPath(field)
f, exists := doc.fields[path]
if !exists {
return nil, NewErrFieldNotExist(path)
}
val, err := doc.GetValueWithField(f)
if err != nil {
return nil, err
}
if !hasSubPaths {
return val, nil
} else if hasSubPaths && !val.IsDocument() {
return nil, ErrFieldNotObject
} else {
return val.Value().(*Document).GetValue(subPaths)
}
}
// GetValueWithField gets the Value type from a given Field type
func (doc *Document) GetValueWithField(f Field) (Value, error) {
doc.mu.RLock()
defer doc.mu.RUnlock()
v, exists := doc.values[f]
if !exists {
return nil, NewErrFieldNotExist(f.Name())
}
return v, nil
}
// SetWithJSON sets all the fields of a document using the provided
// JSON Merge Patch object. Note: fields indicated as nil in the Merge
// Patch are to be deleted
// @todo: Handle sub documents for SetWithJSON
func (doc *Document) SetWithJSON(patch []byte) error {
var patchObj map[string]any
err := json.Unmarshal(patch, &patchObj)
if err != nil {
return err
}
for k, v := range patchObj {
if v == nil {
err = doc.Delete(k)
} else {
err = doc.Set(k, v)
}
if err != nil {
return err
}
}
return nil
}
// Set the value of a field
func (doc *Document) Set(field string, value any) error {
return doc.setAndParseType(field, value)
}
// SetAs is the same as set, but you can manually set the CRDT type
func (doc *Document) SetAs(field string, value any, t CType) error {
return doc.setCBOR(t, field, value)
}
// Delete removes a field, and marks it to be deleted on the following db.Update() call
func (doc *Document) Delete(fields ...string) error {
doc.mu.Lock()
defer doc.mu.Unlock()
for _, f := range fields {
field, exists := doc.fields[f]
if !exists {
return NewErrFieldNotExist(f)
}
doc.values[field].Delete()
}
return nil
}
// SetAsType Sets the value of a field along with a specific type
// func (doc *Document) SetAsType(t client.CType, field string, value any) error {
// return doc.set(t, field, value)
// }
func (doc *Document) set(t CType, field string, value Value) error {
doc.mu.Lock()
defer doc.mu.Unlock()
var f Field
if v, exists := doc.fields[field]; exists {
f = v
} else {
f = doc.newField(t, field)
doc.fields[field] = f
}
doc.values[f] = value
doc.isDirty = true
return nil
}
func (doc *Document) setCBOR(t CType, field string, val any) error {
value := newCBORValue(t, val)
return doc.set(t, field, value)
}
func (doc *Document) setObject(t CType, field string, val *Document) error {
value := newValue(t, val)
return doc.set(t, field, &value)
}
// @todo: Update with document schemas
func (doc *Document) setAndParseType(field string, value any) error {
if value == nil {
return nil
}
switch val := value.(type) {
// int (any number)
case int:
err := doc.setCBOR(LWW_REGISTER, field, int64(val))
if err != nil {
return err
}
case float64:
// case int64:
// Check if its actually a float or just an int
if float64(int64(val)) == val { //int
err := doc.setCBOR(LWW_REGISTER, field, int64(val))
if err != nil {
return err
}
} else { //float
err := doc.setCBOR(LWW_REGISTER, field, val)
if err != nil {
return err
}
}
// string, bool, and more
case string, bool, []any:
err := doc.setCBOR(LWW_REGISTER, field, val)
if err != nil {
return err
}
// sub object, recurse down.
// @TODO: Object Definitions
// You can use an object as a way to override defaults
// and types for JSON literals.
// Eg.
// Instead of { "Timestamp": 123 }
// - which is parsed as an int
// Use { "Timestamp" : { "_Type": "uint64", "_Value": 123 } }
// - Which is parsed as an uint64
case map[string]any:
subDoc := newEmptyDoc()
err := subDoc.setAndParseObjectType(val)
if err != nil {
return err
}
err = doc.setObject(OBJECT, field, subDoc)
if err != nil {
return err
}
default:
return errors.New(fmt.Sprintf("Unhandled type in raw JSON: %v => %T", field, val))
}
return nil
}
func (doc *Document) setAndParseObjectType(value map[string]any) error {
for k, v := range value {
err := doc.setAndParseType(k, v)
if err != nil {
return err
}
}
return nil
}
// Fields gets the document fields as a map
func (doc *Document) Fields() map[string]Field {
doc.mu.RLock()
defer doc.mu.RUnlock()
return doc.fields
}
// Values gets the document values as a map
func (doc *Document) Values() map[Field]Value {
doc.mu.RLock()
defer doc.mu.RUnlock()
return doc.values
}
// Bytes returns the document as a serialzed byte array
// using CBOR encoding
func (doc *Document) Bytes() ([]byte, error) {
docMap, err := doc.toMap()
if err != nil {
return nil, err
}
// Important: CanonicalEncOptions ensures consistent serialization of
// indeterministic datastructures, like Go Maps
em, err := cbor.CanonicalEncOptions().EncMode()
if err != nil {
return nil, err
}
return em.Marshal(docMap)
}
// String returns the document as a stringified JSON Object.
// Note: This representation should not be used for any
// cryptographic operations, such as signatures, or hashes
// as it does not guarantee canonical representation or
// ordering.
func (doc *Document) String() (string, error) {
docMap, err := doc.toMap()
if err != nil {
return "", err
}
j, err := json.MarshalIndent(docMap, "", "\t")
if err != nil {
return "", err
}
return string(j), nil
}
// ToMap returns the document as a map[string]any
// object.
func (doc *Document) ToMap() (map[string]any, error) {
return doc.toMapWithKey()
}
func (doc *Document) Clean() {
for _, v := range doc.Fields() {
val, _ := doc.GetValueWithField(v)
if val.IsDirty() {
if val.IsDelete() {
doc.SetAs(v.Name(), nil, v.Type()) //nolint:errcheck
}
val.Clean()
}
}
}
// converts the document into a map[string]any
// including any sub documents
func (doc *Document) toMap() (map[string]any, error) {
doc.mu.RLock()
defer doc.mu.RUnlock()
docMap := make(map[string]any)
for k, v := range doc.fields {
value, exists := doc.values[v]
if !exists {
return nil, NewErrFieldNotExist(v.Name())
}
if value.IsDocument() {
subDoc := value.Value().(*Document)
subDocMap, err := subDoc.toMap()
if err != nil {
return nil, err
}
docMap[k] = subDocMap
}
docMap[k] = value.Value()
}
return docMap, nil
}
func (doc *Document) toMapWithKey() (map[string]any, error) {
doc.mu.RLock()
defer doc.mu.RUnlock()
docMap := make(map[string]any)
for k, v := range doc.fields {
value, exists := doc.values[v]
if !exists {
return nil, NewErrFieldNotExist(v.Name())
}
if value.IsDocument() {
subDoc := value.Value().(*Document)
subDocMap, err := subDoc.toMapWithKey()
if err != nil {
return nil, err
}
docMap[k] = subDocMap
}
docMap[k] = value.Value()
}
docMap["_key"] = doc.Key().String()
return docMap, nil
}
// loops through an object of the form map[string]any
// and fills in the Document with each field it finds in the object.
// Automatically handles sub objects and arrays.
// Does not allow anonymous fields, error is thrown in this case
// Eg. The JSON value [1,2,3,4] by itself is a valid JSON Object, but has no
// field name.
// func parseJSONObject(doc *Document, data map[string]any) error {
// for k, v := range data {
// switch v.(type) {
// // int (any number)
// case float64:
// // case int64:
// // Check if its actually a float or just an int
// val := v.(float64)
// if float64(int64(val)) == val { //int
// doc.setCBOR(crdt.LWW_REGISTER, k, int64(val))
// } else { //float
// panic("todo")
// }
// break
// // string
// case string:
// doc.setCBOR(crdt.LWW_REGISTER, k, v)
// break
// // array
// case []any:
// break
// // sub object, recurse down.
// // @TODO: Object Definitions
// // You can use an object as a way to override defaults
// // and types for JSON literals.
// // Eg.
// // Instead of { "Timestamp": 123 }
// // - which is parsed as an int
// // Use { "Timestamp" : { "_Type": "uint64", "_Value": 123 } }
// // - Which is parsed as an uint64
// case map[string]any:
// subDoc := newEmptyDoc()
// err := parseJSONObject(subDoc, v.(map[string]any))
// if err != nil {
// return err
// }
// doc.setObject(crdt.OBJECT, k, subDoc)
// break
// default:
// return errors.Wrap("Unhandled type in raw JSON: %v => %T", k, v)
// }
// }
// return nil
// }
// parses a document field path, can have sub elements if we have embedded objects.
// Returns the first path, the remaining split paths, and a bool indicating if there are sub paths
func parseFieldPath(path string) (string, string, bool) {
splitKeys := strings.SplitN(path, "/", 2)
return splitKeys[0], strings.Join(splitKeys[1:], ""), len(splitKeys) > 1
}
// Example Usage: Create/Insert new object
/*
obj := `{
Hello: "World"
}`
objData := make(map[string]any)
err := json.Unmarshal(&objData, obj)
docA := document.NewFromJSON(objData)
err := db.Save(document)
=> New batch transaction/store
=> Loop through doc values
=> instantiate MerkleCRDT objects
=> Set/Publish new CRDT values
// One-to-one relationship example
obj := `{
Hello: "world",
Author: {
Name: "Bob",
}
}`
docA := document.NewFromJSON(obj)
// method 1
docA.Patch(...)
col.Save(docA)
// method 2
docA.Get("Author").Set("Name", "Eric")
col.Save(docA)
// method 3
docB := docA.GetObject("Author")
docB.Set("Name", "Eric")
authorCollection.Save(docB)
// method 4
docA.Set("Author.Name")
// method 5
doc := col.GetWithRelations("key")
// equivalent
doc := col.Get(key, db.WithRelationsOpt)
*/