Skip to content

Commit

Permalink
Add ServerSeq into ChangeInfo (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum authored Jun 10, 2024
1 parent 1f2d352 commit 9cec889
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
31 changes: 23 additions & 8 deletions Sources/API/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ extension Converter {
static func fromChangeID(_ pbChangeID: PbChangeID) -> ChangeID {
ChangeID(clientSeq: pbChangeID.clientSeq,
lamport: pbChangeID.lamport,
actor: pbChangeID.actorID.toHexString)
actor: pbChangeID.actorID.toHexString,
serverSeq: pbChangeID.serverSeq)
}
}

Expand Down Expand Up @@ -510,13 +511,27 @@ extension Converter {
executedAt: fromTimeTicket(pbTreeEditOperation.executedAt),
maxCreatedAtMapByActor: createdAtMapByActor)
} else if case let .treeStyle(pbTreeStyleOperation) = pbOperation.body {
return TreeStyleOperation(parentCreatedAt: fromTimeTicket(pbTreeStyleOperation.parentCreatedAt),
fromPos: fromTreePos(pbTreeStyleOperation.from),
toPos: fromTreePos(pbTreeStyleOperation.to),
maxCreatedAtMapByActor: pbTreeStyleOperation.createdAtMapByActor.compactMapValues({ fromTimeTicket($0) }),
attributes: pbTreeStyleOperation.attributes,
attributesToRemove: pbTreeStyleOperation.attributesToRemove,
executedAt: fromTimeTicket(pbTreeStyleOperation.executedAt))
var createdAtMapByActor = [String: TimeTicket]()
pbTreeStyleOperation.createdAtMapByActor.forEach { key, value in
createdAtMapByActor[key] = fromTimeTicket(value)
}
if !pbTreeStyleOperation.attributesToRemove.isEmpty {
return TreeStyleOperation(parentCreatedAt: fromTimeTicket(pbTreeStyleOperation.parentCreatedAt),
fromPos: fromTreePos(pbTreeStyleOperation.from),
toPos: fromTreePos(pbTreeStyleOperation.to),
maxCreatedAtMapByActor: createdAtMapByActor,
attributes: [:],
attributesToRemove: pbTreeStyleOperation.attributesToRemove,
executedAt: fromTimeTicket(pbTreeStyleOperation.executedAt))
} else {
return TreeStyleOperation(parentCreatedAt: fromTimeTicket(pbTreeStyleOperation.parentCreatedAt),
fromPos: fromTreePos(pbTreeStyleOperation.from),
toPos: fromTreePos(pbTreeStyleOperation.to),
maxCreatedAtMapByActor: createdAtMapByActor,
attributes: pbTreeStyleOperation.attributes,
attributesToRemove: [],
executedAt: fromTimeTicket(pbTreeStyleOperation.executedAt))
}
} else {
throw YorkieError.unimplemented(message: "unimplemented operation \(pbOperation)")
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Document/Change/Change.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct Change {
}

self.operations = operations
self.id.setActor(actorID)
self.id = self.id.setActor(actorID)
}

/**
Expand Down
18 changes: 15 additions & 3 deletions Sources/Document/Change/ChangeID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ struct ChangeID {
private var lamport: Int64
private var actor: ActorID

init(clientSeq: UInt32, lamport: Int64, actor: ActorID) {
init(clientSeq: UInt32, lamport: Int64, actor: ActorID, serverSeq: Int64? = nil) {
self.clientSeq = clientSeq
self.lamport = lamport
self.actor = actor
self.serverSeq = serverSeq
}

/**
Expand Down Expand Up @@ -69,8 +70,8 @@ struct ChangeID {
/**
* `setActor` sets the given actor.
*/
mutating func setActor(_ actorID: ActorID) {
self.actor = actorID
func setActor(_ actorID: ActorID) -> ChangeID {
ChangeID(clientSeq: self.clientSeq, lamport: self.lamport, actor: actorID, serverSeq: self.serverSeq)
}

/**
Expand All @@ -80,6 +81,17 @@ struct ChangeID {
return self.clientSeq
}

/**
* `getServerSeq` returns the server sequence of this ID.
*/
func getServerSeq() -> String {
if let serverSeq {
return String(serverSeq)
} else {
return ""
}
}

/**
* `getLamport` returns the lamport clock of this ID.
*/
Expand Down
2 changes: 2 additions & 0 deletions Sources/Document/DocEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public struct ChangeInfo {
public let message: String
public let operations: [any OperationInfo]
public let actorID: ActorID?
public let clientSeq: UInt32
public let serverSeq: String
}

/**
Expand Down
18 changes: 14 additions & 4 deletions Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ public actor Document {
if !opInfos.isEmpty {
let changeInfo = ChangeInfo(message: change.message ?? "",
operations: opInfos,
actorID: actorID)
actorID: actorID,
clientSeq: change.id.getClientSeq(),
serverSeq: change.id.getServerSeq())
let changeEvent = LocalChangeEvent(value: changeInfo)
self.publish(changeEvent)
}
Expand Down Expand Up @@ -322,7 +324,7 @@ public actor Document {

self.localChanges = changes

self.changeID.setActor(actorID)
self.changeID = self.changeID.setActor(actorID)

// TODOs also apply into root.
}
Expand Down Expand Up @@ -484,7 +486,11 @@ public actor Document {
let opInfos = try change.execute(root: self.root, presences: &self.presences)

if change.hasOperations {
changeInfo = ChangeInfo(message: change.message ?? "", operations: opInfos, actorID: actorID)
changeInfo = ChangeInfo(message: change.message ?? "",
operations: opInfos,
actorID: actorID,
clientSeq: change.id.getClientSeq(),
serverSeq: change.id.getServerSeq())
}

// DocEvent should be emitted synchronously with applying changes.
Expand Down Expand Up @@ -650,7 +656,11 @@ public actor Document {
}

for (key, value) in operations {
let info = ChangeInfo(message: event.value.message, operations: value, actorID: event.value.actorID)
let info = ChangeInfo(message: event.value.message,
operations: value,
actorID: event.value.actorID,
clientSeq: event.value.clientSeq,
serverSeq: event.value.serverSeq)

if let callback = self.subscribeCallbacks[key] {
callback(event.type == .localChange ? LocalChangeEvent(value: info) : RemoteChangeEvent(value: info), self)
Expand Down

0 comments on commit 9cec889

Please sign in to comment.