btrie

package
v0.0.0-...-c3c44a6 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Copyright (c) 2018 The MATRIX Authors Distributed under the MIT software license, see the accompanying file COPYING or http://www.opensource.org/licenses/mit-license.php

Index

Constants

View Source
const (
	DefaultFreeListSize = 32
)

Variables

This section is empty.

Functions

func BtreeSaveHash

func BtreeSaveHash(node *bnode, db *trie.Database, typ byte, stateDB vm.StateDB) common.Hash

Used for Btree save to triedb

func RestoreBtree

func RestoreBtree(btree *BTree, itemNode *bnode, nodeHash common.Hash, db *trie.Database, typ byte, stateDB vm.StateDB) error

Types

type BTree

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

BTree is an implementation of a B-Tree.

BTree stores Item instances in an ordered structure, allowing easy insertion, removal, and iteration.

Write operations are not safe for concurrent mutation by multiple goroutines, but Read operations are.

func NewBtree

func NewBtree(degree int, db *trie.Database) *BTree

New creates a new B-Tree with the given degree.

New(2), for example, will create a 2-3-4 tree (each node contains 1-3 items and 2-4 children).

func NewWithFreeList

func NewWithFreeList(degree int, f *FreeList, db *trie.Database) *BTree

NewWithFreeList creates a new B-Tree that uses the given node free list.

func (*BTree) Ascend

func (t *BTree) Ascend(iterator ItemIterator)

Ascend calls the iterator for every value in the tree within the range [first, last], until iterator returns false.

func (*BTree) AscendGreaterOrEqual

func (t *BTree) AscendGreaterOrEqual(pivot Item, iterator ItemIterator)

AscendGreaterOrEqual calls the iterator for every value in the tree within the range [pivot, last], until iterator returns false.

func (*BTree) AscendLessThan

func (t *BTree) AscendLessThan(pivot Item, iterator ItemIterator)

AscendLessThan calls the iterator for every value in the tree within the range [first, pivot), until iterator returns false.

func (*BTree) AscendRange

func (t *BTree) AscendRange(greaterOrEqual, lessThan Item, iterator ItemIterator)

AscendRange calls the iterator for every value in the tree within the range [greaterOrEqual, lessThan), until iterator returns false.

func (*BTree) Clear

func (t *BTree) Clear(addNodesToFreelist bool)

Clear removes all items from the btree. If addNodesToFreelist is true, t's nodes are added to its freelist as part of this call, until the freelist is full. Otherwise, the root node is simply dereferenced and the subtree left to Go's normal GC processes.

This can be much faster than calling Delete on all elements, because that requires finding/removing each element in the tree and updating the tree accordingly. It also is somewhat faster than creating a new tree to replace the old one, because nodes from the old tree are reclaimed into the freelist for use by the new one, instead of being lost to the garbage collector.

This call takes:

O(1): when addNodesToFreelist is false, this is a single operation.
O(1): when the freelist is already full, it breaks out immediately
O(freelist size):  when the freelist is empty and the nodes are all owned
    by this tree, nodes are added to the freelist until full.
O(tree size):  when all nodes are owned by another tree, all nodes are
    iterated over looking for nodes to add to the freelist, and due to
    ownership, none are.

func (*BTree) Clone

func (t *BTree) Clone() (t2 *BTree)

Clone clones the btree, lazily. Clone should not be called concurrently, but the original tree (t) and the new tree (t2) can be used concurrently once the Clone call completes.

The internal tree structure of b is marked read-only and shared between t and t2. Writes to both t and t2 use copy-on-write logic, creating new nodes whenever one of b's original nodes would have been modified. Read operations should have no performance degredation. Write operations for both t and t2 will initially experience minor slow-downs caused by additional allocs and copies due to the aforementioned copy-on-write logic, but should converge to the original performance characteristics of the original tree.

func (*BTree) Commit

func (t *BTree) Commit() (err error)

func (*BTree) Delete

func (t *BTree) Delete(item Item) Item

Delete removes an item equal to the passed in item from the tree, returning it. If no such item exists, returns nil.

