Skip to content

Commit

Permalink
test for multi index and repeated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
yathit committed Feb 13, 2016
1 parent fee6417 commit 97bdee3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/ydn/db/algo/abstract_solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,42 @@ ydn.db.algo.AbstractSolver.prototype.pusher = function(advance, keys, values,
};


/**
* Find the lowest key.
* @param {Array<IDBKey>} keys
* @return {number} advancement array for lowest key
*/
ydn.db.algo.AbstractSolver.lowestAdvance = function(keys) {
var adv = [];
var idx = -1;
for (var i = 0; i < keys.length; i++) {
if (goog.isDefAndNotNull(keys[i])) {
if (adv.length == 0) {
adv[i] = 1;
idx = i;
} else {
var cmp = ydn.db.cmp(keys[i], keys[idx]);
if (cmp == -1) {
idx = i;
adv = [];
adv[i] = 1;
} else if (cmp == 0) {
adv[i] = 1;
}
}
}
}
return adv;
};


/**
* Find the lowest key.
* @param {Array<IDBKey>} keys
* @return {number} index of the lowest key.
*/
ydn.db.algo.AbstractSolver.lowest = function(keys) {
var idx = -1;
var lowest = null;
for (var i = 0; i < keys.length; i++) {
if (goog.isDefAndNotNull(keys[i])) {
if (!goog.isDef(keys[idx])) {
Expand Down
11 changes: 8 additions & 3 deletions src/ydn/db/algo/multi_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ ydn.db.algo.MultiQuery.prototype.begin = function(iterators, callback) {
* @inheritDoc
*/
ydn.db.algo.MultiQuery.prototype.solver = function(keys, values) {
var lowest = ydn.db.algo.AbstractSolver.lowest(keys);
var advancement = [];
advancement[lowest] = 1;
var advancement = ydn.db.algo.AbstractSolver.lowestAdvance(keys);
var lowest = -1;
for (var i = 0; i < advancement.length; i++) {
if (goog.isDefAndNotNull(advancement[i])) {
lowest = i;
break;
}
}
return this.pusher({'advance': advancement}, keys, values, values[lowest], keys[lowest]);
};
64 changes: 63 additions & 1 deletion src/ydn/db/algo/multi_query_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function test_lowest_key() {
}


function test_lowest_advancement() {
assertArrayEquals([], ydn.db.algo.AbstractSolver.lowestAdvance([]));
assertArrayEquals([1], ydn.db.algo.AbstractSolver.lowestAdvance([1]));
assertArrayEquals([undefined, 1], ydn.db.algo.AbstractSolver.lowestAdvance([2, 1]));
assertArrayEquals([undefined, undefined, 1], ydn.db.algo.AbstractSolver.lowestAdvance([1, 1, -1]));
assertArrayEquals([undefined, 1, 1], ydn.db.algo.AbstractSolver.lowestAdvance([1, 0, 0, 2]));
}


function test_basic() {
var animals = [
{id: 1, name: 'rat', color: 'brown', horn: 0, legs: 4},
Expand Down Expand Up @@ -72,7 +81,60 @@ function test_basic() {
var result = [];
var multi = new ydn.db.algo.MultiQuery(result);
db.scan(multi, iters).addCallback(function(x) {
db.values('animals', keys).add
result.sort(function(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
});
assertArrayEquals(exp, result);
ydn.db.deleteDatabase(db.getName(), db.getType());
db.close();
asyncTestCase.continueTesting();
});
});
}


function test_multi_index() {
var animals = [
{id: 1, name: 'rat', color: 'brown', horn: 0, legs: 4},
{id: 2, name: 'leopard', color: 'spots', horn: 2, legs: 4},
{id: 3, name: 'galon', color: 'gold', horn: 10, legs: 2},
{id: 4, name: 'tiger', color: 'spots', horn: 2, legs: 4},
{id: 5, name: 'snake', color: 'spots', horn: 0, legs: 0},
{id: 6, name: 'rhino', color: 'spots', horn: 1, legs: 4},
{id: 7, name: 'ox', color: 'black', horn: 2, legs: 4},
{id: 8, name: 'cow', color: 'spots', horn: 2, legs: 4},
{id: 9, name: 'chicken', color: 'red', horn: 0, legs: 2},
{id: 10, name: 'unicon', color: 'pink', horn: 1, legs: 4},
{id: 11, name: 'cat', color: 'spots', horn: 0, legs: 4},
{id: 12, name: 'human', color: 'pink', horn: 0, legs: 2}
];
var schema = {
stores: [{
name: 'animals',
keyPath: 'id',
indexes: [{
name: 'color'
}, {
name: 'horn'
}, {
name: 'legs'
}]
}]
};
var db = new ydn.db.Storage('multi-queyr-basic', schema);
asyncTestCase.waitForAsync('test');
db.putAll('animals', animals).addCallback(function() {
var iters = [ydn.db.IndexIterator.where('animals', 'horn', '=', 0),
ydn.db.IndexIterator.where('animals', 'legs', '=', 0)];
var exp = animals.filter(function(x) {
return x.horn == 0 || x.legs == 0;
});
exp = exp.map(function(x) {
return x.id;
});
var result = [];
var multi = new ydn.db.algo.MultiQuery(result);
db.scan(multi, iters).addCallback(function(x) {
result.sort(function(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
});
Expand Down

0 comments on commit 97bdee3

Please sign in to comment.