Classes
The following classes are available globally.
-
A write buffer that accumulates document insertions for a single batch flush.
Obtain one via
NyaruCollection.insertBatch(_:). All insertions are synchronous — noawaitneeded. Documents are not written to disk until theinsertBatchbody completes without throwing.See moreNote
Deliberately notSendable— the buffer is unsynchronised and must only be used from theinsertBatchbody that received it.Declaration
Swift
public final class NyaruInsertBatch<T> where T : Decodable, T : Encodable, T : Sendable -
An in-memory sorted array-based index that maps
FieldValuekeys to posting lists ofRecordPointervalues.Architecture choice. This index uses a sorted array with binary search, giving O(log n) point lookups and trivially correct range scans — the same asymptotic performance as a B-tree. An array is significantly simpler to implement, test, and debug. Since the entire index is kept in memory, the O(n) insertion cost of shifting array elements is acceptable for the typical mobile workloads NyaruDB targets (hundreds to low thousands of unique keys).
Persistence is O(n): the entire array is serialised at once using MsgPack and compressed with gzip. For the expected key counts this is negligible.
Class semantics.
OrderedIndexis afinal classrather than a value type to avoid Swift’s Copy-on-Write (COW) penalties during bulk inserts. With COW, mutating an array-backed value type duplicates the entire internal storage on every mutation, which would make building indexes from a large shard scan quadratic.Thread safety. The
See more@unchecked Sendableconformance is safe becauseOrderedIndexis always accessed from within a singleCollectionCoreactor, which serialises all mutations.Declaration
Swift
public final class OrderedIndex : Codable, @unchecked Sendable
View on GitHub
Classes Reference