Skip to content

NodeStorageProtocol

ratranqu edited this page Apr 7, 2021 · 2 revisions

NodeStorageProtocol

public protocol NodeStorageProtocol: CustomStringConvertible

Inheritance

CustomStringConvertible

Requirements

root

Current working root (unless just reset or committed, not committed yet)

var root: Node

version

Current working version (not committed yet)

var version: Int64

versions

Orphans organized by version All available versions from the Storage

var versions: [Int64]

root(at:​)

Get the root node at provided version.

func root(at version: Int64) throws -> Node?

Parameters

  • version: requested version number

Returns

the corresponding root node or nil if doesn't exist

get(key:​at:​)

func get(key: Key, at version: Int64) throws -> (index: Int64, value: Value?)

get(index:​at:​)

func get(index: Int64, at version: Int64) throws -> (key: Key, value: Value)?

has(key:​at:​)

func has(key: Key, at version: Int64) throws -> Bool

next(key:​at:​)

func next(key: Key, at version: Int64) throws -> Key?

get(key:​)

func get(key: Key) throws -> (index: Int64, value: Value?)

get(index:​)

func get(index: Int64) throws -> (key: Key, value: Value)?

has(key:​)

func has(key: Key) throws -> Bool

next(key:​)

func next(key: Key) throws -> Key?

deleteLast()

Deletes the last saved version, adjusts the current version number accordingly It will delete the root and the orphans The current version is set to the deleted version, and the current orphans are set to the orphans of the deleted version

func deleteLast() throws

deleteAll(from:​)

Deletes all versions from version from onwards. The roots and orphans are adjusted as if deleteLast() was called as many times as needed to reach version from The current version is set to from, as well as corresponding orphans list.

func deleteAll(from: Int64) throws

rollback()

Rolls back all changes to the tree since last save. That means that the root is now the same as the root as of version - 1 and the orphans list for version version is empty version remains as is

func rollback()

commit()

Commits changes to the tree by saving them in the backing Storage. version is incremented, current root is set to just saved version and orphans for this version is set to empty.

func commit() throws

description

var description: String

set(key:​value:​)

Adds or Updates a leaf in the tree and recalculates (hashes, size, height, balancing) the tree when required. When the item is updated, will return true. Returns false otherwise This will lead to updating the tree and orphans for the current version

func set(key: Key, value: Value) throws -> Bool

remove(key:​)

Removes the leaf with key key if it exists. If the leaf exists, removes the leaf node, returns its value and true and rebalances the tree as needed it also adjusts as hashes, size and height where needed. If the leaf doesn't exist, returns nil fort he value and false for the removed boolean.

func remove(key: Key) -> (Value?, Bool)

makeEmpty()

func makeEmpty() -> Node

makeLeaf(key:​value:​)

func makeLeaf(key: Key, value: Value) -> Node

makeInner(key:​left:​right:​)

func makeInner(key: Key, left: Node, right: Node) -> Node