-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
819 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.