-
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.
Co-authored-by: droak <[email protected]>
- Loading branch information
1 parent
b5998ad
commit 90a4f3d
Showing
4 changed files
with
123 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
export interface ElementTuple<T> { | ||
element: T, | ||
tag: number, | ||
nodeId: string | ||
} | ||
|
||
/* Implementation of the Optimized Observed-Remove Set CRDT | ||
Based on the paper: https://pages.lip6.fr/Marek.Zawirski/papers/RR-8083.pdf | ||
*/ | ||
export class OORSet<T> { | ||
elements: Set<ElementTuple<T>> = new Set<ElementTuple<T>>(); | ||
summary: Map<string, number> = new Map<string, number>(); | ||
nodeId: string= ""; | ||
|
||
constructor(nodeId?: string, elements?: Set<ElementTuple<T>>) { | ||
if(nodeId !== undefined) { | ||
this.nodeId = nodeId; | ||
this.summary = new Map<string, number>([[this.nodeId,this.elements.size]]); | ||
} | ||
|
||
if(elements !== undefined) { | ||
this.elements = elements; | ||
} | ||
} | ||
|
||
lookup(element: T): boolean { | ||
return [...this.elements].some(elem => elem.element === element); | ||
} | ||
|
||
add(nodeId: string, element: T): void { | ||
let tag: number = this.summary.get(this.nodeId)! + 1; | ||
this.summary.set(this.nodeId, tag); | ||
this.elements.add({ element, tag, nodeId: nodeId }); | ||
} | ||
|
||
remove(element: T): void { | ||
for (let tuple of this.elements.values()) { | ||
if (tuple.element === element) { | ||
this.elements.delete(tuple); //removes element from the elements | ||
} | ||
} | ||
} | ||
|
||
// When comparing both element sets, it just needs to compare them one way because | ||
// the "tag" and "nodeId" are going to be unique for a given element so there are | ||
// not equal elements in the set before they're merged | ||
compare(peerSet: OORSet<T>): boolean { | ||
return (this.elements.size == peerSet.elements.size && | ||
[...this.elements].every((value) => peerSet.elements.has(value))); | ||
} | ||
|
||
merge(peerSet: OORSet<T>): void { | ||
// place: [local, remote, both] | ||
// sets: [elements, removed] | ||
// existence: [in, notIn] | ||
let bothInElements = [...this.elements].filter((element) => peerSet.elements.has(element)); | ||
let localInElementsRemoteNotInRemoved = [...this.elements].filter((element) => | ||
!peerSet.elements.has(element) && element.tag > peerSet.summary.get(element.nodeId)! | ||
); | ||
let localNotInRemovedRemoteInElements = [...peerSet.elements].filter((element) => | ||
!this.elements.has(element) && element.tag > this.summary.get(element.nodeId)! | ||
); | ||
|
||
this.elements = new Set<ElementTuple<T>>([...bothInElements, ...localInElementsRemoteNotInRemoved, ...localNotInRemovedRemoteInElements]); | ||
this.elements = new Set( | ||
[...this.elements].filter((e) => | ||
[...this.elements].every((e2) => e.tag > e2.tag) | ||
) | ||
); | ||
|
||
// update summary | ||
for (let e of peerSet.summary.entries()) { | ||
this.summary.set(e[0], Math.max(e[1], this.summary.get(e[0])!)); | ||
} | ||
} | ||
} |
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
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,44 @@ | ||
import { describe, test, expect, beforeEach } from "vitest"; | ||
import { OORSet, ElementTuple } from "../src/crdts/OORSet"; | ||
|
||
describe("OR-Set Tests", () => { | ||
let set1: OORSet<string>; | ||
let set2: OORSet<string>; | ||
|
||
const testValues = ["walter", "jesse", "mike"]; | ||
|
||
beforeEach(() => { | ||
set1 = new OORSet<string>("set1", new Set<ElementTuple<string>>()); | ||
set2 = new OORSet<string>("set2", new Set<ElementTuple<string>>()); | ||
|
||
testValues.forEach((value) => { | ||
set1.add("set1",value); | ||
set2.add("set2",value); | ||
}); | ||
}); | ||
|
||
test("Test Add Elements", () => { | ||
expect(set1.lookup("gustavo")).toBe(false); | ||
|
||
set1.add("set1","gustavo"); | ||
|
||
expect(set1.lookup("gustavo")).toBe(true); | ||
}); | ||
|
||
test("Test Remove Elements", () => { | ||
expect(set1.lookup("mike")).toBe(true); | ||
|
||
set1.remove("mike"); | ||
|
||
expect(set1.lookup("mike")).toBe(false); | ||
}); | ||
|
||
test("Test Merge Elements", () => { | ||
expect(set1.compare(set2)).toBe(false); | ||
|
||
set1.merge(set2); | ||
set2.merge(set1); | ||
|
||
expect(set1.compare(set2)).toBe(true); | ||
}); | ||
}); |
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