IndexManager
public actor IndexManager<Key> where Key : IndexKey, Key : Comparable
An actor responsible for managing indices in a database system.
IndexManager
provides functionality to handle the creation, storage, and
metrics of indices for efficient data retrieval. It is generic over a Key
type that conforms to both IndexKey
and Comparable
protocols, ensuring
that the keys used in the indices are suitable for indexing and comparison.
Note
This actor is designed to be thread-safe, leveraging Swift’s actor model to protect its internal state from concurrent access.Properties:
indices
: A dictionary mapping index names (String
) to their correspondingBTreeIndex
instances, which store the actual index data.createdIndexes
: A set of index names (String
) that have been created, ensuring uniqueness and preventing duplication.metrics
: A dictionary mapping index names (String
) to their associatedIndexMetrics
, which provide performance and usage statistics for each index.
-
Initializes a new instance of the
IndexManager
class.This initializer sets up the
IndexManager
with default values.Declaration
Swift
public init()
-
createIndex(for:
AsynchronousminimumDegree: ) Creates an index for the specified field with a given minimum degree.
Note
This function is asynchronous and may involve I/O operations.Declaration
Swift
public func createIndex(for field: String, minimumDegree: Int = 2) async
Parameters
field
The name of the field for which the index will be created.
minimumDegree
The minimum degree of the B-tree used for the index. Defaults to 2.
-
insert(index:
Asynchronouskey: data: ) Inserts a new index entry into the index manager.
Note
This is an asynchronous operation.Declaration
Swift
public func insert(index field: String, key: Key, data: Data) async
Parameters
field
The name of the index field to insert the entry into.
key
The unique key associated with the index entry.
data
The data to be stored in the index entry.
-
search(_:
Asynchronousvalue: ) Searches for records in the database that match the specified field and value.
Note
This is an asynchronous function and should be awaited.Declaration
Swift
public func search(_ field: String, value: Key) async -> [Data]
Parameters
field
The name of the field to search for.
value
The value to match against the specified field.
Return Value
An array of
Data
objects representing the matching records. -
Retrieves the metrics for all indexes managed by the
IndexManager
.Declaration
Swift
public func getMetrics() -> [String : IndexMetrics]
Return Value
A dictionary where the keys are index names (as
String
) and the values areIndexMetrics
objects containing the metrics for each index. -
getIndexCounts()
AsynchronousAsynchronously retrieves the count of indices.
Declaration
Swift
public func getIndexCounts() async -> [String : Int]
Return Value
A dictionary where the keys are index names (as
String
) and the values are their respective counts (asInt
). -
Lists all the indexes managed by the
IndexManager
.Declaration
Swift
public func listIndexes() -> [String]
Return Value
An array of strings representing the names of all indexes.
-
Drops the index associated with the specified field.
Declaration
Swift
public func dropIndex(for field: String) -> Bool
Parameters
field
The name of the field for which the index should be dropped.
Return Value
A Boolean value indicating whether the index was successfully dropped.
-
upsertIndex(for:
AsynchronousjsonData: ) Inserts or updates an index for the specified field using the provided JSON data.
Throws
An error if the operation fails.Note
This method is asynchronous and must be called withawait
.Declaration
Swift
public func upsertIndex(for field: String, jsonData: Data) async throws
Parameters
field
The name of the field for which the index is being upserted.
jsonData
The JSON data containing the index information.