Skip to content

Commit

Permalink
impr(remove): use draw in e2e test + improve removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Werner committed Oct 3, 2019
1 parent d4347b1 commit fde59aa
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/adapters/MemoryAdapter/MemoryAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MemoryAdapter.prototype.findInLeaf = require('./methods/findInLeaf')
MemoryAdapter.prototype.getDocument = require('./methods/getDocument')
MemoryAdapter.prototype.openLeaf = require('./methods/openLeaf')
MemoryAdapter.prototype.removeInLeaf = require('./methods/removeInLeaf')
MemoryAdapter.prototype.removeDocument = require('./methods/removeDocument')
MemoryAdapter.prototype.saveDocument = require('./methods/saveDocument')
MemoryAdapter.prototype.splitLeaf = require('./methods/splitLeaf');
module.exports = MemoryAdapter;
16 changes: 16 additions & 0 deletions src/adapters/MemoryAdapter/methods/removeDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function removeDocument(identifier){
console.log(identifier)
console.log(identifier)
console.log(identifier)
console.log(identifier)
console.log(identifier)
if(!this.documents[identifier]){
console.log(this.documents[identifier])
console.log(this.documents[identifier])
console.log(this.documents[identifier])
console.log(this.documents[identifier])
console.log(this.documents[identifier])
console.log(this.documents[identifier])
}
}
module.exports = removeDocument
24 changes: 20 additions & 4 deletions src/types/SBFLeaf/methods/mergeWithSiblings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@ async function mergeWithSiblings(){


if(siblings.left) siblings.leftStatus = await siblings.left.getFillStatus();
if(siblings.right) siblings.rightStatus = await siblings.left.getFillStatus();
if(siblings.right) siblings.rightStatus = await siblings.right.getFillStatus();

if(selfPos===0 && siblings.right){
const rightSib = siblings.right;

throw new Error('Implementation required.');
const rightSibPos = selfPos+1;
const {identifiers, keys} = await rightSib.getAll();

// Repair for parent.
const p = [];
identifiers.forEach((identifier,i)=>{
const key = keys[i];
p.push(this.insert(identifier, key));
});
await Promise.all(p);

// Kill parent's children
delete parent.childrens[rightSibPos];

// Remove the undefined corpse from the array
parent.childrens.splice(rightSibPos,1);


// Repair parent keys TODO FIXME
hasMerged=true;

}else if(siblings.left){
const leftSib = siblings.left;
Expand All @@ -40,7 +56,7 @@ async function mergeWithSiblings(){
// Remove the undefined corpse from the array
parent.childrens.splice(leftSibPos,1);

// Repair parent keys
// Repair parent keys TODO FIXME
hasMerged=true;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/SBFNode/SBFNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ SBFNode.prototype.getTreeOptions = require('./methods/getTreeOptions')
SBFNode.prototype.insert = require('./methods/insert')
SBFNode.prototype.insertReferenceKey = require('./methods/insertReferenceKey')
SBFNode.prototype.isFull = require('./methods/isFull')
SBFNode.prototype.remove = require('./methods/remove')
SBFNode.prototype.split = require('./methods/split')
module.exports = SBFNode;
1 change: 0 additions & 1 deletion src/types/SBFNode/methods/insertReferenceKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const {insertSorted} = require('../../../utils/array');
module.exports =async function insertReferenceKey(value){
if(this.isFull()){
await this.split();
throw new Error('Root is full');
}
const index = insertSorted(this.keys, value);
return index;
Expand Down
12 changes: 12 additions & 0 deletions src/types/SBFNode/methods/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
async function remove(value){
let leafIndex = 0;
this.keys.forEach((_key)=>{
if(value<=_key) return;
leafIndex++;
});

const leaf = this.childrens[leafIndex];
await leaf.remove(value);
};
module.exports = remove;

1 change: 1 addition & 0 deletions src/types/SBFTree/SBFTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SBFTree {

SBFTree.prototype.find = require('./methods/find');
SBFTree.prototype.get = require('./methods/get');
SBFTree.prototype.getAdapter = require('./methods/getAdapter');
SBFTree.prototype.insert = require('./methods/insert');
SBFTree.prototype.remove = require('./methods/remove');
module.exports = SBFTree;
3 changes: 3 additions & 0 deletions src/types/SBFTree/methods/getAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function getAdapter(){
return this.adapter;
}
1 change: 1 addition & 0 deletions src/types/SBTree/SBTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SBTree extends EventEmitter {

SBTree.prototype.deleteDocuments = require('./methods/deleteDocuments')
SBTree.prototype.findDocuments = require('./methods/findDocuments')
SBTree.prototype.getAdapter = require('./methods/getAdapter');
SBTree.prototype.getDocument = require('./methods/getDocument')
SBTree.prototype.getFieldTree = require('./methods/getFieldTree')
SBTree.prototype.insertDocuments = require('./methods/insertDocuments')
Expand Down
3 changes: 3 additions & 0 deletions src/types/SBTree/methods/getAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function getAdapter(){
return this.adapter;
}
6 changes: 5 additions & 1 deletion src/types/SBTree/ops/remove.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {map} = require('lodash')
const query = require('./query')
const ascii = require('../../../utils/ascii');


async function remove(_query) {
Expand All @@ -10,8 +11,11 @@ async function remove(_query) {
const fieldNode = this.getFieldTree(_fieldName);
await fieldNode.remove(result._id);

console.log('REMOVE')
// Remove documents
await this.getAdapter().removeDocument(result._id);
await (this.getAdapter()).removeDocument(result._id);
console.log('REMOVED')

}
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/utils/ascii.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ const {forEach}=require('./array');
Cheap drawing implementation. Would need deep rework to make it really working.
*/
const draw = async (fieldNode)=>{
if(fieldNode.id[0]==='t'){
console.log(`======== SBTree Tree ========`);
console.log(`=== Id : ${fieldNode.id}`)
console.log(`=== Order : ${fieldNode.options.order}`)
if(Object.keys(fieldNode.fieldTrees).length === 0){
console.log(`=== Empty.`)
}else{
await forEach(Object.keys(fieldNode.fieldTrees), async (fieldKey)=>{
const fieldTree = fieldNode.fieldTrees[fieldKey]
await draw(fieldTree);
})
}
return;
}

console.log(`======== SBTree Node ========`);
console.log(`=== Id : ${fieldNode.id}`)
console.log(`=== Order : ${fieldNode.options.order}`)
Expand Down Expand Up @@ -43,7 +58,6 @@ const processFromRoot = async (_root) =>{
rows.push(_root.keys);
const childrensToProcess = await processRootChildrens(_root.childrens)

console.log(childrensToProcess)
if(childrensToProcess.length){
await processLeafs(childrensToProcess);
}
Expand All @@ -59,7 +73,8 @@ const biggestRepeatTimes = biggestChildLen * spanVal;

rows.forEach((row,i)=>{
// const next = rows[i+1] || [];
const repeatTimes = biggestRepeatTimes - (i*spanVal*2);
const calc = biggestRepeatTimes - (i*spanVal*2)
const repeatTimes = (calc>0) ? calc : 0;
console.log(`${' '.repeat(repeatTimes)}${JSON.stringify(row)}`);
})
};
Expand Down
58 changes: 54 additions & 4 deletions test/e2e/use.case.1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {expect} = require('chai');
const {SBTree} = require('../..');
const userList = require('../fixtures/use.cases/1.reg.users').users;
const {draw}= require('../../src/utils/ascii');
describe('E2E - Classic UseCase', function suite() {
describe('RegUser DB', () => {
const shiftedUsers = [];
Expand Down Expand Up @@ -35,16 +36,18 @@ describe('E2E - Classic UseCase', function suite() {
it('should allow to insert documents', async function () {
const doc = userList.shift();
shiftedUsers.push(doc);

await customTree.insertDocuments(doc);

});
it('should correctly create fieldTrees', function () {
it('should correctly create fieldTrees', async function () {
expect(customTree.size).to.equal(1);
expect(customTree.id[0]).to.equal('t');

const fieldTrees = Object.keys(customTree.fieldTrees);
const expectedFieldTrees = ['age','firstname','lastname','email'];
const expectedFieldTrees = ['age', 'firstname', 'lastname', 'email'];
expect(fieldTrees).to.deep.equal(expectedFieldTrees);
fieldTrees.forEach((fieldTreeName)=>{
fieldTrees.forEach((fieldTreeName) => {
const fieldTree = customTree.fieldTrees[fieldTreeName];
expect(fieldTree.id[0]).to.equal('f');
expect(fieldTree.options.order).to.equal(3);
Expand All @@ -59,7 +62,7 @@ describe('E2E - Classic UseCase', function suite() {
});
it('should allow to find document', async function () {
const doc = shiftedUsers[0];
const findRes = await customTree.findDocuments({age:doc.age});
const findRes = await customTree.findDocuments({age: doc.age});
expect(findRes).to.deep.equal([doc])
});
it('should allow more than order', async function () {
Expand All @@ -68,8 +71,55 @@ describe('E2E - Classic UseCase', function suite() {
shiftedUsers.push(doc2);
shiftedUsers.push(doc3);
await customTree.insertDocuments(doc2);

await customTree.insertDocuments(doc3);

expect(customTree.fieldTrees['age'].root.keys).to.deep.equal([30]);
expect(customTree.fieldTrees['age'].root.childrens.length).to.deep.equal(2);
expect(await customTree.fieldTrees['age'].root.childrens[0].getAll()).to.deep.equal({
identifiers: ['5d73d1e14f24b21368a42631'],
keys: [24]
});
expect(await customTree.fieldTrees['age'].root.childrens[1].getAll()).to.deep.equal({
identifiers: ['5d73d1e14f24b21368185bb6', '5d73d1e14f24b213686f48a6'
],
keys: [30, 31]
});
});
it('should still allow to find document', async function () {
const doc = shiftedUsers[0];
const findRes = await customTree.findDocuments({age: doc.age});
expect(findRes).to.deep.equal([doc])
});
it('should allow to remove document', async function () {
// const doc = shiftedUsers[0];
// const delRes = await customTree.deleteDocuments({email: doc.email});
// console.log(delRes)
// expect(delRes).to.deep.equal([doc])
});
it('should allow to add even more', async function () {
const doc4 = userList.shift();
const doc5 = userList.shift();
const doc6 = userList.shift();
const doc7 = userList.shift();
shiftedUsers.push(doc4);
shiftedUsers.push(doc5);
shiftedUsers.push(doc6);
shiftedUsers.push(doc7);
await customTree.insertDocuments(doc4);
await customTree.insertDocuments(doc5);
await customTree.insertDocuments(doc6);
await customTree.insertDocuments(doc7);

});
it('should allow to remove document', async function () {
const doc = shiftedUsers[0];
await draw(customTree);
const delRes = await customTree.deleteDocuments({email: doc.email});
await draw(customTree);

// console.log(delRes)
// expect(delRes).to.deep.equal([doc])
});
});

Expand Down

0 comments on commit fde59aa

Please sign in to comment.