util

package
v0.0.0-...-41febbd Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DSync indicates that O_DSYNC should be set on the underlying file,
	// ensuring that data writes do not return until the data is flushed
	// to disk.
	DSync = 1 << iota
	// ReadOnly opens the underlying file on a read-only basis.
	ReadOnly
)
View Source
const (
	// MaxNodeSize is the memory footprint of a node of maximum height.
	MaxNodeSize = int(unsafe.Sizeof(node{}))
)

Variables

View Source
var (
	// LSMSize has size of the LSM in bytes
	LSMSize *expvar.Map
	// VlogSize has size of the value log in bytes
	VlogSize *expvar.Map
	// PendingWrites tracks the number of pending writes.
	PendingWrites *expvar.Map

	// NumReads has cumulative number of reads
	NumReads *expvar.Int
	// NumWrites has cumulative number of writes
	NumWrites *expvar.Int
	// NumBytesRead has cumulative number of bytes read
	NumBytesRead *expvar.Int
	// NumBytesWritten has cumulative number of bytes written
	NumBytesWritten *expvar.Int
	// NumLSMGets is number of LMS gets
	NumLSMGets *expvar.Map
	// NumLSMBloomHits is number of LMS bloom hits
	NumLSMBloomHits *expvar.Map
	// NumGets is number of gets
	NumGets *expvar.Int
	// NumPuts is number of puts
	NumPuts *expvar.Int
	// NumBlockedPuts is number of blocked puts
	NumBlockedPuts *expvar.Int
	// NumMemtableGets is number of memtable gets
	NumMemtableGets *expvar.Int
)
View Source
var (

	// CastagnoliCrcTable is a CRC32 polynomial table
	CastagnoliCrcTable = crc32.MakeTable(crc32.Castagnoli)
)
View Source
var ErrEOF = errors.New("End of mapped region")

ErrEOF indicates an end of file when trying to read from a memory mapped file and encountering the end of slice.

Functions

func AssertTrue

func AssertTrue(b bool)

AssertTrue asserts that b is true. Otherwise, it would log fatal.

func AssertTruef

func AssertTruef(b bool, format string, args ...interface{})

AssertTruef is AssertTrue with extra info.

func Check

func Check(err error)

Check logs fatal if err != nil.

func Check2

func Check2(_ interface{}, err error)

Check2 acts as convenience wrapper around Check, using the 2nd argument as error.

func CompareKeys

func CompareKeys(key1, key2 []byte) int

CompareKeys checks the key without timestamp and checks the timestamp if keyNoTs is same. a<timestamp> would be sorted higher than aa<timestamp> if we use bytes.compare All keys should have timestamp.

func Copy

func Copy(a []byte) []byte

Copy copies a byte slice and returns the copied slice.

func CreateSyncedFile

func CreateSyncedFile(filename string, sync bool) (*os.File, error)

CreateSyncedFile creates a new file (using O_EXCL), errors if it already existed.

func FixedDuration

func FixedDuration(d time.Duration) string

FixedDuration returns a string representation of the given duration with the hours, minutes, and seconds.

func KeyWithTs

func KeyWithTs(key []byte, ts uint64) []byte

KeyWithTs generates a new key by appending ts to key.

func Lock

func Lock(b []byte) error

Lock locks the maped slice, preventing it from being swapped out.

func Madvise

func Madvise(b []byte) error

Madvise advises the kernel about how to handle the mapped slice.

func Mmap

func Mmap(fd *os.File, offset, length int64, writable bool) ([]byte, error)

Mmap use the mmap system call to memory mapped file or device.

func Munmap

func Munmap(b []byte) error

Munmap unmaps mapped slice, this will also flush any remaining changes.

func OpenExistingFile

func OpenExistingFile(filename string, flags uint32) (*os.File, error)

OpenExistingFile opens an existing file, errors if it doesn't exist.

func OpenSyncedFile

func OpenSyncedFile(filename string, sync bool) (*os.File, error)

OpenSyncedFile creates the file if one doesn't exist.

func OpenTruncFile

func OpenTruncFile(filename string, sync bool) (*os.File, error)

OpenTruncFile opens the file with O_RDWR | O_CREATE | O_TRUNC

func ParseKey

func ParseKey(key []byte) []byte

ParseKey parses the actual key from the key bytes.

func ParseTs

func ParseTs(key []byte) uint64

ParseTs parses the timestamp from the key bytes.

func SafeCopy

func SafeCopy(a, src []byte) []byte

SafeCopy does append(a[:0], src...).

func SameKey

func SameKey(src, dst []byte) bool

SameKey checks for key equality ignoring the version timestamp suffix.

func Sync

func Sync(b []byte) error

Sync flushes mmap slice's all changes back to the device.

func SyncDir

func SyncDir(dir string) error

