| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 1 | package common |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | const BucketHeaderSize = int(unsafe.Sizeof(InBucket{})) |
| 9 | |
| 10 | // InBucket represents the on-file representation of a bucket. |
| 11 | // This is stored as the "value" of a bucket key. If the bucket is small enough, |
| 12 | // then its root page can be stored inline in the "value", after the bucket |
| 13 | // header. In the case of inline buckets, the "root" will be 0. |
| 14 | type InBucket struct { |
| 15 | root Pgid // page id of the bucket's root-level page |
| 16 | sequence uint64 // monotonically incrementing, used by NextSequence() |
| 17 | } |
| 18 | |
| 19 | func NewInBucket(root Pgid, seq uint64) InBucket { |
| 20 | return InBucket{ |
| 21 | root: root, |
| 22 | sequence: seq, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func (b *InBucket) RootPage() Pgid { |
| 27 | return b.root |
| 28 | } |
| 29 | |
| 30 | func (b *InBucket) SetRootPage(id Pgid) { |
| 31 | b.root = id |
| 32 | } |
| 33 | |
| 34 | // InSequence returns the sequence. The reason why not naming it `Sequence` |
| 35 | // is to avoid duplicated name as `(*Bucket) Sequence()` |
| 36 | func (b *InBucket) InSequence() uint64 { |
| 37 | return b.sequence |
| 38 | } |
| 39 | |
| 40 | func (b *InBucket) SetInSequence(v uint64) { |
| 41 | b.sequence = v |
| 42 | } |
| 43 | |
| 44 | func (b *InBucket) IncSequence() { |
| 45 | b.sequence++ |
| 46 | } |
| 47 | |
| 48 | func (b *InBucket) InlinePage(v []byte) *Page { |
| 49 | return (*Page)(unsafe.Pointer(&v[BucketHeaderSize])) |
| 50 | } |
| 51 | |
| 52 | func (b *InBucket) String() string { |
| 53 | return fmt.Sprintf("<pgid=%d,seq=%d>", b.root, b.sequence) |
| 54 | } |