Documentation ¶
Overview ¶
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
start with https://github.com/benbjohnson/immutable and specialize Map<uint32,int64>
Copyright 2019 Ben Johnson ¶
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0 Package rbf implements the roaring b-tree file format.
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Copyright 2022 Molecula Corp. (DBA FeatureBase). SPDX-License-Identifier: Apache-2.0
Index ¶
- Constants
- Variables
- func ConvertToLeafArgs(key uint64, c *roaring.Container) (result leafCell)
- func Dumpdot(tx *Tx, pgno uint32, parent string, writer io.Writer)
- func IsBitmapHeader(page []byte) bool
- func IsMetaPage(page []byte) bool
- func Magic32() uint32
- func Pagedump(b []byte, indent string, writer io.Writer)
- func RowValues(b []uint64) []uint64
- func Walk(tx *Tx, pgno uint32, v func(uint32, []*RootRecord))
- func WalkPage(tx *Tx, pgno uint32, walker Walker)
- func WalkRootRecordPages(page []byte) uint32
- func WriteRootRecord(data []byte, rec *RootRecord) (remaining []byte, err error)
- type BitmapPage
- type BitmapPageInfo
- type BranchCell
- type BranchPage
- type BranchPageInfo
- type ContainerType
- type Cursor
- func (c *Cursor) Add(v uint64) (changed bool, err error)
- func (c *Cursor) AddRoaring(bm *roaring.Bitmap) (changed bool, err error)
- func (c *Cursor) Close()
- func (c *Cursor) Contains(v uint64) (exists bool, err error)
- func (c *Cursor) CurrentPageType() ContainerType
- func (c *Cursor) Dump(name string)
- func (c *Cursor) DumpKeys()
- func (c *Cursor) DumpStack()
- func (c *Cursor) First() error
- func (c *Cursor) Key() uint64
- func (c *Cursor) Last() error
- func (c *Cursor) Next() error
- func (c *Cursor) Prev() error
- func (c *Cursor) Remove(v uint64) (changed bool, err error)
- func (c *Cursor) RemoveRoaring(bm *roaring.Bitmap) (changed bool, err error)
- func (c *Cursor) Row(shard, rowID uint64) (*roaring.Bitmap, error)
- func (c *Cursor) Rows() ([]uint64, error)
- func (c *Cursor) Seek(key uint64) (exact bool, err error)
- func (c *Cursor) Values() []uint16
- type DB
- func (db *DB) Begin(writable bool) (_ *Tx, err error)
- func (db *DB) Check() error
- func (db *DB) Checkpoint() error
- func (db *DB) Close() (err error)
- func (db *DB) DataPath() string
- func (db *DB) DebugInfo() *DebugInfo
- func (db *DB) HasData(requireOneHotBit bool) (hasAnyRecords bool, err error)
- func (db *DB) Open() (err error)
- func (db *DB) Size() (int64, error)
- func (db *DB) TxN() int
- func (db *DB) WALPath() string
- func (db *DB) WALSize() int64
- type DebugInfo
- type ErrorList
- type FreePage
- type FreePageInfo
- type LeafCell
- type LeafPage
- type LeafPageInfo
- type MetaPage
- type MetaPageInfo
- type Metric
- type Nodetype
- type Page
- type PageInfo
- type PageMap
- type PageMapBuilder
- type PageMapIterator
- type RootRecord
- type RootRecordPage
- type RootRecordPageInfo
- type Tx
- func (tx *Tx) Add(name string, a ...uint64) (changeCount int, err error)
- func (tx *Tx) AddRoaring(name string, bm *roaring.Bitmap) (changed bool, err error)
- func (tx *Tx) ApplyFilter(name string, key uint64, filter roaring.BitmapFilter) (err error)
- func (tx *Tx) ApplyRewriter(name string, key uint64, rewriter roaring.BitmapRewriter) (err error)
- func (tx *Tx) BitmapExists(name string) (bool, error)
- func (tx *Tx) BitmapNames() ([]string, error)
- func (tx *Tx) Check() error
- func (tx *Tx) Commit() error
- func (tx *Tx) Container(name string, key uint64) (*roaring.Container, error)
- func (tx *Tx) ContainerIterator(name string, key uint64) (citer roaring.ContainerIterator, found bool, err error)
- func (tx *Tx) Contains(name string, v uint64) (bool, error)
- func (tx *Tx) Count(name string) (uint64, error)
- func (tx *Tx) CountRange(name string, start, end uint64) (uint64, error)
- func (tx *Tx) CreateBitmap(name string) error
- func (tx *Tx) CreateBitmapIfNotExists(name string) error
- func (tx *Tx) Cursor(name string) (*Cursor, error)
- func (tx *Tx) DBPath() string
- func (tx *Tx) DebugInfo() *TxDebugInfo
- func (tx *Tx) DeleteBitmap(name string) error
- func (tx *Tx) DeleteBitmapsWithPrefix(prefix string) error
- func (tx *Tx) Depth(name string) (int, error)
- func (tx *Tx) FieldViews() []string
- func (tx *Tx) GetSizeBytesWithPrefix(prefix string) (n uint64, err error)
- func (tx *Tx) GetSortedFieldViewList() (fvs []txkey.FieldView, _ error)
- func (tx *Tx) ImportRoaringBits(name string, itr roaring.RoaringIterator, clear bool, log bool, rowSize uint64) (changed int, rowSet map[uint64]int, err error)
- func (tx *Tx) Max(name string) (uint64, error)
- func (tx *Tx) Min(name string) (uint64, bool, error)
- func (tx *Tx) OffsetRange(name string, offset, start, endx uint64) (*roaring.Bitmap, error)
- func (tx *Tx) PageData(pgno uint32) ([]byte, error)
- func (tx *Tx) PageInfos() ([]PageInfo, error)
- func (tx *Tx) PageN() int
- func (tx *Tx) Pages(pgnos []uint32) ([]Page, error)
- func (tx *Tx) PutContainer(name string, key uint64, ct *roaring.Container) error
- func (tx *Tx) Remove(name string, a ...uint64) (changeCount int, err error)
- func (tx *Tx) RemoveContainer(name string, key uint64) error
- func (tx *Tx) RenameBitmap(oldname, newname string) error
- func (tx *Tx) RoaringBitmap(name string) (*roaring.Bitmap, error)
- func (tx *Tx) Rollback()
- func (tx *Tx) Root(name string) (uint32, error)
- func (tx *Tx) RootRecords() (records *immutable.SortedMap[string, uint32], err error)
- func (tx *Tx) SnapshotReader() (io.Reader, error)
- func (tx *Tx) Writable() bool
- type TxDebugInfo
- type Walker
Constants ¶
const ( // Magic is the first 4 bytes of the RBF file. Magic = "\xFFRBF" // PageSize is the fixed size for every database page. PageSize = 8192 // ShardWidth represents the number of bits per shard. ShardWidth = 1 << shardwidth.Exponent // RowValueMask masks the low bits for a row. RowValueMask = ShardWidth - 1 // ArrayMaxSize represents the maximum size of array containers. // This is sligtly less than roaring to accommodate the page header. ArrayMaxSize = 4079 // RLEMaxSize represents the maximum size of run length encoded containers. RLEMaxSize = 2039 )
const ( PageTypeRootRecord = 1 PageTypeLeaf = 2 PageTypeBranch = 4 PageTypeBitmapHeader = 8 // Only used by the WAL for marking next page PageTypeBitmap = 16 // Only used internally when walking the b-tree )
Page types.
const ( MetaPageFlagCommit = 1 MetaPageFlagRollback = 2 )
Meta commit/rollback flags.
const (
BitmapN = (1 << 16) / 64
)
Variables ¶
var ( ErrTxClosed = errors.New("transaction closed") ErrTxNotWritable = errors.New("transaction not writable") ErrBitmapNameRequired = errors.New("bitmap name required") ErrBitmapNotFound = errors.New("bitmap not found") ErrBitmapExists = errors.New("bitmap already exists") ErrTxTooLarge = errors.New("rbf tx too large") )
var Debug bool
Debug is just a temporary flag used for debugging.
var ErrClosed = errors.New("rbf: database closed")
Functions ¶
func ConvertToLeafArgs ¶
func Dumpdot ¶
Dumpdot recursively writes the tree representation starting from a given page to STDERR.
func IsBitmapHeader ¶
func Magic32 ¶
func Magic32() uint32
Magic32 returns the magic bytes as a big endian encoded uint32.
func WalkRootRecordPages ¶
func WriteRootRecord ¶
func WriteRootRecord(data []byte, rec *RootRecord) (remaining []byte, err error)
WriteRootRecord writes a root record with the pgno & name. Returns io.ErrShortBuffer if there is not enough space.
Types ¶
type BitmapPage ¶
type BitmapPage struct { *BitmapPageInfo Values []uint16 }
type BitmapPageInfo ¶
type BranchCell ¶
BranchCell represents a branch cell in the public API.
type BranchPage ¶
type BranchPage struct { *BranchPageInfo Cells []*BranchCell }
type BranchPageInfo ¶
type ContainerType ¶
type ContainerType int
const ( ContainerTypeNone ContainerType = iota ContainerTypeArray ContainerTypeRLE ContainerTypeBitmap ContainerTypeBitmapPtr )
Container types.
func (ContainerType) String ¶
func (typ ContainerType) String() string
ContainerTypeString returns a string representation of the container type.
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
Cursor represents an object for traversing forward and backward in the keyspace. Users should initialize a cursor with either First(), Last(), or Seek() and then move forward or backward using Next() or Prev(), respectively.
If you mutate the b-tree that a cursor is attached to then you'll need to re-initialize the cursor. This may be changed in the future though.
Cursors can be reused in some cases; if you're keeping a cursor around, be sure to nil out the tx, or it will hold a reference to it.
func (*Cursor) Add ¶
Add sets a bit on the underlying bitmap. If changed is true, this cursor will need to be reinitialized before use.
func (*Cursor) AddRoaring ¶
func (*Cursor) Close ¶
func (c *Cursor) Close()
Close closes a cursor out, invalidating it for future use, and puts it back in a pool to reduce allocations. Contrast with unpooledClose, which you probably shouldn't use.
func (*Cursor) CurrentPageType ¶
func (c *Cursor) CurrentPageType() ContainerType
CurrentPageType returns the type of the container currently pointed to by cursor used in testing sometimes the cursor needs to be positions prior to this call with First/Last etc.
func (*Cursor) First ¶
First moves to the first element of the btree. Returns io.EOF if no elements exist. The first call to Next() will not move the position but subsequent calls will move the position forward until it reaches the end and return io.EOF.
func (*Cursor) Next ¶
Next moves to the next element of the btree. Returns EOF if no more elements exist.
func (*Cursor) Prev ¶
Prev moves to the previous element of the btree. Returns io.EOF if at the beginning of the b-tree.
func (*Cursor) Remove ¶
Remove unsets a bit on the underlying bitmap. If changed is true, the cursor will need to be reinitialized before use.
func (*Cursor) RemoveRoaring ¶
type DB ¶
type DB struct { // Path represents the path to the database file. Path string // contains filtered or unexported fields }
DB options like MaxSize, FsyncEnabled, DoAllocZero can be set before calling DB.Open().
func NewDB ¶
NewDB returns a new instance of DB. If cfg is nil we will use the rbfcfg.DefaultConfig().
func (*DB) Checkpoint ¶
Checkpoint performs a manual checkpoint. This is not necessary except for tests.
func (*DB) HasData ¶
HasData with requireOneHotBit=false returns hasAnyRecords true if any record has been stored, even if the value for that bitmap record turned out to have no bits hot (be all zeroes).
In this case, we are taking the attempted storage of any named bitmap into the database as evidence that the db is in use, and we return hasAnyRecords true.
Conversely, if requireOneHotBit is true, then a database consisting of only a named bitmap with an all zeroes (no bits hot) will return hasAnyRecords false. We must find at least a single hot bit inside the db in order to return hasAnyRecords true.
HasData is used by backend migration.
If there is a disk error we return (false, error), so always check the error before deciding if hasAnyRecords is valid.
We will internally create and rollback a read-only transaction to answer this query.
func (*DB) Open ¶
Open opens a database with the file specified in Path. Creates a new file if one does not already exist.
type DebugInfo ¶
type DebugInfo struct { Path string `json:"path"` Txs []*TxDebugInfo `json:"txs"` }
type ErrorList ¶
type ErrorList []error
ErrorList represents a list of errors.
func (*ErrorList) Append ¶
Append appends an error to the list. If err is an ErrorList then all errors are appended.
type FreePage ¶
type FreePage struct {
*FreePageInfo
}
type FreePageInfo ¶
type FreePageInfo struct {
Pgno uint32
}
type LeafCell ¶
type LeafCell struct { Key uint64 Type ContainerType Pgno uint32 // bitmap pointer only Values []uint16 // array & rle containers only }
LeafCell represents a leaf cell in the public API.
type LeafPage ¶
type LeafPage struct { *LeafPageInfo Cells []*LeafCell }
type LeafPageInfo ¶
type MetaPage ¶
type MetaPage struct {
*MetaPageInfo
}
type MetaPageInfo ¶
type Metric ¶
type Metric struct {
// contains filtered or unexported fields
}
Metric is a simple, internal metric for check duration of operations.
type PageMap ¶
type PageMap struct {
// contains filtered or unexported fields
}
PageMap represents an immutable hash map implementation. The map uses a Hasher to generate hashes and check for equality of key values.
It is implemented as an Hash Array Mapped Trie.
func NewPageMap ¶
func NewPageMap() *PageMap
NewPageMap returns a new instance of PageMap. If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. Default hasher implementations only exist for int, string, and byte slice types.
func (*PageMap) Delete ¶
Delete returns a map with the given key removed. Removing a non-existent key will cause this method to return the same map.
func (*PageMap) Get ¶
Get returns the value for a given key and a flag indicating whether the key exists. This flag distinguishes a zero value set on a key versus a non-existent key in the map.
func (*PageMap) Iterator ¶
func (m *PageMap) Iterator() *PageMapIterator
Iterator returns a new iterator for the map.
type PageMapBuilder ¶
type PageMapBuilder struct {
// contains filtered or unexported fields
}
PageMapBuilder represents an efficient builder for creating PageMaps.
func NewPageMapBuilder ¶
func NewPageMapBuilder() *PageMapBuilder
NewPageMapBuilder returns a new instance of PageMapBuilder.
func (*PageMapBuilder) Delete ¶
func (b *PageMapBuilder) Delete(key uint32)
Delete removes the given key. See PageMap.Delete() for additional details.
func (*PageMapBuilder) Get ¶
func (b *PageMapBuilder) Get(key uint32) (value int64, ok bool)
Get returns the value for the given key.
func (*PageMapBuilder) Iterator ¶
func (b *PageMapBuilder) Iterator() *PageMapIterator
Iterator returns a new iterator for the underlying map.
func (*PageMapBuilder) Len ¶
func (b *PageMapBuilder) Len() int
Len returns the number of elements in the underlying map.
func (*PageMapBuilder) Map ¶
func (b *PageMapBuilder) Map() *PageMap
Map returns the underlying map. Only call once. Builder is invalid after call. Will panic on second invocation.
func (*PageMapBuilder) Set ¶
func (b *PageMapBuilder) Set(key uint32, value int64)
Set sets the value of the given key. See PageMap.Set() for additional details.
type PageMapIterator ¶
type PageMapIterator struct {
// contains filtered or unexported fields
}
PageMapIterator represents an iterator over a map's key/value pairs. Although map keys are not sorted, the iterator's order is deterministic.
func (*PageMapIterator) Done ¶
func (itr *PageMapIterator) Done() bool
Done returns true if no more elements remain in the iterator.
func (*PageMapIterator) First ¶
func (itr *PageMapIterator) First()
First resets the iterator to the first key/value pair.
type RootRecord ¶
func ReadRootRecord ¶
func ReadRootRecord(data []byte) (rec *RootRecord, remaining []byte, err error)
ReadRootRecord reads the page number & name for a root record. If there is not enough space or the pgno is zero then a nil record is returned. Returns the remaining buffer.
type RootRecordPage ¶
type RootRecordPage struct { *RootRecordPageInfo Records []*RootRecord }
type RootRecordPageInfo ¶
type Tx ¶
type Tx struct { // DeleteEmptyContainer lets us by default match the roaring // behavior where an existing container has all its bits cleared // but still sticks around in the database. DeleteEmptyContainer bool // contains filtered or unexported fields }
Tx represents an RBF transaction. Transactions provide guarantees such as atomicity for all writes that occur as well as serializable isolation. Transactions can be obtained by calling DB.Begin() and provide a snapshot view at the point-in-time they are started.
func (*Tx) AddRoaring ¶
func (*Tx) ApplyFilter ¶
func (*Tx) ApplyRewriter ¶
func (*Tx) BitmapExists ¶
BitmapExist returns true if bitmap exists. Returns an error if name is empty.
func (*Tx) BitmapNames ¶
BitmapNames returns a list of all bitmap names.
func (*Tx) Commit ¶
Commit completes the transaction and persists data changes. If this method fails, changes may or may not have been persisted to disk. If no changes have been made during the transaction, this functions the same as a rollback.
Attempting to commit an already committed or rolled back transaction will return an ErrTxClosed error.
func (*Tx) ContainerIterator ¶
func (*Tx) CountRange ¶
roaring.countRange counts the number of bits set between [start, end).
func (*Tx) CreateBitmap ¶
CreateBitmap creates a new empty bitmap with the given name. Returns an error if the bitmap already exists.
func (*Tx) CreateBitmapIfNotExists ¶
CreateBitmapIfNotExists creates a new empty bitmap with the given name. This is a no-op if the bitmap already exists.
func (*Tx) DebugInfo ¶
func (tx *Tx) DebugInfo() *TxDebugInfo
func (*Tx) DeleteBitmap ¶
DeleteBitmap removes a bitmap with the given name. Returns an error if the bitmap does not exist.
func (*Tx) DeleteBitmapsWithPrefix ¶
DeleteBitmapsWithPrefix removes all bitmaps with a given prefix.
func (*Tx) FieldViews ¶
func (*Tx) GetSizeBytesWithPrefix ¶
GetSizeBytesWithPrefix returns the size of bitmaps with a given key prefix.
func (*Tx) GetSortedFieldViewList ¶
func (*Tx) ImportRoaringBits ¶
func (*Tx) OffsetRange ¶
func (*Tx) PutContainer ¶
PutContainer inserts a container into a bitmap. Overwrites if key already exists.
func (*Tx) RemoveContainer ¶
RemoveContainer removes a container from the bitmap by key.
func (*Tx) RenameBitmap ¶
RenameBitmap updates the name of an existing bitmap. Returns an error if the bitmap does not exist.
func (*Tx) RoaringBitmap ¶
RoaringBitmap returns a bitmap as a Roaring bitmap.
func (*Tx) Rollback ¶
func (tx *Tx) Rollback()
Rollback discards any changes that have been made by the transaction. A commit or rollback must always be called after a transaction finishes. If this is a writable transaction, the write lock will be released on the DB.
func (*Tx) Root ¶
Root returns the root page number for a bitmap. Returns 0 if the bitmap does not exist.
func (*Tx) RootRecords ¶
RootRecords returns a list of root records.
func (*Tx) SnapshotReader ¶
SnapshotReader returns a reader that provides a snapshot for the current database state.