NyaruDB2
public class NyaruDB2
A class representing the NyaruDB2 database system.
This class provides the core functionality for managing collections, storage, indexing, and statistics within the database.
- Properties:
storage: The storage engine responsible for handling data persistence.indexManager: The index manager responsible for managing indexes for efficient data retrieval.statsEngine: A private engine for tracking and managing database statistics.collections: A private dictionary mapping collection names to their respective document collections.
-
Undocumented
Declaration
Swift
public let storage: StorageEngine -
Undocumented
Declaration
Swift
public let indexManager: IndexManager<String> -
Initializes a new instance of
NyaruDB2.Throws
An error if the initialization fails.Declaration
Swift
public init( path: String = "NyaruDB2", compressionMethod: CompressionMethod = .none, fileProtectionType: FileProtectionType = .none ) throwsParameters
pathThe file path where the database will be stored. Defaults to
"NyaruDB2".compressionMethodThe method used for compressing the database. Defaults to
.none.fileProtectionTypeThe file protection level for the database. Defaults to
.none. -
Retrieves a
DocumentCollectionwith the specified name.Declaration
Swift
public func getCollection(named name: String) -> DocumentCollection?Parameters
nameThe name of the collection to retrieve.
Return Value
A
DocumentCollectioninstance if a collection with the specified name exists, otherwisenil. -
createCollection(name:Asynchronousindexes: partitionKey: ) Creates a new collection in the database.
Throws
An error if the collection cannot be created.Declaration
Swift
public func createCollection(name: String, indexes: [String] = [], partitionKey: String) async throws -> DocumentCollectionParameters
nameThe name of the collection to be created.
indexesAn optional array of field names to create indexes on. Defaults to an empty array.
partitionKeyThe field name to be used as the partition key for the collection.
Return Value
A
DocumentCollectionrepresenting the newly created collection. -
insert(_:Asynchronousinto: indexField: ) Inserts a document into the specified collection in the database.
Throws
An error if the insertion fails.Note
This is an asynchronous operation.Declaration
Swift
public func insert<T: Codable>( _ document: T, into 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 inserted.
indexFieldAn optional field to be used as an index for the document. Defaults to
nil. -
bulkInsert(_:Asynchronousinto: indexField: ) Inserts multiple documents into the specified collection in bulk.
Throws
An error if the insertion fails.Note
This method is asynchronous and should be called withawait.Declaration
Swift
public func bulkInsert<T: Codable>( _ documents: [T], into 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. -
update(_: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 update<T: Codable>( _ document: T, in collection: String, matching predicate: @escaping (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 for updating.indexFieldAn optional field name to use as an index for optimizing the update operation. Defaults to
nil. -
delete(where:Asynchronousfrom: ) Deletes objects of type
Tfrom the specified collection that satisfy the given predicate.Throws
An error if the deletion process fails.Note
This is an asynchronous operation.Declaration
Swift
public func delete<T: Codable>( where predicate: @escaping (T) -> Bool, from collection: String ) async throwsParameters
predicateA closure that takes an object of type
Tas its argument and returns a Boolean value indicating whether the object should be deleted.collectionThe name of the collection from which the objects should be deleted.
-
fetch(from:Asynchronous) Fetches all documents from the specified collection and decodes them into an array of the specified
Codabletype.Throws
An error if the fetch operation fails or if decoding the documents fails.Declaration
Swift
public func fetch<T>(from collection: String) async throws -> [T] where T : Decodable, T : EncodableParameters
collectionThe name of the collection to fetch documents from.
Return Value
An array of decoded objects of type
T. -
fetchLazy(from:Asynchronous) Fetches documents lazily from the specified collection as an asynchronous throwing stream.
This method allows you to retrieve documents from a collection in a lazy manner, meaning that documents are fetched and processed one at a time, reducing memory usage for large datasets. The returned stream can throw errors during iteration if any issues occur while fetching the documents.
Throws
An error if the fetching process encounters an issue.
Note
The generic type
Tmust conform to theCodableprotocol.Declaration
Swift
public func fetchLazy<T>(from collection: String) async -> AsyncThrowingStream<T, Error> where T : Decodable, T : EncodableParameters
collectionThe name of the collection to fetch documents from.
Return Value
An
AsyncThrowingStreamof typeTcontaining the fetched documents. -
query(from:Asynchronous) Executes a query on the specified collection and returns a
Queryobject.Throws
An error if retrieving index or shard statistics fails.Note
This function is asynchronous and must be called withawait.Declaration
Swift
public func query<T>(from collection: String) async throws -> Query<T> where T : Decodable, T : EncodableParameters
collectionThe name of the collection to query.
Return Value
A
Queryobject of the specified generic typeTconforming toCodable. -
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.
-
listCollections()AsynchronousLists all the collections available in the database.
Throws
An error if the operation to list collections fails.Note
This is an asynchronous function and must be called withawait.Declaration
Swift
public func listCollections() async throws -> [String]Return Value
An array of strings representing the names of the collections.
-
dropCollection(_:Asynchronous) Drops the specified collection from the database.
Throws
An error if the operation fails.Note
This operation is asynchronous and may involve I/O operations.Declaration
Swift
public func dropCollection(_ collection: String) async throwsParameters
collectionThe name of the collection to be dropped.
-
getCollectionStats(for:Asynchronous) Retrieves the statistics for a specified collection.
Throws
An error if the statistics could not be retrieved.Note
This function is asynchronous and must be awaited.Declaration
Swift
public func getCollectionStats(for collection: String) async throws -> CollectionStatsParameters
collectionThe name of the collection for which to retrieve statistics.
Return Value
A
CollectionStatsobject containing the statistics of the specified collection. -
getGlobalStats()AsynchronousRetrieves the global statistics of the database.
This asynchronous function fetches and returns the global statistics by utilizing the
statsEngine. The returnedGlobalStatsobject contains aggregated information about the database’s state.Throws
An error if the statistics retrieval fails.Declaration
Swift
public func getGlobalStats() async throws -> GlobalStatsReturn Value
A
GlobalStatsobject containing the global statistics. -
getIndexStats()AsynchronousRetrieves statistics for all indexes in the database.
This asynchronous function fetches index statistics using the
statsEngine.Throws
An error if the operation to fetch index statistics fails.Declaration
Swift
public func getIndexStats() async throws -> [String : IndexStat]Return Value
A dictionary where the keys are index names (
String) and the values areIndexStatobjects containing the statistics for each index. -
getShardStats()AsynchronousRetrieves statistics for all shards.
This asynchronous function fetches and returns an array of
ShardStatobjects, which contain statistical information about the shards managed by the database.Throws
An error if the statistics could not be retrieved.Declaration
Swift
public func getShardStats() async throws -> [ShardStat]Return Value
An array of
ShardStatobjects representing the statistics of each shard.
View on GitHub