|
| 1 | +// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use |
| 2 | +// of this source code is governed by a BSD-style license that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package blob |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/binary" |
| 9 | + "unsafe" |
| 10 | + |
| 11 | + "github.com/cockroachdb/pebble/internal/base" |
| 12 | + "github.com/cockroachdb/pebble/sstable/valblk" |
| 13 | + "github.com/cockroachdb/redact" |
| 14 | +) |
| 15 | + |
| 16 | +// MaxInlineHandleLength is the maximum length of an inline blob handle. |
| 17 | +// |
| 18 | +// Handle fields are varint encoded, so maximum 5 bytes each. |
| 19 | +const MaxInlineHandleLength = 4 * binary.MaxVarintLen32 |
| 20 | + |
| 21 | +// Handle describes the location of a value stored within a blob file. |
| 22 | +type Handle struct { |
| 23 | + FileNum base.DiskFileNum |
| 24 | + BlockNum uint32 |
| 25 | + OffsetInBlock uint32 |
| 26 | + ValueLen uint32 |
| 27 | +} |
| 28 | + |
| 29 | +// String implements the fmt.Stringer interface. |
| 30 | +func (h Handle) String() string { |
| 31 | + return redact.StringWithoutMarkers(h) |
| 32 | +} |
| 33 | + |
| 34 | +// SafeFormat implements redact.SafeFormatter. |
| 35 | +func (h Handle) SafeFormat(w redact.SafePrinter, _ rune) { |
| 36 | + w.Printf("(%s,blk%d[%d:%d])", |
| 37 | + h.FileNum, h.BlockNum, h.OffsetInBlock, h.OffsetInBlock+h.ValueLen) |
| 38 | +} |
| 39 | + |
| 40 | +// TODO(jackson): Consider encoding the handle's data using columnar block |
| 41 | +// primitives, rather than a variable-width encoding in the value column. |
| 42 | + |
| 43 | +// InlineHandle describes a handle as it is encoded within a sstable block. The |
| 44 | +// inline handle does not encode the blob file number outright. Instead it |
| 45 | +// encodes an index into the containing sstable's BlobReferences. |
| 46 | +// |
| 47 | +// The inline handle is composed of two parts: a preface (InlineHandlePreface) |
| 48 | +// and a suffix (HandleSuffix). The preface is eagerly decoded from the encoded |
| 49 | +// handle when returning an InternalValue to higher layers. The remaining bits |
| 50 | +// (the suffix) are decoded only when the value is being fetched from the blob |
| 51 | +// file. |
| 52 | +type InlineHandle struct { |
| 53 | + InlineHandlePreface |
| 54 | + HandleSuffix |
| 55 | +} |
| 56 | + |
| 57 | +// InlineHandlePreface is the prefix of an inline handle. It's eagerly decoded |
| 58 | +// when returning an InternalValue to higher layers. |
| 59 | +type InlineHandlePreface struct { |
| 60 | + ReferenceIndex uint32 |
| 61 | + ValueLen uint32 |
| 62 | +} |
| 63 | + |
| 64 | +// HandleSuffix is the suffix of an inline handle. It's decoded only when the |
| 65 | +// value is being fetched from the blob file. |
| 66 | +type HandleSuffix struct { |
| 67 | + BlockNum uint32 |
| 68 | + OffsetInBlock uint32 |
| 69 | +} |
| 70 | + |
| 71 | +// String implements the fmt.Stringer interface. |
| 72 | +func (h InlineHandle) String() string { |
| 73 | + return redact.StringWithoutMarkers(h) |
| 74 | +} |
| 75 | + |
| 76 | +// SafeFormat implements redact.SafeFormatter. |
| 77 | +func (h InlineHandle) SafeFormat(w redact.SafePrinter, _ rune) { |
| 78 | + w.Printf("(f%d,blk%d[%d:%d])", |
| 79 | + h.ReferenceIndex, h.BlockNum, h.OffsetInBlock, h.OffsetInBlock+h.ValueLen) |
| 80 | +} |
| 81 | + |
| 82 | +// Encode encodes the inline handle into the provided buffer, returning the |
| 83 | +// number of bytes encoded. |
| 84 | +func (h InlineHandle) Encode(b []byte) int { |
| 85 | + n := 0 |
| 86 | + n += binary.PutUvarint(b[n:], uint64(h.ReferenceIndex)) |
| 87 | + n += valblk.EncodeHandle(b[n:], valblk.Handle{ |
| 88 | + BlockNum: h.BlockNum, |
| 89 | + OffsetInBlock: h.OffsetInBlock, |
| 90 | + ValueLen: h.ValueLen, |
| 91 | + }) |
| 92 | + return n |
| 93 | +} |
| 94 | + |
| 95 | +// DecodeInlineHandlePrefix decodes the blob reference index and value length |
| 96 | +// from the beginning of a variable-width encoded InlineHandle. |
| 97 | +func DecodeInlineHandlePrefix(src []byte) (InlineHandlePreface, []byte) { |
| 98 | + ptr := unsafe.Pointer(&src[0]) |
| 99 | + var refIdx uint32 |
| 100 | + if a := *((*uint8)(ptr)); a < 128 { |
| 101 | + refIdx = uint32(a) |
| 102 | + src = src[1:] |
| 103 | + } else if a, b := a&0x7f, *((*uint8)(unsafe.Add(ptr, 1))); b < 128 { |
| 104 | + refIdx = uint32(b)<<7 | uint32(a) |
| 105 | + src = src[2:] |
| 106 | + } else if b, c := b&0x7f, *((*uint8)(unsafe.Add(ptr, 2))); c < 128 { |
| 107 | + refIdx = uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 108 | + src = src[3:] |
| 109 | + } else if c, d := c&0x7f, *((*uint8)(unsafe.Add(ptr, 3))); d < 128 { |
| 110 | + refIdx = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 111 | + src = src[4:] |
| 112 | + } else { |
| 113 | + d, e := d&0x7f, *((*uint8)(unsafe.Add(ptr, 4))) |
| 114 | + refIdx = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 115 | + src = src[5:] |
| 116 | + } |
| 117 | + |
| 118 | + ptr = unsafe.Pointer(&src[0]) |
| 119 | + var valueLen uint32 |
| 120 | + if a := *((*uint8)(ptr)); a < 128 { |
| 121 | + valueLen = uint32(a) |
| 122 | + src = src[1:] |
| 123 | + } else if a, b := a&0x7f, *((*uint8)(unsafe.Add(ptr, 1))); b < 128 { |
| 124 | + valueLen = uint32(b)<<7 | uint32(a) |
| 125 | + src = src[2:] |
| 126 | + } else if b, c := b&0x7f, *((*uint8)(unsafe.Add(ptr, 2))); c < 128 { |
| 127 | + valueLen = uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 128 | + src = src[3:] |
| 129 | + } else if c, d := c&0x7f, *((*uint8)(unsafe.Add(ptr, 3))); d < 128 { |
| 130 | + valueLen = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 131 | + src = src[4:] |
| 132 | + } else { |
| 133 | + d, e := d&0x7f, *((*uint8)(unsafe.Add(ptr, 4))) |
| 134 | + valueLen = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a) |
| 135 | + src = src[5:] |
| 136 | + } |
| 137 | + |
| 138 | + return InlineHandlePreface{ |
| 139 | + ReferenceIndex: refIdx, |
| 140 | + ValueLen: valueLen, |
| 141 | + }, src |
| 142 | +} |
| 143 | + |
| 144 | +// DecodeHandleSuffix decodes the block number and offset in block from the |
| 145 | +// encoded handle. |
| 146 | +func DecodeHandleSuffix(src []byte) HandleSuffix { |
| 147 | + h := valblk.DecodeRemainingHandle(src) |
| 148 | + return HandleSuffix{ |
| 149 | + BlockNum: h.BlockNum, |
| 150 | + OffsetInBlock: h.OffsetInBlock, |
| 151 | + } |
| 152 | +} |
0 commit comments