Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/reduce-action-…
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
JanLewDev committed Aug 29, 2024
2 parents afa3587 + 7f0b362 commit 916daf1
Show file tree
Hide file tree
Showing 20 changed files with 1,161 additions and 985 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ jobs:
- shell: bash
run: |
pnpm install
pnpm test
cd packages/object && pnpm build
cd ../.. && pnpm test
6 changes: 4 additions & 2 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ version: v2
plugins:
- local: ./node_modules/ts-proto/protoc-gen-ts_proto
strategy: directory
out: ./packages
# out: ./packages/network/src
out: ./packages/object/src
opt:
- esModuleInterop=true
- fileSuffix=_pb
inputs:
- directory: ./packages
#- directory: ./packages/network/src
- directory: ./packages/object/src
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/node": "^22.5.0",
"assemblyscript": "^0.27.29",
"release-it": "^17.6.0",
"ts-proto": "^2.0.3",
"typedoc": "^0.26.6",
"typescript": "^5.5.4",
"vite": "^5.4.1",
Expand Down
76 changes: 45 additions & 31 deletions packages/crdt/src/cros/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
import {
ActionType,
HashGraph,
Operation,
OperationType,
type CRO,
type Operation,
type Vertex,
} from "@topology-foundation/object";

/// AddWinsSet with support for state and op changes
export class AddWinsSet<T> {
state: Map<T, number>;
hashGraph: HashGraph<T>;
export class AddWinsSet<T> implements CRO<T> {
operations: string[] = ["add", "remove"];
state: Map<T, boolean>;

constructor(nodeId: string) {
this.state = new Map<T, number>();
this.hashGraph = new HashGraph<T>(nodeId);
constructor() {
this.state = new Map<T, boolean>();
}

resolveConflicts(op1: Operation<T>, op2: Operation<T>): ActionType {
if (op1.type !== op2.type && op1.value === op2.value) {
return op1.type === OperationType.Add
? ActionType.DropRight
: ActionType.DropLeft;
}
return ActionType.Nop;
private _add(value: T): void {
if (!this.state.get(value)) this.state.set(value, true);
}

add(value: T): void {
const op = new Operation(OperationType.Add, value);
this.state.set(value, (this.state.get(value) || 0) + 1);
this._add(value);
}

remove(value: T): void {
const op = new Operation(OperationType.Remove, value);
this.add(value);
private _remove(value: T): void {
if (this.state.get(value)) this.state.set(value, false);
}

getValue(value: T): number {
return this.state.get(value) || 0;
remove(value: T): void {
this._remove(value);
}

isInSet(value: T): boolean {
const count = this.getValue(value);
return count > 0 && count % 2 === 1;
contains(value: T): boolean {
return this.state.get(value) === true;
}

values(): T[] {
return Array.from(this.state.entries())
.filter(([_, count]) => count % 2 === 1)
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
}

merge(other: AddWinsSet<T>): void {
for (const [value, count] of other.state) {
this.state.set(value, Math.max(this.getValue(value), count));
// in this case is an array of length 2 and there are only two possible operations
resolveConflicts(vertices: Vertex<T>[]): ActionType {
if (
vertices[0].operation.type !== vertices[1].operation.type &&
vertices[0].operation.value === vertices[1].operation.value
) {
return vertices[0].operation.type === "add"
? ActionType.DropRight
: ActionType.DropLeft;
}
return ActionType.Nop;
}

// merged at HG level and called as a callback
mergeCallback(operations: Operation<T>[]): void {
this.state = new Map<T, boolean>();
for (const op of operations) {
switch (op.type) {
case "add":
if (op.value !== null) this._add(op.value);
break;
case "remove":
if (op.value !== null) this._remove(op.value);
break;
default:
break;
}
}
}
}
Loading

0 comments on commit 916daf1

Please sign in to comment.