Skip to content

Commit

Permalink
refactor for multiple vertices in conflict resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 27, 2024
1 parent 2dada1c commit 7527669
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
27 changes: 15 additions & 12 deletions packages/crdt/src/cros/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { ActionType, CRO } from "@topology-foundation/object";
import { Vertex } from "@topology-foundation/object/src";
import {
ActionType,
type CRO,
type Operation,
type Vertex,
} from "@topology-foundation/object";

enum OperationType {
Add = 0,
Remove = 1,
Nop = 2,
}

interface Operation<T> {
type: OperationType;
value: T;
}

/// AddWinsSet with support for state and op changes
export class AddWinsSet<T> implements CRO<T> {
operations: Operation<T>[];
Expand All @@ -23,13 +22,13 @@ export class AddWinsSet<T> implements CRO<T> {
}

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

remove(value: T): void {
const op = new Operation(OperationType.Remove, value);
const op: Operation<T> = { type: OperationType.Remove, value };
this.operations.push(op);
this.add(value);
}
Expand Down Expand Up @@ -64,9 +63,13 @@ export class AddWinsSet<T> implements CRO<T> {
.map(([value, _]) => value);
}

resolveConflicts(v1: Vertex<T>, v2: Vertex<T>): ActionType {
if (v1.operation.type !== v2.operation.type && v1.operation.value === v2.operation.value) {
return v1.operation.type === OperationType.Add
// in this case is an array of length 2
resolveConflicts(vertices: Vertex<T>[]): ActionType {
if (
vertices[0].operation.type !== vertices[1].operation.type &&
vertices[0].operation.value === vertices[1].operation.value
) {
return vertices[0].operation.type === OperationType.Add
? ActionType.DropRight
: ActionType.DropLeft;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/object/src/hashgraph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as crypto from "node:crypto";

type Hash = string;
type Operation<T> = { type: number; value: T | null };
export type Operation<T> = { type: number; value: T | null };

enum OperationType {
NOP = -1,
Expand All @@ -25,7 +25,7 @@ export interface Vertex<T> {

export class HashGraph<T> {
nodeId: string;
resolveConflicts: (v1: Vertex<T>, v2: Vertex<T>) => ActionType;
resolveConflicts: (vertices: Vertex<T>[]) => ActionType;

vertices: Map<Hash, Vertex<T>> = new Map();
frontier: Set<Hash> = new Set();
Expand All @@ -38,7 +38,7 @@ export class HashGraph<T> {

constructor(
nodeId: string,
resolveConflicts: (v1: Vertex<T>, v2: Vertex<T>) => ActionType,
resolveConflicts: (vertices: Vertex<T>[]) => ActionType,
) {
this.nodeId = nodeId;
this.resolveConflicts = resolveConflicts;
Expand Down Expand Up @@ -155,13 +155,13 @@ export class HashGraph<T> {
const moving = order[j];

if (!this.areCausallyRelated(anchor, moving)) {
const op1 = this.vertices.get(anchor);
const op2 = this.vertices.get(moving);
const v1 = this.vertices.get(anchor);
const v2 = this.vertices.get(moving);
let action: ActionType;
if (!op1 || !op2) {
if (!v1 || !v2) {
action = ActionType.Nop;
} else {
action = this.resolveConflicts(op1, op2);
action = this.resolveConflicts([v1, v2]);
}

switch (action) {
Expand Down
6 changes: 1 addition & 5 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export * from "./proto/object_pb.js";
export * from "./hashgraph.js";

export interface CRO<T> {
resolveConflicts: (
op1: hashgraph.Vertex<T>,
op2: hashgraph.Vertex<T>,
) => hashgraph.ActionType;
merge: (cro: CRO<T>) => void;
resolveConflicts: (vertices: hashgraph.Vertex<T>[]) => hashgraph.ActionType;
}

/* Creates a new TopologyObject */
Expand Down

0 comments on commit 7527669

Please sign in to comment.