btree

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Get = iota
	Add
	Update
	Remove
)
View Source
const (
	// BTreeNode is the entity type of the B-Tree Node.
	BTreeNode = iota
	// ValuePart is the entity type of the value part in the key/value pair
	// that a B-Tree supports persistence & access.
	ValuePart
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Btree

type Btree[TK Comparable, TV any] struct {
	Store Store
	// contains filtered or unexported fields
}

Btree manages items using B-tree data structure and algorithm.

func NewBtree

func NewBtree[TK Comparable, TV any](store Store, si *StoreInterface[TK, TV]) *Btree[TK, TV]

NewBtree creates a new B-Tree instance.

func (*Btree[TK, TV]) Add

func (btree *Btree[TK, TV]) Add(key TK, value TV) (bool, error)

Add a key/value pair item to the tree.

func (*Btree[TK, TV]) AddIfNotExist

func (btree *Btree[TK, TV]) AddIfNotExist(key TK, value TV) (bool, error)

AddIfNotExist will add an item if its key is not yet in the B-Tree.

func (*Btree[TK, TV]) FindOne

func (btree *Btree[TK, TV]) FindOne(key TK, firstItemWithKey bool) (bool, error)

FindOne will traverse the tree to find an item with such key.

func (*Btree[TK, TV]) GetCurrentItem

func (btree *Btree[TK, TV]) GetCurrentItem() Item[TK, TV]

GetCurrentItem returns the current item containing key/value pair.

func (*Btree[TK, TV]) GetCurrentKey

func (btree *Btree[TK, TV]) GetCurrentKey() TK

GetCurrentKey returns the current item's key part.

func (*Btree[TK, TV]) GetCurrentValue

func (btree *Btree[TK, TV]) GetCurrentValue() (TV, error)

GetCurrentValue returns the current item's value part.

func (*Btree[TK, TV]) IsUnique

func (btree *Btree[TK, TV]) IsUnique() bool

IsUnique returns true if B-Tree is specified to store items with Unique keys, otherwise false.

func (*Btree[TK, TV]) IsValueDataInNodeSegment

func (btree *Btree[TK, TV]) IsValueDataInNodeSegment() bool

IsValueDataInNodeSegment is true if Item's Values are stored in the Node segment together with the Items' Keys. Always true in in-memory B-Tree.

func (*Btree[TK, TV]) MoveToFirst

func (btree *Btree[TK, TV]) MoveToFirst() (bool, error)

MoveToFirst will traverse the tree and find the first item, first according to the key ordering sequence.

func (*Btree[TK, TV]) MoveToLast

func (btree *Btree[TK, TV]) MoveToLast() (bool, error)

func (*Btree[TK, TV]) MoveToNext

func (btree *Btree[TK, TV]) MoveToNext() (bool, error)

func (*Btree[TK, TV]) MoveToPrevious

func (btree *Btree[TK, TV]) MoveToPrevious() (bool, error)

func (*Btree[TK, TV]) Remove

func (btree *Btree[TK, TV]) Remove(key TK) (bool, error)

Remove will find the item with given key and delete it.

func (*Btree[TK, TV]) RemoveCurrentItem

func (btree *Btree[TK, TV]) RemoveCurrentItem() (bool, error)

RemoveCurrentItem will remove the current item, i.e. - referenced by CurrentItemRef.

func (*Btree[TK, TV]) Update

func (btree *Btree[TK, TV]) Update(key TK, value TV) (bool, error)

Update will find the item with matching key as the key parameter & update its value with the provided value parameter.

func (*Btree[TK, TV]) UpdateCurrentItem

func (btree *Btree[TK, TV]) UpdateCurrentItem(newValue TV) (bool, error)

type BtreeInterface

type BtreeInterface[TK Comparable, TV any] interface {
	// Add adds an item to the b-tree and does not check for duplicates.
	Add(key TK, value TV) (bool, error)
	// AddIfNotExist adds an item if there is no item matching the key yet.
	// Otherwise, it will do nothing and return false, for not adding the item.
	// This is useful for cases one wants to add an item without creating a duplicate entry.
	AddIfNotExist(key TK, value TV) (bool, error)
	// FindOne will search Btree for an item with a given key. Return true if found,
	// otherwise false. firstItemWithKey is useful when there are items with same key.
	// true will position pointer to the first item with the given key,
	// according to key ordering sequence.
	FindOne(key TK, firstItemWithKey bool) (bool, error)
	// GetCurrentKey returns the current item's key.
	GetCurrentKey() TK
	// GetCurrentValue returns the current item's value.
	GetCurrentValue() (TV, error)
	// Update finds the item with key and update its value to the value argument.
	Update(key TK, value TV) (bool, error)
	// UpdateCurrentItem will update the Value of the current item.
	// Key is read-only, thus, no argument for the key.
	UpdateCurrentItem(newValue TV) (bool, error)
	// Remove will find the item with a given key then remove that item.
	Remove(key TK) (bool, error)
	// RemoveCurrentItem will remove the current key/value pair from the store.
	RemoveCurrentItem() (bool, error)

	// Cursor like "move" functions. Use the CurrentKey/CurrentValue to retrieve the
	// "current item" details(key &/or value).
	MoveToFirst() (bool, error)
	MoveToLast() (bool, error)
	MoveToNext() (bool, error)
	MoveToPrevious() (bool, error)
	// IsValueDataInNodeSegment is true if "Value" data is stored in the B-Tree node's segment.
	// Otherwise is false.
	IsValueDataInNodeSegment() bool

	// IsUnique returns true if B-Tree is specified to store items with Unique keys, otherwise false.
	// Specifying uniqueness base on key makes the B-Tree permanently set. If you want just a temporary
	// unique check during Add of an item, then you can use AddIfNotExist method for that.
	IsUnique() bool
}

BtreeInterface defines publicly callable methods of Btree.

type Comparable

type Comparable interface {
	cmp.Ordered | *Comparer | any
}

Comparable interface is used as a B-Tree store (generics) constraint for Key types.

type Comparer

type Comparer interface {
	// Implement Compare to compare this object with the 'other' object.
	// Returns -1 (this object < other), 0 (this object == other), 1 (this object > other)
	Compare(other interface{}) int
}

Comparer interface specifies the Compare function.

type EntityType

type EntityType uint

type Handle

type Handle struct {
	LogicalId   UUID
	PhysicalIdA UUID
	PhysicalIdB UUID
	IsActiveIdB bool
	Version     int
	IsDeleted   bool
}

Handle is a structure that holds Logical Id and the underlying current Physical Id it maps to. E.g. - Node, Slot Value, etc... It also contains other fields useful for allowing transaction manager to effectively manage & allow seamless switching of data "objects", e.g. a modified Node or Value Data in a transaction can get switched to be the "active" one upon commit, and thus, start to get seen by succeeding SOP I/O.

func NewHandle

func NewHandle() Handle

NewHandle creates a new Handle.

func ToHandle

func ToHandle(lid UUID, physIdA UUID) Handle

ToHandle converts logical & physical UUIDs to a handle, a.k.a. - virtual Id.

func (Handle) GetActiveId

func (id Handle) GetActiveId() UUID

GetActiveId returns the currently active (if there is) UUID of a given Handle.

func (Handle) IsEmpty

func (id Handle) IsEmpty() bool

func (Handle) ToString

func (id Handle) ToString() string

ToString method of Handle returns the Handle's currently Active Id's string value.

type Item

type Item[TK Comparable, TV any] struct {
	// Key is the key part in key/value pair.
	Key TK
	// Value is saved nil if data is to be persisted in the "data segment"(& ValueId set to a valid UUID),
	// otherwise it should point to the actual data and persisted in B-Tree Node segment together with the Key.
	Value *TV
	// ValueLogicalId should be a valid (logical) Id of the data if it is saved in the "data segment",
	// otherwise this should be nil(unused).
	ValueLogicalId UUID
	Version        int
	// contains filtered or unexported fields
}

Item contains key & value pair, plus the version number.

type Node

type Node[TK Comparable, TV any] struct {
	Id        UUID
	ParentId  UUID
	Slots     []*Item[TK, TV]
	Count     int
	Version   int
	IsDeleted bool
	// contains filtered or unexported fields
}

Node contains a B-Tree node's data.

type NodeDataBlocks

type NodeDataBlocks struct {
	Id               Handle
	SlotDataBlock    []byte
	SlotDataBlockIds []UUID
	Children         []UUID

	Version   int
	IsDeleted bool
	// contains filtered or unexported fields
}

type NodeRepository

type NodeRepository[TK Comparable, TV any] interface {
	Get(nodeId UUID) (*Node[TK, TV], error)
	Upsert(*Node[TK, TV]) error
	Remove(nodeId UUID) error
}

NodeRepository interface specifies the node repository.

type Recyclable

type Recyclable struct {
	ObjectType int
	ObjectId   UUID
	LockDate   int64
	IsDeleted  bool
}

type RecyclerRepository

type RecyclerRepository interface {
	Get(itemCount int, objectType int) []Recyclable
	Add(recyclables []Recyclable) error
	Remove(items []Recyclable) error
}

RecyclerRepository provides capability to recycle storage areas for storing data such as Node, etc... There are backends where this is not needed at all, e.g. Cassandra backend will not need this.

type SlotValue

type SlotValue struct {
	Id        Handle
	Value     []byte
	IsDeleted bool
}

type SlotValueDataBlocks

type SlotValueDataBlocks struct {
	Id                Handle
	Value             []byte
	ValueDataBlockIds []UUID
	IsDeleted         bool
}

type Store

type Store struct {
	// Name of this (B-Tree store).
	Name string
	// Count of items that can be stored on a given node.
	SlotLength int
	// IsUnique tells whether key/value pair (items) of this tree should be unique on key.
	IsUnique  bool
	KeyInfo   string
	ValueInfo string
	// RootNodeId is the root node's Id.
	RootNodeId UUID
	// Total count of items stored.
	Count int64
	// Version number.
	Version int
	// Is marked deleted or not.
	IsDeleted bool
	// IsValueDataInNodeSegment is true if "Value" data is stored in the B-Tree node's data segment.
	// Otherwise is false.
	IsValueDataInNodeSegment bool
}

Store contains a given (B-Tree) store details.

func NewStore

func NewStore(name string, slotLength int, isUnique bool, isValueDataInNodeSegment bool) Store

NewStore instantiates a new Store.

type StoreInterface

type StoreInterface[TK Comparable, TV any] struct {
	// StoreRepository is used to manage/access stores.
	StoreRepository StoreRepository
	// NodeRepository is used to manage/access B-Tree nodes.
	NodeRepository NodeRepository[TK, TV]
	// VirtualIdRepository is used to manage/access all objects keyed off of their virtual Ids (UUIDs).
	VirtualIdRepository VirtualIdRepository
	// RecyclerRepository is used to manage/access all deleted objects' "data blocks".
	RecyclerRepository RecyclerRepository
	// TransactionRepository is used to manage a transaction.
	TransactionRepository TransactionRepository
	// Transaction object if there is one.
	Transaction Transaction
}

StoreInterface contains different repositories needed/used by B-Tree to manage/access its data/objects.

type StoreRepository

type StoreRepository interface {
	Get(name string) (Store, error)
	Add(Store) error
	Remove(name string) error
}

StoreRepository interface specifies the store repository.

type Transaction

type Transaction interface {
	Begin() error
	Commit() error
	Rollback() error
	HasBegun() bool
}

Transaction interface defines the "enduser facing" transaction methods.

type TransactionActionType

type TransactionActionType uint

type TransactionEntry

type TransactionEntry struct {
	EntityLogicalId UUID
	EntityType      EntityType
	Sequence        int
	Action          TransactionActionType
	IsDeleted       bool
}

TransactionEntry contain info about each Store Item modified within a Transaction. NOTE: newly created Stores themselves don't get tracked within the Transaction Entry table. Their items do. New Stores are cached in-memory and get saved (conflict resolved) during Transaction Commit.

type TransactionRepository

type TransactionRepository interface {
	Get(transactionId UUID) ([]TransactionEntry, error)
	GetByStore(transactionId UUID, storeName string) ([]TransactionEntry, error)
	Add([]TransactionEntry) error
	MarkDone([]TransactionEntry) error
}

type UUID

type UUID uuid.UUID

UUID type.

var NilUUID UUID

NillUUID is an empty UUID.

func NewUUID

func NewUUID() UUID

NewUUID returns a new UUID.

func ToUUID

func ToUUID(id string) UUID

func (UUID) IsNil

func (id UUID) IsNil() bool

func (UUID) ToString

func (id UUID) ToString() string

type VirtualIdRepository

type VirtualIdRepository interface {
	Get(lid UUID) (Handle, error)
	Add(Handle) error
	Update(Handle) error
	Remove(lid UUID) error
}

VirtualIdRepository interface specifies the "virtualized Id" repository, a.k.a. Id registry.

Jump to

Keyboard shortcuts

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