| 1 |
package tsi1 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
|
| 10 |
"github.com/influxdata/influxdb/pkg/rhh" |
| 11 |
) |
| 12 |
|
| 13 |
// TagBlockVersion is the version of the tag block. |
| 14 |
const TagBlockVersion = 1 |
| 15 |
|
| 16 |
// Tag key flag constants. |
| 17 |
const ( |
| 18 |
TagKeyTombstoneFlag = 0x01 |
| 19 |
) |
| 20 |
|
| 21 |
// Tag value flag constants. |
| 22 |
const ( |
| 23 |
TagValueTombstoneFlag = 0x01 |
| 24 |
) |
| 25 |
|
| 26 |
// TagBlock variable size constants. |
| 27 |
const ( |
| 28 |
// TagBlock key block fields. |
| 29 |
TagKeyNSize = 8 |
| 30 |
TagKeyOffsetSize = 8 |
| 31 |
|
| 32 |
// TagBlock value block fields. |
| 33 |
TagValueNSize = 8 |
| 34 |
TagValueOffsetSize = 8 |
| 35 |
) |
| 36 |
|
| 37 |
// TagBlock errors. |
| 38 |
var ( |
| 39 |
ErrUnsupportedTagBlockVersion = errors.New("unsupported tag block version") |
| 40 |
ErrTagBlockSizeMismatch = errors.New("tag block size mismatch") |
| 41 |
) |
| 42 |
|
| 43 |
// TagBlock represents tag key/value block for a single measurement. |
| 44 |
type TagBlock struct { |
| 45 |
data []byte |
| 46 |
|
| 47 |
valueData []byte |
| 48 |
keyData []byte |
| 49 |
hashData []byte |
| 50 |
|
| 51 |
version int // tag block version |
| 52 |
} |
| 53 |
|
| 54 |
// Version returns the encoding version parsed from the data. |
| 55 |
// Only valid after UnmarshalBinary() has been successfully invoked. |
| 56 |
func (blk *TagBlock) Version() int { return blk.version } |
| 57 |
|
| 58 |
// UnmarshalBinary unpacks data into the tag block. Tag block is not copied so data |
| 59 |
// should be retained and unchanged after being passed into this function. |
| 60 |
func (blk *TagBlock) UnmarshalBinary(data []byte) error { |
| 61 |
// Read trailer. |
| 62 |
t, err := ReadTagBlockTrailer(data) |
| 63 |
if err != nil { |
| 64 |
return err |
| 65 |
} |
| 66 |
|
| 67 |
// Verify data size is correct. |
| 68 |
if int64(len(data)) != t.Size { |
| 69 |
return ErrTagBlockSizeMismatch |
| 70 |
} |
| 71 |
|
| 72 |
// Save data section. |
| 73 |
blk.valueData = data[t.ValueData.Offset:] |
| 74 |
blk.valueData = blk.valueData[:t.ValueData.Size] |
| 75 |
|
| 76 |
// Save key data section. |
| 77 |
blk.keyData = data[t.KeyData.Offset:] |
| 78 |
blk.keyData = blk.keyData[:t.KeyData.Size] |
| 79 |
|
| 80 |
// Save hash index block. |
| 81 |
blk.hashData = data[t.HashIndex.Offset:] |
| 82 |
blk.hashData = blk.hashData[:t.HashIndex.Size] |
| 83 |
|
| 84 |
// Save entire block. |
| 85 |
blk.data = data |
| 86 |
|
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
// TagKeyElem returns an element for a tag key. |
| 91 |
// Returns an element with a nil key if not found. |
| 92 |
func (blk *TagBlock) TagKeyElem(key []byte) TagKeyElem { |
| 93 |
var elem TagBlockKeyElem |
| 94 |
if !blk.DecodeTagKeyElem(key, &elem) { |
| 95 |
return nil |
| 96 |
} |
| 97 |
return &elem |
| 98 |
} |
| 99 |
|
| 100 |
func (blk *TagBlock) DecodeTagKeyElem(key []byte, elem *TagBlockKeyElem) bool { |
| 101 |
keyN := int64(binary.BigEndian.Uint64(blk.hashData[:TagKeyNSize])) |
| 102 |
hash := rhh.HashKey(key) |
| 103 |
pos := hash % keyN |
| 104 |
|
| 105 |
// Track current distance |
| 106 |
var d int64 |
| 107 |
for { |
| 108 |
// Find offset of tag key. |
| 109 |
offset := binary.BigEndian.Uint64(blk.hashData[TagKeyNSize+(pos*TagKeyOffsetSize):]) |
| 110 |
if offset == 0 { |
| 111 |
return false |
| 112 |
} |
| 113 |
|
| 114 |
// Parse into element. |
| 115 |
elem.unmarshal(blk.data[offset:], blk.data) |
| 116 |
|
| 117 |
// Return if keys match. |
| 118 |
if bytes.Equal(elem.key, key) { |
| 119 |
return true |
| 120 |
} |
| 121 |
|
| 122 |
// Check if we've exceeded the probe distance. |
| 123 |
if d > rhh.Dist(rhh.HashKey(elem.key), pos, keyN) { |
| 124 |
return false |
| 125 |
} |
| 126 |
|
| 127 |
// Move position forward. |
| 128 |
pos = (pos + 1) % keyN |
| 129 |
d++ |
| 130 |
|
| 131 |
if d > keyN { |
| 132 |
return false |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// TagValueElem returns an element for a tag value. |
| 138 |
func (blk *TagBlock) TagValueElem(key, value []byte) TagValueElem { |
| 139 |
var valueElem TagBlockValueElem |
| 140 |
if !blk.DecodeTagValueElem(key, value, &valueElem) { |
| 141 |
return nil |
| 142 |
} |
| 143 |
return &valueElem |
| 144 |
} |
| 145 |
|
| 146 |
// TagValueElem returns an element for a tag value. |
| 147 |
func (blk *TagBlock) TagValueSeriesData(key, value []byte) (uint64, []byte) { |
| 148 |
var valueElem TagBlockValueElem |
| 149 |
if !blk.DecodeTagValueElem(key, value, &valueElem) { |
| 150 |
return 0, nil |
| 151 |
} |
| 152 |
return valueElem.series.n, valueElem.series.data |
| 153 |
} |
| 154 |
|
| 155 |
// DecodeTagValueElem returns an element for a tag value. |
| 156 |
func (blk *TagBlock) DecodeTagValueElem(key, value []byte, valueElem *TagBlockValueElem) bool { |
| 157 |
// Find key element, exit if not found. |
| 158 |
var keyElem TagBlockKeyElem |
| 159 |
if !blk.DecodeTagKeyElem(key, &keyElem) { |
| 160 |
return false |
| 161 |
} |
| 162 |
|
| 163 |
// Slice hash index data. |
| 164 |
hashData := keyElem.hashIndex.buf |
| 165 |
|
| 166 |
valueN := int64(binary.BigEndian.Uint64(hashData[:TagValueNSize])) |
| 167 |
hash := rhh.HashKey(value) |
| 168 |
pos := hash % valueN |
| 169 |
|
| 170 |
// Track current distance |
| 171 |
var d int64 |
| 172 |
for { |
| 173 |
// Find offset of tag value. |
| 174 |
offset := binary.BigEndian.Uint64(hashData[TagValueNSize+(pos*TagValueOffsetSize):]) |
| 175 |
if offset == 0 { |
| 176 |
return false |
| 177 |
} |
| 178 |
|
| 179 |
// Parse into element. |
| 180 |
valueElem.unmarshal(blk.data[offset:]) |
| 181 |
|
| 182 |
// Return if values match. |
| 183 |
if bytes.Equal(valueElem.value, value) { |
| 184 |
return true |
| 185 |
} |
| 186 |
|
| 187 |
// Check if we've exceeded the probe distance. |
| 188 |
max := rhh.Dist(rhh.HashKey(valueElem.value), pos, valueN) |
| 189 |
if d > max { |
| 190 |
return false |
| 191 |
} |
| 192 |
|
| 193 |
// Move position forward. |
| 194 |
pos = (pos + 1) % valueN |
| 195 |
d++ |
| 196 |
|
| 197 |
if d > valueN { |
| 198 |
return false |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// TagKeyIterator returns an iterator over all the keys in the block. |
| 204 |
func (blk *TagBlock) TagKeyIterator() TagKeyIterator { |
| 205 |
return &tagBlockKeyIterator{ |
| 206 |
blk: blk, |
| 207 |
keyData: blk.keyData, |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// tagBlockKeyIterator represents an iterator over all keys in a TagBlock. |
| 212 |
type tagBlockKeyIterator struct { |
| 213 |
blk *TagBlock |
| 214 |
keyData []byte |
| 215 |
e TagBlockKeyElem |
| 216 |
} |
| 217 |
|
| 218 |
// Next returns the next element in the iterator. |
| 219 |
func (itr *tagBlockKeyIterator) Next() TagKeyElem { |
| 220 |
// Exit when there is no data left. |
| 221 |
if len(itr.keyData) == 0 { |
| 222 |
return nil |
| 223 |
} |
| 224 |
|
| 225 |
// Unmarshal next element & move data forward. |
| 226 |
itr.e.unmarshal(itr.keyData, itr.blk.data) |
| 227 |
itr.keyData = itr.keyData[itr.e.size:] |
| 228 |
|
| 229 |
assert(len(itr.e.Key()) > 0, "invalid zero-length tag key") |
| 230 |
return &itr.e |
| 231 |
} |
| 232 |
|
| 233 |
// tagBlockValueIterator represents an iterator over all values for a tag key. |
| 234 |
type tagBlockValueIterator struct { |
| 235 |
data []byte |
| 236 |
e TagBlockValueElem |
| 237 |
} |
| 238 |
|
| 239 |
// Next returns the next element in the iterator. |
| 240 |
func (itr *tagBlockValueIterator) Next() TagValueElem { |
| 241 |
// Exit when there is no data left. |
| 242 |
if len(itr.data) == 0 { |
| 243 |
return nil |
| 244 |
} |
| 245 |
|
| 246 |
// Unmarshal next element & move data forward. |
| 247 |
itr.e.unmarshal(itr.data) |
| 248 |
itr.data = itr.data[itr.e.size:] |
| 249 |
|
| 250 |
assert(len(itr.e.Value()) > 0, "invalid zero-length tag value") |
| 251 |
return &itr.e |
| 252 |
} |
| 253 |
|
| 254 |
// TagBlockKeyElem represents a tag key element in a TagBlock. |
| 255 |
type TagBlockKeyElem struct { |
| 256 |
flag byte |
| 257 |
key []byte |
| 258 |
|
| 259 |
// Value data |
| 260 |
data struct { |
| 261 |
offset uint64 |
| 262 |
size uint64 |
| 263 |
buf []byte |
| 264 |
} |
| 265 |
|
| 266 |
// Value hash index data |
| 267 |
hashIndex struct { |
| 268 |
offset uint64 |
| 269 |
size uint64 |
| 270 |
buf []byte |
| 271 |
} |
| 272 |
|
| 273 |
size int |
| 274 |
} |
| 275 |
|
| 276 |
// Deleted returns true if the key has been tombstoned. |
| 277 |
func (e *TagBlockKeyElem) Deleted() bool { return (e.flag & TagKeyTombstoneFlag) != 0 } |
| 278 |
|
| 279 |
// Key returns the key name of the element. |
| 280 |
func (e *TagBlockKeyElem) Key() []byte { return e.key } |
| 281 |
|
| 282 |
// TagValueIterator returns an iterator over the key's values. |
| 283 |
func (e *TagBlockKeyElem) TagValueIterator() TagValueIterator { |
| 284 |
return &tagBlockValueIterator{data: e.data.buf} |
| 285 |
} |
| 286 |
|
| 287 |
// unmarshal unmarshals buf into e. |
| 288 |
// The data argument represents the entire block data. |
| 289 |
func (e *TagBlockKeyElem) unmarshal(buf, data []byte) { |
| 290 |
start := len(buf) |
| 291 |
|
| 292 |
// Parse flag data. |
| 293 |
e.flag, buf = buf[0], buf[1:] |
| 294 |
|
| 295 |
// Parse data offset/size. |
| 296 |
e.data.offset, buf = binary.BigEndian.Uint64(buf), buf[8:] |
| 297 |
e.data.size, buf = binary.BigEndian.Uint64(buf), buf[8:] |
| 298 |
|
| 299 |
// Slice data. |
| 300 |
e.data.buf = data[e.data.offset:] |
| 301 |
e.data.buf = e.data.buf[:e.data.size] |
| 302 |
|
| 303 |
// Parse hash index offset/size. |
| 304 |
e.hashIndex.offset, buf = binary.BigEndian.Uint64(buf), buf[8:] |
| 305 |
e.hashIndex.size, buf = binary.BigEndian.Uint64(buf), buf[8:] |
| 306 |
|
| 307 |
// Slice hash index data. |
| 308 |
e.hashIndex.buf = data[e.hashIndex.offset:] |
| 309 |
e.hashIndex.buf = e.hashIndex.buf[:e.hashIndex.size] |
| 310 |
|
| 311 |
// Parse key. |
| 312 |
n, sz := binary.Uvarint(buf) |
| 313 |
e.key, buf = buf[sz:sz+int(n)], buf[int(n)+sz:] |
| 314 |
|
| 315 |
// Save length of elem. |
| 316 |
e.size = start - len(buf) |
| 317 |
} |
| 318 |
|
| 319 |
// TagBlockValueElem represents a tag value element. |
| 320 |
type TagBlockValueElem struct { |
| 321 |
flag byte |
| 322 |
value []byte |
| 323 |
series struct { |
| 324 |
n uint64 // Series count |
| 325 |
data []byte // Raw series data |
| 326 |
} |
| 327 |
|
| 328 |
size int |
| 329 |
} |
| 330 |
|
| 331 |
// Deleted returns true if the element has been tombstoned. |
| 332 |
func (e *TagBlockValueElem) Deleted() bool { return (e.flag & TagValueTombstoneFlag) != 0 } |
| 333 |
|
| 334 |
// Value returns the value for the element. |
| 335 |
func (e *TagBlockValueElem) Value() []byte { return e.value } |
| 336 |
|
| 337 |
// SeriesN returns the series count. |
| 338 |
func (e *TagBlockValueElem) SeriesN() uint64 { return e.series.n } |
| 339 |
|
| 340 |
// SeriesData returns the raw series data. |
| 341 |
func (e *TagBlockValueElem) SeriesData() []byte { return e.series.data } |
| 342 |
|
| 343 |
// SeriesID returns series ID at an index. |
| 344 |
func (e *TagBlockValueElem) SeriesID(i int) uint64 { |
| 345 |
return binary.BigEndian.Uint64(e.series.data[i*SeriesIDSize:]) |
| 346 |
} |
| 347 |
|
| 348 |
// SeriesIDs returns a list decoded series ids. |
| 349 |
func (e *TagBlockValueElem) SeriesIDs() ([]uint64, error) { |
| 350 |
a := make([]uint64, 0, e.series.n) |
| 351 |
var prev uint64 |
| 352 |
for data := e.series.data; len(data) > 0; { |
| 353 |
delta, n, err := uvarint(data) |
| 354 |
if err != nil { |
| 355 |
return nil, err |
| 356 |
} |
| 357 |
data = data[n:] |
| 358 |
|
| 359 |
seriesID := prev + uint64(delta) |
| 360 |
a = append(a, seriesID) |
| 361 |
prev = seriesID |
| 362 |
} |
| 363 |
return a, nil |
| 364 |
} |
| 365 |
|
| 366 |
// Size returns the size of the element. |
| 367 |
func (e *TagBlockValueElem) Size() int { return e.size } |
| 368 |
|
| 369 |
// unmarshal unmarshals buf into e. |
| 370 |
func (e *TagBlockValueElem) unmarshal(buf []byte) { |
| 371 |
start := len(buf) |
| 372 |
|
| 373 |
// Parse flag data. |
| 374 |
e.flag, buf = buf[0], buf[1:] |
| 375 |
|
| 376 |
// Parse value. |
| 377 |
sz, n := binary.Uvarint(buf) |
| 378 |
e.value, buf = buf[n:n+int(sz)], buf[n+int(sz):] |
| 379 |
|
| 380 |
// Parse series count. |
| 381 |
v, n := binary.Uvarint(buf) |
| 382 |
e.series.n = uint64(v) |
| 383 |
buf = buf[n:] |
| 384 |
|
| 385 |
// Parse data block size. |
| 386 |
sz, n = binary.Uvarint(buf) |
| 387 |
buf = buf[n:] |
| 388 |
|
| 389 |
// Save reference to series data. |
| 390 |
e.series.data = buf[:sz] |
| 391 |
buf = buf[sz:] |
| 392 |
|
| 393 |
// Save length of elem. |
| 394 |
e.size = start - len(buf) |
| 395 |
} |
| 396 |
|
| 397 |
// TagBlockTrailerSize is the total size of the on-disk trailer. |
| 398 |
const TagBlockTrailerSize = 0 + |
| 399 |
8 + 8 + // value data offset/size |
| 400 |
8 + 8 + // key data offset/size |
| 401 |
8 + 8 + // hash index offset/size |
| 402 |
8 + // size |
| 403 |
2 // version |
| 404 |
|
| 405 |
// TagBlockTrailer represents meta data at the end of a TagBlock. |
| 406 |
type TagBlockTrailer struct { |
| 407 |
Version int // Encoding version |
| 408 |
Size int64 // Total size w/ trailer |
| 409 |
|
| 410 |
// Offset & size of value data section. |
| 411 |
ValueData struct { |
| 412 |
Offset int64 |
| 413 |
Size int64 |
| 414 |
} |
| 415 |
|
| 416 |
// Offset & size of key data section. |
| 417 |
KeyData struct { |
| 418 |
Offset int64 |
| 419 |
Size int64 |
| 420 |
} |
| 421 |
|
| 422 |
// Offset & size of hash map section. |
| 423 |
HashIndex struct { |
| 424 |
Offset int64 |
| 425 |
Size int64 |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// WriteTo writes the trailer to w. |
| 430 |
func (t *TagBlockTrailer) WriteTo(w io.Writer) (n int64, err error) { |
| 431 |
// Write data info. |
| 432 |
if err := writeUint64To(w, uint64(t.ValueData.Offset), &n); err != nil { |
| 433 |
return n, err |
| 434 |
} else if err := writeUint64To(w, uint64(t.ValueData.Size), &n); err != nil { |
| 435 |
return n, err |
| 436 |
} |
| 437 |
|
| 438 |
// Write key data info. |
| 439 |
if err := writeUint64To(w, uint64(t.KeyData.Offset), &n); err != nil { |
| 440 |
return n, err |
| 441 |
} else if err := writeUint64To(w, uint64(t.KeyData.Size), &n); err != nil { |
| 442 |
return n, err |
| 443 |
} |
| 444 |
|
| 445 |
// Write hash index info. |
| 446 |
if err := writeUint64To(w, uint64(t.HashIndex.Offset), &n); err != nil { |
| 447 |
return n, err |
| 448 |
} else if err := writeUint64To(w, uint64(t.HashIndex.Size), &n); err != nil { |
| 449 |
return n, err |
| 450 |
} |
| 451 |
|
| 452 |
// Write total size & encoding version. |
| 453 |
if err := writeUint64To(w, uint64(t.Size), &n); err != nil { |
| 454 |
return n, err |
| 455 |
} else if err := writeUint16To(w, IndexFileVersion, &n); err != nil { |
| 456 |
return n, err |
| 457 |
} |
| 458 |
|
| 459 |
return n, nil |
| 460 |
} |
| 461 |
|
| 462 |
// ReadTagBlockTrailer returns the tag block trailer from data. |
| 463 |
func ReadTagBlockTrailer(data []byte) (TagBlockTrailer, error) { |
| 464 |
var t TagBlockTrailer |
| 465 |
|
| 466 |
// Read version. |
| 467 |
t.Version = int(binary.BigEndian.Uint16(data[len(data)-2:])) |
| 468 |
if t.Version != TagBlockVersion { |
| 469 |
return t, ErrUnsupportedTagBlockVersion |
| 470 |
} |
| 471 |
|
| 472 |
// Slice trailer data. |
| 473 |
buf := data[len(data)-TagBlockTrailerSize:] |
| 474 |
|
| 475 |
// Read data section info. |
| 476 |
t.ValueData.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 477 |
t.ValueData.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 478 |
|
| 479 |
// Read key section info. |
| 480 |
t.KeyData.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 481 |
t.KeyData.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 482 |
|
| 483 |
// Read hash section info. |
| 484 |
t.HashIndex.Offset, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 485 |
t.HashIndex.Size, buf = int64(binary.BigEndian.Uint64(buf[0:8])), buf[8:] |
| 486 |
|
| 487 |
// Read total size. |
| 488 |
t.Size = int64(binary.BigEndian.Uint64(buf[0:8])) |
| 489 |
|
| 490 |
return t, nil |
| 491 |
} |
| 492 |
|
| 493 |
// TagBlockEncoder encodes a tags to a TagBlock section. |
| 494 |
type TagBlockEncoder struct { |
| 495 |
w io.Writer |
| 496 |
buf bytes.Buffer |
| 497 |
|
| 498 |
// Track value offsets. |
| 499 |
offsets *rhh.HashMap |
| 500 |
|
| 501 |
// Track bytes written, sections. |
| 502 |
n int64 |
| 503 |
trailer TagBlockTrailer |
| 504 |
|
| 505 |
// Track tag keys. |
| 506 |
keys []tagKeyEncodeEntry |
| 507 |
prevValue []byte |
| 508 |
} |
| 509 |
|
| 510 |
// NewTagBlockEncoder returns a new TagBlockEncoder. |
| 511 |
func NewTagBlockEncoder(w io.Writer) *TagBlockEncoder { |
| 512 |
return &TagBlockEncoder{ |
| 513 |
w: w, |
| 514 |
offsets: rhh.NewHashMap(rhh.Options{LoadFactor: LoadFactor}), |
| 515 |
trailer: TagBlockTrailer{ |
| 516 |
Version: TagBlockVersion, |
| 517 |
}, |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
// N returns the number of bytes written. |
| 522 |
func (enc *TagBlockEncoder) N() int64 { return enc.n } |
| 523 |
|
| 524 |
// EncodeKey writes a tag key to the underlying writer. |
| 525 |
func (enc *TagBlockEncoder) EncodeKey(key []byte, deleted bool) error { |
| 526 |
// An initial empty byte must be written. |
| 527 |
if err := enc.ensureHeaderWritten(); err != nil { |
| 528 |
return err |
| 529 |
} |
| 530 |
|
| 531 |
// Verify key is lexicographically after previous key. |
| 532 |
if len(enc.keys) > 0 { |
| 533 |
prev := enc.keys[len(enc.keys)-1].key |
| 534 |
if cmp := bytes.Compare(prev, key); cmp == 1 { |
| 535 |
return fmt.Errorf("tag key out of order: prev=%s, new=%s", prev, key) |
| 536 |
} else if cmp == 0 { |
| 537 |
return fmt.Errorf("tag key already encoded: %s", key) |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
// Flush values section for key. |
| 542 |
if err := enc.flushValueHashIndex(); err != nil { |
| 543 |
return err |
| 544 |
} |
| 545 |
|
| 546 |
// Append key on to the end of the key list. |
| 547 |
entry := tagKeyEncodeEntry{ |
| 548 |
key: key, |
| 549 |
deleted: deleted, |
| 550 |
} |
| 551 |
entry.data.offset = enc.n |
| 552 |
|
| 553 |
enc.keys = append(enc.keys, entry) |
| 554 |
|
| 555 |
// Clear previous value. |
| 556 |
enc.prevValue = nil |
| 557 |
|
| 558 |
return nil |
| 559 |
} |
| 560 |
|
| 561 |
// EncodeValue writes a tag value to the underlying writer. |
| 562 |
// The tag key must be lexicographical sorted after the previous encoded tag key. |
| 563 |
func (enc *TagBlockEncoder) EncodeValue(value []byte, deleted bool, seriesIDs []uint64) error { |
| 564 |
if len(enc.keys) == 0 { |
| 565 |
return fmt.Errorf("tag key must be encoded before encoding values") |
| 566 |
} else if len(value) == 0 { |
| 567 |
return fmt.Errorf("zero length tag value not allowed") |
| 568 |
} |
| 569 |
|
| 570 |
// Validate that keys are in-order. |
| 571 |
if cmp := bytes.Compare(enc.prevValue, value); cmp == 1 { |
| 572 |
return fmt.Errorf("tag value out of order: prev=%s, new=%s", enc.prevValue, value) |
| 573 |
} else if cmp == 0 { |
| 574 |
return fmt.Errorf("tag value already encoded: %s", value) |
| 575 |
} |
| 576 |
|
| 577 |
// Save offset to hash map. |
| 578 |
enc.offsets.Put(value, enc.n) |
| 579 |
|
| 580 |
// Write flag. |
| 581 |
if err := writeUint8To(enc.w, encodeTagValueFlag(deleted), &enc.n); err != nil { |
| 582 |
return err |
| 583 |
} |
| 584 |
|
| 585 |
// Write value. |
| 586 |
if err := writeUvarintTo(enc.w, uint64(len(value)), &enc.n); err != nil { |
| 587 |
return err |
| 588 |
} else if err := writeTo(enc.w, value, &enc.n); err != nil { |
| 589 |
return err |
| 590 |
} |
| 591 |
|
| 592 |
// Build series data in buffer. |
| 593 |
enc.buf.Reset() |
| 594 |
var prev uint64 |
| 595 |
for _, seriesID := range seriesIDs { |
| 596 |
delta := seriesID - prev |
| 597 |
|
| 598 |
var buf [binary.MaxVarintLen32]byte |
| 599 |
i := binary.PutUvarint(buf[:], uint64(delta)) |
| 600 |
if _, err := enc.buf.Write(buf[:i]); err != nil { |
| 601 |
return err |
| 602 |
} |
| 603 |
|
| 604 |
prev = seriesID |
| 605 |
} |
| 606 |
|
| 607 |
// Write series count. |
| 608 |
if err := writeUvarintTo(enc.w, uint64(len(seriesIDs)), &enc.n); err != nil { |
| 609 |
return err |
| 610 |
} |
| 611 |
|
| 612 |
// Write data size & buffer. |
| 613 |
if err := writeUvarintTo(enc.w, uint64(enc.buf.Len()), &enc.n); err != nil { |
| 614 |
return err |
| 615 |
} |
| 616 |
nn, err := enc.buf.WriteTo(enc.w) |
| 617 |
if enc.n += nn; err != nil { |
| 618 |
return err |
| 619 |
} |
| 620 |
|
| 621 |
// Save previous value. |
| 622 |
enc.prevValue = value |
| 623 |
|
| 624 |
return nil |
| 625 |
} |
| 626 |
|
| 627 |
// Close flushes the trailer of the encoder to the writer. |
| 628 |
func (enc *TagBlockEncoder) Close() error { |
| 629 |
// Flush last value set. |
| 630 |
if err := enc.ensureHeaderWritten(); err != nil { |
| 631 |
return err |
| 632 |
} else if err := enc.flushValueHashIndex(); err != nil { |
| 633 |
return err |
| 634 |
} |
| 635 |
|
| 636 |
// Save ending position of entire data block. |
| 637 |
enc.trailer.ValueData.Size = enc.n - enc.trailer.ValueData.Offset |
| 638 |
|
| 639 |
// Write key block to point to value blocks. |
| 640 |
if err := enc.encodeTagKeyBlock(); err != nil { |
| 641 |
return err |
| 642 |
} |
| 643 |
|
| 644 |
// Compute total size w/ trailer. |
| 645 |
enc.trailer.Size = enc.n + TagBlockTrailerSize |
| 646 |
|
| 647 |
// Write trailer. |
| 648 |
nn, err := enc.trailer.WriteTo(enc.w) |
| 649 |
enc.n += nn |
| 650 |
return err |
| 651 |
} |
| 652 |
|
| 653 |
// ensureHeaderWritten writes a single byte to offset the rest of the block. |
| 654 |
func (enc *TagBlockEncoder) ensureHeaderWritten() error { |
| 655 |
if enc.n > 0 { |
| 656 |
return nil |
| 657 |
} else if _, err := enc.w.Write([]byte{0}); err != nil { |
| 658 |
return err |
| 659 |
} |
| 660 |
|
| 661 |
enc.n++ |
| 662 |
enc.trailer.ValueData.Offset = enc.n |
| 663 |
|
| 664 |
return nil |
| 665 |
} |
| 666 |
|
| 667 |
// flushValueHashIndex builds writes the hash map at the end of a value set. |
| 668 |
func (enc *TagBlockEncoder) flushValueHashIndex() error { |
| 669 |
// Ignore if no keys have been written. |
| 670 |
if len(enc.keys) == 0 { |
| 671 |
return nil |
| 672 |
} |
| 673 |
key := &enc.keys[len(enc.keys)-1] |
| 674 |
|
| 675 |
// Save size of data section. |
| 676 |
key.data.size = enc.n - key.data.offset |
| 677 |
|
| 678 |
// Encode hash map length. |
| 679 |
key.hashIndex.offset = enc.n |
| 680 |
if err := writeUint64To(enc.w, uint64(enc.offsets.Cap()), &enc.n); err != nil { |
| 681 |
return err |
| 682 |
} |
| 683 |
|
| 684 |
// Encode hash map offset entries. |
| 685 |
for i := int64(0); i < enc.offsets.Cap(); i++ { |
| 686 |
_, v := enc.offsets.Elem(i) |
| 687 |
offset, _ := v.(int64) |
| 688 |
if err := writeUint64To(enc.w, uint64(offset), &enc.n); err != nil { |
| 689 |
return err |
| 690 |
} |
| 691 |
} |
| 692 |
key.hashIndex.size = enc.n - key.hashIndex.offset |
| 693 |
|
| 694 |
// Clear offsets. |
| 695 |
enc.offsets = rhh.NewHashMap(rhh.Options{LoadFactor: LoadFactor}) |
| 696 |
|
| 697 |
return nil |
| 698 |
} |
| 699 |
|
| 700 |
// encodeTagKeyBlock encodes the keys section to the writer. |
| 701 |
func (enc *TagBlockEncoder) encodeTagKeyBlock() error { |
| 702 |
offsets := rhh.NewHashMap(rhh.Options{Capacity: int64(len(enc.keys)), LoadFactor: LoadFactor}) |
| 703 |
|
| 704 |
// Encode key list in sorted order. |
| 705 |
enc.trailer.KeyData.Offset = enc.n |
| 706 |
for i := range enc.keys { |
| 707 |
entry := &enc.keys[i] |
| 708 |
|
| 709 |
// Save current offset so we can use it in the hash index. |
| 710 |
offsets.Put(entry.key, enc.n) |
| 711 |
|
| 712 |
if err := writeUint8To(enc.w, encodeTagKeyFlag(entry.deleted), &enc.n); err != nil { |
| 713 |
return err |
| 714 |
} |
| 715 |
|
| 716 |
// Write value data offset & size. |
| 717 |
if err := writeUint64To(enc.w, uint64(entry.data.offset), &enc.n); err != nil { |
| 718 |
return err |
| 719 |
} else if err := writeUint64To(enc.w, uint64(entry.data.size), &enc.n); err != nil { |
| 720 |
return err |
| 721 |
} |
| 722 |
|
| 723 |
// Write value hash index offset & size. |
| 724 |
if err := writeUint64To(enc.w, uint64(entry.hashIndex.offset), &enc.n); err != nil { |
| 725 |
return err |
| 726 |
} else if err := writeUint64To(enc.w, uint64(entry.hashIndex.size), &enc.n); err != nil { |
| 727 |
return err |
| 728 |
} |
| 729 |
|
| 730 |
// Write key length and data. |
| 731 |
if err := writeUvarintTo(enc.w, uint64(len(entry.key)), &enc.n); err != nil { |
| 732 |
return err |
| 733 |
} else if err := writeTo(enc.w, entry.key, &enc.n); err != nil { |
| 734 |
return err |
| 735 |
} |
| 736 |
} |
| 737 |
enc.trailer.KeyData.Size = enc.n - enc.trailer.KeyData.Offset |
| 738 |
|
| 739 |
// Encode hash map length. |
| 740 |
enc.trailer.HashIndex.Offset = enc.n |
| 741 |
if err := writeUint64To(enc.w, uint64(offsets.Cap()), &enc.n); err != nil { |
| 742 |
return err |
| 743 |
} |
| 744 |
|
| 745 |
// Encode hash map offset entries. |
| 746 |
for i := int64(0); i < offsets.Cap(); i++ { |
| 747 |
_, v := offsets.Elem(i) |
| 748 |
offset, _ := v.(int64) |
| 749 |
if err := writeUint64To(enc.w, uint64(offset), &enc.n); err != nil { |
| 750 |
return err |
| 751 |
} |
| 752 |
} |
| 753 |
enc.trailer.HashIndex.Size = enc.n - enc.trailer.HashIndex.Offset |
| 754 |
|
| 755 |
return nil |
| 756 |
} |
| 757 |
|
| 758 |
type tagKeyEncodeEntry struct { |
| 759 |
key []byte |
| 760 |
deleted bool |
| 761 |
|
| 762 |
data struct { |
| 763 |
offset int64 |
| 764 |
size int64 |
| 765 |
} |
| 766 |
hashIndex struct { |
| 767 |
offset int64 |
| 768 |
size int64 |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
func encodeTagKeyFlag(deleted bool) byte { |
| 773 |
var flag byte |
| 774 |
if deleted { |
| 775 |
flag |= TagKeyTombstoneFlag |
| 776 |
} |
| 777 |
return flag |
| 778 |
} |
| 779 |
|
| 780 |
func encodeTagValueFlag(deleted bool) byte { |
| 781 |
var flag byte |
| 782 |
if deleted { |
| 783 |
flag |= TagValueTombstoneFlag |
| 784 |
} |
| 785 |
return flag |
| 786 |
} |