_pager

package
v0.0.0-...-fc425af Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinRecordSize = pageSlotSize
	MaxRecordSize = pageSize - pageHeaderSize - pageSlotSize
)

Variables

View Source
var (
	ErrBadAlignmentSize        = errors.New("pageManagerFile: bad alignment size")
	ErrNoMoreRoomInPage        = errors.New("Page: there is not enough room left in the Page")
	ErrInvalidRecordID         = errors.New("Page: invalid record id")
	ErrRecordHasBeenMarkedFree = errors.New("Page: record has been marked free (aka, removed)")
	ErrRecordNotFound          = errors.New("Page: record could not be found")
	ErrPageNotFound            = errors.New("pageManagerFile: Page could not be found")
	ErrWritingPage             = errors.New("pageManagerFile: error writing Page")
	ErrDeletingPage            = errors.New("pageManagerFile: error deleting Page")
	ErrMinRecordSize           = errors.New("Page: record is smaller than the min record size allowed")
	ErrMaxRecordSize           = errors.New("Page: record is larger than the max record size allowed")
	ErrRecordMaxKeySize        = errors.New("record: record key is longer than max size allowed (255)")
	ErrPageIsNotOverflow       = errors.New("pagemanager: error Page is not an overflow Page")
)

Functions

func BinarySearch

func BinarySearch(set []int, find int) int

func BtoGB

func BtoGB(b uint64) uint64

func BtoKB

func BtoKB(b uint64) uint64

func BtoMB

func BtoMB(b uint64) uint64

func Pack2U32

func Pack2U32(dst *uint64, src1, src2 uint32)

func PrintMemUsage2

func PrintMemUsage2()

PrintMemUsage outputs the current, total and OS memory being used. As well as the number of garage collection cycles completed.

func PrintStats

func PrintStats(mem runtime.MemStats)

func PrintStatsTab

func PrintStatsTab(mem runtime.MemStats)

func Remove

func Remove(path string) error

Remove is somewhat of a helper function to facilitate easier removing of a PageManager

func Sizeof

func Sizeof(objs ...interface{}) (sz uint64)

Sizeof returns the estimated memory usage of object(s) not just the size of the type. On 64bit Sizeof("test") == 12 (8 = sizeof(StringHeader) + 4 bytes).

func Unpack2U32

