BTreeIndex

public actor BTreeIndex<Key> where Key : Comparable, Key : Decodable, Key : Encodable

An actor-based implementation of a B-Tree index for managing and organizing data.

BTreeIndex is a generic class that supports keys conforming to both Comparable and Codable protocols. It provides thread-safe operations for indexing and retrieving data in a concurrent environment.

Note

This implementation leverages Swift’s actor model to ensure data consistency and thread safety.

Parameters

Key

The type of the keys used in the B-Tree. Keys must conform to Comparable for ordering and Codable for serialization.

  • Initializes a new instance of the BTreeIndex with a specified minimum degree.

    Declaration

    Swift

    public init(minimumDegree: Int = 2)

    Parameters

    minimumDegree

    The minimum degree of the B-tree. This determines the minimum number of children each internal node must have. The default value is 2.

  • Searches for the specified key in the B-Tree index and returns the associated data.

    Declaration

    Swift

    public func search(key: Key) -> [Data]?

    Parameters

    key

    The key to search for in the B-Tree index.

    Return Value

    An optional array of Data associated with the given key. Returns nil if the key is not found.

  • Loads a persisted index from the provided data.

    This method asynchronously reads and reconstructs the index structure from the given binary data. It is used to restore the state of the index from a previously saved state.

    Throws

    An error if the data cannot be parsed or the index cannot be reconstructed.

    Declaration

    Swift

    public func loadPersistedIndex(from data: Data) async throws

    Parameters

    data

    The binary data containing the persisted index.

    Return Value

    This method does not return a value but updates the index state internally.

  • Loads a persisted index from the specified file URL.

    This asynchronous method attempts to load the index data from the given file URL. If the operation fails, it throws an error.

    Throws

    An error if the index cannot be loaded from the specified URL.

    Declaration

    Swift

    public func loadPersistedIndex(from url: URL) async throws

    Parameters

    url

    The file URL pointing to the persisted index data.

    Return Value

    This method does not return a value but completes asynchronously once the index is successfully loaded or an error is thrown.

  • Inserts a key-value pair into the B-tree index.

    Declaration

    Swift

    public func insert(key: Key, data: Data)

    Parameters

    key

    The key to be inserted into the index.

    data

    The associated data to be stored with the key.

  • Performs an in-order traversal of the B-Tree index and returns an array of Data elements.

    Declaration

    Swift

    public func inOrder() -> [Data]

    Return Value

    An array of Data elements in the order they appear during an in-order traversal.

  • persist(to:) Asynchronous

    Persists the current state of the B-tree index to the specified file URL.

    This method asynchronously writes the index data to the provided URL, ensuring that the index can be saved and restored later. It is useful for maintaining the state of the index across application launches or sessions.

    Throws

    An error if the persistence operation fails.

    Note

    Ensure that the provided URL is writable and that there is sufficient storage space available for the operation to complete successfully.

    Declaration

    Swift

    public func persist(to url: URL) async throws

    Parameters

    url

    The file URL where the index data should be persisted.

  • Retrieves a subset of data from the B-tree index based on the specified offset and limit.

    Declaration

    Swift

    public func page(offset: Int, limit: Int) -> [Data]

    Parameters

    offset

    The starting position in the data set from which to begin retrieving items.

    limit

    The maximum number of items to retrieve.

    Return Value

    An array of Data objects representing the requested subset of the index.

  • getTotalCount() Asynchronous

    Undocumented

    Declaration

    Swift

    public func getTotalCount() async -> Int