StorageEngine
public actor StorageEngine
The StorageEngine actor is responsible for managing the core storage functionality of the database.
It handles the configuration, partitioning, and management of collections, shards, and indexes.
Properties:
baseURL: The base URL where the storage engine operates.compressionMethod: The method used for compressing stored data.fileProtectionType: The type of file protection applied to stored data.collectionPartitionKeys: A dictionary mapping collection names to their partition keys.activeShardManagers: A dictionary of active shard managers, keyed by collection name.indexManagers: A dictionary of index managers, keyed by collection name.statsEngine: A lazily initialized instance ofStatsEnginefor gathering storage statistics.
-
Undocumented
Declaration
Swift
public var collectionPartitionKeys: [String : String] -
Undocumented
Declaration
Swift
public var activeShardManagers: [String : ShardManager] -
Undocumented
Declaration
Swift
public var indexManagers: [String : IndexManager<String>] -
An enumeration representing errors that can occur in the storage engine.
This enum conforms to the
See moreErrorprotocol, allowing instances ofStorageErrorto be thrown and handled as part of Swift’s error-handling mechanism.Declaration
Swift
public enum StorageError : Error -
Initializes a new instance of the
StorageEngineclass.Declaration
Swift
public init( path: String, compressionMethod: CompressionMethod = .none, fileProtectionType: FileProtectionType = .none ) throwsParameters
pathThe file path where the storage engine will operate.
compressionMethodThe method used for compressing data. Defaults to
.none.fileProtectionTypeThe file protection level to apply. Defaults to
.none. -
insertDocument(_:Asynchronouscollection: indexField: ) Inserts a document into the specified collection in the storage engine.
Throws
An error if the insertion fails.Note
This method is asynchronous and must be called withawait.Declaration
Swift
public func insertDocument<T: Codable>( _ document: T, collection: String, indexField: String? = nil ) async throwsParameters
documentThe document to be inserted. Must conform to the
Codableprotocol.collectionThe name of the collection where the document will be stored.
indexFieldAn optional field to be used as an index for the document. Defaults to
nil. -
fetchDocuments(from:Asynchronous) Fetches documents from the specified collection and decodes them into the specified type.
This method retrieves all documents from the given collection and attempts to decode them into the specified
CodabletypeT. The operation is asynchronous and may throw an error if the fetch or decoding process fails.Throws
An error if the fetch operation or decoding process fails.Declaration
Swift
public func fetchDocuments<T: Codable>(from collection: String) async throws -> [T]Parameters
collectionThe name of the collection to fetch documents from.
Return Value
An array of decoded objects of type
T. -
fetchFromIndex(collection:Asynchronousfield: value: ) Fetches records from the specified collection that match the given field and value.
This method performs an asynchronous operation to retrieve records from the index of the specified collection. The records are decoded into the specified generic type
T.Throws
An error if the fetch operation fails or if decoding the records fails.Declaration
Swift
public func fetchFromIndex<T: Codable>( collection: String, field: String, value: String ) async throws -> [T]Parameters
collectionThe name of the collection to fetch records from.
fieldThe name of the field to match against.
valueThe value to match in the specified field.
Return Value
An array of decoded objects of type
Tthat match the specified criteria. -
Fetches documents lazily from the specified collection.
This method returns an
AsyncThrowingStreamthat allows you to asynchronously iterate over the documents in the collection. Each document is decoded into the specifiedCodabletypeT.Throws
An error if the operation fails during document retrieval or decoding.Declaration
Swift
public func fetchDocumentsLazy<T: Codable>(from collection: String) -> AsyncThrowingStream<T, Error>Parameters
collectionThe name of the collection to fetch documents from.
Return Value
An
AsyncThrowingStreamof typeTthat provides asynchronous access to the documents in the collection. -
deleteDocuments(where:Asynchronousfrom: ) Deletes documents from the specified collection that satisfy the given predicate.
Throws
An error if the deletion process fails.Note
This method is asynchronous and must be called withawait.Declaration
Swift
public func deleteDocuments<T: Codable>( where predicate: @escaping (T) -> Bool, from collection: String ) async throwsParameters
predicateA closure that takes an instance of type
Tand returns a Boolean value indicating whether the document should be deleted.collectionThe name of the collection from which documents will be deleted.
-
updateDocument(_:Asynchronousin: matching: indexField: ) Updates a document in the specified collection that matches the given predicate.
Throws
An error if the update operation fails.
Note
This method is asynchronous and must be called with
await.Declaration
Swift
public func updateDocument<T: Codable>( _ document: T, in collection: String, matching predicate: (T) -> Bool, indexField: String? = nil ) async throwsParameters
documentThe document of type
Tto update. Must conform toCodable.collectionThe name of the collection where the document resides.
predicateA closure that takes a document of type
Tas its argument and returns a Boolean value indicating whether the document matches the criteria.indexFieldAn optional field name to use as an index for optimizing the update operation.
-
bulkInsertDocuments(_:Asynchronouscollection: indexField: ) Inserts multiple documents into the specified collection in bulk.
Throws
An error if the insertion fails.Note
This method is asynchronous and must be called withawait.Declaration
Swift
public func bulkInsertDocuments<T: Codable>( _ documents: [T], collection: String, indexField: String? = nil ) async throwsParameters
documentsAn array of documents conforming to the
Codableprotocol to be inserted.collectionThe name of the collection where the documents will be inserted.
indexFieldAn optional field name to be used as an index for the documents. Defaults to
nil. -
countDocuments(in:Asynchronous) Counts the number of documents in the specified collection.
Throws
An error if the operation fails.Note
This is an asynchronous method and must be called withawait.Declaration
Swift
public func countDocuments(in collection: String) async throws -> IntParameters
collectionThe name of the collection to count documents in.
Return Value
The total number of documents in the specified collection.
-
dropCollection(_:Asynchronous) Drops the specified collection from the storage engine.
This method removes all data associated with the given collection name.
Throws
An error if the operation fails.Note
This operation is asynchronous and must be awaited.Declaration
Swift
public func dropCollection(_ collection: String) async throwsParameters
collectionThe name of the collection to be dropped.
-
Lists all the collections available in the storage engine.
Throws
An error if the operation fails.Declaration
Swift
public func listCollections() throws -> [String]Return Value
An array of strings representing the names of the collections.
-
getShardManagers(for:Asynchronous) Retrieves the shard managers associated with a specific collection.
This asynchronous function fetches all the shards for the given collection name.
Throws
An error if the operation fails, such as issues with accessing the storage or invalid collection name.Declaration
Swift
public func getShardManagers(for collection: String) async throws -> [Shard]Parameters
collectionThe name of the collection for which shard managers are to be retrieved.
Return Value
An array of
Shardobjects representing the shard managers for the specified collection. -
getShard(forPartition:Asynchronousin: ) Retrieves the shard associated with a specific partition within a given collection.
Declaration
Swift
public func getShard(forPartition partition: String, in collection: String) async throws -> Shard?Parameters
partitionThe identifier of the partition for which the shard is being retrieved.
collectionThe name of the collection containing the partition.
-
bulkUpdateIndexes(collection:Asynchronousupdates: ) Performs a bulk update of indexes for a specified collection.
This method is asynchronous and allows for batch processing of index updates.
Declaration
Swift
public func bulkUpdateIndexes( collection: String, updates: [(indexField: String, indexKey: String, data: Data)] ) asyncParameters
collectionThe name of the collection where the indexes will be updated.
updatesAn array of tuples containing the index field, index key, and associated data to update.
-
Sets the partition key for a specified collection.
Declaration
Swift
public func setPartitionKey(for collection: String, key: String)Parameters
collectionThe name of the collection for which the partition key is being set.
keyThe key to be used as the partition key for the collection.
-
collectionDirectory(for:Asynchronous) Returns the directory URL for the specified collection.
This method asynchronously retrieves the directory URL where the data for the given collection is stored.
Throws
An error if the directory cannot be determined or accessed.Declaration
Swift
public func collectionDirectory(for collection: String) async throws -> URLParameters
collectionThe name of the collection for which the directory URL is required.
Return Value
A
URLpointing to the directory of the specified collection. -
Repartitions a collection by changing its partition key.
This method allows you to repartition an existing collection by specifying a new partition key. The operation is performed asynchronously and may throw an error if the operation fails.
Throws
An error if the repartitioning operation fails.
Declaration
Swift
public func repartitionCollection<T: Codable>( collection: String, newPartitionKey: String, as type: T.Type ) async throwsParameters
collectionThe name of the collection to repartition.
newPartitionKeyThe new partition key to be used for the collection.
typeThe type of the objects stored in the collection, conforming to
Codable. -
cleanupEmptyShards(for:Asynchronous) Undocumented
Declaration
Swift
public func cleanupEmptyShards(for collection: String) async throws
View on GitHub