Skip to content

Commit

Permalink
fix: issue with lower fill factor than order for single element tree (#…
Browse files Browse the repository at this point in the history
…14)

* fix: issue with lower fill factor than order for single element tree (#13)

* fix(SBFRoot/findLowerThan): finding document on equal case for lte

Specific case when order is bigger than actual size

* style: formatting

* test: add tests for tree with lower size than order

Co-authored-by: Anurag Vohra <53807480+anuragvohraec@users.noreply.github.com>
  • Loading branch information
Alex-Werner and anuragvohraec authored May 15, 2020
1 parent 1bd6f95 commit 4924272
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/types/SBFRoot/methods/ops/findLowerThan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async function findLowerThan(key, includeKey = false) {
let result = {identifiers: [], keys: []};
const {childrens,identifiers, keys} = this;
const { childrens, identifiers, keys } = this;

// We first see where our key is located;
let leafIndex = 0;
Expand All @@ -12,21 +12,24 @@ async function findLowerThan(key, includeKey = false) {

let p = [];

if(childrens.length===0){
if(identifiers[leafIndex]){
if (childrens.length === 0) {
if(!identifiers[leafIndex]){
leafIndex--;
}
if (identifiers[leafIndex]) {
const last = keys.lastIndexOf(key);

keys.slice(0, last+1 || leafIndex+1).forEach((_key, i)=>{
if(_key<=key){
if(_key === key && !includeKey){
keys.slice(0, last + 1 || leafIndex + 1).forEach((_key, i) => {
if (_key <= key) {
if (_key === key && !includeKey) {
return;
}

result.identifiers.push(identifiers[i])
result.keys.push(keys[i])
}
})
}
}else{
} else {
// All smaller leaf that our leafIndex needs to be included
if (leafIndex >= 1) {
childrens.slice(0, leafIndex).forEach((child) => {
Expand Down
59 changes: 59 additions & 0 deletions test/e2e/use.case.4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const {expect} = require('chai');
const {SBTree} = require('../..');
const users = require('./users');

const toNames = (arr)=>{
return arr.reduce((acc, el)=>{
acc.push(el.name);
return acc
},[]);
}
describe('E2E - UseCase with lower fill factor than order', function suite() {
let ts = 1588913801357;
describe('RegUser DB', async () => {
const customTree = new SBTree({order:40});
const doc = {
_id: '754aeea7c0919797d6eb2710d200eafd',
key: 'VATEmq',
value: 4,
ts
};

it('should insert', async function () {
await customTree.insertDocuments(doc);
});
it('should get document', async function () {
const res = await customTree.getDocument('754aeea7c0919797d6eb2710d200eafd')
expect(res).to.deep.equal(doc);
});
it('should be able to search the document', async function () {
const found = await customTree.findDocuments({ts});
expect(found).to.deep.equal([doc]);

const foundLowerThanEq = await customTree.findDocuments({ts:{$lte: ts}});
expect(foundLowerThanEq).to.deep.equal([doc]);

const foundLowerThanEq1 = await customTree.findDocuments({ts:{$lte: ts+1000}});
expect(foundLowerThanEq1).to.deep.equal([doc]);

const foundGreaterThanEq = await customTree.findDocuments({ts:{$gte: ts}});
expect(foundGreaterThanEq).to.deep.equal([doc]);

const foundGreaterThanEq1 = await customTree.findDocuments({ts:{$gte: ts-1000}});
expect(foundGreaterThanEq1).to.deep.equal([doc]);

const foundEq = await customTree.findDocuments({ts:{$eq: ts}});
expect(foundEq).to.deep.equal([doc]);
});
it('should be able to delete the document', async function () {
const deleted = await customTree.deleteDocuments({ts: {$lte: ts+1}});
expect(deleted).to.deep.equal([doc]);

const found = await customTree.findDocuments({ts});
expect(found).to.deep.equal([]);

const res = await customTree.getDocument('754aeea7c0919797d6eb2710d200eafd')
expect(res).to.deep.equal({});
});
});
})

0 comments on commit 4924272

Please sign in to comment.