-
Notifications
You must be signed in to change notification settings - Fork 1
/
objects.go
609 lines (554 loc) · 14.9 KB
/
objects.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package chotki
import (
"context"
"encoding/binary"
"fmt"
"unicode/utf8"
"github.com/cockroachdb/pebble"
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
"github.com/pkg/errors"
)
func OKey(id rdx.ID, rdt byte) (key []byte) {
var ret = [16]byte{'O'}
key = binary.BigEndian.AppendUint64(ret[:1], uint64(id))
key = append(key, rdt)
return
}
const LidLKeyLen = 1 + 8 + 1
func OKeyIdRdt(key []byte) (id rdx.ID, rdt byte) {
if len(key) != LidLKeyLen {
return rdx.BadId, 0
}
id = rdx.IDFromBytes(key[1 : LidLKeyLen-1])
rdt = key[LidLKeyLen-1]
return
}
var VKey0 = []byte{'V', 0, 0, 0, 0, 0, 0, 0, 0, 'V'}
func VKey(id rdx.ID) (key []byte) {
var ret = [16]byte{'V'}
block := id | SyncBlockMask
key = binary.BigEndian.AppendUint64(ret[:1], uint64(block))
key = append(key, 'V')
return
}
func VKeyId(key []byte) rdx.ID {
if len(key) != LidLKeyLen {
return rdx.BadId
}
return rdx.IDFromBytes(key[1:]) & ^SyncBlockMask
}
// A class contains a number of fields. Each Field has
// some RDT type. A class can inherit another class.
// New fields can be appended to a class, but never removed.
// Max number of fields is 128, max inheritance depth 32.
// When stored, a class is an append-only sequence of Ts.
// The syntax for each T: "XName", where X is the RDT.
// For the map types, can use "MSS_Name" or similar.
// Each field has an Offset. The Offset+RdxType pair is the
// *actual key* for the field in the database.
// Entries having identical Offset+RdxType are considered *renames*!
type Field struct {
Offset int64
Name string
RdxType byte
RdxTypeExt []byte
}
// Fields
type Fields []Field
func (f Field) Valid() bool {
for _, l := range f.Name { // has unsafe chars
if l < ' ' {
return false
}
}
return (f.RdxType >= 'A' && f.RdxType <= 'Z' &&
len(f.Name) > 0 && utf8.ValidString(f.Name))
}
func (fs Fields) MaxOffset() (off int64) {
for _, f := range fs {
if f.Offset > off {
off = f.Offset
}
}
return
}
func (f Fields) FindRdtOff(rdx byte, off int64) int {
for i := 0; i < len(f); i++ {
if f[i].RdxType == rdx && f[i].Offset == off {
return i
}
}
return -1
}
func (f Fields) FindName(name string) (ndx int) { // fixme double naming?
for i := 0; i < len(f); i++ {
if f[i].Name == name {
return i
}
}
return -1
}
func ObjectKeyRange(oid rdx.ID) (fro, til []byte) {
oid = oid & ^rdx.OffMask
return OKey(oid, 'O'), OKey(oid+rdx.ProInc, 0)
}
// returns nil for "not found"
func (cho *Chotki) ObjectIterator(oid rdx.ID, snap *pebble.Snapshot) *pebble.Iterator {
fro, til := ObjectKeyRange(oid)
io := pebble.IterOptions{
LowerBound: fro,
UpperBound: til,
}
var it *pebble.Iterator
if snap != nil {
it = snap.NewIter(&io)
} else {
it = cho.db.NewIter(&io)
}
if it.SeekGE(fro) { // fixme
id, rdt := OKeyIdRdt(it.Key())
if rdt == 'O' && id == oid {
// An iterator is returned from a function, it cannot be closed
return it
}
}
if it != nil {
_ = it.Close()
}
return nil
}
// todo note that the class may change as the program runs; in such a case
// if the class fields are already cached, the current session will not
// understand the new fields!
func (cho *Chotki) ClassFields(cid rdx.ID) (fields Fields, err error) {
if fields, ok := cho.types.Load(cid); ok {
return fields, nil
}
okey := OKey(cid, 'C')
tlv, clo, e := cho.db.Get(okey)
if e != nil {
return nil, ErrTypeUnknown
}
it := rdx.FIRSTIterator{TLV: tlv}
fields = append(fields, Field{ // todo inheritance
Offset: 0,
Name: "_ref",
RdxType: rdx.Reference,
})
for it.Next() {
lit, t, name := it.ParsedValue()
if lit != rdx.Term || len(name) == 0 {
break // todo unique names etc
}
rdt := rdx.String
if name[0] >= 'A' && name[0] <= 'Z' {
rdt = name[0]
name = name[1:]
}
fields = append(fields, Field{
Offset: t.Rev,
RdxType: rdt,
Name: string(name),
})
}
_ = clo.Close()
cho.types.Store(cid, fields)
return
}
func (cho *Chotki) ObjectFieldsByClass(oid rdx.ID, form []string) (tid rdx.ID, tlvs protocol.Records, err error) {
it := cho.ObjectIterator(oid, nil)
if it == nil {
return rdx.BadId, nil, ErrObjectUnknown
}
defer it.Close()
tid = rdx.IDFromZipBytes(it.Value())
for it.Next() {
id, rdt := OKeyIdRdt(it.Key())
off := int(id.Off())
if off == 0 || off > len(form) {
continue
}
decl := form[off-1]
if len(decl) == 0 || rdt != decl[0] {
continue
}
for off > len(tlvs)+1 {
tlvs = append(tlvs, nil)
}
tlvs = append(tlvs, it.Value())
}
return
}
func (cho *Chotki) ObjectFields(oid rdx.ID) (tid rdx.ID, decl Fields, fact protocol.Records, err error) {
it := cho.ObjectIterator(oid, nil)
if it == nil {
err = ErrObjectUnknown
return
}
defer it.Close()
tid = rdx.IDFromZipBytes(it.Value())
decl, err = cho.ClassFields(tid)
fact = make(protocol.Records, len(decl))
if err != nil {
return
}
fact = append(fact, it.Value())
for it.Next() {
id, rdt := OKeyIdRdt(it.Key())
off := int64(id.Off())
ndx := decl.FindRdtOff(rdt, off)
if ndx == -1 {
continue
}
fact[ndx] = it.Value()
}
return
}
func (cho *Chotki) ObjectFieldsTLV(oid rdx.ID) (tid rdx.ID, tlv protocol.Records, err error) {
it := cho.ObjectIterator(oid, nil)
if it == nil {
return rdx.BadId, nil, ErrObjectUnknown
}
defer it.Close()
tid = rdx.IDFromZipBytes(it.Value())
for it.Next() {
cp := make([]byte, len(it.Value()))
copy(cp, it.Value())
tlv = append(tlv, cp)
}
return
}
func FieldOffset(fields []string, name string) rdx.ID {
for i := 0; i < len(fields); i++ {
fn := fields[i]
if len(fn) > 0 && fn[1:] == name {
return rdx.ID(i + 1)
}
}
return 0
}
// ObjectFieldTLV picks one field fast. No class checks, etc.
func (cho *Chotki) ObjectFieldTLV(fid rdx.ID) (rdt byte, tlv []byte, err error) {
db := cho.db
if db == nil {
return 0, nil, ErrClosed
}
it := cho.db.NewIter(&pebble.IterOptions{})
defer it.Close()
key := OKey(fid, 0)
if !it.SeekGE(key) {
return 0, nil, pebble.ErrNotFound
}
var fidfact rdx.ID
fidfact, rdt = OKeyIdRdt(it.Key())
if fidfact != fid {
return 0, nil, pebble.ErrNotFound
}
tlv = it.Value()
return
}
// ObjectFieldTLV picks one field given its id and RDT.
func (cho *Chotki) ObjectRDTFieldTLV(fid rdx.ID, rdt byte) (tlv []byte, err error) {
db := cho.db
if db == nil {
return nil, ErrClosed
}
it := cho.db.NewIter(&pebble.IterOptions{})
defer it.Close()
key := OKey(fid, rdt)
if !it.SeekGE(key) {
return nil, pebble.ErrNotFound
}
fidfact, rdtfact := OKeyIdRdt(it.Key())
if fidfact != fid || rdtfact != rdt {
return nil, pebble.ErrNotFound
}
tlv = it.Value()
return
}
func (cho *Chotki) ObjectVVField(fid rdx.ID) (vv rdx.VV, err error) {
var rdt byte
var tlv []byte
rdt, tlv, err = cho.ObjectFieldTLV(fid)
if err != nil {
return
}
if rdt != rdx.VVector {
return nil, ErrWrongFieldType
}
vv = make(rdx.VV)
err = vv.PutTLV(tlv)
return
}
func (cho *Chotki) NewClass(ctx context.Context, parent rdx.ID, fields ...Field) (id rdx.ID, err error) {
var fspecs protocol.Records
maxidx := int64(-1)
for _, field := range fields {
if field.Offset > maxidx {
maxidx = field.Offset
} else if field.Offset == 0 {
maxidx++
field.Offset = maxidx
}
if !field.Valid() {
return rdx.BadId, ErrBadTypeDescription
}
name := append([]byte{}, field.RdxType)
name = append(name, field.Name...)
fspecs = append(fspecs, protocol.Record('T', rdx.FIRSTtlv(maxidx, 0, name)))
}
//head := protocol.AppendHeader(nil) fspecs.TotalLen()
return cho.CommitPacket(ctx, 'C', parent, fspecs)
}
// Creates a new object from enveloped TLV fields; no class checks.
func (cho *Chotki) NewObjectTLV(ctx context.Context, tid rdx.ID, fields protocol.Records) (id rdx.ID, err error) {
return cho.CommitPacket(ctx, 'O', tid, fields)
}
func (cho *Chotki) NewObject(ctx context.Context, tid rdx.ID, fields ...string) (id rdx.ID, err error) {
var form Fields
form, err = cho.ClassFields(tid)
if err != nil {
return
}
if len(fields) > len(form) {
return rdx.BadId, ErrUnknownFieldInAType
}
var packet protocol.Records
for i := 0; i < len(fields); i++ {
rdt := form[i+1].RdxType
tlv := rdx.Xparse(rdt, fields[i])
if tlv == nil {
return rdx.BadId, rdx.ErrBadValueForAType
}
packet = append(packet, protocol.Record(rdt, tlv))
}
return cho.NewObjectTLV(ctx, tid, packet)
}
// Deprecated: does not handle non-trivial cases
func (cho *Chotki) EditObject(ctx context.Context, oid rdx.ID, fields ...string) (id rdx.ID, err error) {
formula, err := cho.ClassFields(oid)
if err != nil {
return rdx.BadId, err
}
if len(fields) > len(formula) {
return rdx.BadId, ErrUnknownFieldInAType
}
_, obj, err := cho.ObjectFieldsTLV(oid)
if err != nil {
return rdx.BadId, err
}
// fetch type desc
var packet protocol.Records
for i := 0; i < len(fields); i++ {
rdt := byte(formula[i].RdxType)
tlv := rdx.X2string(rdt, obj[i], fields[i], cho.src)
if tlv == nil {
return rdx.BadId, rdx.ErrBadValueForAType
}
packet = append(packet, protocol.Record('F', rdx.ZipUint64(uint64(i))))
packet = append(packet, protocol.Record(rdt, tlv))
}
return cho.CommitPacket(ctx, 'E', oid, packet)
}
/*func (cho *Chotki) GetObject(oid rdx.ID) (tid rdx.ID, fields []string, err error) {
i := cho.ObjectIterator(oid)
if i == nil || !i.Valid() {
return rdx.BadId, nil, ErrObjectUnknown
}
tid = rdx.IDFromZipBytes(i.Value())
for i.Next() {
_, rdt := OKeyIdRdt(i.Key())
str := rdx.Xstring(rdt, i.Value())
fields = append(fields, str)
}
return
}*/
func (cho *Chotki) ObjectString(oid rdx.ID) (txt string, err error) {
_, form, fact, e := cho.ObjectFields(oid)
if e != nil {
return "", e
}
ret := []byte{'{'}
for n, d := range form {
if n != 0 {
ret = append(ret, ',')
}
ret = append(ret, d.Name...)
ret = append(ret, ':')
ret = append(ret, rdx.Xstring(d.RdxType, fact[n])...)
}
ret = append(ret, '}')
txt = string(ret)
return
}
func (cho *Chotki) EditObjectRDX(ctx context.Context, oid rdx.ID, pairs []rdx.RDX) (id rdx.ID, err error) {
tlvs := protocol.Records{}
_, form, fact, e := cho.ObjectFields(oid)
if e != nil {
return rdx.BadId, e
}
tmp := make(protocol.Records, len(fact))
for i := 0; i+1 < len(pairs); i += 2 {
if pairs[i].RdxType != rdx.Term {
return
}
name := pairs[i].String()
value := &pairs[i+1]
ndx := form.FindName(name)
if ndx == -1 {
err = fmt.Errorf("unknown field %s", name)
return
}
tmp[ndx] = protocol.Record(value.RdxType, rdx.FIRSTrdx2tlv(value))
}
for i := 0; i < len(form); i++ {
if tmp[i] != nil {
tlvs = append(tlvs, protocol.TinyRecord('F', rdx.ZipUint64(uint64(i))))
tlvs = append(tlvs, tmp[i])
}
}
return cho.CommitPacket(ctx, 'E', oid, tlvs)
}
func (cho *Chotki) SetFieldTLV(ctx context.Context, fid rdx.ID, tlve []byte) (id rdx.ID, err error) {
oid := fid.ZeroOff()
f := protocol.Record('F', rdx.ZipUint64(uint64(fid.Off())))
return cho.CommitPacket(ctx, 'E', oid, protocol.Records{f, tlve})
}
var ErrWrongFieldType = errors.New("wrong field type")
func (cho *Chotki) AddToNField(ctx context.Context, fid rdx.ID, count uint64) (id rdx.ID, err error) {
rdt, tlv, err := cho.ObjectFieldTLV(fid)
if err != nil || rdt != rdx.Natural {
return rdx.BadId, ErrWrongFieldType
}
src := cho.Source()
mine := rdx.Nmine(tlv, src)
tlvs := protocol.Records{
protocol.Record('F', rdx.ZipUint64(fid.Off())),
protocol.Record(rdx.Natural, rdx.Ntlvt(mine+count, src)),
}
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), tlvs)
return
}
func (cho *Chotki) IncNField(ctx context.Context, fid rdx.ID) (id rdx.ID, err error) {
return cho.AddToNField(ctx, fid, 1)
}
func (cho *Chotki) MapTRField(fid rdx.ID) (themap rdx.MapTR, err error) {
rdt, tlv, e := cho.ObjectFieldTLV(fid)
if e != nil {
return nil, e
}
if rdt != rdx.Mapping {
return nil, ErrWrongFieldType
}
themap = rdx.MnativeTR(tlv)
return
}
func (cho *Chotki) MapSSField(fid rdx.ID) (themap rdx.MapSS, err error) {
rdt, tlv, e := cho.ObjectFieldTLV(fid)
if e != nil {
return nil, e
}
if rdt != rdx.Mapping {
return nil, ErrWrongFieldType
}
themap = rdx.MnativeSS(tlv)
return
}
// Adds/removes elements to/from a map (removed should map to nil)
func (cho *Chotki) AddToMapTRField(ctx context.Context, fid rdx.ID, changes rdx.MapTR) (id rdx.ID, err error) {
rdt, tlv := cho.GetFieldTLV(fid) // todo error?
if rdt != rdx.Mapping {
return rdx.BadId, ErrWrongFieldType
}
newtlv := rdx.MtlvTR(changes)
dtlv := rdx.Mdelta2(tlv, newtlv)
if len(dtlv) == 0 {
return rdx.ID0, nil
}
packet := protocol.Records{
protocol.Record('F', rdx.ZipUint64(fid.Off())),
protocol.Record(rdx.Mapping, dtlv),
}
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), packet)
return
}
func (cho *Chotki) SetMapTRField(ctx context.Context, fid rdx.ID, changes rdx.MapTR) (id rdx.ID, err error) {
rdt, tlv := cho.GetFieldTLV(fid) // todo error?
if rdt != rdx.Mapping {
return rdx.BadId, ErrWrongFieldType
}
newtlv := rdx.MtlvTR(changes)
dtlv := rdx.Mdelta(tlv, newtlv)
if len(dtlv) == 0 {
return rdx.ID0, nil
}
packet := protocol.Records{
protocol.Record('F', rdx.ZipUint64(fid.Off())),
protocol.Record(rdx.Mapping, dtlv),
}
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), packet)
return
}
func (cho *Chotki) AddToMapSSField(ctx context.Context, fid rdx.ID, changes rdx.MapSS) (id rdx.ID, err error) {
rdt, tlv := cho.GetFieldTLV(fid) // todo error?
if rdt != rdx.Mapping {
return rdx.BadId, ErrWrongFieldType
}
newtlv := rdx.MtlvSS(changes)
dtlv := rdx.Mdelta2(tlv, newtlv)
if len(dtlv) == 0 {
return rdx.ID0, nil
}
packet := protocol.Records{
protocol.Record('F', rdx.ZipUint64(fid.Off())),
protocol.Record(rdx.Mapping, dtlv),
}
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), packet)
return
}
func (cho *Chotki) SetMapSSField(ctx context.Context, fid rdx.ID, changes rdx.MapSS) (id rdx.ID, err error) {
rdt, tlv := cho.GetFieldTLV(fid) // todo error?
if rdt != rdx.Mapping {
return rdx.BadId, ErrWrongFieldType
}
newtlv := rdx.MtlvSS(changes)
dtlv := rdx.Mdelta(tlv, newtlv)
if len(dtlv) == 0 {
return rdx.ID0, nil
}
packet := protocol.Records{
protocol.Record('F', rdx.ZipUint64(fid.Off())),
protocol.Record(rdx.Mapping, dtlv),
}
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), packet)
return
}
func (cho *Chotki) GetFieldTLV(id rdx.ID) (rdt byte, tlv []byte) {
key := OKey(id, 'A')
it := cho.db.NewIter(&pebble.IterOptions{
LowerBound: []byte{'O'},
UpperBound: []byte{'P'},
})
defer it.Close()
if it.SeekGE(key) {
fact, r := OKeyIdRdt(it.Key())
if fact == id {
tlv = it.Value()
rdt = r
}
}
return
}
func EditTLV(off uint64, rdt byte, tlv []byte) (edit []byte) {
edit = append(edit, protocol.TinyRecord('F', rdx.ZipUint64(off))...)
edit = append(edit, protocol.Record(rdt, tlv)...)
return
}
func (cho *Chotki) EditFieldTLV(ctx context.Context, fid rdx.ID, delta []byte) (id rdx.ID, err error) {
tlvs := protocol.Records{}
tlvs = append(tlvs, protocol.TinyRecord('F', rdx.ZipUint64(fid.Off())))
tlvs = append(tlvs, delta)
id, err = cho.CommitPacket(ctx, 'E', fid.ZeroOff(), tlvs)
return
}