libcombindex

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: Apache-2.0, CC0-1.0 Imports: 3 Imported by: 0

Documentation

Overview

Package libcombindex is a hash to ordered integers index.

It allows you to store hashes, which are mapped to a successive height integers.

The Table is a probabilistic data structure, in the sense that it does not simply allow you to Get the right height value. Instead, it provides a successive height guesses, one of which is guaranteed to be correct, and the remaining ones may be incorrect. The number of guesses can be reduced by decreasing the MaxLoad or Hops.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Min, Max uint64 // place holders, define the height range stored in this bucket
	// contains filtered or unexported fields
}

Bucket is an internal data holder type used by the Table structure. It can optionally also hold the Min, Max describing the height range stored by this bucket. Note: Max bucket size is around 4GB.

func BucketInit

func BucketInit(p uint32, k byte, hops uint32, tweak uint16) *Bucket

BucketInit initializes a new bucket. It only fails when OOM.

func (*Bucket) Cap

func (b *Bucket) Cap() uint32

Cap returns the maximum number of items that can be stored in this bucket.

func (*Bucket) Delete

func (b *Bucket) Delete(height uint32, hashes []uint32) uint32

Delete deletes the height values at a specific hashes keys from the bucket. It returns the number of items successfully deleted. All hashes are at the same height, the height must be the highest height that exists in the bucket.

func (*Bucket) Get

func (b *Bucket) Get(hash uint32, maxHeight uint32) (ret Continuation)

Get creates the Continuation to get a hash height that was inserted at height which is less or equal to maxHeight.

func (*Bucket) HeightMask

func (b *Bucket) HeightMask() uint32

HeightMask returns the height mask, that is, how many successive heights can this bucket hold.

func (*Bucket) Insert

func (b *Bucket) Insert(height uint32, hash uint32) bool

Insert inserts the height value at a specific hash key into the bucket. It returns true when it succeeded, false when it gave up. The height inserted must be higher or equal to any height previously inserted. The same hash can be inserted repeadedly, but it performs better when each hash is unique.

func (*Bucket) Len

func (b *Bucket) Len() uint32

Len returns the number of items occupied in this bucket.

func (*Bucket) UsedPercent

func (b *Bucket) UsedPercent() byte

UsedPercent returns the percentage occupied in this bucket. Empty bucket returns 0%.

type Buffer added in v0.0.7

type Buffer []byte

func MakeBuffer added in v0.0.7

func MakeBuffer(items map[uint64]uint64, seed, seed2 uint64) Buffer

func MakeBufferMinMax added in v0.0.10

func MakeBufferMinMax(items map[uint64]uint64, seed, seed2 uint64, min, max byte) Buffer

func (*Buffer) Get added in v0.0.7

func (b *Buffer) Get(hash uint64) (value uint64)

func (*Buffer) GetSeed added in v0.0.7

func (b *Buffer) GetSeed() (seed uint64)

func (*Buffer) GetSeed2 added in v0.0.7

func (b *Buffer) GetSeed2() (seed uint64)

type Continuation

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

Continuation is a get reading continuation. It is used to read out successive height guesses for any hash that exists or not exists in the bucket.

func (*Continuation) Bucket

func (c *Continuation) Bucket() *Bucket

Bucket returns the Bucket from which the Continuation Gets data.

func (*Continuation) Get

func (c *Continuation) Get() (height uint32)

Get returns the next height guess. If there is no more guess, it returns 0.

func (*Continuation) Order

func (c *Continuation) Order() uint32

Order returns the importance of this continuation relative to other continuations.

type Index added in v0.0.7

type Index map[uint64]Buffer

func MakeIndex added in v0.0.7

func MakeIndex() Index

func NewIndex added in v0.0.7

func NewIndex() *Index

func (*Index) Delete added in v0.0.7

func (i *Index) Delete(height uint64)

func (*Index) Get added in v0.0.7

func (i *Index) Get(height uint64, hash [32]byte) (value uint64)

func (*Index) Insert added in v0.0.7

func (i *Index) Insert(height uint64, items map[[32]byte]uint64) bool

func (*Index) InsertCfg added in v0.0.10

func (i *Index) InsertCfg(height uint64, items map[[32]byte]uint64, cfg IndexConfig) bool

type IndexConfig added in v0.0.10

type IndexConfig struct {
	Seed1, Seed2 uint64
	Min, Max     byte
}

type LoopDetector

type LoopDetector [2][32]uint32

LoopDetector is a detector capable of detecting loops in an infinite sequence of 32bit integers, as well as providing information whether we are not yet in loop.

func (*LoopDetector) Add

func (c *LoopDetector) Add(n uint32) (ret bool)

Add adds a specific number n into the loop detector. It ocasionally reports true when we are not in a loop (when Add changes the loop detector state).

func (*LoopDetector) Seen

func (c *LoopDetector) Seen(n uint32) bool

Seen reports whether a specific number n was already seen in the sequence. This means we are in a loop.

type Table

type Table struct {
	Seed    uint32 // a true random seed should be provided to randomize the structure
	Bytes   byte   // number of bytes per stored entry in the table, usually 3
	Mega    uint16 // number of millions of entries per bucket, when creating a new one. The size is a slightly higher pseudoprime.
	MaxLoad byte   // Max load percentage of the last bucket, when exceeded, it creates a new bucket.
	Hops    uint16 // Number of hops after which to give up and make a new bucket. Used to break loops.
	Tweak   uint16 // Tweak is a multiplication factor when hopping to another hash position.
	// contains filtered or unexported fields
}

Table is the main libcombindex table. It maps a 256bit hash to successive 64bit height integers. Important: Seed may not be changed when the Table is in use.

func (*Table) Cap

func (c *Table) Cap() (ret uint64)

Cap returns the maximum number of items that can currently be stored in this table, but it grows automatically when inserting.

func (*Table) ConfiguredCorrectly

func (c *Table) ConfiguredCorrectly() bool

ConfiguredCorrectly reports whether the Table is configured correctly and therefore it can be used.

func (*Table) Delete

func (c *Table) Delete(hashes [][32]byte, height uint64) (success bool)

Delete deletes a specific height value from a specific 32byte hash keys. The height must be equal to the highest existing height in the table. Hashes must contain all the existing hashes that currently exist in the table and which were inserted at that height (all hashes that share the same height at once). If success returns false, the Table is now corrupt (will become panic in a future version).

func (*Table) Erase added in v0.0.7

func (c *Table) Erase()

Erase clears the table

func (*Table) Get

func (c *Table) Get(hash [32]byte, maxHeight uint64, cb func(uint64) bool) bool

Get repeatedly guesses the height for a 32byte key hash, using the callback. The callback cb can report success true to stop the get iteration, or false to continue guessing. MaxHeight is used to limit the height values guessed to heights lower than the provided maxHeight parameter.

func (*Table) Insert

func (c *Table) Insert(hash [32]byte, height uint64)

Insert inserts a specific height value at a specific 32byte hash key. The height must be equal or higher than the highest currently existing height. You can insert the same hash multiple times at different or same heights, just be sure to delete it equal number of times once it is needed to delete it.

func (*Table) Len

func (c *Table) Len() (ret uint64)

Len returns the number of items occupied in this table.

func (*Table) UsedPercent

func (c *Table) UsedPercent() byte

UsedPercent returns the percentage occupied in this table. Empty table returns 0%.

Jump to

Keyboard shortcuts

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