This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
/
value_decoder.go
483 lines (427 loc) · 11 KB
/
value_decoder.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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package types
import "github.com/attic-labs/noms/go/d"
type valueDecoder struct {
typedBinaryNomsReader
vrw ValueReadWriter
}
// typedBinaryNomsReader provides some functionality for reading and skipping types that is shared by both valueDecoder and refWalker.
type typedBinaryNomsReader struct {
binaryNomsReader
validating bool
}
func newValueDecoder(buff []byte, vrw ValueReadWriter) valueDecoder {
nr := binaryNomsReader{buff, 0}
return valueDecoder{typedBinaryNomsReader{nr, false}, vrw}
}
func newValueDecoderWithValidation(nr binaryNomsReader, vrw ValueReadWriter) valueDecoder {
return valueDecoder{typedBinaryNomsReader{nr, true}, vrw}
}
func (r *valueDecoder) copyString(w nomsWriter) {
start := r.pos()
r.skipString()
end := r.pos()
w.writeRaw(r.byteSlice(start, end))
}
func (r *valueDecoder) readRef() Ref {
return readRef(&(r.typedBinaryNomsReader))
}
func (r *valueDecoder) skipRef() {
skipRef(&(r.typedBinaryNomsReader))
}
func (r *valueDecoder) skipBlobLeafSequence() ([]uint32, uint64) {
size := r.readCount()
valuesPos := r.pos()
r.offset += uint32(size)
return []uint32{valuesPos, r.pos()}, size
}
func (r *valueDecoder) skipValueSequence(elementsPerIndex int) ([]uint32, uint64) {
count := r.readCount()
offsets := make([]uint32, count+1)
offsets[0] = r.pos()
for i := uint64(0); i < count; i++ {
for j := 0; j < elementsPerIndex; j++ {
r.skipValue()
}
offsets[i+1] = r.pos()
}
return offsets, count
}
func (r *valueDecoder) skipListLeafSequence() ([]uint32, uint64) {
return r.skipValueSequence(getValuesPerIdx(ListKind))
}
func (r *valueDecoder) skipSetLeafSequence() ([]uint32, uint64) {
return r.skipValueSequence(getValuesPerIdx(SetKind))
}
func (r *valueDecoder) skipMapLeafSequence() ([]uint32, uint64) {
return r.skipValueSequence(getValuesPerIdx(MapKind))
}
func (r *valueDecoder) readSequence(kind NomsKind, leafSkipper func() ([]uint32, uint64)) sequence {
start := r.pos()
offsets := []uint32{start}
r.skipKind()
offsets = append(offsets, r.pos())
level := r.readCount()
offsets = append(offsets, r.pos())
var seqOffsets []uint32
var length uint64
if level > 0 {
seqOffsets, length = r.skipMetaSequence(kind, level)
} else {
seqOffsets, length = leafSkipper()
}
offsets = append(offsets, seqOffsets...)
end := r.pos()
if level > 0 {
return newMetaSequence(r.vrw, r.byteSlice(start, end), offsets, length)
}
return newLeafSequence(r.vrw, r.byteSlice(start, end), offsets, length)
}
func (r *valueDecoder) readBlobSequence() sequence {
seq := r.readSequence(BlobKind, r.skipBlobLeafSequence)
if seq.isLeaf() {
return blobLeafSequence{seq.(leafSequence)}
}
return seq
}
func (r *valueDecoder) readListSequence() sequence {
seq := r.readSequence(ListKind, r.skipListLeafSequence)
if seq.isLeaf() {
return listLeafSequence{seq.(leafSequence)}
}
return seq
}
func (r *valueDecoder) readSetSequence() orderedSequence {
seq := r.readSequence(SetKind, r.skipSetLeafSequence)
if seq.isLeaf() {
return setLeafSequence{seq.(leafSequence)}
}
return seq.(orderedSequence)
}
func (r *valueDecoder) readMapSequence() orderedSequence {
seq := r.readSequence(MapKind, r.skipMapLeafSequence)
if seq.isLeaf() {
return mapLeafSequence{seq.(leafSequence)}
}
return seq.(orderedSequence)
}
func (r *valueDecoder) skipList() {
r.skipSequence(ListKind, r.skipListLeafSequence)
}
func (r *valueDecoder) skipSet() {
r.skipSequence(SetKind, r.skipSetLeafSequence)
}
func (r *valueDecoder) skipMap() {
r.skipSequence(MapKind, r.skipMapLeafSequence)
}
func (r *valueDecoder) skipBlob() {
r.skipSequence(BlobKind, r.skipBlobLeafSequence)
}
func (r *valueDecoder) skipSequence(kind NomsKind, leafSkipper func() ([]uint32, uint64)) {
r.skipKind()
level := r.readCount()
if level > 0 {
r.skipMetaSequence(kind, level)
} else {
leafSkipper()
}
}
func (r *valueDecoder) skipOrderedKey() {
switch r.peekKind() {
case hashKind:
r.skipKind()
r.skipHash()
default:
r.skipValue()
}
}
func (r *valueDecoder) skipMetaSequence(k NomsKind, level uint64) ([]uint32, uint64) {
count := r.readCount()
offsets := make([]uint32, count+1)
offsets[0] = r.pos()
length := uint64(0)
for i := uint64(0); i < count; i++ {
r.skipRef()
r.skipOrderedKey()
length += r.readCount()
offsets[i+1] = r.pos()
}
return offsets, length
}
func (r *valueDecoder) readValue() Value {
k := r.peekKind()
switch k {
case BlobKind:
return newBlob(r.readBlobSequence())
case BoolKind:
r.skipKind()
return Bool(r.readBool())
case NumberKind:
r.skipKind()
return r.readNumber()
case StringKind:
r.skipKind()
return String(r.readString())
case ListKind:
return newList(r.readListSequence())
case MapKind:
return newMap(r.readMapSequence())
case RefKind:
return r.readRef()
case SetKind:
return newSet(r.readSetSequence())
case StructKind:
return r.readStruct()
case TypeKind:
r.skipKind()
return r.readType()
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
}
panic("not reachable")
}
func (r *valueDecoder) skipValue() {
k := r.peekKind()
switch k {
case BlobKind:
r.skipBlob()
case BoolKind:
r.skipKind()
r.skipBool()
case NumberKind:
r.skipKind()
r.skipNumber()
case StringKind:
r.skipKind()
r.skipString()
case ListKind:
r.skipList()
case MapKind:
r.skipMap()
case RefKind:
r.skipRef()
case SetKind:
r.skipSet()
case StructKind:
r.skipStruct()
case TypeKind:
r.skipKind()
r.skipType()
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
default:
panic("not reachable")
}
}
// readTypeOfValue is basically readValue().typeOf() but it ensures that we do
// not allocate values where we do not need to.
func (r *valueDecoder) readTypeOfValue() *Type {
k := r.peekKind()
switch k {
case BlobKind:
r.skipBlob()
return BlobType
case BoolKind:
r.skipKind()
r.skipBool()
return BoolType
case NumberKind:
r.skipKind()
r.skipNumber()
return NumberType
case StringKind:
r.skipKind()
r.skipString()
return StringType
case ListKind, MapKind, RefKind, SetKind:
// These do not decode the actual values anyway.
return r.readValue().typeOf()
case StructKind:
return readStructTypeOfValue(r)
case TypeKind:
r.skipKind()
r.skipType()
return TypeType
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
}
panic("not reachable")
}
// isValueSameTypeForSure may return false even though the type of the value is
// equal. We do that in cases wherer it would be too expensive to compute the
// type.
// If this returns false the decoder might not have visited the whole value and
// its offset is no longer valid.
func (r *valueDecoder) isValueSameTypeForSure(t *Type) bool {
k := r.peekKind()
if k != t.TargetKind() {
return false
}
switch k {
case BlobKind, BoolKind, NumberKind, StringKind:
r.skipValue()
return true
case ListKind, MapKind, RefKind, SetKind:
// TODO: Maybe do some simple cases here too. Performance metrics should determine
// what is going to be worth doing.
// https://github.com/attic-labs/noms/issues/3776
return false
case StructKind:
return isStructSameTypeForSure(r, t)
case TypeKind:
return false
case CycleKind, UnionKind, ValueKind:
d.Panic("A value instance can never have type %s", k)
}
panic("not reachable")
}
// isStringSame checks if the next string in the decoder matches string. It
// moves the decoder to after the string in all cases.
func (r *valueDecoder) isStringSame(s string) bool {
count := r.readCount()
start := uint64(r.offset)
r.offset += uint32(count)
if uint64(len(s)) != count {
return false
}
for i := uint64(0); i < count; i++ {
if s[i] != r.buff[start+i] {
return false
}
}
return true
}
func (r *valueDecoder) copyValue(w nomsWriter) {
start := r.pos()
r.skipValue()
end := r.pos()
w.writeRaw(r.byteSlice(start, end))
}
func (r *valueDecoder) readStruct() Value {
return readStruct(r)
}
func (r *valueDecoder) skipStruct() {
skipStruct(r)
}
func boolToUint32(b bool) uint32 {
if b {
return 1
}
return 0
}
func (r *valueDecoder) readOrderedKey() orderedKey {
switch r.peekKind() {
case hashKind:
r.skipKind()
h := r.readHash()
return orderedKeyFromHash(h)
default:
v := r.readValue()
return newOrderedKey(v)
}
}
func (r *typedBinaryNomsReader) readType() *Type {
t := r.readTypeInner(map[string]*Type{})
if r.validating {
validateType(t)
}
return t
}
func (r *typedBinaryNomsReader) skipType() {
if r.validating {
r.readType()
return
}
r.skipTypeInner()
}
func (r *typedBinaryNomsReader) readTypeInner(seenStructs map[string]*Type) *Type {
k := r.readKind()
switch k {
case ListKind:
return makeCompoundType(ListKind, r.readTypeInner(seenStructs))
case MapKind:
return makeCompoundType(MapKind, r.readTypeInner(seenStructs), r.readTypeInner(seenStructs))
case RefKind:
return makeCompoundType(RefKind, r.readTypeInner(seenStructs))
case SetKind:
return makeCompoundType(SetKind, r.readTypeInner(seenStructs))
case StructKind:
return r.readStructType(seenStructs)
case UnionKind:
return r.readUnionType(seenStructs)
case CycleKind:
name := r.readString()
d.PanicIfTrue(name == "") // cycles to anonymous structs are disallowed
t, ok := seenStructs[name]
d.PanicIfFalse(ok)
return t
}
d.PanicIfFalse(IsPrimitiveKind(k))
return MakePrimitiveType(k)
}
func (r *typedBinaryNomsReader) skipTypeInner() {
k := r.readKind()
switch k {
case ListKind, RefKind, SetKind:
r.skipTypeInner()
case MapKind:
r.skipTypeInner()
r.skipTypeInner()
case StructKind:
r.skipStructType()
case UnionKind:
r.skipUnionType()
case CycleKind:
r.skipString()
default:
d.PanicIfFalse(IsPrimitiveKind(k))
}
}
func (r *typedBinaryNomsReader) readStructType(seenStructs map[string]*Type) *Type {
name := r.readString()
count := r.readCount()
fields := make(structTypeFields, count)
t := newType(StructDesc{name, fields})
seenStructs[name] = t
for i := uint64(0); i < count; i++ {
t.Desc.(StructDesc).fields[i] = StructField{
Name: r.readString(),
}
}
for i := uint64(0); i < count; i++ {
t.Desc.(StructDesc).fields[i].Type = r.readTypeInner(seenStructs)
}
for i := uint64(0); i < count; i++ {
t.Desc.(StructDesc).fields[i].Optional = r.readBool()
}
return t
}
func (r *typedBinaryNomsReader) skipStructType() {
r.skipString() // name
count := r.readCount()
for i := uint64(0); i < count; i++ {
r.skipString() // name
}
for i := uint64(0); i < count; i++ {
r.skipTypeInner()
}
for i := uint64(0); i < count; i++ {
r.skipBool() // optional
}
}
func (r *typedBinaryNomsReader) readUnionType(seenStructs map[string]*Type) *Type {
l := r.readCount()
ts := make(typeSlice, l)
for i := uint64(0); i < l; i++ {
ts[i] = r.readTypeInner(seenStructs)
}
return makeUnionType(ts...)
}
func (r *typedBinaryNomsReader) skipUnionType() {
l := r.readCount()
for i := uint64(0); i < l; i++ {
r.skipTypeInner()
}
}