func (*BTree) DeleteMax

func (t *BTree) DeleteMax() Item

DeleteMax removes the largest item in the tree and returns it. If no such item exists, returns nil.

func (*BTree) DeleteMin

func (t *BTree) DeleteMin() Item

DeleteMin removes the smallest item in the tree and returns it. If no such item exists, returns nil.

func (*BTree) Descend

func (t *BTree) Descend(iterator ItemIterator)

Descend calls the iterator for every value in the tree within the range [last, first], until iterator returns false.

func (*BTree) DescendGreaterThan

func (t *BTree) DescendGreaterThan(pivot Item, iterator ItemIterator)

DescendGreaterThan calls the iterator for every value in the tree within the range (pivot, last], until iterator returns false.

func (*BTree) DescendLessOrEqual

func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator)

DescendLessOrEqual calls the iterator for every value in the tree within the range [pivot, first], until iterator returns false.

func (*BTree) DescendRange

func (t *BTree) DescendRange(lessOrEqual, greaterThan Item, iterator ItemIterator)

DescendRange calls the iterator for every value in the tree within the range [lessOrEqual, greaterThan), until iterator returns false.

func (*BTree) Get

func (t *BTree) Get(key Item) Item

Get looks for the key item in the tree, returning it. It returns nil if unable to find that item.

func (*BTree) Has

func (t *BTree) Has(key Item) bool

Has returns true if the given key is in the tree.

func (*BTree) Len

func (t *BTree) Len() int

Len returns the number of items currently in the tree.

func (*BTree) Max

func (t *BTree) Max() Item

Max returns the largest item in the tree, or nil if the tree is empty.

func (*BTree) Min

func (t *BTree) Min() Item

Min returns the smallest item in the tree, or nil if the tree is empty.

func (*BTree) ReplaceOrInsert

func (t *BTree) ReplaceOrInsert(item Item) Item

ReplaceOrInsert adds the given item to the tree. If an item in the tree already equals the given one, it is removed from the tree and returned. Otherwise, nil is returned.

nil cannot be added to the tree (will panic).

func (*BTree) Root

func (t *BTree) Root() (root *bnode)

type BnodeSave

type BnodeSave struct {
	Key   []TransferTxData
	Child []common.Hash
}

type FreeList

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

FreeList represents a free list of btree nodes. By default each BTree has its own FreeList, but multiple BTrees can share the same FreeList. Two Btrees using the same freelist are safe for concurrent write access.

func NewFreeList

func NewFreeList(size int) *FreeList

NewFreeList creates a new free list. size is the maximum size of the returned free list.

type Item

type Item interface {
	// Less tests whether the current item is less than the given argument.
	//
	// This must provide a strict weak ordering.
	// If !a.Less(b) && !b.Less(a), we treat this to mean a == b (i.e. we can only
	// hold one of either a or b in the tree).
	Less(than Item) bool
	InsertTxData(i Item) bool
}

Item represents a single object in the tree.

type ItemIterator

type ItemIterator func(i Item) bool

ItemIterator allows callers of Ascend* to iterate in-order over portions of the tree. When this function returns false, iteration will stop and the associated Ascend* function will immediately return.

type SpcialTimeTxData

type SpcialTimeTxData struct {
	Key_Time uint32
	Value_Tx map[common.Hash][]byte
}

func (SpcialTimeTxData) InsertTxData

func (a SpcialTimeTxData) InsertTxData(b Item) bool

func (SpcialTimeTxData) Less

func (a SpcialTimeTxData) Less(b Item) bool

type SpcialTxData

type SpcialTxData struct {
	Key_Time uint32
	Value_Tx map[common.Hash][]byte
}

func (SpcialTxData) InsertTxData

func (a SpcialTxData) InsertTxData(b Item) bool

func (SpcialTxData) Less

func (a SpcialTxData) Less(b Item) bool

type TransferTxData

type TransferTxData struct {
	Key_Time uint32
	Value_Tx []map[common.Hash][]byte
}

Jump to

Keyboard shortcuts

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