Skip to content

Commit

Permalink
add lte / lt querys to use index
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Bittmann committed Nov 18, 2021
1 parent 3ee8e0b commit f533313
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/CodeInterpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,23 @@ export default class CodeInterpreter {
count: 0
}
}

if(conditionKey === "$lte" || conditionKey === "$lt") {
return {
*[Symbol.iterator]() {
let range = db.getRange({
start: indexKey(key, dbValues.LO, dbValues.LO)
,end: indexKey(key, conditionValue, conditionKey === "$lte"? dbValues.HI : dbValues.LO)
})

for(let row of range) {
this.count++
yield row.key.pop()
}
},
count: 0
}
}

throw new Error(`Search value could not be a object / Unknown search type ${conditionKey}`)
}
Expand Down
81 changes: 81 additions & 0 deletions test/006_TestQuerys.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,85 @@ describe("Querys", () => {
expect(q.interpreterNeeded).to.be.equal(false)
});
});

describe("(row) => { return row.activeSince >= activeSince }", async () => {
let result
let activeSince = 2002

before(async () => {
result = await db.table("persons").filter((row) => {
return row.activeSince >= activeSince
}, { activeSince })
});

it("should find one entry", async () => {
expect(result.length).to.be.equal(3);
});

it("should find entry with correct data", async () => {
for(let row of result) {
expect(row.activeSince).to.be.greaterThanOrEqual(activeSince);
}
});

it("should use index without interpreter", async () => {
let q = result.getQuery();
expect(q.indexes.activeSince).to.be.gte(1);
expect(q.interpreterNeeded).to.be.equal(false)
});
});

describe("(row) => { return row.activeSince < activeSince }", async () => {
let result
let activeSince = 2002

before(async () => {
result = await db.table("persons").filter((row) => {
return row.activeSince < activeSince
}, { activeSince })
});

it("should find one entry", async () => {
expect(result.length).to.be.equal(1);
});

it("should find entry with correct data", async () => {
for(let row of result) {
expect(row.activeSince).to.be.lessThan(activeSince);
}
});

it("should use index without interpreter", async () => {
let q = result.getQuery();
expect(q.indexes.activeSince).to.be.gte(1);
expect(q.interpreterNeeded).to.be.equal(false)
});
});

describe("(row) => { return row.activeSince <= activeSince }", async () => {
let result
let activeSince = 2002

before(async () => {
result = await db.table("persons").filter((row) => {
return row.activeSince <= activeSince
}, { activeSince })
});

it("should find one entry", async () => {
expect(result.length).to.be.equal(2);
});

it("should find entry with correct data", async () => {
for(let row of result) {
expect(row.activeSince).to.be.lessThanOrEqual(activeSince);
}
});

it("should use index without interpreter", async () => {
let q = result.getQuery();
expect(q.indexes.activeSince).to.be.gte(1);
expect(q.interpreterNeeded).to.be.equal(false)
});
});
})

0 comments on commit f533313

Please sign in to comment.