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
    ) throws

    Parameters

    path

    The file path where the database will be stored. Defaults to "NyaruDB2".

    compressionMethod

    The method used for compressing the database. Defaults to .none.

    fileProtectionType

    The file protection level for the database. Defaults to .none.

  • Retrieves a DocumentCollection with the specified name.

    Declaration

    Swift

    public func getCollection(named name: String) -> DocumentCollection?

    Parameters

    name

    The name of the collection to retrieve.

    Return Value

    A DocumentCollection instance if a collection with the specified name exists, otherwise nil.

  • 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 -> DocumentCollection

    Parameters

    name

    The name of the collection to be created.

    indexes

    An optional array of field names to create indexes on. Defaults to an empty array.

    partitionKey

    The field name to be used as the partition key for the collection.

    Return Value

    A DocumentCollection representing the newly created collection.

  • 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 throws

    Parameters

    document

    The document to be inserted. Must conform to the Codable protocol.

    collection

    The name of the collection where the document will be inserted.

    indexField

    An optional field to be used as an index for the document. Defaults to nil.

  • 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 with await.

    Declaration

    Swift

    public func bulkInsert<T: Codable>(
        _ documents: [T],
        into collection: String,
        indexField: String? = nil
    ) async throws

    Parameters

    documents

    An array of documents conforming to the Codable protocol to be inserted.

    collection

    The name of the collection where the documents will be inserted.

    indexField

    An optional field name to be used as an index for the documents. Defaults to nil.

  • 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 throws

    Parameters

    document

    The document of type T to update. Must conform to Codable.

    collection

    The name of the collection where the document resides.

    predicate

    A closure that takes a document of type T as its argument and returns a Boolean value indicating whether the document matches the criteria for updating.

    indexField

    An optional field name to use as an index for optimizing the update operation. Defaults to nil.

  • delete(where:from:) Asynchronous

    Deletes objects of type T from 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 throws

    Parameters

    predicate

    A closure that takes an object of type T as its argument and returns a Boolean value indicating whether the object should be deleted.

    collection

    The 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 Codable type.

    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 : Encodable

    Parameters

    collection

    The 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 T must conform to the Codable protocol.

    Declaration

    Swift

    public func fetchLazy<T>(from collection: String) async -> AsyncThrowingStream<T, Error> where T : Decodable, T : Encodable

    Parameters

    collection

    The name of the collection to fetch documents from.

    Return Value

    An AsyncThrowingStream of type T containing the fetched documents.

  • query(from:) Asynchronous

    Executes a query on the specified collection and returns a Query object.

    Throws

    An error if retrieving index or shard statistics fails.

    Note

    This function is asynchronous and must be called with await.

    Declaration

    Swift

    public func query<T>(from collection: String) async throws -> Query<T> where T : Decodable, T : Encodable

    Parameters

    collection

    The name of the collection to query.

    Return Value

    A Query object of the specified generic type T conforming to Codable.

  • 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 with await.

    Declaration

    Swift

    public func countDocuments(in collection: String) async throws -> Int

    Parameters

    collection

    The name of the collection to count documents in.

    Return Value

    The total number of documents in the specified collection.

  • listCollections() Asynchronous

    Lists 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 with await.

    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 throws

    Parameters

    collection

    The name of the collection to be dropped.

  • 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 -> CollectionStats

    Parameters

    collection

    The name of the collection for which to retrieve statistics.

    Return Value

    A CollectionStats object containing the statistics of the specified collection.

  • getGlobalStats() Asynchronous

    Retrieves the global statistics of the database.

    This asynchronous function fetches and returns the global statistics by utilizing the statsEngine. The returned GlobalStats object contains aggregated information about the database’s state.

    Throws

    An error if the statistics retrieval fails.

    Declaration

    Swift

    public func getGlobalStats() async throws -> GlobalStats

    Return Value

    A GlobalStats object containing the global statistics.

  • getIndexStats() Asynchronous

    Retrieves 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 are IndexStat objects containing the statistics for each index.

  • getShardStats() Asynchronous

    Retrieves statistics for all shards.

    This asynchronous function fetches and returns an array of ShardStat objects, 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 ShardStat objects representing the statistics of each shard.