Documentation ¶
Index ¶
- Constants
- Variables
- func Allocators() string
- func BytesToUint64Slice(b []byte) []uint64
- func CPUTicks() int64
- func Calloc(n int, tag string) []byte
- func CallocNoRef(n int, tag string) []byte
- func FastRand() uint32
- func Fibonacci(num int) []float64
- func Free(b []byte)
- func HistogramBounds(minExponent, maxExponent uint32) []float64
- func KeyToHash(key interface{}) (uint64, uint64)
- func Leaks() string
- func Madvise(b []byte, readahead bool) error
- func MemHash(data []byte) uint64
- func MemHashString(str string) uint64
- func Memclr(b []byte)
- func Mmap(fd *os.File, writable bool, size int64) ([]byte, error)
- func Msync(b []byte) error
- func Munmap(b []byte) error
- func NanoTime() int64
- func NumAllocBytes() int64
- func ReadMemStats(_ *MemStats)
- func SetTmpDir(dir string)
- func StatsPrint()
- func SyncDir(dir string) error
- func ZeroOut(dst []byte, start, end int)
- type Allocator
- func (a *Allocator) Allocate(sz int) []byte
- func (a *Allocator) AllocateAligned(sz int) []byte
- func (a *Allocator) Allocated() uint64
- func (a *Allocator) Copy(buf []byte) []byte
- func (a *Allocator) MaxAlloc() int
- func (a *Allocator) Release()
- func (a *Allocator) Reset()
- func (a *Allocator) Size() int
- func (a *Allocator) String() string
- func (a *Allocator) TrimTo(max int)
- type AllocatorPool
- type Bloom
- func (bl *Bloom) Add(hash uint64)
- func (bl *Bloom) AddIfNotHas(hash uint64) bool
- func (bl *Bloom) Clear()
- func (bl Bloom) Has(hash uint64) bool
- func (bl *Bloom) IsSet(idx uint64) bool
- func (bl Bloom) JSONMarshal() []byte
- func (bl *Bloom) Set(idx uint64)
- func (bl *Bloom) Size(sz uint64)
- func (bl *Bloom) TotalSize() int
- type Buffer
- func (b *Buffer) Allocate(n int) []byte
- func (b *Buffer) AllocateOffset(n int) int
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Data(offset int) []byte
- func (b *Buffer) Grow(n int)
- func (b *Buffer) IsEmpty() bool
- func (b *Buffer) LenNoPadding() int
- func (b *Buffer) LenWithPadding() int
- func (b *Buffer) Release() error
- func (b *Buffer) Reset()
- func (b *Buffer) Slice(offset int) ([]byte, int)
- func (b *Buffer) SliceAllocate(sz int) []byte
- func (b *Buffer) SliceIterate(f func(slice []byte) error) error
- func (b *Buffer) SliceOffsets() []int
- func (b *Buffer) SortSlice(less func(left, right []byte) bool)
- func (b *Buffer) SortSliceBetween(start, end int, less LessFunc)
- func (b *Buffer) StartOffset() int
- func (b *Buffer) WithAutoMmap(threshold int, path string) *Buffer
- func (b *Buffer) WithMaxSize(size int) *Buffer
- func (b *Buffer) Write(p []byte) (n int, err error)
- func (b *Buffer) WriteSlice(slice []byte)
- type BufferType
- type Closer
- type HistogramData
- func (histogram *HistogramData) Clear()
- func (histogram *HistogramData) Copy() *HistogramData
- func (histogram *HistogramData) Mean() float64
- func (histogram *HistogramData) Percentile(p float64) float64
- func (histogram *HistogramData) String() string
- func (histogram *HistogramData) Update(value int64)
- type LessFunc
- type MemStats
- type MmapFile
- func (m *MmapFile) AllocateSlice(sz, offset int) ([]byte, int, error)
- func (m *MmapFile) Bytes(off, sz int) ([]byte, error)
- func (m *MmapFile) Close(maxSz int64) error
- func (m *MmapFile) Delete() error
- func (m *MmapFile) NewReader(offset int) io.Reader
- func (m *MmapFile) Slice(offset int) []byte
- func (m *MmapFile) Sync() error
- func (m *MmapFile) Truncate(maxSz int64) error
- type SuperFlag
- func (sf *SuperFlag) GetBool(opt string) bool
- func (sf *SuperFlag) GetDuration(opt string) time.Duration
- func (sf *SuperFlag) GetFloat64(opt string) float64
- func (sf *SuperFlag) GetInt64(opt string) int64
- func (sf *SuperFlag) GetPath(opt string) string
- func (sf *SuperFlag) GetString(opt string) string
- func (sf *SuperFlag) GetUint32(opt string) uint32
- func (sf *SuperFlag) GetUint64(opt string) uint64
- func (sf *SuperFlag) Has(opt string) bool
- func (sf *SuperFlag) MergeAndCheckDefault(flag string) *SuperFlag
- func (sf *SuperFlag) String() string
- type SuperFlagHelp
- type Tree
- func (t *Tree) Close() error
- func (t *Tree) DeleteBelow(ts uint64)
- func (t *Tree) Get(k uint64) uint64
- func (t *Tree) Iterate(fn func(node))
- func (t *Tree) IterateKV(f func(key, val uint64) (newVal uint64))
- func (t *Tree) Print()
- func (t *Tree) Reset()
- func (t *Tree) Set(k, v uint64)
- func (t *Tree) Stats() TreeStats
- type TreeStats
Constants ¶
const ( // MaxArrayLen is a safe maximum length for slices on this architecture. MaxArrayLen = 1<<50 - 1 // MaxBufferSize is the size of virtually unlimited buffer on this architecture. MaxBufferSize = 256 << 30 )
Variables ¶
var NewFile = errors.New("Create a new file")
Functions ¶
func Allocators ¶ added in v0.1.0
func Allocators() string
func BytesToUint64Slice ¶ added in v0.1.0
BytesToUint64Slice converts a byte slice to a uint64 slice.
func CPUTicks ¶
func CPUTicks() int64
CPUTicks is a faster alternative to NanoTime to measure time duration.
func CallocNoRef ¶ added in v0.1.0
CallocNoRef will not give you memory back without jemalloc.
func HistogramBounds ¶ added in v0.1.0
Creates bounds for an histogram. The bounds are powers of two of the form [2^min_exponent, ..., 2^max_exponent].
func KeyToHash ¶
TODO: Figure out a way to re-use memhash for the second uint64 hash, we
already know that appending bytes isn't reliable for generating a second hash (see Ristretto PR #88). We also know that while the Go runtime has a runtime memhash128 function, it's not possible to use it to generate [2]uint64 or anything resembling a 128bit hash, even though that's exactly what we need in this situation.
func Madvise ¶ added in v0.1.0
Madvise uses the madvise system call to give advise about the use of memory when using a slice that is memory-mapped to a file. Set the readahead flag to false if page references are expected in random order.
func MemHash ¶
MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func MemHashString ¶
MemHashString is the hash function used by go map, it utilizes available hardware instructions (behaves as aeshash if aes instruction is available). NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
func Mmap ¶ added in v0.1.0
Mmap uses the mmap system call to memory-map a file. If writable is true, memory protection of the pages is set so that they may be written to as well.
func NanoTime ¶
func NanoTime() int64
NanoTime returns the current time in nanoseconds from a monotonic clock.
func NumAllocBytes ¶ added in v0.1.0
func NumAllocBytes() int64
NumAllocBytes returns the number of bytes allocated using calls to z.Calloc. The allocations could be happening via either Go or jemalloc, depending upon the build flags.
func ReadMemStats ¶ added in v0.1.0
func ReadMemStats(_ *MemStats)
ReadMemStats doesn't do anything since all the memory is being managed by the Go runtime.
func SetTmpDir ¶ added in v0.1.0
func SetTmpDir(dir string)
SetTmpDir sets the temporary directory for the temporary buffers.
func StatsPrint ¶ added in v0.1.0
func StatsPrint()
Types ¶
type Allocator ¶ added in v0.1.0
type Allocator struct { sync.Mutex Ref uint64 Tag string // contains filtered or unexported fields }
Allocator amortizes the cost of small allocations by allocating memory in bigger chunks. Internally it uses z.Calloc to allocate memory. Once allocated, the memory is not moved, so it is safe to use the allocated bytes to unsafe cast them to Go struct pointers. Maintaining a freelist is slow. Instead, Allocator only allocates memory, with the idea that finally we would just release the entire Allocator.
func AllocatorFrom ¶ added in v0.1.0
AllocatorFrom would return the allocator corresponding to the ref.
func NewAllocator ¶ added in v0.1.0
NewAllocator creates an allocator starting with the given size.
func (*Allocator) AllocateAligned ¶ added in v0.1.0
func (*Allocator) Release ¶ added in v0.1.0
func (a *Allocator) Release()
Release would release the memory back. Remember to make this call to avoid memory leaks.
type AllocatorPool ¶ added in v0.1.0
type AllocatorPool struct {
// contains filtered or unexported fields
}
func NewAllocatorPool ¶ added in v0.1.0
func NewAllocatorPool(sz int) *AllocatorPool
func (*AllocatorPool) Get ¶ added in v0.1.0
func (p *AllocatorPool) Get(sz int, tag string) *Allocator
func (*AllocatorPool) Release ¶ added in v0.1.0
func (p *AllocatorPool) Release()
func (*AllocatorPool) Return ¶ added in v0.1.0
func (p *AllocatorPool) Return(a *Allocator)
type Bloom ¶
type Bloom struct { ElemNum uint64 // contains filtered or unexported fields }
Bloom filter
func JSONUnmarshal ¶
JSONUnmarshal takes JSON-Object (type bloomJSONImExport) as []bytes returns bloom32 / bloom64 object.
func NewBloomFilter ¶
NewBloomFilter returns a new bloomfilter.
func (*Bloom) AddIfNotHas ¶
AddIfNotHas only Adds hash, if it's not present in the bloomfilter. Returns true if hash was added. Returns false if hash was already registered in the bloomfilter.
func (Bloom) Has ¶
Has checks if bit(s) for entry hash is/are set, returns true if the hash was added to the Bloom Filter.
func (Bloom) JSONMarshal ¶
JSONMarshal returns JSON-object (type bloomJSONImExport) as []byte.
type Buffer ¶ added in v0.1.0
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is equivalent of bytes.Buffer without the ability to read. It is NOT thread-safe.
In UseCalloc mode, z.Calloc is used to allocate memory, which depending upon how the code is compiled could use jemalloc for allocations.
In UseMmap mode, Buffer uses file mmap to allocate memory. This allows us to store big data structures without using physical memory.
MaxSize can be set to limit the memory usage.
func NewBufferPersistent ¶ added in v0.1.0
It is the caller's responsibility to set offset after this, because Buffer doesn't remember what it was.
func NewBufferSlice ¶ added in v0.1.0
func (*Buffer) Allocate ¶ added in v0.1.0
Allocate is a way to get a slice of size n back from the buffer. This slice can be directly written to. Warning: Allocate is not thread-safe. The byte slice returned MUST be used before further calls to Buffer.
func (*Buffer) AllocateOffset ¶ added in v0.1.0
AllocateOffset works the same way as allocate, but instead of returning a byte slice, it returns the offset of the allocation.
func (*Buffer) Grow ¶ added in v0.1.0
Grow would grow the buffer to have at least n more bytes. In case the buffer is at capacity, it would reallocate twice the size of current capacity + n, to ensure n bytes can be written to the buffer without further allocation. In UseMmap mode, this might result in underlying file expansion.
func (*Buffer) LenNoPadding ¶ added in v0.1.0
LenNoPadding would return the number of bytes written to the buffer so far (without the padding).
func (*Buffer) LenWithPadding ¶ added in v0.1.0
LenWithPadding would return the number of bytes written to the buffer so far plus the padding at the start of the buffer.
func (*Buffer) Release ¶ added in v0.1.0
Release would free up the memory allocated by the buffer. Once the usage of buffer is done, it is important to call Release, otherwise a memory leak can happen.
func (*Buffer) Reset ¶ added in v0.1.0
func (b *Buffer) Reset()
Reset would reset the buffer to be reused.
func (*Buffer) SliceAllocate ¶ added in v0.1.0
SliceAllocate would encode the size provided into the buffer, followed by a call to Allocate, hence returning the slice of size sz. This can be used to allocate a lot of small buffers into this big buffer. Note that SliceAllocate should NOT be mixed with normal calls to Write.
func (*Buffer) SliceIterate ¶ added in v0.1.0
func (*Buffer) SliceOffsets ¶ added in v0.1.0
SliceOffsets is an expensive function. Use sparingly.
func (*Buffer) SortSlice ¶ added in v0.1.0
SortSlice is like SortSliceBetween but sorting over the entire buffer.
func (*Buffer) SortSliceBetween ¶ added in v0.1.0
func (*Buffer) StartOffset ¶ added in v0.1.0
func (*Buffer) WithAutoMmap ¶ added in v0.1.0
func (*Buffer) WithMaxSize ¶ added in v0.1.0
func (*Buffer) WriteSlice ¶ added in v0.1.0
type BufferType ¶ added in v0.1.0
type BufferType int
const ( UseCalloc BufferType = iota UseMmap UseInvalid )
func (BufferType) String ¶ added in v0.1.0
func (t BufferType) String() string
type Closer ¶ added in v0.1.0
type Closer struct {
// contains filtered or unexported fields
}
Closer holds the two things we need to close a goroutine and wait for it to finish: a chan to tell the goroutine to shut down, and a WaitGroup with which to wait for it to finish shutting down.
func NewCloser ¶ added in v0.1.0
NewCloser constructs a new Closer, with an initial count on the WaitGroup.
func (*Closer) AddRunning ¶ added in v0.1.0
AddRunning Add()'s delta to the WaitGroup.
func (*Closer) Ctx ¶ added in v0.1.0
Ctx can be used to get a context, which would automatically get cancelled when Signal is called.
func (*Closer) HasBeenClosed ¶ added in v0.1.0
func (lc *Closer) HasBeenClosed() <-chan struct{}
HasBeenClosed gets signaled when Signal() is called.
func (*Closer) Signal ¶ added in v0.1.0
func (lc *Closer) Signal()
Signal signals the HasBeenClosed signal.
func (*Closer) SignalAndWait ¶ added in v0.1.0
func (lc *Closer) SignalAndWait()
SignalAndWait calls Signal(), then Wait().
type HistogramData ¶ added in v0.1.0
type HistogramData struct { Bounds []float64 Count int64 CountPerBucket []int64 Min int64 Max int64 Sum int64 }
HistogramData stores the information needed to represent the sizes of the keys and values as a histogram.
func NewHistogramData ¶ added in v0.1.0
func NewHistogramData(bounds []float64) *HistogramData
NewHistogramData returns a new instance of HistogramData with properly initialized fields.
func (*HistogramData) Clear ¶ added in v0.1.0
func (histogram *HistogramData) Clear()
Clear reset the histogram. Helpful in situations where we need to reset the metrics
func (*HistogramData) Copy ¶ added in v0.1.0
func (histogram *HistogramData) Copy() *HistogramData
func (*HistogramData) Mean ¶ added in v0.1.0
func (histogram *HistogramData) Mean() float64
Mean returns the mean value for the histogram.
func (*HistogramData) Percentile ¶ added in v0.1.0
func (histogram *HistogramData) Percentile(p float64) float64
Percentile returns the percentile value for the histogram. value of p should be between [0.0-1.0]
func (*HistogramData) String ¶ added in v0.1.0
func (histogram *HistogramData) String() string
String converts the histogram data into human-readable string.
func (*HistogramData) Update ¶ added in v0.1.0
func (histogram *HistogramData) Update(value int64)
Update changes the Min and Max fields if value is less than or greater than the current values.
type MemStats ¶ added in v0.1.0
type MemStats struct { // Total number of bytes allocated by the application. // http://jemalloc.net/jemalloc.3.html#stats.allocated Allocated uint64 // Total number of bytes in active pages allocated by the application. This // is a multiple of the page size, and greater than or equal to // Allocated. // http://jemalloc.net/jemalloc.3.html#stats.active Active uint64 // Maximum number of bytes in physically resident data pages mapped by the // allocator, comprising all pages dedicated to allocator metadata, pages // backing active allocations, and unused dirty pages. This is a maximum // rather than precise because pages may not actually be physically // resident if they correspond to demand-zeroed virtual memory that has not // yet been touched. This is a multiple of the page size, and is larger // than stats.active. // http://jemalloc.net/jemalloc.3.html#stats.resident Resident uint64 // Total number of bytes in virtual memory mappings that were retained // rather than being returned to the operating system via e.g. munmap(2) or // similar. Retained virtual memory is typically untouched, decommitted, or // purged, so it has no strongly associated physical memory (see extent // hooks http://jemalloc.net/jemalloc.3.html#arena.i.extent_hooks for // details). Retained memory is excluded from mapped memory statistics, // e.g. stats.mapped (http://jemalloc.net/jemalloc.3.html#stats.mapped). // http://jemalloc.net/jemalloc.3.html#stats.retained Retained uint64 }
MemStats is used to fetch JE Malloc Stats. The stats are fetched from the mallctl namespace http://jemalloc.net/jemalloc.3.html#mallctl_namespace.
type MmapFile ¶ added in v0.1.0
MmapFile represents an mmapd file and includes both the buffer to the data and the file descriptor.
func OpenMmapFile ¶ added in v0.1.0
OpenMmapFile opens an existing file or creates a new file. If the file is created, it would truncate the file to maxSz. In both cases, it would mmap the file to maxSz and returned it. In case the file is created, z.NewFile is returned.
func OpenMmapFileUsing ¶ added in v0.1.0
func (*MmapFile) AllocateSlice ¶ added in v0.1.0
AllocateSlice allocates a slice of the given size at the given offset.
func (*MmapFile) Bytes ¶ added in v0.1.0
Bytes returns data starting from offset off of size sz. If there's not enough data, it would return nil slice and io.EOF.
func (*MmapFile) Close ¶ added in v0.1.0
Close would close the file. It would also truncate the file if maxSz >= 0.
type SuperFlag ¶ added in v0.1.0
type SuperFlag struct {
// contains filtered or unexported fields
}
func NewSuperFlag ¶ added in v0.1.0
func (*SuperFlag) GetDuration ¶ added in v0.1.0
func (*SuperFlag) GetFloat64 ¶ added in v0.1.0
func (*SuperFlag) MergeAndCheckDefault ¶ added in v0.1.0
type SuperFlagHelp ¶ added in v0.1.0
type SuperFlagHelp struct {
// contains filtered or unexported fields
}
SuperFlagHelp makes it really easy to generate command line `--help` output for a SuperFlag. For example:
const flagDefaults = `enabled=true; path=some/path;` var help string = z.NewSuperFlagHelp(flagDefaults). Flag("enabled", "Turns on <something>."). Flag("path", "The path to <something>."). Flag("another", "Not present in defaults, but still included."). String()
The `help` string would then contain:
enabled=true; Turns on <something>. path=some/path; The path to <something>. another=; Not present in defaults, but still included.
All flags are sorted alphabetically for consistent `--help` output. Flags with default values are placed at the top, and everything else goes under.
func NewSuperFlagHelp ¶ added in v0.1.0
func NewSuperFlagHelp(defaults string) *SuperFlagHelp
func (*SuperFlagHelp) Flag ¶ added in v0.1.0
func (h *SuperFlagHelp) Flag(name, description string) *SuperFlagHelp
func (*SuperFlagHelp) Head ¶ added in v0.1.0
func (h *SuperFlagHelp) Head(head string) *SuperFlagHelp
func (*SuperFlagHelp) String ¶ added in v0.1.0
func (h *SuperFlagHelp) String() string
type Tree ¶ added in v0.1.0
type Tree struct {
// contains filtered or unexported fields
}
Tree represents the structure for custom mmaped B+ tree. It supports keys in range [1, math.MaxUint64-1] and values [1, math.Uint64].
func NewTreePersistent ¶ added in v0.1.0
NewTree returns a persistent on-disk B+ tree.
func (*Tree) DeleteBelow ¶ added in v0.1.0
DeleteBelow deletes all keys with value under ts.
func (*Tree) Get ¶ added in v0.1.0
Get looks for key and returns the corresponding value. If key is not found, 0 is returned.
func (*Tree) Iterate ¶ added in v0.1.0
func (t *Tree) Iterate(fn func(node))
Iterate iterates over the tree and executes the fn on each node.
func (*Tree) IterateKV ¶ added in v0.1.0
IterateKV iterates through all keys and values in the tree. If newVal is non-zero, it will be set in the tree.
func (*Tree) Print ¶ added in v0.1.0
func (t *Tree) Print()
Print iterates over the tree and prints all valid KVs.
func (*Tree) Reset ¶ added in v0.1.0
func (t *Tree) Reset()
Reset resets the tree and truncates it to maxSz.