func Unpack2U32(dst *uint64) (uint32, uint32)

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a page table cache [https://en.wikipedia.org/wiki/Page_table]

func (*Cache) Lookup

func (c *Cache) Lookup(pid uint32) *Page

Lookup attempts to acquire a page if it is already in the in-memory cache. It does not read the page from disk. It will return nil if it is not found.

func (*Cache) ReadPage

func (c *Cache) ReadPage(pid uint32) error

ReadPage attempts to read the Page located at the offset calculated by the provided pageID. It returns an error if a Page could not be located

func (*Cache) SwapOut

func (c *Cache) SwapOut()

SwapOut writes from memory to the disk

type File

type File struct {
	// contains filtered or unexported fields
}

File is mainly just a synchronized abstraction on top of a PageManager

func (*File) Close

func (f *File) Close() error

func (*File) Range

func (f *File) Range(fn func(rid *RecordID) bool) error

func (*File) Read

func (f *File) Read(recordID *RecordID) ([]byte, error)

func (*File) Save

func (f *File) Save() error

func (*File) Write

func (f *File) Write(record []byte) (*RecordID, error)

Write attempts to write data with as little hassle as possible

type LRU

type LRU struct {
	// contains filtered or unexported fields
}

func NewLRU

func NewLRU(c int) *LRU

func (*LRU) Get

func (l *LRU) Get(k keyType) (valType, bool)

func (*LRU) Set

func (l *LRU) Set(k keyType, v valType)

type Page

type Page struct {
	// contains filtered or unexported fields
}

Page is a pageSized data Page structure that may contain one or more data records

func LinkPages

func LinkPages(a, b *Page) *Page

LinkPages links Page "a" with Page "b"; they are marked as overflow pages and have their nextPageID and prevPageID linked to each other. The next and prev pageID's can be used to traverse linked pages in the same fashion that a linked list allows you to traverse nodes.

func NewPage

func NewPage(pid uint32) *Page

NewPage is a new Page constructor that creates and returns a new *Page

func (*Page) AddRecord

func (p *Page) AddRecord(r []byte) (*RecordID, error)

AddRecord adds a new record to the Page, if there is not enough room for the record to fit within the Page or the remaining Page's available space, an error will be returned.

**It should be noted that (on insertion of a record) all pages slots are sorted lexicography by the prefix of the record data that they point to.

func (*Page) CheckRecord

func (p *Page) CheckRecord(recordSize uint16) error

CheckRecord checks if there is room for the record but, it also checks if the recordSize is outside the bounds of the minimum or maximum record size and returns an applicable error if so

func (*Page) DelRecord

func (p *Page) DelRecord(rid *RecordID) error

DelRecord removes a record from a Page. It will preserve the slot for later use.

func (*Page) GetRecord

func (p *Page) GetRecord(rid *RecordID) ([]byte, error)

GetRecord attempts to return the record data for a record found within this *Page using the provided *RecordID. If the record cannot be located, nil data and an error will be returned

func (*Page) Len

func (p *Page) Len() int

Len is here to satisfy the sort interface for sorting the Page slots by the record prefix

func (*Page) Less

func (p *Page) Less(i, j int) bool

Less is here to satisfy the sort interface for sorting the Page slots by the record prefix

func (p *Page) Link(next *Page) *Page

Link links the calling Page to the next Page and is provided as an alternate method to LinkPages. All the same specs apply.

func (*Page) NextID

func (p *Page) NextID() uint32

NextID returns the current pageID

func (*Page) PageID

func (p *Page) PageID() uint32

PageID returns the current pageID

func (*Page) PrevID

func (p *Page) PrevID() uint32

PrevID returns the current pageID

func (*Page) Range

func (p *Page) Range(fn func(rid *RecordID) bool)

Range is a record iterator method for a Page's records

func (*Page) Reset

func (p *Page) Reset()

Reset resets the Page, all data and header information will return to the same state it was in when it was created.

func (*Page) SortRecords

func (p *Page) SortRecords()

SortRecords is a convenience wrapper for the internal sortSlotsByRecordPrefix call

func (*Page) String

func (p *Page) String() string

String is a Page stringer method

func (*Page) Swap

func (p *Page) Swap(i, j int)

Swap is here to satisfy the sort interface for sorting the Page slots by the record prefix

type PageBuffer

type PageBuffer struct {
	// contains filtered or unexported fields
}

func NewPageBuffer

func NewPageBuffer(pm *PageManager) (*PageBuffer, error)

func NewPageBufferSize

func NewPageBufferSize(pm *PageManager, np int) (*PageBuffer, error)

func (*PageBuffer) AddRecord

func (pb *PageBuffer) AddRecord(r []byte) (*RecordID, error)

func (*PageBuffer) Close

func (pb *PageBuffer) Close() error

func (*PageBuffer) DelRecord

func (pb *PageBuffer) DelRecord(rid *RecordID) error

func (*PageBuffer) DirtyPages

func (pb *PageBuffer) DirtyPages() int

func (*PageBuffer) Flush

func (pb *PageBuffer) Flush() error

func (*PageBuffer) FreeSpace

func (pb *PageBuffer) FreeSpace() int

func (*PageBuffer) GetRecord

func (pb *PageBuffer) GetRecord(rid *RecordID) ([]byte, error)

func (*PageBuffer) Range

func (pb *PageBuffer) Range(fn func(rid *RecordID) bool)

type PageManager

type PageManager struct {
	// contains filtered or unexported fields
}

PageManager is a slotted Page PageManager manager

func OpenPageManager

func OpenPageManager(path string) (*PageManager, error)

OpenPageManager opens an existing PageManager at the location provided, or creates and returns a new PageManager at the path provided.

func (*PageManager) AllocatePage

func (f *PageManager) AllocatePage() *Page

AllocatePage allocates and returns a new Page. The newly allocated Page is not persisted unless a call to WritePage is made

func (*PageManager) Close

func (f *PageManager) Close() error

Close closes the underlying PageManager, after flushing any buffers to disk.

func (*PageManager) DeletePage

func (f *PageManager) DeletePage(pid uint32) error

DeletePage marks the Page with the matching pageID provided as "free" and writes zeros to the underlying Page on disk

func (*PageManager) GetFreeOrAllocate

func (f *PageManager) GetFreeOrAllocate() *Page

GetFreeOrAllocate attempts to find a free Page (a Page that is not in use that can be reused) and if one cannot be found, it will allocate and return a new one. Any alterations to the returned Page are not persisted unless a call to WritePage is made

func (*PageManager) GetFreePageIDs

func (f *PageManager) GetFreePageIDs() []uint32

GetFreePageIDs returns a list of any Page id's that are marked "free"

func (*PageManager) PageCount

func (f *PageManager) PageCount() int

PageCount returns the total number of pages in the PageManager (including "free" pages)

func (*PageManager) Range

func (f *PageManager) Range(start uint32, fn func(rid *RecordID) bool)

func (*PageManager) ReadPage

func (f *PageManager) ReadPage(pid uint32) (*Page, error)

ReadPage attempts to read the Page located at the offset calculated by the provided pageID. It returns an error if a Page could not be located

func (*PageManager) ReadPages

func (f *PageManager) ReadPages(pid uint32) ([]*Page, error)

ReadPages attempts to read the pages located at the offset calculated by the provided pageID. It returns an error if a Page could not be located

func (*PageManager) WritePage

func (f *PageManager) WritePage(p *Page) error

WritePage writes the provided Page to the underlying PageManager on disk. If something goes wrong it returns a non-nil error

func (*PageManager) WritePages

func (f *PageManager) WritePages(ps []*Page) error

WritePages writes the provided pages to the underlying PageManager on disk. If something goes wrong it returns a non-nil error

type PageReader

type PageReader interface {
	ReadPage(p []byte, off int64) (n int, err error)
}

PageReader reads a pageSize of bytes into p starting at offset off in the underlying input source. It returns the number of bytes read and any error encountered

If ReadPage is reading from an input source with a seek offset, ReadPage should not affect nor be affected by the underlying seek offset.

type PageWriter

type PageWriter interface {
	WritePage(p []byte, off int64) (n int, err error)
}

PageWriter writes a pageSize of bytes from p to the under- lying data stream at offset off. It returns the number of bytes written from p and any error encountered that caused the write to stop early. WritePage must return a non-nil error if it returns n < a pageSize.

If WritePage is writing to a destination with a seek offset, WritePage should not affect nor be affected by the underlying seek offset.

type RecordID

type RecordID struct {
	PageID uint32
	SlotID uint16
}

RecordID represents the unique id for a single data record held within a Page

Jump to

Keyboard shortcuts

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