Skip to content

Commit

Permalink
serialization: improve storage strategy for commit info of parts
Browse files Browse the repository at this point in the history
  • Loading branch information
merryman committed Feb 17, 2019
1 parent 0371baa commit 86ad5cc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
34 changes: 29 additions & 5 deletions morphicdb/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { loadMorphFromSnapshot, createMorphSnapshot } from "../serialization.js"
import { resource } from "lively.resources";
import { obj, Path, arr, string } from "lively.lang";


/*
$world.execCommand("open workspace", {
Expand All @@ -24,7 +25,25 @@ await versionDB.getAll()
*/
var morphicDBs = morphicDBs || (morphicDBs = new Map());
const defaultServerURL = (typeof document !== "undefined" ?
document.location.origin : "http://localhost:9001") + "/objectdb/"
document.location.origin : "http://localhost:9001") + "/objectdb/";

export function convertToSerializableCommit(commit) {
commit.__serialize__ = () => {
let {type, name, _id} = commit
return {
__expr__: `({type: "${type}", name: "${name}", _id: "${_id}"})`,
}
};
return commit;
}

export async function ensureCommitInfo(commit) {
if (!commit) return false;
if (commit && commit._rev) return commit;
let { type, name, _id } = commit;
Object.assign(commit, await MorphicDB.default.fetchCommit(type, name, _id));
return commit;
}

export default class MorphicDB {

Expand Down Expand Up @@ -73,7 +92,7 @@ export default class MorphicDB {
let {name, serverURL, snapshotLocation} = this;
return {
__expr__: `MorphicDB.named("${name}", {snapshotLocation: "${snapshotLocation}", serverURL: "${serverURL}"})`,
bindings: {"lively.morphic/morphicdb.js": [{exported: "default", local: "MorphicDB"}]}
bindings: {"lively.morphic/morphicdb/index.js": ["MorphicDB"]}
}
}

Expand Down Expand Up @@ -114,8 +133,9 @@ export default class MorphicDB {
await this.initializeIfNecessary();
let typesAndNames = [{type, name, ref}],
commits = await this.httpDB.fetchCommits(
{db: this.name, ref, type, typesAndNames});
return commits[0];
{db: this.name, ref, type, typesAndNames, knownCommitIds: {}}),
commit = commits[0];
return convertToSerializableCommit(commits[0])
}