SyncDir When you create or delete a file, you have to ensure the directory entry for the file is synced in order to guarantee the file is visible (if the system crashes). (See the man page for fsync, or see https://github.com/coreos/etcd/issues/6368 for an example.)

func Unlock

func Unlock(b []byte) error

Unlock unlocks the mapped slice, allowing it to swap out again.

func Wrap

func Wrap(err error) error

Wrap wraps errors from external lib.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf is Wrap with extra info.

Types

type Allocator

type Allocator interface {
	Allocate(n uint32) []byte
	AllocateAligned(n, align uint32) []byte
	AllocateSlot(n uint32) (begin, end uint32)
	AllocateSlotAligned(n, align uint32) (begin, end uint32)
	Acquire(offset, size uint32) []byte
	Size() int64
	Reset()
}

Allocator the interface of allocator

type Arena

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

Arena should be lock-free.

func NewArena

func NewArena(n int64) *Arena

NewArena returns a new arena.

func (*Arena) Acquire

func (a *Arena) Acquire(offset, size uint32) []byte

Acquire get the byte by given offset and size. it will painc when index out of slice.

func (*Arena) Allocate

func (a *Arena) Allocate(n uint32) []byte

Allocate allocs n bytes of slice from the buffer. If the free space isn't enough, it will panic.

func (*Arena) AllocateAligned

func (a *Arena) AllocateAligned(n, align uint32) []byte

AllocateAligned align allocs n bytes of slice from the buffer. If the free space isn't enough, it will panic.

func (*Arena) AllocateSlot

func (a *Arena) AllocateSlot(n uint32) (begin, end uint32)

AllocateSlot allocate n bytes return the pos begin and end. If the free space isn't enough, it will panic.

func (*Arena) AllocateSlotAligned

func (a *Arena) AllocateSlotAligned(n, align uint32) (begin, end uint32)

AllocateSlotAligned allocate aligned n bytes return the pos begin and end. If the free space isn't enough, it will panic.

func (*Arena) Reset

func (a *Arena) Reset()

Reset reset arena.

func (*Arena) Size

func (a *Arena) Size() int64

Size return used mem size.

type Iterator

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

Iterator is an iterator over skiplist object. For new objects, you just need to initialize Iterator.list.

func (*Iterator) Close

func (s *Iterator) Close() error

Close frees the resources held by the iterator

func (*Iterator) Key

func (s *Iterator) Key() []byte

Key returns the key at the current position.

func (*Iterator) Next

func (s *Iterator) Next()

Next advances to the next position.

func (*Iterator) Prev

func (s *Iterator) Prev()

Prev advances to the previous position.

func (*Iterator) Seek

func (s *Iterator) Seek(key []byte)

Seek advances to the first entry with a key >= target.

func (*Iterator) SeekForPrev

func (s *Iterator) SeekForPrev(key []byte)

SeekForPrev finds an entry with key <= target.

func (*Iterator) SeekToFirst

func (s *Iterator) SeekToFirst()

SeekToFirst seeks position at the first entry in list. Final state of iterator is Valid() iff list is not empty.

func (*Iterator) SeekToLast

func (s *Iterator) SeekToLast()

SeekToLast seeks position at the last entry in list. Final state of iterator is Valid() iff list is not empty.

func (*Iterator) Valid

func (s *Iterator) Valid() bool

Valid returns true iff the iterator is positioned at a valid node.

func (*Iterator) Value

func (s *Iterator) Value() []byte

Value returns value.

type Option

type Option func(s *Skiplist)

Option used to init user defined params

func KeyComparator

func KeyComparator(keyComparator func(key1, key2 []byte) int) Option

KeyComparator user defined key comparator

func KeyEqualizer

func KeyEqualizer(keyEqualizer func(key1, key2 []byte) bool) Option

KeyEqualizer user defined key euqalizer used to get.

type Skiplist

type Skiplist struct {
	KeyComparator func(key1, key2 []byte) int
	KeyEqualizer  func(key1, key2 []byte) bool
	// contains filtered or unexported fields
}

Skiplist maps keys to values (in memory)

func NewSkiplist

func NewSkiplist(arenaSize int64, options ...Option) *Skiplist

NewSkiplist makes a new empty skiplist, with a given arena size

func (*Skiplist) DecrRef

func (s *Skiplist) DecrRef()

DecrRef decrements the refcount, deallocating the Skiplist when done using it

func (*Skiplist) Empty

func (s *Skiplist) Empty() bool

Empty returns if the Skiplist is empty.

func (*Skiplist) Get

func (s *Skiplist) Get(key []byte) []byte

Get gets the value associated with the key. It returns a valid value if it finds equal or earlier version of the same key.

func (*Skiplist) GetWithKey

func (s *Skiplist) GetWithKey(key []byte) (nkey, value []byte)

GetWithKey gets the value associated with the key. It returns a valid key and value if it finds equal or earlier version of the same key, it can be used impl multi version key.

func (*Skiplist) IncrRef

func (s *Skiplist) IncrRef()

IncrRef increases the refcount

func (*Skiplist) MemSize

func (s *Skiplist) MemSize() int64

MemSize returns the size of the Skiplist in terms of how much memory is used within its internal allocator.

func (*Skiplist) NewIterator

func (s *Skiplist) NewIterator() *Iterator

NewIterator returns a skiplist iterator. You have to Close() the iterator.

func (*Skiplist) NewUniIterator

func (s *Skiplist) NewUniIterator(reversed bool) *UniIterator

NewUniIterator returns a UniIterator.

func (*Skiplist) Put

func (s *Skiplist) Put(key, value []byte)

Put inserts the key-value pair.

type Slice

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

Slice holds a reusable buf, will reallocate if you request a larger size than ever before. One problem is with n distinct sizes in random order it'll reallocate log(n) times.

func (*Slice) Resize

func (s *Slice) Resize(sz int) []byte

Resize reuses the Slice's buffer (or makes a new one) and returns a slice in that buffer of length sz.

type Throttle

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

Throttle allows a limited number of workers to run at a time. It also provides a mechanism to check for errors encountered by workers and wait for them to finish.

func NewThrottle

func NewThrottle(max int) *Throttle

NewThrottle creates a new throttle with a max number of workers.

func (*Throttle) Do

func (t *Throttle) Do() error

Do should be called by workers before they start working. It blocks if there are already maximum number of workers working. If it detects an error from previously Done workers, it would return it.

func (*Throttle) Done

func (t *Throttle) Done(err error)

Done should be called by workers when they finish working. They can also pass the error status of work done.

func (*Throttle) Finish

func (t *Throttle) Finish() error

Finish waits until all workers have finished working. It would return any error passed by Done. If Finish is called multiple time, it will wait for workers to finish only once(first time). From next calls, it will return same error as found on first call.

type UniIterator

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

UniIterator is a unidirectional memtable iterator. It is a thin wrapper around Iterator. We like to keep Iterator as before, because it is more powerful and we might support bidirectional iterators in the future.

func (*UniIterator) Close

func (s *UniIterator) Close() error

Close implements y.Interface (and frees up the iter's resources)

func (*UniIterator) Key

func (s *UniIterator) Key() []byte

Key implements y.Interface

func (*UniIterator) Next

func (s *UniIterator) Next()

Next implements y.Interface

func (*UniIterator) Rewind

func (s *UniIterator) Rewind()

Rewind implements y.Interface

func (*UniIterator) Seek

func (s *UniIterator) Seek(key []byte)

Seek implements y.Interface

func (*UniIterator) Valid

func (s *UniIterator) Valid() bool

Valid implements y.Interface

func (*UniIterator) Value

func (s *UniIterator) Value() []byte

Value implements y.Interface

type WaterMark

type WaterMark struct {
	Name string
	// contains filtered or unexported fields
}

WaterMark is used to keep track of the minimum un-finished index. Typically, an index k becomes finished or "done" according to a WaterMark once Done(k) has been called

  1. as many times as Begin(k) has, AND
  2. a positive number of times.

An index may also become "done" by calling SetDoneUntil at a time such that it is not inter-mingled with Begin/Done calls.

Since doneUntil and lastIndex addresses are passed to sync/atomic packages, we ensure that they are 64-bit aligned by putting them at the beginning of the structure.

func NewWaterMark

func NewWaterMark(name string) *WaterMark

NewWaterMark return a instance of watermark

func (*WaterMark) Begin

func (w *WaterMark) Begin(index uint64)

Begin sets the last index to the given value.

func (*WaterMark) BeginMany

func (w *WaterMark) BeginMany(indices []uint64)

BeginMany works like Begin but accepts multiple indices.

func (*WaterMark) Close

func (w *WaterMark) Close() error

Close the watermark, it will stop the process.

func (*WaterMark) Done

func (w *WaterMark) Done(index uint64)

Done sets a single index as done.

func (*WaterMark) DoneMany

func (w *WaterMark) DoneMany(indices []uint64)

DoneMany works like Done but accepts multiple indices.

func (*WaterMark) DoneUntil

func (w *WaterMark) DoneUntil() uint64

DoneUntil returns the maximum index that has the property that all indices less than or equal to it are done.

func (*WaterMark) LastIndex

func (w *WaterMark) LastIndex() uint64

LastIndex returns the last index for which Begin has been called.

func (*WaterMark) SetDoneUntil

func (w *WaterMark) SetDoneUntil(val uint64)

SetDoneUntil sets the maximum index that has the property that all indices less than or equal to it are done.

func (*WaterMark) WaitForMark

func (w *WaterMark) WaitForMark(ctx context.Context, index uint64) error

WaitForMark waits until the given index is marked as done.

Jump to

Keyboard shortcuts

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