| index 734a4d9..a2bce5c 100644 |
| --- a/native/apple/Sources/DocsKit/CRDTReplica.swift |
| +++ b/native/apple/Sources/DocsKit/CRDTReplica.swift |
| @@ -58,9 +58,22 @@ actor CRDTReplica { |
| private var acceptedParking = "" |
| /// Updates applied to the authority since the replica was last persisted. |
| private var unpersisted: [Data] = [] |
| + private var unpersistedBytes = 0 |
| + /// Moves when `unpersisted` stops being a record of every change (it |
| + /// grew past `unpersistedLimit` while nothing could be stored, or the |
| + /// replica reopened): only a whole state can be stored after that. |
| + private var unpersistedEpoch: UInt64 = 0 |
| + private var needsWhole = false |
| + let unpersistedLimit: Int |
| + private func recordUnpersisted(_ update: Data) { |
| + unpersisted.append(update); unpersistedBytes += update.count |
| + if unpersistedBytes > unpersistedLimit { |
| + unpersisted.removeAll(); unpersistedBytes = 0; unpersistedEpoch &+= 1; needsWhole = true |
| + } |
| + } |
| private let guid: String |
| enum Failure: Error { case unavailable, rejected, invalidProjection, generationLimit } |
| - init(guid: String) { self.guid = guid } |
| + init(guid: String, unpersistedLimit: Int = 4 * 1024 * 1024) { self.guid = guid; self.unpersistedLimit = unpersistedLimit } |
| struct PersistentState: Sendable { |
| let state: Data |
| let sequence: Int64 |
| @@ -84,28 +97,37 @@ actor CRDTReplica { |
| let guid: String |
| /// The parking accepted as the server's own when this was taken. |
| let parked: String |
| + let epoch: UInt64 |
| } |
| /// What changed since the replica was last persisted, so a writer can |
| /// append it instead of rewriting the whole state; and the whole state |
| /// too when those changes exceed `wholeAbove` bytes (-1: always). |
| func persistentChanges(wholeAbove limit: Int) throws -> PersistentChanges { |
| let authority = try currentAuthority() |
| - let bytes = unpersisted.reduce(0) { $0 + $1.count } |
| var whole: Data? |
| - if limit < 0 || bytes > limit { |
| + if limit < 0 || unpersistedBytes > limit || needsWhole { |
| if !lastGood.tail.isEmpty { try compact(authority) } |
| whole = lastGood.base |
| } |
| - return PersistentChanges(updates: unpersisted, whole: whole, sequence: durableSequence, commandsApplied: commandsApplied, guid: guid, parked: acceptedParking) |
| + return PersistentChanges(updates: unpersisted, whole: whole, sequence: durableSequence, commandsApplied: commandsApplied, guid: guid, parked: acceptedParking, epoch: unpersistedEpoch) |
| } |
| /// The first `count` updates of the last `persistentChanges` are stored. |
| - func persisted(_ count: Int) { unpersisted.removeFirst(min(count, unpersisted.count)) } |
| + /// `changes` (or, with `count`, that many first updates) are stored. |
| + func persisted(_ changes: PersistentChanges) { |
| + guard changes.epoch == unpersistedEpoch else { return } |
| + let count = min(changes.updates.count, unpersisted.count) |
| + unpersistedBytes -= unpersisted.prefix(count).reduce(0) { $0 + $1.count } |
| + unpersisted.removeFirst(count) |
| + if changes.whole != nil { needsWhole = false } |
| + } |
| + /// Everything applied so far is already stored elsewhere (on reopen). |
| + func persistedAll() { unpersisted.removeAll(); unpersistedBytes = 0; unpersistedEpoch &+= 1; needsWhole = false } |
| func currentSequence() -> Int64 { durableSequence } |
| func setSequence(_ sequence: Int64) { durableSequence = max(durableSequence, sequence) } |
| func close() { |
| opened = false; authority = nil; merger = nil; presented = nil |
| sources.removeAll(); sourceStates.removeAll(); sourceVersions.removeAll() |
| - lastGood = State(); unpersisted.removeAll() |
| + lastGood = State(); unpersisted.removeAll(); unpersistedBytes = 0; unpersistedEpoch &+= 1 |
| } |
| private func fresh(update: Data) throws -> (UInt64, Sandbox) { |
| let sandbox = try Sandbox() |
| @@ -122,7 +144,8 @@ actor CRDTReplica { |
| } |
| func open(update: Data) throws { |
| sources.removeAll(); sourceStates.removeAll(); sourceVersions.removeAll() |
| - commandsApplied = 0; durableSequence = 0; unpersisted.removeAll(); presented = nil; acceptedParking = "" |
| + commandsApplied = 0; durableSequence = 0; presented = nil; acceptedParking = "" |
| + unpersisted.removeAll(); unpersistedBytes = 0; unpersistedEpoch &+= 1; needsWhole = false |
| authority = try fresh(update: update).1 |
| opened = true |
| lastGood = State(base: try authority!.invoke(["op": .string("snapshot")])) |
| @@ -284,7 +307,7 @@ actor CRDTReplica { |
| let authority = try currentAuthority() |
| do { _ = try authority.invoke(Data([255]) + update) } |
| catch { recover(); throw error } |
| - lastGood.append(update); version &+= 1; unpersisted.append(update) |
| + lastGood.append(update); version &+= 1; recordUnpersisted(update) |
| compactIfWanted(authority) |
| } |
| /// Reads that change nothing; any other command is an edit. |
| @@ -312,7 +335,7 @@ actor CRDTReplica { |
| let update = try writer.invoke(command) |
| if writer !== authority { authorityTouched = true; _ = try authority.invoke(Data([254]) + update) } |
| let wasInSync = generation.map { sourceVersions[$0] == version } ?? false |
| - lastGood.append(update); version &+= 1; unpersisted.append(update) |
| + lastGood.append(update); version &+= 1; recordUnpersisted(update) |
| if writer !== authority, let generation { |
| if wasInSync { sourceStates[generation] = lastGood; sourceVersions[generation] = version } |
| else { |