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

Implement Document.GarbageCollect in JS SDK #101

Merged
merged 16 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
24,493 changes: 24,493 additions & 0 deletions dist/yorkie.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ function toChangePack(pack: ChangePack): PbChangePack {
pbChangePack.setCheckpoint(toCheckpoint(pack.getCheckpoint()));
pbChangePack.setChangesList(toChanges(pack.getChanges()));
pbChangePack.setSnapshot(pack.getSnapshot());
pbChangePack.setMinSyncedTicket(toTimeTicket(pack.getMinSyncedTicket()));
return pbChangePack;
}

Expand Down Expand Up @@ -768,6 +769,7 @@ function fromChangePack(pbPack: PbChangePack): ChangePack {
fromCheckpoint(pbPack.getCheckpoint()),
fromChanges(pbPack.getChangesList()),
pbPack.getSnapshot_asU8(),
fromTimeTicket(pbPack.getMinSyncedTicket()),
);
}

Expand Down
77 changes: 33 additions & 44 deletions src/api/yorkie_grpc_web_pb.d.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,89 @@
import * as grpcWeb from 'grpc-web';

import {
ActivateClientRequest,
ActivateClientResponse,
AttachDocumentRequest,
AttachDocumentResponse,
DeactivateClientRequest,
DeactivateClientResponse,
DetachDocumentRequest,
DetachDocumentResponse,
PushPullRequest,
PushPullResponse,
WatchDocumentsRequest,
WatchDocumentsResponse} from './yorkie_pb';
import * as src_api_yorkie_pb from '../../src/api/yorkie_pb';


export class YorkieClient {
constructor (hostname: string,
credentials?: null | { [index: string]: string; },
options?: null | { [index: string]: string; });
options?: null | { [index: string]: any; });

activateClient(
request: ActivateClientRequest,
request: src_api_yorkie_pb.ActivateClientRequest,
metadata: grpcWeb.Metadata | undefined,
callback: (err: grpcWeb.Error,
response: ActivateClientResponse) => void
): grpcWeb.ClientReadableStream<ActivateClientResponse>;
response: src_api_yorkie_pb.ActivateClientResponse) => void
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.ActivateClientResponse>;

deactivateClient(
request: DeactivateClientRequest,
request: src_api_yorkie_pb.DeactivateClientRequest,
metadata: grpcWeb.Metadata | undefined,
callback: (err: grpcWeb.Error,
response: DeactivateClientResponse) => void
): grpcWeb.ClientReadableStream<DeactivateClientResponse>;
response: src_api_yorkie_pb.DeactivateClientResponse) => void
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.DeactivateClientResponse>;

attachDocument(
request: AttachDocumentRequest,
request: src_api_yorkie_pb.AttachDocumentRequest,
metadata: grpcWeb.Metadata | undefined,
callback: (err: grpcWeb.Error,
response: AttachDocumentResponse) => void
): grpcWeb.ClientReadableStream<AttachDocumentResponse>;
response: src_api_yorkie_pb.AttachDocumentResponse) => void
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.AttachDocumentResponse>;

detachDocument(
request: DetachDocumentRequest,
request: src_api_yorkie_pb.DetachDocumentRequest,
metadata: grpcWeb.Metadata | undefined,
callback: (err: grpcWeb.Error,
response: DetachDocumentResponse) => void
): grpcWeb.ClientReadableStream<DetachDocumentResponse>;
response: src_api_yorkie_pb.DetachDocumentResponse) => void
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.DetachDocumentResponse>;

watchDocuments(
request: WatchDocumentsRequest,
request: src_api_yorkie_pb.WatchDocumentsRequest,
metadata?: grpcWeb.Metadata
): grpcWeb.ClientReadableStream<WatchDocumentsResponse>;
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.WatchDocumentsResponse>;

pushPull(
request: PushPullRequest,
request: src_api_yorkie_pb.PushPullRequest,
metadata: grpcWeb.Metadata | undefined,
callback: (err: grpcWeb.Error,
response: PushPullResponse) => void
): grpcWeb.ClientReadableStream<PushPullResponse>;
response: src_api_yorkie_pb.PushPullResponse) => void
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.PushPullResponse>;

}

export class YorkiePromiseClient {
constructor (hostname: string,
credentials?: null | { [index: string]: string; },
options?: null | { [index: string]: string; });
options?: null | { [index: string]: any; });

activateClient(
request: ActivateClientRequest,
request: src_api_yorkie_pb.ActivateClientRequest,
metadata?: grpcWeb.Metadata
): Promise<ActivateClientResponse>;
): Promise<src_api_yorkie_pb.ActivateClientResponse>;

deactivateClient(
request: DeactivateClientRequest,
request: src_api_yorkie_pb.DeactivateClientRequest,
metadata?: grpcWeb.Metadata
): Promise<DeactivateClientResponse>;
): Promise<src_api_yorkie_pb.DeactivateClientResponse>;

attachDocument(
request: AttachDocumentRequest,
request: src_api_yorkie_pb.AttachDocumentRequest,
metadata?: grpcWeb.Metadata
): Promise<AttachDocumentResponse>;
): Promise<src_api_yorkie_pb.AttachDocumentResponse>;

detachDocument(
request: DetachDocumentRequest,
request: src_api_yorkie_pb.DetachDocumentRequest,
metadata?: grpcWeb.Metadata
): Promise<DetachDocumentResponse>;
): Promise<src_api_yorkie_pb.DetachDocumentResponse>;

watchDocuments(
request: WatchDocumentsRequest,
request: src_api_yorkie_pb.WatchDocumentsRequest,
metadata?: grpcWeb.Metadata
): grpcWeb.ClientReadableStream<WatchDocumentsResponse>;
): grpcWeb.ClientReadableStream<src_api_yorkie_pb.WatchDocumentsResponse>;

pushPull(
request: PushPullRequest,
request: src_api_yorkie_pb.PushPullRequest,
metadata?: grpcWeb.Metadata
): Promise<PushPullResponse>;
): Promise<src_api_yorkie_pb.PushPullResponse>;

}

