Documentation
¶
Index ¶
- Constants
- Variables
- func EncodeHeader(dst []byte, h *Header) error
- func SchemaHash(fields []FieldDescriptor) [32]byte
- type AccessPattern
- type FieldDef
- type FieldDescriptor
- type FieldLayout
- type FieldType
- type Header
- type RecordLayout
- type Region
- type Store
- func (s *Store) Append() (int, error)
- func (s *Store) Cap() int
- func (s *Store) Close() error
- func (s *Store) Len() int
- func (s *Store) ReadBool(idx int, offset uint32) (bool, error)
- func (s *Store) ReadBytes(idx int, offset, fieldSize, maxSize uint32) ([]byte, error)
- func (s *Store) ReadFloat32(idx int, offset uint32) (float32, error)
- func (s *Store) ReadFloat64(idx int, offset uint32) (float64, error)
- func (s *Store) ReadInt8(idx int, offset uint32) (int8, error)
- func (s *Store) ReadInt16(idx int, offset uint32) (int16, error)
- func (s *Store) ReadInt32(idx int, offset uint32) (int32, error)
- func (s *Store) ReadInt64(idx int, offset uint32) (int64, error)
- func (s *Store) ReadString(idx int, offset, fieldSize, maxSize uint32) (string, error)
- func (s *Store) ReadUint8(idx int, offset uint32) (uint8, error)
- func (s *Store) ReadUint16(idx int, offset uint32) (uint16, error)
- func (s *Store) ReadUint32(idx int, offset uint32) (uint32, error)
- func (s *Store) ReadUint64(idx int, offset uint32) (uint64, error)
- func (s *Store) SeqBeginWrite(idx int)
- func (s *Store) SeqEndWrite(idx int)
- func (s *Store) SeqReadBegin(idx int) uint64
- func (s *Store) SeqReadValid(idx int, seq uint64) bool
- func (s *Store) Sync() error
- func (s *Store) WriteBool(idx int, offset uint32, val bool) error
- func (s *Store) WriteBytes(idx int, offset, fieldSize, maxSize uint32, val []byte) error
- func (s *Store) WriteFloat32(idx int, offset uint32, val float32) error
- func (s *Store) WriteFloat64(idx int, offset uint32, val float64) error
- func (s *Store) WriteInt8(idx int, offset uint32, val int8) error
- func (s *Store) WriteInt16(idx int, offset uint32, val int16) error
- func (s *Store) WriteInt32(idx int, offset uint32, val int32) error
- func (s *Store) WriteInt64(idx int, offset uint32, val int64) error
- func (s *Store) WriteString(idx int, offset, fieldSize, maxSize uint32, val string) error
- func (s *Store) WriteUint8(idx int, offset uint32, val uint8) error
- func (s *Store) WriteUint16(idx int, offset uint32, val uint16) error
- func (s *Store) WriteUint32(idx int, offset uint32, val uint32) error
- func (s *Store) WriteUint64(idx int, offset uint32, val uint64) error
Constants ¶
const DefaultMaxVA = 1 << 30
DefaultMaxVA is the fallback virtual address reservation when no reserveVA is passed to Map. Set low because callers should always provide an explicit value (e.g. StoreReserveVA). The actual reservation is clamped to at least the page-aligned file size, so this only controls headroom for future growth.
const HeaderSize = 64
HeaderSize is the fixed size of the file header in bytes.
const MagicString = "MMFG"
MagicString is the string form of Magic for display purposes.
const SeqFieldSize = 8
const StoreReserveVA = 1 << 30
StoreReserveVA is the default virtual address reservation for Store files (1 GB).
const Version uint32 = 1
Version is the current binary format version.
Variables ¶
var ( ErrSchemaMismatch = errors.New("mmapforge: schema hash mismatch") ErrOutOfBounds = errors.New("mmapforge: index out of bounds") ErrCorrupted = errors.New("mmapforge: file corrupted") ErrBadMagic = errors.New("mmapforge: invalid magic bytes") ErrStringTooLong = errors.New("mmapforge: string exceeds max size") ErrBytesTooLong = errors.New("mmapforge: bytes exceeds max size") ErrReadOnly = errors.New("mmapforge: store is read-only") ErrClosed = errors.New("mmapforge: store is closed") ErrInvalidBool = errors.New("mmapforge: invalid bool value") ErrTypeMismatch = errors.New("mmapforge: field type changed during migration") )
Sentinel errors returned by Store and Region operations.
var Magic = [4]byte{'M', 'M', 'F', 'G'}
Magic is the 4-byte file signature written at the start of every mmapforge file.
Functions ¶
func EncodeHeader ¶
EncodeHeader writes h into the first 64 bytes of dst.
func SchemaHash ¶
func SchemaHash(fields []FieldDescriptor) [32]byte
SchemaHash computes the SHA-256 of a canonical field descriptor string. Fields are sorted by name so the hash is layout-order-independent
Types ¶
type AccessPattern ¶
type AccessPattern int
hints to the kernel about how we plan to read the mapped region when you touch a mapped page the OS loads it from disk on demand ("page fault") if we tell it our pattern ahead of time it can prefetch smarter
const ( // Sequential <- we read front to back; kernel will aggressively prefetch ahead Sequential AccessPattern = iota // Random <- we jump around; kernel skips prefetch, keeps more pages cached instead Random )
type FieldDescriptor ¶
FieldDescriptor is the canonical representation of a field for schema hashing.
type FieldLayout ¶
FieldLayout is the output: a field with its computed offset and size.
type Header ¶
type Header struct {
Magic [4]byte
FormatVersion uint32
SchemaHash [32]byte
SchemaVersion uint32
RecordSize uint32
RecordCount uint64
Capacity uint64
}
Header is the 64-byte metadata block at the start of every mmapforge file.
func DecodeHeader ¶
DecodeHeader reads the first 64 bytes of src into a Header.
type RecordLayout ¶
type RecordLayout struct {
Fields []FieldLayout
RecordSize uint32
}
RecordLayout is the complete layout for one struct.
func ComputeLayout ¶
func ComputeLayout(fields []FieldDef) (*RecordLayout, error)
ComputeLayout takes field definitions in declaration order and returns the byte layout with proper alignment. Returns an error if any field definition is invalid.
The first 8 bytes of every record are reserved for the seqlock sequence counter. User fields start at offset 8.
func (*RecordLayout) Descriptors ¶
func (r *RecordLayout) Descriptors() []FieldDescriptor
Descriptors converts the layout to FieldDescriptors for schema hashing
type Region ¶
type Region struct {
// contains filtered or unexported fields
}
Region is a page-aligned, memory-mapped view of a file with a stable base address. A large virtual address range is reserved up front with PROT_NONE. The file is mapped over the start of that range using MAP_FIXED. On Grow the file is extended and remapped at the same base address, so pointers and slices obtained from Slice remain valid as long as they fall within the previously mapped size.
Owns the underlying *os.File. Safe for concurrent reads after Map returns.
func Map ¶
func Map(f *os.File, size int, writable bool, access AccessPattern, reserveVA ...int) (*Region, error)
Map opens a memory-mapped view of f starting at offset 0.
A virtual address range of maxVA bytes is reserved (PROT_NONE, anonymous). The file is then mapped over the first `size` bytes of that reservation using MAP_FIXED|MAP_SHARED. If the file is smaller than the requested size it is extended via Truncate.
reserveVA must be >= size. Pass 0 to use DefaultMaxVA.
Caller must call Close when done.
func (*Region) Grow ¶
Grow remaps the file to at least minSize bytes (page-aligned) at the same base address using MAP_FIXED. No-op if already large enough.
Because the base address never changes, slices from previous Slice calls remain valid (they point into the same VA range). New pages beyond the old size become accessible after Grow returns.
Must be externally serialized (Store.appendMu).
func (*Region) Slice ¶
Slice returns the mmap byte range [off, off+n) from the stable base. Out-of-range panics on purpose so layout bugs surface fast. Valid for the lifetime of the Region (base address never changes).
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the base mmap-backed record store.
func CreateStore ¶
func CreateStore(path string, layout *RecordLayout, schemaVersion uint32) (*Store, error)
CreateStore creates a new mmapforge file at path with the given layout and schema version.
func OpenStore ¶
func OpenStore(path string, layout *RecordLayout) (*Store, error)
OpenStore opens an existing mmapforge file and validates the schema hash.
func (*Store) Close ¶
Close syncs and closes the store. All references into store memory become invalid.
func (*Store) ReadBytes ¶
ReadBytes returns a zero-copy byte slice from the mmap region. The returned slice is valid only until Close() is called.
func (*Store) ReadFloat32 ¶
ReadFloat32 reads a float32 from record idx at the given byte offset.
func (*Store) ReadFloat64 ¶
ReadFloat64 reads a float64 from record idx at the given byte offset.
func (*Store) ReadString ¶
ReadString returns a zero-copy string from the mmap region. The returned string is valid only until Close() is called. fieldSize is the total field size
func (*Store) ReadUint16 ¶
ReadUint16 reads a uint16 from record idx at the given byte offset.
func (*Store) ReadUint32 ¶
ReadUint32 reads a uint32 from record idx at the given byte offset.
func (*Store) ReadUint64 ¶
ReadUint64 reads a uint64 from record idx at the given byte offset.
func (*Store) SeqBeginWrite ¶
SeqBeginWrite marks the start of a write to record idx. Increments the 8-byte sequence counter at offset 0 of the record to an odd value. Caller must call SeqEndWrite when the write is complete.
func (*Store) SeqEndWrite ¶
SeqEndWrite marks the end of a write to record idx. Increments the sequence counter to an even value.
func (*Store) SeqReadBegin ¶
SeqReadBegin loads the sequence counter for record idx. If the value is odd, a write is in progress and the caller should spin.
func (*Store) SeqReadValid ¶
SeqReadValid returns true if seq is even (no write in progress) and the current counter still matches seq (no write happened during the read).
func (*Store) WriteBytes ¶
WriteBytes writes a length-prefixed byte slice into the field, zero-padding the remainder.
func (*Store) WriteFloat32 ¶
WriteFloat32 writes a float32 to record idx at the given byte offset.
func (*Store) WriteFloat64 ¶
WriteFloat64 writes a float64 to record idx at the given byte offset.
func (*Store) WriteInt16 ¶
WriteInt16 writes an int16 to record idx at the given byte offset.
func (*Store) WriteInt32 ¶
WriteInt32 writes an int32 to record idx at the given byte offset.
func (*Store) WriteInt64 ¶
WriteInt64 writes an int64 to record idx at the given byte offset.
func (*Store) WriteString ¶
WriteString writes a length-prefixed string into the field, zero-padding the remainder.
func (*Store) WriteUint8 ¶
WriteUint8 writes a uint8 to record idx at the given byte offset.
func (*Store) WriteUint16 ¶
WriteUint16 writes a uint16 to record idx at the given byte offset.
func (*Store) WriteUint32 ¶
WriteUint32 writes a uint32 to record idx at the given byte offset.