7 "github.com/syndtr/goleveldb/leveldb/errors"
8 "github.com/syndtr/goleveldb/leveldb/storage"
11 type ErrIkeyCorrupted struct {
16 func (e *ErrIkeyCorrupted) Error() string {
17 return fmt.Sprintf("leveldb: iKey %q corrupted: %s", e.Ikey, e.Reason)
20 func newErrIkeyCorrupted(ikey []byte, reason string) error {
21 return errors.NewErrCorrupted(storage.FileDesc{}, &ErrIkeyCorrupted{append([]byte{}, ikey...), reason})
26 func (kt kType) String() string {
36 // Value types encoded as the last component of internal keys.
37 // Don't modify; this value are saved to disk.
43 // ktSeek defines the kType that should be passed when constructing an
44 // internal key for seeking to a particular sequence number (since we
45 // sort sequence numbers in decreasing order and the value type is
46 // embedded as the low 8 bits in the sequence number in internal keys,
47 // we need to use the highest-numbered ValueType, not the lowest).
51 // Maximum value possible for sequence number; the 8-bits are
52 // used by value type, so its can packed together in single
54 kMaxSeq uint64 = (uint64(1) << 56) - 1
55 // Maximum value possible for packed sequence number and type.
56 kMaxNum uint64 = (kMaxSeq << 8) | uint64(ktSeek)
59 // Maximum number encoded in bytes.
60 var kMaxNumBytes = make([]byte, 8)
63 binary.LittleEndian.PutUint64(kMaxNumBytes, kMaxNum)
68 func newIkey(ukey []byte, seq uint64, kt kType) iKey {
70 panic("leveldb: invalid sequence number")
71 } else if kt > ktVal {
72 panic("leveldb: invalid type")
75 ik := make(iKey, len(ukey)+8)
77 binary.LittleEndian.PutUint64(ik[len(ukey):], (seq<<8)|uint64(kt))
81 func parseIkey(ik []byte) (ukey []byte, seq uint64, kt kType, err error) {
83 return nil, 0, 0, newErrIkeyCorrupted(ik, "invalid length")
85 num := binary.LittleEndian.Uint64(ik[len(ik)-8:])
86 seq, kt = uint64(num>>8), kType(num&0xff)
88 return nil, 0, 0, newErrIkeyCorrupted(ik, "invalid type")
94 func validIkey(ik []byte) bool {
95 _, _, _, err := parseIkey(ik)
99 func (ik iKey) assert() {
101 panic("leveldb: nil iKey")
104 panic(fmt.Sprintf("leveldb: iKey %q, len=%d: invalid length", ik, len(ik)))
108 func (ik iKey) ukey() []byte {
110 return ik[:len(ik)-8]
113 func (ik iKey) num() uint64 {
115 return binary.LittleEndian.Uint64(ik[len(ik)-8:])
118 func (ik iKey) parseNum() (seq uint64, kt kType) {
120 seq, kt = uint64(num>>8), kType(num&0xff)
122 panic(fmt.Sprintf("leveldb: iKey %q, len=%d: invalid type %#x", ik, len(ik), kt))
127 func (ik iKey) String() string {
132 if ukey, seq, kt, err := parseIkey(ik); err == nil {
133 return fmt.Sprintf("%x,%s%d", ukey, kt, seq)