NyaruDB2
public class NyaruDB2A 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 DeclarationSwift public let storage: StorageEngine
- 
                  
                  Undocumented DeclarationSwift public let indexManager: IndexManager<String>
- 
                  
                  Initializes a new instance of NyaruDB2.Throws An error if the initialization fails.DeclarationSwift public init( path: String = "NyaruDB2", compressionMethod: CompressionMethod = .none, fileProtectionType: FileProtectionType = .none ) throwsParameterspathThe 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.DeclarationSwift public func getCollection(named name: String) -> DocumentCollection?ParametersnameThe name of the collection to retrieve. Return ValueA 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.DeclarationSwift public func createCollection(name: String, indexes: [String] = [], partitionKey: String) async throws -> DocumentCollectionParametersnameThe 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 ValueA 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.DeclarationSwift public func insert<T: Codable>( _ document: T, into collection: String, indexField: String? = nil ) async throwsParametersdocumentThe 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.DeclarationSwift public func bulkInsert<T: Codable>( _ documents: [T], into collection: String, indexField: String? = nil ) async throwsParametersdocumentsAn 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.DeclarationSwift public func update<T: Codable>( _ document: T, in collection: String, matching predicate: @escaping (T) -> Bool, indexField: String? = nil ) async throwsParametersdocumentThe 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.DeclarationSwift public func delete<T: Codable>( where predicate: @escaping (T) -> Bool, from collection: String ) async throwsParameterspredicateA 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.DeclarationSwift public func fetch<T>(from collection: String) async throws -> [T] where T : Decodable, T : EncodableParameterscollectionThe name of the collection to fetch documents from. Return ValueAn 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.DeclarationSwift public func fetchLazy<T>(from collection: String) async -> AsyncThrowingStream<T, Error> where T : Decodable, T : EncodableParameterscollectionThe name of the collection to fetch documents from. Return ValueAn 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.DeclarationSwift public func query<T>(from collection: String) async throws -> Query<T> where T : Decodable, T : EncodableParameterscollectionThe name of the collection to query. Return ValueA 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.DeclarationSwift public func countDocuments(in collection: String) async throws -> IntParameterscollectionThe name of the collection to count documents in. Return ValueThe 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.DeclarationSwift public func listCollections() async throws -> [String]Return ValueAn 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.DeclarationSwift public func dropCollection(_ collection: String) async throwsParameterscollectionThe 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.DeclarationSwift public func getCollectionStats(for collection: String) async throws -> CollectionStatsParameterscollectionThe name of the collection for which to retrieve statistics. Return ValueA 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.DeclarationSwift public func getGlobalStats() async throws -> GlobalStatsReturn ValueA 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.DeclarationSwift public func getIndexStats() async throws -> [String : IndexStat]Return ValueA 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.DeclarationSwift public func getShardStats() async throws -> [ShardStat]Return ValueAn array of ShardStatobjects representing the statistics of each shard.
 View on GitHub
View on GitHub