Skip to content

Commit

Permalink
Refactor clone/copy of a database.
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatorus committed Apr 19, 2018
1 parent de7116c commit 97c743d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
15 changes: 11 additions & 4 deletions packages/loki/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ export class Collection<TData extends object = object, TNested extends object =
this.flushChanges();
}

public copy(): Collection<TData, TNested> {
const collCopy = clone(this, "shallow");
collCopy._dynamicViews = [];

for (let i = 0; i < this._dynamicViews.length; i++) {
collCopy._dynamicViews[i] = this._dynamicViews[i].copy();
}
return collCopy;
}

toJSON(): Collection.Serialized {
return {
name: this.name,
Expand All @@ -330,11 +340,8 @@ export class Collection<TData extends object = object, TNested extends object =
};
}

static fromJSONObject(obj: Collection | Collection.Serialized, options?: Collection.DeserializeOptions) {
if (obj instanceof Collection) {
return clone(this, "shallow");
}

static fromJSONObject(obj: Collection.Serialized, options?: Collection.DeserializeOptions) {
let coll = new Collection<any>(obj.name, {
disableChangesApi: obj.disableChangesApi,
disableDeltaChangesApi: obj.disableDeltaChangesApi
Expand Down
5 changes: 5 additions & 0 deletions packages/loki/src/dynamic_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ResultSet} from "./result_set";
import {Collection} from "./collection";
import {Doc} from "../../common/types";
import {Scorer} from "../../full-text-search/src/scorer";
import {clone} from "./clone";

/**
* DynamicView class is a versatile 'live' view class which can have filters and sorts applied.
Expand Down Expand Up @@ -147,6 +148,10 @@ export class DynamicView<TData extends object = object, TNested extends object =
return rs.transform(transform, parameters);
}

public copy(): DynamicView<TData, TNested> {
return clone(this, "shallow");
}

/**
* Override of toJSON to avoid circular references.
*/
Expand Down
39 changes: 17 additions & 22 deletions packages/loki/src/loki.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global global */
import {LokiEventEmitter} from "./event_emitter";
import {Collection} from "./collection";
import {clone} from "./clone";
import {Doc, StorageAdapter} from "../../common/types";
import {PLUGINS} from "../../common/plugin";

Expand Down Expand Up @@ -191,26 +192,21 @@ export class Loki extends LokiEventEmitter {
* @param {boolean} options.removeNonSerializable - nulls properties not safe for serialization.
*/
public copy(options: Loki.CopyOptions = {}): Loki {
const databaseCopy = new Loki(this.filename, {env: this._env});
const dbCopy = clone(this, "shallow");
dbCopy._collections = [];

// currently inverting and letting loadJSONObject do most of the work
databaseCopy.loadJSONObject(this, {
retainDirtyFlags: true
});
for (let i = 0; i < this._collections.length; i++) {
dbCopy._collections[i] = this._collections[i].copy();
}

// since our toJSON is not invoked for reference database adapters, this will let us mimic
if (options.removeNonSerializable) {
// databaseCopy._autosaveHandle = null;
// databaseCopy._persistenceAdapter = null;

for (let idx = 0; idx < databaseCopy._collections.length; idx++) {
// TODO: Move to class.
// databaseCopy._collections[idx]._constraints = null;
// databaseCopy._collections[idx]._ttl = null;
}
dbCopy._autosaveHandler = Promise.resolve();
dbCopy._persistenceAdapter = null;
dbCopy._throttledSaveRunning = null;
dbCopy._throttledSavePending = null;
}

return databaseCopy;
return dbCopy;
}

/**
Expand Down Expand Up @@ -341,7 +337,7 @@ export class Loki extends LokiEventEmitter {
_serializationMethod: this._serializationMethod,
_autosave: this._autosave,
_autosaveInterval: this._autosaveInterval,
_collections: this._collections,
_collections: this._collections as any as Collection.Serialized[],
databaseVersion: this.databaseVersion,
engineVersion: this.engineVersion,
filename: this.filename,
Expand Down Expand Up @@ -388,8 +384,9 @@ export class Loki extends LokiEventEmitter {
}

// not just an individual collection, so we will need to serialize db container via shallow copy
let dbcopy = new Loki(this.filename);
dbcopy.loadJSONObject(this);
// let dbcopy = new Loki(this.filename);
// dbcopy.loadJSONObject(this);
let dbcopy = this.copy();

for (let idx = 0; idx < dbcopy._collections.length; idx++) {
dbcopy._collections[idx]._data = [];
Expand Down Expand Up @@ -680,13 +677,11 @@ export class Loki extends LokiEventEmitter {

/**
* Inflates a loki database from a JS object
*
* @param {object} dbObject - a serialized loki database object
* @param {object} options - apply or override collection level settings
* @param {boolean} options.retainDirtyFlags - whether collection dirty flags will be preserved
*/
// public loadJSONObject(dbObject: Loki, options?: Collection.DeserializeOptions): void;
public loadJSONObject(dbObject: Loki | Loki.Serialized, options: Collection.DeserializeOptions = {}): void {
public loadJSONObject(dbObject: Loki.Serialized, options: Collection.DeserializeOptions = {}): void {
// Legacy support.
// if (dbObject.databaseVersion === 1.5) {
// dbObject = dbObject as LokiJS.Loki;
Expand Down Expand Up @@ -1084,7 +1079,7 @@ export namespace Loki {
_serializationMethod: SerializationMethod;
_autosave: boolean;
_autosaveInterval: number;
_collections: Collection[];
_collections: Collection.Serialized[];
databaseVersion: 2.0;
engineVersion: 2.0;
filename: string;
Expand Down

0 comments on commit 97c743d

Please sign in to comment.