mmap

package
v0.0.0-...-0f72fe8 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2017 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	PAGE_SIZE               = 4096
	PAGE_USE_OFFSET         = 4094
	PAGE_LOCAL_DEPTH_OFFSET = 4092
)
View Source
const (
	RECORD_HEADER_SIZE       = 2
	RECORD_TOTAL_HEADER_SIZE = 4
)
View Source
const (
	FILE_NAME         = "bluck.data"
	META_FILE_NAME    = "bluck.meta"
	DB_DEFAULT_FOLDER = "/tmp/"
)

Variables

This section is empty.

Functions

func EncodeMeta

func EncodeMeta(dir *Directory) *bytes.Buffer

func FindBucketNumber

func FindBucketNumber(fileSize int64) int64

func FindTwoToPowerOfN

func FindTwoToPowerOfN(v uint) uint

func NextPowerOfTwo

func NextPowerOfTwo(v uint) uint

Types

type ByteRecord

type ByteRecord []byte

A Record that is mapped on a byte slice

func (ByteRecord) Write

func (r ByteRecord) Write(key, val string)

Record order = [key, value, lenVal, lenKey] Write does it fast because it skips "String to Byte Slice conversion", using copy(dst, src[]) special case with string

type Directory

type Directory struct {
	Table []int `json:"table"`

	Gd uint `json:"globalDepth"`

	LastPageId int `json:"LastPageId"`
	// contains filtered or unexported fields
}

func DecodeMeta

func DecodeMeta(buff *bytes.Buffer) *Directory

func (*Directory) String

func (dir *Directory) String() string

type MmapKVStore

type MmapKVStore struct {
	Dir  *Directory
	Path string
}

func (*MmapKVStore) Close

func (s *MmapKVStore) Close()

Close must be called at the end of the connection with the store, to persist safely the data and to persist metadata on disk. The persistence of the metadata is only done in this method. Usage : defer store.Close()

func (*MmapKVStore) DumpPage

func (s *MmapKVStore) DumpPage(pageId int) string

func (*MmapKVStore) Get

func (s *MmapKVStore) Get(k string) string

func (*MmapKVStore) Meta

func (s *MmapKVStore) Meta() *Directory

func (*MmapKVStore) Open

func (store *MmapKVStore) Open(absolutePath string)

Open create the datafile and the metadata file if they do not exist. Else if they exist, it loads from the disk and mmap the datafile.

func (*MmapKVStore) Put

func (s *MmapKVStore) Put(k, v string) error

Put inserts data into the memory which is mapped on disk, but does not persit the metadata If the store crashes in an inconsistent way (metadata != data), you need to use the recovery tool (RestoreMETA func)

func (*MmapKVStore) RestoreMETA

func (s *MmapKVStore) RestoreMETA()

func (*MmapKVStore) Rm

func (s *MmapKVStore) Rm(absolutePath string)

type Page

type Page []byte

A page is an array of 4096 bytes (the same size as the SSD Hard Drive default configuration on Linux)

from 4092 to 4094, 2 bytes to store local depth (ld) from 4094 to 4096, 2 bytes to store the number of bytes used

func (Page) Gc

func (p Page) Gc() Page

func (Page) Get

func (p Page) Get(k string) (v string, err error)

Get iterates through the page using PageIterator with cursor setted to p.Use(). It compares the key length first, if it matches it compares all bytes and then returns the value. It begins to watch the last record because the page is append only, so the last version of the value for a key is more likely to be near the end

func (Page) Put

func (p Page) Put(k, v string) error

Put writes key and value on disk, within the Page boundaries. It checks if the page's available space is more than the record size. If so, it writes record after the last offset given by page.use() If not, it returns an error. There is no lookup in this function to know if the key already exists. It is append only, so the duplicates will be garbage collected during the next split of the page.

func (Page) Use

func (p Page) Use() int

Read the Trailer of the Page where Use is stored (4094). Do LittleEndian deserialization on a 2 bytes slice

type PageIterator

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

Implementation of pattern Iterator to scan a Page The scan is made in reverse, from the last byte to the first (right to left).

func (*PageIterator) HasNext

func (it *PageIterator) HasNext() bool

Return false when there is no other record to read. Return true if the cursor has found the beginning of the Page

func (*PageIterator) Next

func (it *PageIterator) Next() ByteRecord

Return the next pointer to the byte array casted in a ByteRecord struct (provides methods). Use an cursor to know the current Record index in the Page. Update the cursor (current) to minus the payload of the Record

type Record

type Record interface {
	// contains filtered or unexported methods
}

This interface is for documentation purpose only. A Record is composed of two byte arrays for key and value data, and footers that contain meta info for variable length data. In theory, A key or a value could not excead 16384 bytes length (uint16 is used to store length). But at the moment, there is no logic to handle a record length higher than a Page (4096 bytes)

type RecordWriter

type RecordWriter interface {
	Write(key, val string)
}

Jump to

Keyboard shortcuts

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