async fetchSnapshot(type, name, commitIdOrCommit, ref) {
Expand Down Expand Up @@ -147,7 +167,7 @@ export default class MorphicDB {
async log(commit, limit, includeCommits = false, knownCommitIds) {
await this.initializeIfNecessary();
let {name: db} = this;
return this.httpDB.fetchLog({db, commit, limit, includeCommits, knownCommitIds});
return (await this.httpDB.fetchLog({db, commit, limit, includeCommits, knownCommitIds})).map(convertToSerializableCommit);
}

async revert(type, name, toCommitId, ref = "HEAD") {
Expand All @@ -159,6 +179,7 @@ export default class MorphicDB {
async snapshotAndCommit(type, name, morph, snapshotOptions, commitSpec, ref, expectedParentCommit) {
let snapshot = await createMorphSnapshot(morph, snapshotOptions),
commit = await this.commit(type, name, snapshot, commitSpec, ref, expectedParentCommit);
commit = convertToSerializableCommit(commit);
morph.changeMetaData("commit", obj.dissoc(commit, ["preview"]), /*serialize = */true, /*merge = */false);

return commit;
Expand Down Expand Up @@ -200,6 +221,9 @@ export default class MorphicDB {
}
let snapshot = await this.fetchSnapshot(undefined, undefined, commit._id),
morph = await loadMorphFromSnapshot(snapshot, loadOptions);

commit = convertToSerializableCommit(commit);

morph.changeMetaData("commit", obj.dissoc(commit, ["preview"]),
/*serialize = */true, /*merge = */false);
return morph;
Expand Down
27 changes: 26 additions & 1 deletion object-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { morph, Icon } from "lively.morphic";
import { removeUnreachableObjects } from "lively.serializer2";
import { obj } from "lively.lang";
import { connect, disconnectAll } from "lively.bindings";
import { isReference } from "lively.serializer2/snapshot-navigation.js";

export var migrations = [

Expand Down Expand Up @@ -246,6 +247,30 @@ For now only a simple default theme...
}
return idAndSnapshot;
}
}
},

{
date: "2019-02-17",
name: 'change storage of commit metadata',
snapshotConverter: idAndSnapshot => {
let {id: rootId, snapshot} = idAndSnapshot;
// remove the nodeItemContainer from the tree submorphs, such that
// they do not get initialized at all.
// reconstruction of the tree rendering should happen automatically
Object.values(snapshot).map(m => {
if (m.props.metadata && isReference(m.props.metadata.value)) {
let metaObj = snapshot[m.props.metadata.value.id];
if (metaObj.props.commit && isReference(metaObj.props.commit.value)) {
let {type, name, _id} = snapshot[metaObj.props.commit.value.id].props;
metaObj.props.commit.value = {
__expr__: `({type: "${type.value}", name: "${name.value}", _id: "${_id.value}"})`,
}
}
}
});
removeUnreachableObjects([rootId], snapshot)
return idAndSnapshot;
}
},

];
11 changes: 6 additions & 5 deletions partsbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { pt, Color, Rectangle } from "lively.graphics";
import { connect } from "lively.bindings";
import LoadingIndicator from "lively.components/loading-indicator.js";
import { emit } from "lively.notifications";
import { SnapshotPackageHelper, default as MorphicDB } from "./morphicdb/db.js";
import { SnapshotPackageHelper, ensureCommitInfo, default as MorphicDB } from "./morphicdb/db.js";
import { createMorphSnapshot } from "./serialization.js";

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Expand All @@ -29,7 +29,8 @@ export async function loadPart(nameOrCommit, options = {}) {
// is re-attached to actual part
if (part.isWindow && part.targetMorph/* && part.targetMorph.name === name*/) {
let target = part.targetMorph,
commit = part.metadata && part.metadata.commit;
commit = await ensureCommitInfo(part.metadata && part.metadata.commit);

if (commit) {
target.changeMetaData("commit", commit, /*serialize = */true, /*merge = */false);
}
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function interactivelySavePart(part, options = {}) {
world = part.world() || part.env.world;

let name = part.name, tags = [], description = "",
oldCommit = Path("metadata.commit").get(part) || Path("metadata.commit").get(actualPart),
oldCommit = await ensureCommitInfo(Path("metadata.commit").get(part) || Path("metadata.commit").get(actualPart)),
morphicDB = options.morphicDB || MorphicDB.default;

if (!oldCommit) {
Expand Down Expand Up @@ -151,8 +152,8 @@ export async function interactivelySavePart(part, options = {}) {
commit = await savePart(part, name, options, commitSpec, ref, expectedParentCommit);

if (switchedToWindow) {
if (actualPart.metadata) actualPart.metadata.commit = win.metadata.commit;
else actualPart.metadata = {commit: win.metadata.commit};
if (actualPart.metadata) actualPart.metadata.commit = await ensureCommitInfo(win.metadata.commit);
else actualPart.metadata = {commit: await ensureCommitInfo(win.metadata.commit)};
win.metadata = windowMetadata;
}

Expand Down
5 changes: 4 additions & 1 deletion world-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { loadObjectFromPartsbinFolder, loadPart } from "./partsbin.js";
import { interactivelySaveWorld } from "./world-loading.js";
import { show } from "lively.halos/markers.js";
import { LoadingIndicator } from "lively.components";
import { ensureCommitInfo } from "./morphicdb/db.js";

var commands = [

Expand Down Expand Up @@ -717,11 +718,13 @@ var commands = [
{
name: "open PartsBin",
exec: async function(world) {
var li = LoadingIndicator.open('Loading Partsbin...');
var { loadPart } = await System.import("lively.morphic/partsbin.js")
var pb = await loadPart("PartsBin");
pb.openInWorldNearHand();
pb.targetMorph.selectedCategory = "*basics*";
pb.focus();
li.remove();
return pb;
}
},
Expand Down Expand Up @@ -1141,7 +1144,7 @@ var commands = [
}
deployment = {
id: name,
commit: world.metadata.commit,
commit: await ensureCommitInfo(world.metadata.commit),
dbName: MorphicDB.default.name
};
return LoadingIndicator.forPromise(
Expand Down
3 changes: 2 additions & 1 deletion world-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Path, obj, date, promise } from "lively.lang";
import { loadObjectFromPartsbinFolder } from "./partsbin.js";
import LoadingIndicator from "lively.components/loading-indicator.js";
import { Color } from "lively.graphics";
import { ensureCommitInfo } from "./morphicdb/db.js";


export function pathForBrowserHistory(worldName, queryString) {
Expand Down Expand Up @@ -191,7 +192,7 @@ export async function interactivelySaveWorld(world, options) {
options = {showSaveDialog: true, useExpectedCommit: true, errorOnMissingExpectedCommit: false, confirmOverwrite: true, ...options};

let name = world.name, tags = [], description = "",
oldCommit = Path("metadata.commit").get(world),
oldCommit = await ensureCommitInfo(Path("metadata.commit").get(world)),
db = options.morphicdb || MorphicDB.default;

if (options.showSaveDialog) {
Expand Down

0 comments on commit 86ad5cc

Please sign in to comment.