/
tag_block.go
825 lines (669 loc) · 19.9 KB
/
tag_block.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
package tsi1
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/influxdata/influxdb/pkg/rhh"
"github.com/influxdata/influxdb/tsdb"
)
// TagBlockVersion is the version of the tag block.
const TagBlockVersion = 1
// Tag key flag constants.
const (
TagKeyTombstoneFlag = 0x01
)
// Tag value flag constants.
const (
TagValueTombstoneFlag = 0x01
TagValueSeriesIDSetFlag = 0x02
)
// TagBlock variable size constants.
const (
// TagBlock key block fields.
TagKeyNSize = 8
TagKeyOffsetSize = 8
// TagBlock value block fields.
TagValueNSize = 8
TagValueOffsetSize = 8
)
// TagBlock errors.
var (
ErrUnsupportedTagBlockVersion = errors.New("unsupported tag block version")
ErrTagBlockSizeMismatch = errors.New("tag block size mismatch")
)
// TagBlock represents tag key/value block for a single measurement.
type TagBlock struct {
data []byte
valueData []byte
keyData []byte
hashData []byte
version int // tag block version
}
// Version returns the encoding version parsed from the data.
// Only valid after UnmarshalBinary() has been successfully invoked.
func (blk *TagBlock) Version() int { return blk.version }
// UnmarshalBinary unpacks data into the tag block. Tag block is not copied so data
// should be retained and unchanged after being passed into this function.
func (blk *TagBlock) UnmarshalBinary(data []byte) error {
// Read trailer.
t, err := ReadTagBlockTrailer(data)
if err != nil {
return err
}
// Verify data size is correct.
if int64(len(data)) != t.Size {
return ErrTagBlockSizeMismatch
}
// Save data section.
blk.valueData = data[t.ValueData.Offset:]
blk.valueData = blk.valueData[:t.ValueData.Size]
// Save key data section.
blk.keyData = data[t.KeyData.Offset:]
blk.keyData = blk.keyData[:t.KeyData.Size]
// Save hash index block.
blk.hashData = data[t.HashIndex.Offset:]
blk.hashData = blk.hashData[:t.HashIndex.Size]
// Save entire block.
blk.data = data
return nil
}
// TagKeyElem returns an element for a tag key.
// Returns an element with a nil key if not found.
func (blk *TagBlock) TagKeyElem(key []byte) TagKeyElem {
var elem TagBlockKeyElem
if !blk.DecodeTagKeyElem(key, &elem) {
return nil
}
return &elem
}
func (blk *TagBlock) DecodeTagKeyElem(key []byte, elem *TagBlockKeyElem) bool {
keyN := int64(binary.BigEndian.Uint64(blk.hashData[:TagKeyNSize]))
hash := rhh.HashKey(key)
pos := hash % keyN
// Track current distance
var d int64
for {
// Find offset of tag key.
offset := binary.BigEndian.Uint64(blk.hashData[TagKeyNSize+(pos*TagKeyOffsetSize):])
if offset == 0 {
return false
}
// Parse into element.
elem.unmarshal(blk.data[offset:], blk.data)
// Return if keys match.
if bytes.Equal(elem.key, key) {
return true
}
// Check if we've exceeded the probe distance.
if d > rhh.Dist(rhh.HashKey(elem.key), pos, keyN) {
return false
}
// Move position forward.
pos = (pos + 1) % keyN
d++
if d > keyN {
return false
}
}
}
// TagValueElem returns an element for a tag value.
func (blk *TagBlock) TagValueElem(key, value []byte) TagValueElem {
var valueElem TagBlockValueElem
if !blk.DecodeTagValueElem(key, value, &valueElem) {
return nil
}
return &valueElem
}
// DecodeTagValueElem returns an element for a tag value.
func (blk *TagBlock) DecodeTagValueElem(key, value []byte, valueElem *TagBlockValueElem) bool {
// Find key element, exit if not found.
var keyElem TagBlockKeyElem
if !blk.DecodeTagKeyElem(key, &keyElem) {
return false
}
// Slice hash index data.
hashData := keyElem.hashIndex.buf
valueN := int64(binary.BigEndian.Uint64(hashData[:TagValueNSize]))
hash := rhh.HashKey(value)
pos := hash % valueN
// Track current distance
var d int64
for {
// Find offset of tag value.
offset := binary.BigEndian.Uint64(hashData[TagValueNSize+(pos*TagValueOffsetSize):])
if offset == 0 {
return false
}
// Parse into element.
valueElem.unmarshal(blk.data[offset:])
// Return if values match.
if bytes.Equal(valueElem.value, value) {
return true
}
// Check if we've exceeded the probe distance.
max := rhh.Dist(rhh.HashKey(valueElem.value), pos, valueN)
if d > max {
return false
}
// Move position forward.
pos = (pos + 1) % valueN
d++
if d > valueN {
return false
}
}
}
// TagKeyIterator returns an iterator over all the keys in the block.
func (blk *TagBlock) TagKeyIterator() TagKeyIterator {
return &tagBlockKeyIterator{
blk: blk,
keyData: blk.keyData,
}
}
// tagBlockKeyIterator represents an iterator over all keys in a TagBlock.
type tagBlockKeyIterator struct {
blk *TagBlock
keyData []byte
e TagBlockKeyElem
}
// Next returns the next element in the iterator.
func (itr *tagBlockKeyIterator) Next() TagKeyElem {
// Exit when there is no data left.
if len(itr.keyData) == 0 {
return nil
}
// Unmarshal next element & move data forward.
itr.e.unmarshal(itr.keyData, itr.blk.data)
itr.keyData = itr.keyData[itr.e.size:]
assert(len(itr.e.Key()) > 0, "invalid zero-length tag key")
return &itr.e
}
// tagBlockValueIterator represents an iterator over all values for a tag key.
type tagBlockValueIterator struct {
data []byte
e TagBlockValueElem
}
// Next returns the next element in the iterator.
func (itr *tagBlockValueIterator) Next() TagValueElem {
// Exit when there is no data left.
if len(itr.data) == 0 {
return nil
}
// Unmarshal next element & move data forward.
itr.e.unmarshal(itr.data)
itr.data = itr.data[itr.e.size:]
assert(len(itr.e.Value()) > 0, "invalid zero-length tag value")
return &itr.e
}
// TagBlockKeyElem represents a tag key element in a TagBlock.
type TagBlockKeyElem struct {
flag byte
key []byte
// Value data
data struct {
offset uint64
size uint64
buf []byte
}
// Value hash index data
hashIndex struct {
offset uint64
size uint64
buf []byte
}
size int
}
// Deleted returns true if the key has been tombstoned.
func (e *TagBlockKeyElem) Deleted() bool { return (e.flag & TagKeyTombstoneFlag) != 0 }
// Key returns the key name of the element.
func (e *TagBlockKeyElem) Key() []byte { return e.key }
// TagValueIterator returns an iterator over the key's values.
func (e *TagBlockKeyElem) TagValueIterator() TagValueIterator {
return &tagBlockValueIterator{data: e.data.buf}
}
// unmarshal unmarshals buf into e.
// The data argument represents the entire block data.
func (e *TagBlockKeyElem) unmarshal(buf, data []byte) {
start := len(buf)
// Parse flag data.
e.flag, buf = buf[0], buf[1:]
// Parse data offset/size.
e.data.offset, buf = binary.BigEndian.Uint64(buf), buf[8:]
e.data.size, buf = binary.BigEndian.Uint64(buf), buf[8:]
// Slice data.
e.data.buf = data[e.data.offset:]
e.data.buf = e.data.buf[:e.data.size]
// Parse hash index offset/size.
e.hashIndex.offset, buf = binary.BigEndian.Uint64(buf), buf[8:]
e.hashIndex.size, buf = binary.BigEndian.Uint64(buf), buf[8:]
// Slice hash index data.
e.hashIndex.buf = data[e.hashIndex.offset:]
e.hashIndex.buf = e.hashIndex.buf[:e.hashIndex.size]
// Parse key.
n, sz := binary.Uvarint(buf)
e.key, buf = buf[sz:sz+int(n)], buf[int(n)+sz:]
// Save length of elem.
e.size = start - len(buf)
}
// TagBlockValueElem represents a tag value element.
type TagBlockValueElem struct {
flag byte
value []byte
// Legacy uvarint-encoded series data.
// Mutually exclusive with seriesIDSetData field.
series struct {
n uint64 // Series count
data []byte // Raw series data
}
// Roaring bitmap encoded series data.
// Mutually exclusive with series.data field.
seriesIDSetData []byte
size int
}
// Deleted returns true if the element has been tombstoned.
func (e *TagBlockValueElem) Deleted() bool { return (e.flag & TagValueTombstoneFlag) != 0 }
// Value returns the value for the element.
func (e *TagBlockValueElem) Value() []byte { return e.value }
// SeriesN returns the series count.
func (e *TagBlockValueElem) SeriesN() uint64 { return e.series.n }
// SeriesData returns the raw series data.
func (e *TagBlockValueElem) SeriesData() []byte { return e.series.data }
// SeriesID returns series ID at an index.
func (e *TagBlockValueElem) SeriesID(i int) uint64 {
return binary.BigEndian.Uint64(e.series.data[i*SeriesIDSize:])
}
// SeriesIDs returns a list decoded series ids.
func (e *TagBlockValueElem) SeriesIDs() ([]uint64, error) {
if e.seriesIDSetData != nil {
ss, err := e.SeriesIDSet()
if err != nil {
return nil, err
}
return ss.Slice(), nil
}
a := make([]uint64, 0, e.series.n)
var prev uint64
for data := e.series.data; len(data) > 0; {
delta, n, err := uvarint(data)
if err != nil {
return nil, err
}
data = data[n:]
seriesID := prev + uint64(delta)
a = append(a, seriesID)
prev = seriesID
}
return a, nil
}
// SeriesIDSet returns a set of series ids.
func (e *TagBlockValueElem) SeriesIDSet() (*tsdb.SeriesIDSet, error) {
ss := tsdb.NewSeriesIDSet()
// Read bitmap data directly from mmap, if available.
if e.seriesIDSetData != nil {
if err := ss.UnmarshalBinaryUnsafe(e.seriesIDSetData); err != nil {
return nil, err
}
return ss, nil
}
// Otherwise decode series ids from uvarint encoding.
var prev uint64
for data := e.series.data; len(data) > 0; {
delta, n, err := uvarint(data)
if err != nil {
return nil, err
}
data = data[n:]
seriesID := prev + uint64(delta)
ss.AddNoLock(seriesID)
prev = seriesID
}
return ss, nil
}
// Size returns the size of the element.
func (e *TagBlockValueElem) Size() int { return e.size }
// unmarshal unmarshals buf into e.
func (e *TagBlockValueElem) unmarshal(buf []byte) {
start := len(buf)
// Parse flag data.
e.flag, buf = buf[0], buf[1:]
// Parse value.
sz, n := binary.Uvarint(buf)
e.value, buf = buf[n:n+int(sz)], buf[n+int(sz):]
// Parse series count.
v, n := binary.Uvarint(buf)
e.series.n = uint64(v)
buf = buf[n:]
// Parse data block size.
sz, n = binary.Uvarint(buf)
buf = buf[n:]
// Parse series data (original uvarint encoded or roaring bitmap).
if e.flag&TagValueSeriesIDSetFlag == 0 {
e.series.data, buf = buf[:sz], buf[sz:]
} else {
// buf = memalign(buf)
e.seriesIDSetData, buf = buf, buf[sz:]
}
// Save length of elem.
e.size = start - len(buf)
}
// TagBlockTrailerSize is the total size of the on-disk trailer.
const TagBlockTrailerSize = 0 +
8 + 8 + // value data offset/size
8 + 8 + // key data offset/size
8 + 8 + // hash index offset/size
8 + // size
2 // version
// TagBlockTrailer represents meta data at the end of a TagBlock.
type TagBlockTrailer struct {
Version int // Encoding version
Size int64 // Total size w/ trailer
// Offset & size of value data section.
ValueData struct {
Offset int64
Size int64
}
// Offset & size of key data section.
KeyData struct {
Offset int64
Size int64
}
// Offset & size of hash map section.
HashIndex struct {
Offset int64
Size int64
}
}
// WriteTo writes the trailer to w.
func (t *TagBlockTrailer) WriteTo(w io.Writer) (n int64, err error) {
// Write data info.
if err := writeUint64To(w, uint64(t.ValueData.Offset), &n); err != nil {
return n, err
} else if err := writeUint64To(w, uint64(t.ValueData.Size), &n); err != nil {
return n, err
}
// Write key data info.
if err := writeUint64To(w, uint64(t.KeyData.Offset), &n); err != nil {
return n, err
} else if err := writeUint64To(w, uint64(t.KeyData.Size), &n); err != nil {
return n, err
}
// Write hash index info.
if err := writeUint64To(w, uint64(t.HashIndex.Offset), &n); err != nil {
return n, err
} else if err := writeUint64To(w, uint64(t.HashIndex.Size), &n); err != nil {
return n, err
}
// Write total size & encoding version.
if err := writeUint64To(w, uint64(t.Size), &n); err != nil {
return n, err
} else if err := writeUint16To(w, IndexFileVersion, &n); err != nil {
return n, err
}
return n, nil
}
// ReadTagBlockTrailer returns the tag block trailer from data.
func ReadTagBlockTrailer(data []byte) (TagBlockTrailer, error) {
var t TagBlockTrailer
// Read version.
t.Version = int(binary.BigEndian.Uint16(data[len(data)-2:]))
if t.Version != TagBlockVersion {
return t, ErrUnsupportedTagBlockVersion
}
// Slice trailer data.
buf := data[len(data)-TagBlockTrailerSize:]
// Read data section info.
t.ValueData.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
t.ValueData.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
// Read key section info.
t.KeyData.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
t.KeyData.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
// Read hash section info.
t.HashIndex.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
t.HashIndex.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:]
// Read total size.
t.Size = int64(binary.BigEndian.Uint64(buf[0:8]))
return t, nil
}
// TagBlockEncoder encodes a tags to a TagBlock section.
type TagBlockEncoder struct {
w io.Writer
buf bytes.Buffer
// Track value offsets.
offsets *rhh.HashMap
// Track bytes written, sections.
n int64
trailer TagBlockTrailer
// Track tag keys.
keys []tagKeyEncodeEntry
prevValue []byte
}
// NewTagBlockEncoder returns a new TagBlockEncoder.
func NewTagBlockEncoder(w io.Writer) *TagBlockEncoder {
return &TagBlockEncoder{
w: w,
offsets: rhh.NewHashMap(rhh.Options{LoadFactor: LoadFactor}),
trailer: TagBlockTrailer{
Version: TagBlockVersion,
},
}
}
// N returns the number of bytes written.
func (enc *TagBlockEncoder) N() int64 { return enc.n }
// EncodeKey writes a tag key to the underlying writer.
func (enc *TagBlockEncoder) EncodeKey(key []byte, deleted bool) error {
// An initial empty byte must be written.
if err := enc.ensureHeaderWritten(); err != nil {
return err
}
// Verify key is lexicographically after previous key.
if len(enc.keys) > 0 {
prev := enc.keys[len(enc.keys)-1].key
if cmp := bytes.Compare(prev, key); cmp == 1 {
return fmt.Errorf("tag key out of order: prev=%s, new=%s", prev, key)
} else if cmp == 0 {
return fmt.Errorf("tag key already encoded: %s", key)
}
}
// Flush values section for key.
if err := enc.flushValueHashIndex(); err != nil {
return err
}
// Append key on to the end of the key list.
entry := tagKeyEncodeEntry{
key: key,
deleted: deleted,
}
entry.data.offset = enc.n
enc.keys = append(enc.keys, entry)
// Clear previous value.
enc.prevValue = nil
return nil
}
// EncodeValue writes a tag value to the underlying writer.
// The tag key must be lexicographical sorted after the previous encoded tag key.
func (enc *TagBlockEncoder) EncodeValue(value []byte, deleted bool, ss *tsdb.SeriesIDSet) error {
if len(enc.keys) == 0 {
return fmt.Errorf("tag key must be encoded before encoding values")
} else if len(value) == 0 {
return fmt.Errorf("zero length tag value not allowed")
}
// Validate that keys are in-order.
if cmp := bytes.Compare(enc.prevValue, value); cmp == 1 {
return fmt.Errorf("tag value out of order: prev=%s, new=%s", enc.prevValue, value)
} else if cmp == 0 {
return fmt.Errorf("tag value already encoded: %s", value)
}
// Save offset to hash map.
enc.offsets.Put(value, enc.n)
// Write flag.
if err := writeUint8To(enc.w, encodeTagValueFlag(deleted), &enc.n); err != nil {
return err
}
// Write value.
if err := writeUvarintTo(enc.w, uint64(len(value)), &enc.n); err != nil {
return err
} else if err := writeTo(enc.w, value, &enc.n); err != nil {
return err
}
// Build series data in buffer.
enc.buf.Reset()
if _, err := ss.WriteTo(&enc.buf); err != nil {
return err
}
// Write series count.
if err := writeUvarintTo(enc.w, uint64(ss.Cardinality()), &enc.n); err != nil {
return err
}
// Write data size & buffer.
if err := writeUvarintTo(enc.w, uint64(enc.buf.Len()), &enc.n); err != nil {
return err
}
// Word align bitmap data.
// if offset := (enc.n) % 8; offset != 0 {
// if err := writeTo(enc.w, make([]byte, 8-offset), &enc.n); err != nil {
// return err
// }
// }
nn, err := enc.buf.WriteTo(enc.w)
if enc.n += nn; err != nil {
return err
}
// Save previous value.
enc.prevValue = value
return nil
}
// Close flushes the trailer of the encoder to the writer.
func (enc *TagBlockEncoder) Close() error {
// Flush last value set.
if err := enc.ensureHeaderWritten(); err != nil {
return err
} else if err := enc.flushValueHashIndex(); err != nil {
return err
}
// Save ending position of entire data block.
enc.trailer.ValueData.Size = enc.n - enc.trailer.ValueData.Offset
// Write key block to point to value blocks.
if err := enc.encodeTagKeyBlock(); err != nil {
return err
}
// Compute total size w/ trailer.
enc.trailer.Size = enc.n + TagBlockTrailerSize
// Write trailer.
nn, err := enc.trailer.WriteTo(enc.w)
enc.n += nn
return err
}
// ensureHeaderWritten writes a single byte to offset the rest of the block.
func (enc *TagBlockEncoder) ensureHeaderWritten() error {
if enc.n > 0 {
return nil
} else if _, err := enc.w.Write([]byte{0}); err != nil {
return err
}
enc.n++
enc.trailer.ValueData.Offset = enc.n
return nil
}
// flushValueHashIndex builds writes the hash map at the end of a value set.
func (enc *TagBlockEncoder) flushValueHashIndex() error {
// Ignore if no keys have been written.
if len(enc.keys) == 0 {
return nil
}
key := &enc.keys[len(enc.keys)-1]
// Save size of data section.
key.data.size = enc.n - key.data.offset
// Encode hash map length.
key.hashIndex.offset = enc.n
if err := writeUint64To(enc.w, uint64(enc.offsets.Cap()), &enc.n); err != nil {
return err
}
// Encode hash map offset entries.
for i := int64(0); i < enc.offsets.Cap(); i++ {
_, v := enc.offsets.Elem(i)
offset, _ := v.(int64)
if err := writeUint64To(enc.w, uint64(offset), &enc.n); err != nil {
return err
}
}
key.hashIndex.size = enc.n - key.hashIndex.offset
// Clear offsets.
enc.offsets = rhh.NewHashMap(rhh.Options{LoadFactor: LoadFactor})
return nil
}
// encodeTagKeyBlock encodes the keys section to the writer.
func (enc *TagBlockEncoder) encodeTagKeyBlock() error {
offsets := rhh.NewHashMap(rhh.Options{Capacity: int64(len(enc.keys)), LoadFactor: LoadFactor})
// Encode key list in sorted order.
enc.trailer.KeyData.Offset = enc.n
for i := range enc.keys {
entry := &enc.keys[i]
// Save current offset so we can use it in the hash index.
offsets.Put(entry.key, enc.n)
if err := writeUint8To(enc.w, encodeTagKeyFlag(entry.deleted), &enc.n); err != nil {
return err
}
// Write value data offset & size.
if err := writeUint64To(enc.w, uint64(entry.data.offset), &enc.n); err != nil {
return err
} else if err := writeUint64To(enc.w, uint64(entry.data.size), &enc.n); err != nil {
return err
}
// Write value hash index offset & size.
if err := writeUint64To(enc.w, uint64(entry.hashIndex.offset), &enc.n); err != nil {
return err
} else if err := writeUint64To(enc.w, uint64(entry.hashIndex.size), &enc.n); err != nil {
return err
}
// Write key length and data.
if err := writeUvarintTo(enc.w, uint64(len(entry.key)), &enc.n); err != nil {
return err
} else if err := writeTo(enc.w, entry.key, &enc.n); err != nil {
return err
}
}
enc.trailer.KeyData.Size = enc.n - enc.trailer.KeyData.Offset
// Encode hash map length.
enc.trailer.HashIndex.Offset = enc.n
if err := writeUint64To(enc.w, uint64(offsets.Cap()), &enc.n); err != nil {
return err
}
// Encode hash map offset entries.
for i := int64(0); i < offsets.Cap(); i++ {
_, v := offsets.Elem(i)
offset, _ := v.(int64)
if err := writeUint64To(enc.w, uint64(offset), &enc.n); err != nil {
return err
}
}
enc.trailer.HashIndex.Size = enc.n - enc.trailer.HashIndex.Offset
return nil
}
type tagKeyEncodeEntry struct {
key []byte
deleted bool
data struct {
offset int64
size int64
}
hashIndex struct {
offset int64
size int64
}
}
func encodeTagKeyFlag(deleted bool) byte {
var flag byte
if deleted {
flag |= TagKeyTombstoneFlag
}
return flag
}
func encodeTagValueFlag(deleted bool) byte {
flag := byte(TagValueSeriesIDSetFlag)
if deleted {
flag |= TagValueTombstoneFlag
}
return flag
}