page

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package page provides page management for XxSql storage engine.

Index

Constants

View Source
const (
	// PageSize is the size of a page in bytes.
	PageSize = 4096

	// PageHeaderSize is the size of the page header in bytes.
	PageHeaderSize = 24

	// MaxDataSize is the maximum data size in a page.
	MaxDataSize = PageSize - PageHeaderSize
)
View Source
const (
	FlagLeaf     uint8 = 0x01 // Leaf page (vs internal)
	FlagRoot     uint8 = 0x02 // Root page of B+ tree
	FlagOverflow uint8 = 0x04 // Has overflow pages
	FlagDeleted  uint8 = 0x08 // Page is deleted
)

Page flags.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	PageID     PageID
	LSN        uint64
	Type       PageType
	Flags      uint8
	FreeOffset uint16
	SlotCount  uint16
	Checksum   uint16
}

Header represents the page header (24 bytes). Layout:

  • Bytes 0-7: Page ID (8 bytes)
  • Bytes 8-15: LSN (Log Sequence Number) (8 bytes)
  • Bytes 16: Page Type (1 byte)
  • Bytes 17: Flags (1 byte)
  • Bytes 18-19: Free space offset (2 bytes)
  • Bytes 20-21: Slot count (2 bytes)
  • Bytes 22-23: Checksum (2 bytes)

type Page

type Page struct {
	ID       PageID
	Type     PageType
	Data     []byte
	Modified bool
}

Page represents a database page.

func NewPage

func NewPage(id PageID, typ PageType) *Page

NewPage creates a new page.

func NewPageFromBytes

func NewPageFromBytes(data []byte) (*Page, error)

NewPageFromBytes creates a page from bytes.

func (*Page) AppendSlot

func (p *Page) AppendSlot(offset, length uint16) int

AppendSlot appends a new slot entry.

func (*Page) ClearFlag

func (p *Page) ClearFlag(flag uint8)

ClearFlag clears a flag.

func (*Page) DataRange

func (p *Page) DataRange() (start, end uint16)

DataRange returns the data area range (header end to free offset).

func (*Page) DeleteRow

func (p *Page) DeleteRow(index int) error

DeleteRow marks a row as deleted by setting its length to 0. This is a simple deletion - no compaction is performed.

func (*Page) FreeSpace

func (p *Page) FreeSpace() uint16

FreeSpace returns the available free space in the page.

func (*Page) GetChecksum

func (p *Page) GetChecksum() uint16

GetChecksum returns the checksum.

func (*Page) GetFlags

func (p *Page) GetFlags() uint8

GetFlags returns the flags from the header.

func (*Page) GetFreeOffset

func (p *Page) GetFreeOffset() uint16

GetFreeOffset returns the free space offset.

func (*Page) GetLSN

func (p *Page) GetLSN() uint64

GetLSN returns the LSN from the header.

func (*Page) GetPageID

func (p *Page) GetPageID() PageID

GetPageID returns the page ID from the header.

func (*Page) GetRow

func (p *Page) GetRow(index int) ([]byte, error)

GetRow returns the row data at the given slot index.

func (*Page) GetSlot

func (p *Page) GetSlot(index int) SlotEntry

GetSlot returns the slot entry at the given index.

func (*Page) GetSlotCount

func (p *Page) GetSlotCount() uint16

GetSlotCount returns the slot count.

func (*Page) GetType

func (p *Page) GetType() PageType

GetType returns the page type from the header.

func (*Page) HasFlag

func (p *Page) HasFlag(flag uint8) bool

HasFlag checks if a flag is set.

func (*Page) InsertRow

func (p *Page) InsertRow(data []byte) (int, error)

InsertRow inserts a row at the end of the page. Returns the slot index or an error if there's not enough space.

func (*Page) IsLeaf

func (p *Page) IsLeaf() bool

IsLeaf returns true if this is a leaf page.

func (*Page) IsRoot

func (p *Page) IsRoot() bool

IsRoot returns true if this is a root page.

func (*Page) RowCount

func (p *Page) RowCount() int

RowCount returns the number of rows (slots) in the page.

func (*Page) SetChecksum

func (p *Page) SetChecksum(checksum uint16)

SetChecksum sets the checksum.

func (*Page) SetFlag

func (p *Page) SetFlag(flag uint8)

SetFlag sets a flag.

func (*Page) SetFlags

func (p *Page) SetFlags(flags uint8)

SetFlags sets the flags in the header.

func (*Page) SetFreeOffset

func (p *Page) SetFreeOffset(offset uint16)

SetFreeOffset sets the free space offset.

func (*Page) SetLSN

func (p *Page) SetLSN(lsn uint64)

SetLSN sets the LSN in the header.

func (*Page) SetLeaf

func (p *Page) SetLeaf(leaf bool)

SetLeaf sets the leaf flag.

func (*Page) SetPageID

func (p *Page) SetPageID(id PageID)

SetPageID sets the page ID in the header.

func (*Page) SetRoot

func (p *Page) SetRoot(root bool)

SetRoot sets the root flag.

func (*Page) SetSlot

func (p *Page) SetSlot(index int, entry SlotEntry)

SetSlot sets the slot entry at the given index.

func (*Page) SetSlotCount

func (p *Page) SetSlotCount(count uint16)

SetSlotCount sets the slot count.

func (*Page) SetType

func (p *Page) SetType(typ PageType)

SetType sets the page type in the header.

func (*Page) ToBytes

func (p *Page) ToBytes() []byte

ToBytes returns the page as bytes.

func (*Page) UpdateRow

func (p *Page) UpdateRow(index int, data []byte) error

UpdateRow updates the row data at the given slot index. Note: This only works if the new data is the same size or smaller. For larger data, the row should be deleted and reinserted.

type PageID

type PageID uint64

PageID represents a unique page identifier.

const InvalidPageID PageID = 0

InvalidPageID represents an invalid page ID.

type PageType

type PageType uint8

PageType represents the type of a page.

const (
	PageTypeHeader   PageType = iota // Database header page
	PageTypeData                     // Table data page
	PageTypeIndex                    // B+ tree index page
	PageTypeOverflow                 // Overflow page for large values
	PageTypeFree                     // Free page in free list
)

func (PageType) String

func (t PageType) String() string

String returns the string representation of the page type.

type SlotEntry

type SlotEntry struct {
	Offset uint16
	Length uint16
}

SlotEntry represents a slot entry in the page.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL