Skip to content

Commit

Permalink
use dereferenced versions of merge tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Jan 23, 2024
1 parent a803b89 commit b66652a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
60 changes: 59 additions & 1 deletion source/server/routes/scenes/put/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("PUT /scenes/:scene/scene.svx.json", function(){
expect(JSON.parse(data)).to.deep.equal(sampleDoc);
});

it("uses the structured scene merge algorithm when possible", async function(){
it("uses a merge algorithm when possible", async function(){
//Insert a change that happened between checkout and commit

let currentDoc = JSON.parse(sampleDocString);
Expand All @@ -88,4 +88,62 @@ describe("PUT /scenes/:scene/scene.svx.json", function(){
expect(data.metas[0].collection.titles["FR"]).to.equal("Titre 1");
});

it("performs structured merge on the document", async function(){
//This is a slightly less trivial case where we check if proper deduplication is applied
//Insert a change that happened between checkout and commit

let currentDoc = JSON.parse(sampleDocString);
currentDoc.nodes.push({
"name": "Model 1",
"model": 1,
});
currentDoc.models.push({
"units": "mm",
"derivatives": [
{
"usage": "Web3D",
"quality": "High",
"assets": [{
"uri": "models/model1.glb",
"type": "Model",
}
]
}
]
});
await vfs.writeDoc(JSON.stringify(currentDoc), scene_id, user.uid);


//Make our user reference the first doc generation
sampleDoc.asset.id = 1;
sampleDoc.nodes.push({
"name": "Model 2",
"model": 1,
});
sampleDoc.models.push({
"units": "mm",
"derivatives": [
{
"usage": "Web3D",
"quality": "High",
"assets": [{
"uri": "models/model2.glb",
"type": "Model",
}
]
}
]
});
let r = await request(this.server).put(`/scenes/${titleSlug}/scene.svx.json`)
.auth("bob", "12345678")
.set("Content-Type", "application/si-dpo-3d.document+json")
.send(sampleDoc)
.expect(204);

let {ctime, mtime, data:docString, id, ...doc} = await vfs.getDoc(scene_id);
const data = JSON.parse(docString);
expect(data.models).to.have.length(3);
expect(data.nodes).to.have.length(6);
});

});
11 changes: 6 additions & 5 deletions source/server/routes/scenes/put/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import {inspect} from "util";
import path from "path";
import { Request, Response } from "express";

Expand Down Expand Up @@ -48,14 +48,15 @@ export default async function handlePutDocument(req :Request, res :Response){
const {data: refDocString} = await tr.getDocById(refId);
const refDoc = JSON.parse(refDocString);

const docDiff = merge.diff(refDoc, newDoc);
const docDiff = merge.diffDoc(refDoc, newDoc);
if(Object.keys(docDiff).length == 0){
console.log("Nothing to do");
console.log("PUT document: Nothing to do");
//Nothing to do
return res.status(204).send();
}
console.log("Merge changes : ", JSON.stringify(docDiff));
const mergedDoc = merge.apply(currentDoc, docDiff);

console.log("PUT document (merge): ", JSON.stringify(docDiff, null, 2));
const mergedDoc = merge.applyDoc(currentDoc, docDiff);
let s = JSON.stringify(mergedDoc);
if(s == "{}") throw new BadRequestError(`Invalid json document`);
let id = await tr.writeDoc(s, scene, uid);
Expand Down

0 comments on commit b66652a

Please sign in to comment.