Parse would parse the key. ParsedKey does not reuse the key slice, so the key slice can change without affecting the contents of ParsedKey.
(key []byte)
| 528 | // Parse would parse the key. ParsedKey does not reuse the key slice, so the key slice can change |
| 529 | // without affecting the contents of ParsedKey. |
| 530 | func Parse(key []byte) (ParsedKey, error) { |
| 531 | var p ParsedKey |
| 532 | |
| 533 | if len(key) < 9 { |
| 534 | return p, errors.New("Key length less than 9") |
| 535 | } |
| 536 | p.bytePrefix = key[0] |
| 537 | namespace := key[1:9] |
| 538 | key = key[9:] |
| 539 | if p.bytePrefix == ByteUnused { |
| 540 | return p, nil |
| 541 | } |
| 542 | |
| 543 | p.HasStartUid = p.bytePrefix == ByteSplit |
| 544 | |
| 545 | if len(key) < 3 { |
| 546 | return p, errors.Errorf("Invalid format for key %v", key) |
| 547 | } |
| 548 | sz := int(binary.BigEndian.Uint16(key[:2])) |
| 549 | k := key[2:] |
| 550 | |
| 551 | if len(k) < sz { |
| 552 | return p, errors.Errorf("Invalid size %v for key %v", sz, key) |
| 553 | } |
| 554 | p.Attr = NamespaceAttr(binary.BigEndian.Uint64(namespace), string(k[:sz])) |
| 555 | k = k[sz:] |
| 556 | |
| 557 | switch p.bytePrefix { |
| 558 | case ByteSchema, ByteType: |
| 559 | return p, nil |
| 560 | default: |
| 561 | } |
| 562 | |
| 563 | p.ByteType = k[0] |
| 564 | k = k[1:] |
| 565 | |
| 566 | switch p.ByteType { |
| 567 | case ByteData, ByteReverse: |
| 568 | if len(k) < 8 { |
| 569 | return p, errors.Errorf("uid length < 8 for key: %q, parsed key: %+v", key, p) |
| 570 | } |
| 571 | p.Uid = binary.BigEndian.Uint64(k) |
| 572 | if p.Uid == 0 { |
| 573 | return p, errors.Errorf("Invalid UID with value 0 for key: %v", key) |
| 574 | } |
| 575 | if !p.HasStartUid { |
| 576 | break |
| 577 | } |
| 578 | |
| 579 | if len(k) != 16 { |
| 580 | return p, errors.Errorf("StartUid length != 8 for key: %q, parsed key: %+v", key, p) |
| 581 | } |
| 582 | |
| 583 | k = k[8:] |
| 584 | p.StartUid = binary.BigEndian.Uint64(k) |
| 585 | case ByteIndex: |
| 586 | if !p.HasStartUid { |
| 587 | p.Term = string(k) |