-
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
22 changed files
with
756 additions
and
152 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,7 +1,6 @@ | ||
name: Build Docker Image | ||
on: | ||
release: | ||
types: [published] | ||
workflow_call: | ||
permissions: | ||
packages: write | ||
env: | ||
|
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,20 @@ | ||
name: Workflow - release | ||
on: | ||
release: | ||
types: [published] | ||
jobs: | ||
npm_publish: | ||
name: Publish packages to npmjs | ||
secrets: inherit | ||
permissions: | ||
id-token: write | ||
packages: write | ||
uses: ./.github/workflows/npm-publish.yml | ||
|
||
build_docker_images: | ||
name: Build Docker Images | ||
permissions: | ||
packages: write | ||
needs: | ||
- npm_publish | ||
uses: ./.github/workflows/docker.yml |
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
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
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
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,77 @@ | ||
import { GCounter } from "../GCounter/index.js"; | ||
|
||
/// State-based infinite-phase set (IPSet) | ||
export class IPSet<T> { | ||
// Grow-only mapping of elements to GCounters | ||
private _counters: Map<T, GCounter>; | ||
|
||
// State: | ||
// - an element exists in the IPSet if _counter[element] exists and is an odd number | ||
// - otherwise the element doesn't exist in the IPSet | ||
|
||
constructor(counters: Map<T, GCounter> = new Map()) { | ||
this._counters = counters; | ||
} | ||
|
||
counters(): Map<T, GCounter> { | ||
return this._counters; | ||
} | ||
|
||
add(nodeId: string, element: T): void { | ||
if (!this._counters.has(element)) { | ||
this._counters.set(element, new GCounter({ [nodeId]: 1 })); | ||
} else if (this._counters.get(element)?.value()! % 2 === 0) { | ||
this._counters.get(element)?.increment(nodeId, 1); | ||
} | ||
} | ||
|
||
remove(nodeId: string, element: T): void { | ||
if ( | ||
this._counters.has(element) && | ||
this._counters.get(element)?.value()! % 2 === 1 | ||
) { | ||
this._counters.get(element)?.increment(nodeId, 1); | ||
} | ||
} | ||
|
||
contains(element: T): boolean { | ||
if (this._counters.has(element)) { | ||
return this._counters.get(element)?.value()! % 2 === 1; | ||
} | ||
return false; | ||
} | ||
|
||
set(): Set<T> { | ||
let result = new Set<T>(); | ||
for (let [element, counter] of this._counters.entries()) { | ||
if (counter.value() % 2 === 1) { | ||
result.add(element); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
compare(peerSet: IPSet<T>): boolean { | ||
// Returns true if peerSet includes all operations that were performed on the given IPSet and possibly more. | ||
// this._counters has to be a subset of peerSet._counters | ||
// and for each element, the value of the counter in this._counters has to be less than or equal to the value of the counter in peerSet._counters | ||
return [...this._counters.keys()].every( | ||
(element) => | ||
peerSet.counters().has(element) && | ||
this._counters.get(element)?.value()! <= | ||
peerSet.counters().get(element)?.value()! | ||
); | ||
} | ||
|
||
merge(peerSet: IPSet<T>): void { | ||
for (let [element, counter] of peerSet._counters.entries()) { | ||
// if element is not in local replica, set local counter for element to counter | ||
// otherwise, merge the counters | ||
if (!this._counters.has(element)) { | ||
this._counters.set(element, counter); | ||
} else { | ||
this._counters.get(element)?.merge(counter); | ||
} | ||
} | ||
} | ||
} |
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,75 @@ | ||
export enum Bias { | ||
ADD, | ||
REMOVE | ||
} | ||
|
||
export class LWWElementSet<T> { | ||
private _adds: Map<T, number>; | ||
private _removes: Map<T, number>; | ||
public _bias: Bias; | ||
|
||
constructor(adds: Map<T, number>, removes: Map<T, number>, bias: Bias) { | ||
this._adds = adds; | ||
this._removes = removes; | ||
this._bias = bias; | ||
} | ||
|
||
lookup(element: T): boolean { | ||
const addTimestamp = this._adds.get(element); | ||
if(addTimestamp === undefined) { | ||
return false; | ||
} | ||
|
||
const removeTimestamp = this._removes.get(element); | ||
if (removeTimestamp === undefined) { | ||
return true; | ||
} | ||
if (addTimestamp > removeTimestamp) { | ||
return true; | ||
} | ||
if (addTimestamp - removeTimestamp === 0 && this._bias === Bias.ADD) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
add(element: T): void { | ||
this._adds.set(element, Date.now()); | ||
} | ||
|
||
remove(element: T): void { | ||
this._removes.set(element, Date.now()); | ||
} | ||
|
||
getAdds(): Map<T, number> { | ||
return this._adds; | ||
} | ||
|
||
getRemoves(): Map<T, number> { | ||
return this._removes; | ||
} | ||
|
||
compare(peerSet: LWWElementSet<T>): boolean { | ||
return (compareSets(this._adds, peerSet._adds) && compareSets(this._removes, peerSet._removes)); | ||
} | ||
|
||
merge(peerSet: LWWElementSet<T>): void { | ||
for (let [element, timestamp] of peerSet._adds.entries()) { | ||
const thisTimestamp = this._adds.get(element); | ||
if (!thisTimestamp || thisTimestamp < timestamp) { | ||
this._adds.set(element, timestamp); | ||
} | ||
} | ||
for (let [element, timestamp] of peerSet._removes.entries()) { | ||
const thisTimestamp = this._removes.get(element); | ||
if (!thisTimestamp || thisTimestamp < timestamp) { | ||
this._removes.set(element, timestamp); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function compareSets<T>(set1: Map<T, number>, set2: Map<T, number>): boolean { | ||
return (set1.size === set2.size && [...set1.keys()].every(key => set2.has(key))); | ||
} |
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,42 @@ | ||
import { describe, test, expect, beforeEach } from "vitest"; | ||
import { GCounter } from "../src/builtins/GCounter"; | ||
|
||
describe("G-Counter Tests", () => { | ||
let set1: GCounter; | ||
|
||
beforeEach(() => { | ||
set1 = new GCounter({ "node1": 5, "node2": 10}); | ||
}); | ||
|
||
test("Test Initial Values", () => { | ||
expect(set1.value()).toBe(15); | ||
}); | ||
|
||
test("Test Increment", () => { | ||
set1.increment("node1", 10); | ||
set1.increment("node2", 5); | ||
|
||
expect(set1.value()).toBe(30); | ||
}); | ||
|
||
test("Test Compare", () => { | ||
let set2 = new GCounter({ "node1": 5, "node2": 10}); | ||
let set3 = new GCounter({ "node1": 5, "node2": 10, "node3": 15 }); | ||
|
||
expect(set1.compare(set2)).toBe(true); | ||
set1.increment("node1", 5); | ||
expect(set1.compare(set2)).toBe(false); | ||
expect(set1.compare(set3)).toBe(false); | ||
}); | ||
|
||
test("Test Merge", () => { | ||
let set2 = new GCounter({ "node1": 3, "node2": 10}); | ||
let set3 = new GCounter({ "node1": 5, "node3": 15}); | ||
|
||
expect(set1.counts).toEqual({"node1": 5, "node2": 10}); | ||
set2.merge(set1); | ||
expect(set2.counts).toEqual({"node1": 5, "node2": 10}); | ||
set1.merge(set3); | ||
expect(set1.counts).toEqual({"node1": 5, "node2": 10, "node3": 15}); | ||
}); | ||
}); |
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,46 @@ | ||
import { describe, test, expect, beforeEach, afterEach } from "vitest"; | ||
import { GSet } from "../src/builtins/GSet"; | ||
|
||
describe("G-Set Tests", () => { | ||
|
||
let set1: GSet<string>; | ||
let set2: GSet<string>; | ||
|
||
beforeEach(() => { | ||
set1 = new GSet<string>(new Set<string>(["walter", "jesse", "mike"])); | ||
set2 = new GSet<string>(new Set<string>(["walter", "jesse", "mike"])); | ||
}); | ||
|
||
test("Test Add", () => { | ||
set1.add("gustavo"); | ||
set2.add("gustavo"); | ||
|
||
expect(set1.lookup("gustavo")).toBe(true); | ||
expect(set2.lookup("gustavo")).toBe(true); | ||
}); | ||
|
||
test("Test Compare", () => { | ||
expect(set1.compare(set2)).toBe(true); | ||
|
||
set1.add("gustavo"); | ||
|
||
expect(set1.compare(set2)).toBe(false); | ||
|
||
set2.add("gustavo"); | ||
|
||
expect(set1.compare(set2)).toBe(true); | ||
}); | ||
|
||
test("Test Merge", () => { | ||
set1.add("gustavo"); | ||
set2.add("lalo"); | ||
|
||
expect(set1.compare(set2)).toBe(false); | ||
|
||
set1.merge(set2); | ||
set2.merge(set1); | ||
|
||
expect(set1.compare(set2)).toBe(true); | ||
|
||
}); | ||
}); |
Oops, something went wrong.