storage

package
v0.0.0-...-867e715 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package storage implements an off-heap data store which is memory within the runtime that is not subject to Go garbage collection.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrKeyTooLarge is an error that indicates the given key is large than the determined key size.
	// The current maximum key length is 256.
	ErrKeyTooLarge = errors.New("key too large")

	// ErrKeyNotFound is an error that indicates that the requested key could not be found in the DB.
	ErrKeyNotFound = errors.New("key not found")
)
View Source
var ErrFragmented = errors.New("storage fragmented")

ErrFragmented is an error that indicates this storage instance is currently fragmented and it cannot be serialized.

Functions

This section is empty.

Types

type Storage

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

Storage implements a new off-heap data store which uses built-in map to keep metadata and mmap syscall for allocating memory to store values. The allocated memory is not a subject of Golang's GC.

func Import

func Import(data []byte) (*Storage, error)

Import gets the serialized data by Export and creates a new storage instance.

func New

func New(size int) (*Storage, error)

New creates a new storage instance.

func (*Storage) Check

func (s *Storage) Check(hkey uint64) bool

Check checks the key existence.

func (*Storage) Close

func (s *Storage) Close() error

Close closes underlying tables and releases allocated memory with Munmap. It blocks until everything is done.

func (*Storage) Delete

func (s *Storage) Delete(hkey uint64) error

Delete deletes the value for the given key. Delete will not returns error if key doesn't exist.

func (*Storage) Export

func (s *Storage) Export() ([]byte, error)

Export serializes underlying data structes into a byte slice. It may return ErrFragmented if the tables are fragmented. If you get this error, you should try to call Export again some time later.

func (*Storage) Get

func (s *Storage) Get(hkey uint64) (*VData, error)

Get gets the value for the given key. It returns ErrKeyNotFound if the DB does not contains the key. The returned VData is its own copy, it is safe to modify the contents of the returned slice.

func (*Storage) GetRaw

func (s *Storage) GetRaw(hkey uint64) ([]byte, error)

GetRaw extracts un-decoded value for the given hkey. This is useful for merging tables or snapshots.

func (*Storage) Len

func (s *Storage) Len() int

Len returns the key cound in this storage.

func (*Storage) Put

func (s *Storage) Put(hkey uint64, value *VData) error

Put sets the value for the given key. It overwrites any previous value for that key

func (*Storage) PutRaw

func (s *Storage) PutRaw(hkey uint64, value []byte) error

PutRaw sets the raw value for the given key.

func (*Storage) Range

func (s *Storage) Range(f func(hkey uint64, vdata *VData) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

type VData

type VData struct {
	Key   string
	TTL   int64
	Value []byte
}

VData represents a value with its metadata.

func DecodeRaw

func DecodeRaw(raw []byte) *VData

DecodeRaw creates VData for given byte slice. It assumes that the given data is valid. Never returns an error.

Jump to

Keyboard shortcuts

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