Skip to content

Commit

Permalink
Introduce VersionVector
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jul 16, 2024
1 parent 59d94ad commit ef412fa
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 89 deletions.
54 changes: 54 additions & 0 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
InitialTimeTicket,
TimeTicket,
} from '@yorkie-js-sdk/src/document/time/ticket';
import { VersionVector } from '@yorkie-js-sdk/src/document/time/version_vector';
import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id';
import { Operation } from '@yorkie-js-sdk/src/document/operation/operation';
import { SetOperation } from '@yorkie-js-sdk/src/document/operation/set_operation';
Expand Down Expand Up @@ -75,6 +76,7 @@ import {
TextNodeID as PbTextNodeID,
TextNodePos as PbTextNodePos,
TimeTicket as PbTimeTicket,
VersionVector as PbVersionVector,
TreeNode as PbTreeNode,
TreeNodes as PbTreeNodes,
TreePos as PbTreePos,
Expand Down Expand Up @@ -162,6 +164,7 @@ function toChangeID(changeID: ChangeID): PbChangeID {
clientSeq: changeID.getClientSeq(),
lamport: changeID.getLamportAsString(),
actorId: toUint8Array(changeID.getActorID()),
versionVector: toVersionVector(changeID.getVersionVector()),
});
}

Expand All @@ -180,6 +183,18 @@ function toTimeTicket(ticket?: TimeTicket): PbTimeTicket | undefined {
});
}

/**
* `toVersionVector` converts the given model to Protobuf format.
*/
function toVersionVector(vector: VersionVector): PbVersionVector | undefined {
const pbVector = new PbVersionVector();
for (const [actorID, lamport] of vector) {
// TODO(hackerwins): Remove Long after introducing BigInt.
pbVector.vector[actorID] = BigInt(lamport.toString());
}
return pbVector;
}

/**
* `toValueType` converts the given model to Protobuf format.
*/
Expand Down Expand Up @@ -808,10 +823,29 @@ function fromChangeID(pbChangeID: PbChangeID): ChangeID {
pbChangeID.clientSeq,
Long.fromString(pbChangeID.lamport, true),
toHexString(pbChangeID.actorId),
fromVersionVector(pbChangeID.versionVector)!,
serverSeq,
);
}

/**
* `fromVersionVector` converts the given Protobuf format to model format.
*/
function fromVersionVector(
pbVersionVector?: PbVersionVector,
): VersionVector | undefined {
if (!pbVersionVector) {
return;
}

const vector = new VersionVector();
Object.entries(pbVersionVector.vector).forEach(([key, value]) => {
// TODO(hackerwins): Remove Long after introducing BigInt.
vector.set(key, Long.fromString(value.toString(), true));
});
return vector;
}

/**
* `fromTimeTicket` converts the given Protobuf format to model format.
*/
Expand Down Expand Up @@ -1330,6 +1364,7 @@ function fromChangePack<P extends Indexable>(
pbPack.isRemoved,
fromChanges(pbPack.changes),
pbPack.snapshot,
fromVersionVector(pbPack.snapshotVersionVector),
fromTimeTicket(pbPack.minSyncedTicket),
);
}
Expand Down Expand Up @@ -1487,6 +1522,23 @@ function bytesToObject(bytes?: Uint8Array): CRDTObject {
return fromObject(pbElement.body.value! as PbJSONElement_JSONObject);
}

/**
* `versionVectorToHex` converts the given VersionVector to bytes.
*/
function versionVectorToHex(vector: VersionVector): string {
const pbVersionVector = toVersionVector(vector)!;
return bytesToHex(pbVersionVector.toBinary());
}

/**
* `hexToVersionVector` creates a VersionVector from the given bytes.
*/
function hexToVersionVector(hex: string): VersionVector {
const bytes = hexToBytes(hex);
const pbVersionVector = PbVersionVector.fromBinary(bytes);
return fromVersionVector(pbVersionVector)!;
}

/**
* `objectToBytes` converts the given JSONObject to byte array.
*/
Expand Down Expand Up @@ -1598,6 +1650,8 @@ export const converter = {
bytesToSnapshot,
bytesToHex,
hexToBytes,
versionVectorToHex,
hexToVersionVector,
toHexString,
toUint8Array,
toOperation,
Expand Down
6 changes: 6 additions & 0 deletions src/api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ message ChangePack {
string document_key = 1;
Checkpoint checkpoint = 2;
bytes snapshot = 3;
VersionVector snapshot_version_vector = 7;
repeated Change changes = 4;
TimeTicket min_synced_ticket = 5;
bool is_removed = 6;
Expand All @@ -60,6 +61,11 @@ message ChangeID {
int64 server_seq = 2 [jstype = JS_STRING];
int64 lamport = 3 [jstype = JS_STRING];
bytes actor_id = 4;
VersionVector version_vector = 5;
}

message VersionVector {
map<string, int64> vector = 1;
}

message Operation {
Expand Down
36 changes: 35 additions & 1 deletion src/api/yorkie/v1/resources_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// @generated by protoc-gen-es v1.6.0 with parameter "target=js+dts,js_import_style=module"
// @generated by protoc-gen-es v1.10.0 with parameter "target=js+dts,js_import_style=module"
// @generated from file src/api/yorkie/v1/resources.proto (package yorkie.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down Expand Up @@ -176,6 +176,11 @@ export declare class ChangePack extends Message<ChangePack> {
*/
snapshot: Uint8Array;

/**
* @generated from field: yorkie.v1.VersionVector snapshot_version_vector = 7;
*/
snapshotVersionVector?: VersionVector;

/**
* @generated from field: repeated yorkie.v1.Change changes = 4;
*/
Expand Down Expand Up @@ -269,6 +274,11 @@ export declare class ChangeID extends Message<ChangeID> {
*/
actorId: Uint8Array;

/**
* @generated from field: yorkie.v1.VersionVector version_vector = 5;
*/
versionVector?: VersionVector;

constructor(data?: PartialMessage<ChangeID>);

static readonly runtime: typeof proto3;
Expand All @@ -284,6 +294,30 @@ export declare class ChangeID extends Message<ChangeID> {
static equals(a: ChangeID | PlainMessage<ChangeID> | undefined, b: ChangeID | PlainMessage<ChangeID> | undefined): boolean;
}

/**
* @generated from message yorkie.v1.VersionVector
*/
export declare class VersionVector extends Message<VersionVector> {
/**
* @generated from field: map<string, int64> vector = 1;
*/
vector: { [key: string]: bigint };

constructor(data?: PartialMessage<VersionVector>);

static readonly runtime: typeof proto3;
static readonly typeName = "yorkie.v1.VersionVector";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): VersionVector;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): VersionVector;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): VersionVector;

static equals(a: VersionVector | PlainMessage<VersionVector> | undefined, b: VersionVector | PlainMessage<VersionVector> | undefined): boolean;
}

/**
* @generated from message yorkie.v1.Operation
*/
Expand Down
Loading

0 comments on commit ef412fa

Please sign in to comment.