Shard

public class Shard

Represents a shard in the database, which is a unit of data storage and management.

A Shard contains metadata, a unique identifier, and configuration details such as compression and file protection settings. It also includes a cache for storing documents.

  • Properties:
    • id: A unique identifier for the shard.
    • url: The file URL where the shard is stored.
    • metadata: Metadata associated with the shard, such as its state and configuration.
    • compressionMethod: The method used to compress data within the shard.
    • fileProtectionType: The file protection level applied to the shard’s storage.
    • documentCache: An in-memory cache for storing documents, keyed by their identifiers.
  • id

    Undocumented

    Declaration

    Swift

    public let id: String
  • url

    Undocumented

    Declaration

    Swift

    public let url: URL
  • Undocumented

    Declaration

    Swift

    public private(set) var metadata: ShardMetadata { get }
  • Undocumented

    Declaration

    Swift

    public let compressionMethod: CompressionMethod
  • Undocumented

    Declaration

    Swift

    public let fileProtectionType: FileProtectionType
  • Initializes a new instance of Shard.

    Declaration

    Swift

    public init(
        id: String,
        url: URL,
        metadata: ShardMetadata = .init(),
        compressionMethod: CompressionMethod = .none,
        fileProtectionType: FileProtectionType = .none
    )

    Parameters

    id

    A unique identifier for the shard.

    url

    The file URL where the shard is stored.

    metadata

    Metadata associated with the shard. Defaults to an empty ShardMetadata instance.

    compressionMethod

    The compression method used for the shard. Defaults to .none.

    fileProtectionType

    The file protection type for the shard. Defaults to .none.

  • Updates the metadata of the shard with the given document count and update timestamp.

    Declaration

    Swift

    public func updateMetadata(documentCount: Int, updatedAt: Date)

    Parameters

    documentCount

    The number of documents in the shard.

    updatedAt

    The date and time when the shard was last updated.

  • loadDocuments() Asynchronous

    Loads and decodes documents of the specified type from the shard.

    This method asynchronously retrieves and decodes documents stored in the shard into an array of the specified Codable type.

    Throws

    An error if the documents cannot be loaded or decoded.

    Note

    Ensure that the type T conforms to the Codable protocol.

    Declaration

    Swift

    public func loadDocuments<T>() async throws -> [T] where T : Decodable, T : Encodable

    Return Value

    An array of decoded documents of type T.

  • saveDocuments(_:) Asynchronous

    Saves an array of documents to the shard.

    Throws

    An error if the save operation fails.

    Note

    This is an asynchronous function and should be called with await.

    Declaration

    Swift

    public func saveDocuments<T>(_ documents: [T]) async throws where T : Decodable, T : Encodable

    Parameters

    documents

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

  • Appends a document to the shard.

    Declaration

    Swift

    public func appendDocument<T: Codable>(_ document: T, jsonData: Data)
        async throws

    Parameters

    document

    The document to append, conforming to the Codable protocol.

    jsonData

    The JSON-encoded data representation of the document.

  • Loads documents lazily as an asynchronous throwing stream.

    This method returns an AsyncThrowingStream that allows you to iterate over documents of the specified type T conforming to Codable. The documents are loaded lazily, meaning they are fetched and decoded on demand as you iterate through the stream.

    Throws

    An error if the stream encounters an issue while loading or decoding the documents.

    Declaration

    Swift

    public func loadDocumentsLazy<T>() -> AsyncThrowingStream<T, Error> where T : Decodable, T : Encodable

    Return Value

    An AsyncThrowingStream of type T that provides access to the documents. The stream may throw an error during iteration if an issue occurs while loading or decoding the documents.