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.
-
Undocumented
Declaration
Swift
public let id: String -
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
idA unique identifier for the shard.
urlThe file URL where the shard is stored.
metadataMetadata associated with the shard. Defaults to an empty
ShardMetadatainstance.compressionMethodThe compression method used for the shard. Defaults to
.none.fileProtectionTypeThe 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
documentCountThe number of documents in the shard.
updatedAtThe date and time when the shard was last updated.
-
loadDocuments()AsynchronousLoads 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
Codabletype.Throws
An error if the documents cannot be loaded or decoded.Note
Ensure that the typeTconforms to theCodableprotocol.Declaration
Swift
public func loadDocuments<T>() async throws -> [T] where T : Decodable, T : EncodableReturn 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 withawait.Declaration
Swift
public func saveDocuments<T>(_ documents: [T]) async throws where T : Decodable, T : EncodableParameters
documentsAn array of documents conforming to the
Codableprotocol to be saved. -
appendDocument(_:AsynchronousjsonData: ) Appends a document to the shard.
Declaration
Swift
public func appendDocument<T: Codable>(_ document: T, jsonData: Data) async throwsParameters
documentThe document to append, conforming to the
Codableprotocol.jsonDataThe JSON-encoded data representation of the document.
-
Loads documents lazily as an asynchronous throwing stream.
This method returns an
AsyncThrowingStreamthat allows you to iterate over documents of the specified typeTconforming toCodable. 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 : EncodableReturn Value
An
AsyncThrowingStreamof typeTthat provides access to the documents. The stream may throw an error during iteration if an issue occurs while loading or decoding the documents.
View on GitHub