Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LWW-Register impl & Tests #90

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/crdt/src/builtins/LWWRegister/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export class LWWRegister<T> {
private _element: T;
private _timestamp: number;
private _nodeId: string;

constructor(element: T, nodeId: string) {
this._element = element;
this._timestamp = Date.now();
this._nodeId = nodeId;
}

assign(element: T, nodeId: string): void {
this._element = element;
this._timestamp = Date.now();
this._nodeId = nodeId;
}

getElement(): T {
return this._element;
}

getTimestamp(): number {
return this._timestamp;
}

getNodeId(): string {
return this._nodeId;
}

compare(register: LWWRegister<T>): boolean {
return (this._timestamp <= register.getTimestamp());
d-roak marked this conversation as resolved.
Show resolved Hide resolved
}

merge(register: LWWRegister<T>): void {
const otherTimestamp = register.getTimestamp();
const otherNodeId = register.getNodeId();
if (otherTimestamp < this._timestamp) {
return;
}
if (otherTimestamp === this._timestamp && otherNodeId <= this._nodeId) {
return;
}
this._element = register.getElement();
this._timestamp = otherTimestamp;
this._nodeId = otherNodeId;
}
}
64 changes: 64 additions & 0 deletions packages/crdt/tests/LWWRegister.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, test, expect, beforeEach, vi } from "vitest";
import { LWWRegister } from "../src/builtins/LWWRegister/index.ts";

describe('LWW-Register Tests', () => {
test('Test Assign', () => {
let register1 = new LWWRegister<string>("alice", "node1");

expect(register1.getElement()).toBe("alice");
expect(register1.getNodeId()).toEqual("node1");
register1.assign("bob", "node2");
expect(register1.getElement()).toBe("bob");
expect(register1.getNodeId()).toEqual("node2");
});

test('Test Compare', () => {
vi.useFakeTimers();
const date = new Date(2000,1,1,13);
vi.setSystemTime(date);

let register1 = new LWWRegister<string>("alice", "node1");

vi.useRealTimers();

let register2 = new LWWRegister<string>("alice", "node2");

expect(register1.compare(register2)).toEqual(true);
expect(register2.compare(register1)).toEqual(false);
});

test('Test Merge', () => {
let register1 = new LWWRegister<string>("alice", "node1");
let register2 = new LWWRegister<string>("bob", "node2");

register1.merge(register2);
expect(register1.getElement()).toEqual("bob");
expect(register2.getNodeId()).toEqual("node2");

register2.merge(register1);
expect(register1.getElement()).toEqual("bob");
expect(register2.getNodeId()).toEqual("node2");
});

test('Test Merge w/same timestamp', () => {
vi.useFakeTimers();
const date = new Date(2000,1,1,13);
vi.setSystemTime(date);

let register1 = new LWWRegister<string>("alice", "node1");
let register2 = new LWWRegister<string>("bob", "node2");

expect(register1.getElement()).toBe("alice");
expect(register1.getNodeId()).toEqual("node1");

register1.merge(register2);
expect(register1.getElement()).toBe("bob");
expect(register1.getNodeId()).toEqual("node2");

register2.merge(register1);
expect(register1.getElement()).toBe("bob");
expect(register1.getNodeId()).toEqual("node2");

vi.useRealTimers();
});
});