Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 26, 2024
2 parents d7ace34 + 8122c18 commit 870072d
Show file tree
Hide file tree
Showing 8 changed files with 819 additions and 158 deletions.
70 changes: 33 additions & 37 deletions packages/crdt/package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
{
"name": "@topology-foundation/crdt",
"version": "0.0.23-5",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-topology.git"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./wasm": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.asc.ts"
}
},
"scripts": {
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"asbuild:debug": "asc --config asconfig.json --target debug",
"asbuild:release": "asc --config asconfig.json --target release",
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
},
"devDependencies": {
"assemblyscript": "^0.27.29"
}
"name": "@topology-foundation/crdt",
"version": "0.0.23-5",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-topology.git"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": ["src", "dist", "!dist/test", "!**/*.tsbuildinfo"],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./wasm": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.asc.ts"
}
},
"scripts": {
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"asbuild:debug": "asc --config asconfig.json --target debug",
"asbuild:release": "asc --config asconfig.json --target release",
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
},
"devDependencies": {
"@topology-foundation/object": "0.0.23-5",
"assemblyscript": "^0.27.29"
}
}
65 changes: 65 additions & 0 deletions packages/crdt/src/cros/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ActionType, HashGraph, Operation, OperationType } from "@topology-foundation/object";

/// AddWinsSet with support for state and op changes
export class AddWinsSet<T extends number> {
state: Map<T, number>;
hashGraph: HashGraph<T>;

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

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;
}

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

remove(value: T): void {
const op = new Operation(OperationType.Remove, value);
this.add(value);
}

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

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

values(): T[] {
return Array.from(this.state.entries())
.filter(([_, count]) => count % 2 === 1)
.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));
}
}

read(): T[] {
const operations = this.hashGraph.linearizeOps();
const tempCounter = new AddWinsSet<T>("");

for (const op of operations) {
if (op.type === OperationType.Add) {
tempCounter.add(op.value);
} else {
tempCounter.remove(op.value);
}
}

return tempCounter.values();
}
}
2 changes: 2 additions & 0 deletions packages/crdt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from "./crdts/LWWElementSet/index.js";
export * from "./crdts/LWWRegister/index.js";
export * from "./crdts/PNCounter/index.js";
export * from "./crdts/RGA/index.js";

export * from "./cros/AddWinsSet/index.js";
Loading

0 comments on commit 870072d

Please sign in to comment.