10 changes: 5 additions & 5 deletions src/api/yorkie_grpc_web_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ proto.api.YorkieClient.prototype.activateClient =
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!Promise<!proto.api.ActivateClientResponse>}
* A native promise that resolves to the response
* Promise that resolves to the response
*/
proto.api.YorkiePromiseClient.prototype.activateClient =
function(request, metadata) {
Expand Down Expand Up @@ -218,7 +218,7 @@ proto.api.YorkieClient.prototype.deactivateClient =
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!Promise<!proto.api.DeactivateClientResponse>}
* A native promise that resolves to the response
* Promise that resolves to the response
*/
proto.api.YorkiePromiseClient.prototype.deactivateClient =
function(request, metadata) {
Expand Down Expand Up @@ -298,7 +298,7 @@ proto.api.YorkieClient.prototype.attachDocument =
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!Promise<!proto.api.AttachDocumentResponse>}
* A native promise that resolves to the response
* Promise that resolves to the response
*/
proto.api.YorkiePromiseClient.prototype.attachDocument =
function(request, metadata) {
Expand Down Expand Up @@ -378,7 +378,7 @@ proto.api.YorkieClient.prototype.detachDocument =
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!Promise<!proto.api.DetachDocumentResponse>}
* A native promise that resolves to the response
* Promise that resolves to the response
*/
proto.api.YorkiePromiseClient.prototype.detachDocument =
function(request, metadata) {
Expand Down Expand Up @@ -533,7 +533,7 @@ proto.api.YorkieClient.prototype.pushPull =
* @param {?Object<string, string>} metadata User defined
* call metadata
* @return {!Promise<!proto.api.PushPullResponse>}
* A native promise that resolves to the response
* Promise that resolves to the response
*/
proto.api.YorkiePromiseClient.prototype.pushPull =
function(request, metadata) {
Expand Down
16 changes: 9 additions & 7 deletions src/api/yorkie_pb.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as jspb from "google-protobuf"
import * as jspb from 'google-protobuf'



export class RequestHeader extends jspb.Message {
getVersion(): number;
Expand Down Expand Up @@ -1029,12 +1031,12 @@ export class JSONElement extends jspb.Message {

export namespace JSONElement {
export type AsObject = {
object?: Object.AsObject,
array?: Array.AsObject,
primitive?: Primitive.AsObject,
text?: Text.AsObject,
richText?: RichText.AsObject,
counter?: Counter.AsObject,
object?: JSONElement.Object.AsObject,
array?: JSONElement.Array.AsObject,
primitive?: JSONElement.Primitive.AsObject,
text?: JSONElement.Text.AsObject,
richText?: JSONElement.RichText.AsObject,
counter?: JSONElement.Counter.AsObject,
}

export class Object extends jspb.Message {
Expand Down
11 changes: 10 additions & 1 deletion src/document/change/change_pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { DocumentKey } from '../key/document_key';
import { Checkpoint } from '../checkpoint/checkpoint';
import { Change } from './change';
import { TimeTicket } from '../time/ticket';

/**
* ChangePack is a unit for delivering changes in a document to the remote.
Expand All @@ -26,26 +27,30 @@ export class ChangePack {
private checkpoint: Checkpoint;
private changes: Change[];
private snapshot: Uint8Array;
private minSyncedTicket: TimeTicket;

constructor(
key: DocumentKey,
checkpoint: Checkpoint,
changes: Change[],
snapshot: Uint8Array,
minSyncedTicket: TimeTicket,
) {
this.key = key;
this.checkpoint = checkpoint;
this.changes = changes;
this.snapshot = snapshot;
this.minSyncedTicket = minSyncedTicket;
}

public static create(
key: DocumentKey,
checkpoint: Checkpoint,
changes: Change[],
snapshot?: Uint8Array,
minSyncedTicket?: TimeTicket,
): ChangePack {
return new ChangePack(key, checkpoint, changes, snapshot);
return new ChangePack(key, checkpoint, changes, snapshot, minSyncedTicket);
}

public getKey(): DocumentKey {
Expand Down Expand Up @@ -75,4 +80,8 @@ export class ChangePack {
public getSnapshot(): Uint8Array {
return this.snapshot;
}

public getMinSyncedTicket(): TimeTicket {
return this.minSyncedTicket;
}
}
9 changes: 8 additions & 1 deletion src/document/change/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { TimeTicket, InitialDelimiter } from '../time/ticket';
import { JSONRoot } from '../json/root';
import { JSONElement } from '../json/element';
import { JSONContainer, JSONElement } from '../json/element';
import { Operation } from '../operation/operation';
import { ChangeID } from './change_id';
import { Change } from './change';
Expand Down Expand Up @@ -60,6 +60,13 @@ export class ChangeContext {
this.root.registerElement(element);
}

public registerRemovedElementPair(
parent: JSONContainer,
deleted: JSONElement,
): void {
this.root.registerRemovedElementPair(parent, deleted);
}

public getChange(): Change {
return Change.create(this.id, this.message, this.operations);
}
Expand Down
19 changes: 19 additions & 0 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { JSONRoot } from './json/root';
import { JSONObject } from './json/object';
import { createProxy } from './proxy/proxy';
import { Checkpoint, InitialCheckpoint } from './checkpoint/checkpoint';
import { TimeTicket } from './time/ticket';

export enum DocEventType {
Snapshot = 'snapshot',
Expand Down Expand Up @@ -148,6 +149,9 @@ export class Document implements Observable<DocEvent> {
// 03. Update the checkpoint.
this.checkpoint = this.checkpoint.forward(pack.getCheckpoint());

// 04. Do Garbage collection.
this.garbageCollect(pack.getMinSyncedTicket());

if (logger.isEnabled(LogLevel.Trivial)) {
logger.trivial(`${this.root.toJSON()}`);
}
Expand Down Expand Up @@ -195,17 +199,32 @@ export class Document implements Observable<DocEvent> {
return this.key;
}

public getClone(): JSONObject {
return this.clone.getObject();
}

public getRootObject(): JSONObject {
this.ensureClone();

const context = ChangeContext.create(this.changeID.next(), '', this.clone);
return createProxy(context, this.clone.getObject());
}

public garbageCollect(ticket: TimeTicket): number {
if (this.clone) {
this.clone.garbageCollect(ticket);
}
return this.root.garbageCollect(ticket);
}

public getRoot(): JSONObject {
return this.root.getObject();
}

public getGarbageLen(): number {
return this.root.getGarbageLen();
}

public toJSON(): string {
return this.root.toJSON();
}
Expand Down
Loading