Commit cd07ec24 by preetwinder

fix tests, new tests and fix index2 issues

1 parent 50234845
...@@ -33,11 +33,11 @@ ...@@ -33,11 +33,11 @@
} }
this._sub_storage = jIO.createJIO(description.sub_storage); this._sub_storage = jIO.createJIO(description.sub_storage);
this._database_name = "jio:" + description.database; this._database_name = "jio:" + description.database;
this._index_keys = description.index_keys; this._index_keys = description.index_keys || [];
} }
IndexStorage2.prototype.hasCapacity = function (name) { IndexStorage2.prototype.hasCapacity = function (name) {
return ((name === "list") || (name === "query")); return ((name === "list") || (name === "query")) || (name === "limit");
}; };
function handleUpgradeNeeded(evt, index_keys) { function handleUpgradeNeeded(evt, index_keys) {
...@@ -160,26 +160,43 @@ ...@@ -160,26 +160,43 @@
}); });
} }
IndexStorage2.prototype._runQuery = function (index, value) { function filterDocValues(doc, keys) {
var filtered_doc = {}, i;
for (i = 0; i < keys.length; i += 1) {
if (doc[keys[i]]) {
filtered_doc[keys[i]] = doc[keys[i]];
} else {
throw new jIO.util.jIOError(
"Index key '" + keys[i] + "' not found in document",
404
);
}
}
return filtered_doc;
}
IndexStorage2.prototype._runQuery = function (index, value, limit) {
var context = this; var context = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
if ((context._index_keys.indexOf(index) === -1)) { if ((context._index_keys.indexOf(index) === -1)) {
if (context._sub_storage.hasCapacity("query")) { try {
return context._sub_storage.buildQuery( context._sub_storage.hasCapacity("query");
{"query": index + ":" + value} } catch (error) {
) throw new jIO.util.jIOError(
.then(function (result) { "No index for this key and substorage doesn't support queries"
return result; );
});
} }
return context._sub_storage.buildQuery(
{"query": index + ":" + value}
);
} }
return waitForOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._index_keys,
context._index_keys, function (db) { function (db) {
return waitForTransaction(db, ["index-store"], "readonly", return waitForTransaction(db, ["index-store"], "readonly",
function (tx) { function (tx) {
return waitForIDBRequest(tx.objectStore("index-store") return waitForIDBRequest(tx.objectStore("index-store")
.index("Index-" + index).getAll(value)) .index("Index-" + index).getAll(value, limit))
.then(function (evt) { .then(function (evt) {
return evt.target.result; return evt.target.result;
}); });
...@@ -188,12 +205,12 @@ ...@@ -188,12 +205,12 @@
}); });
}; };
IndexStorage2.prototype._processQueryObject = function (object) { IndexStorage2.prototype._processQueryObject = function (object, limit) {
var promise_list = [], context = this, i, j, query_result = new Set(); var promise_list = [], context = this, i, j, query_result = new Set();
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
if (object.type === "simple") { if (object.type === "simple") {
return context._runQuery(object.key, object.value); return context._runQuery(object.key, object.value, limit);
} }
if (object.type === "complex") { if (object.type === "complex") {
for (i = 0; i < object.query_list.length; i += 1) { for (i = 0; i < object.query_list.length; i += 1) {
...@@ -234,18 +251,20 @@ ...@@ -234,18 +251,20 @@
IndexStorage2.prototype.buildQuery = function (options) { IndexStorage2.prototype.buildQuery = function (options) {
var context = this; var context = this;
if (options.query) { if (options.query) {
return this._processQueryObject(parseStringToObject(options.query)) return this._processQueryObject(parseStringToObject(options.query),
.then(function (result) { options.limit)
.push(function (result) {
return result.map(function (value) { return result.map(function (value) {
return {"id": value.id, "value": {} }; return {"id": value.id, "value": {} };
}); });
}); });
} }
return waitForOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._index_keys,
context._index_keys, function (db) { function (db) {
return waitForTransaction(db, ["index-store"], "readonly", return waitForTransaction(db, ["index-store"], "readonly",
function (tx) { function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").getAll()) return waitForIDBRequest(tx.objectStore("index-store")
.getAll(undefined, options.limit))
.then(function (evt) { .then(function (evt) {
return evt.target.result.map(function (value) { return evt.target.result.map(function (value) {
return {"id": value.id, "value": {} }; return {"id": value.id, "value": {} };
...@@ -259,29 +278,18 @@ ...@@ -259,29 +278,18 @@
return this._sub_storage.get.apply(this._sub_storage, arguments); return this._sub_storage.get.apply(this._sub_storage, arguments);
}; };
IndexStorage2.prototype._filter_doc_values = function (doc, keys) {
var filtered_doc = {}, i;
for (i = 0; i < keys.length; i += 1) {
filtered_doc[keys[i]] = doc[keys[i]];
}
return filtered_doc;
};
IndexStorage2.prototype.put = function (id, value) { IndexStorage2.prototype.put = function (id, value) {
var context = this; var context = this;
return context._sub_storage.put(id, value) return context._sub_storage.put(id, value)
.push(function (result) { .push(function () {
return waitForOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._index_keys,
context._index_keys, function (db) { function (db) {
return waitForTransaction(db, ["index-store"], "readwrite", return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) { function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").put({ return waitForIDBRequest(tx.objectStore("index-store").put({
"id": id, "id": id,
"doc": context._filter_doc_values(value, context._index_keys) "doc": filterDocValues(value, context._index_keys)
})) }));
.then(function () {
return result;
});
}); });
}); });
}); });
...@@ -291,13 +299,13 @@ ...@@ -291,13 +299,13 @@
var context = this; var context = this;
return context._sub_storage.post(value) return context._sub_storage.post(value)
.push(function (id) { .push(function (id) {
return waitForOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._index_keys,
context._index_keys, function (db) { function (db) {
return waitForTransaction(db, ["index-store"], "readwrite", return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) { function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").put({ return waitForIDBRequest(tx.objectStore("index-store").put({
"id": id, "id": id,
"doc": context._filter_doc_values(value, context._index_keys) "doc": filterDocValues(value, context._index_keys)
})) }))
.then(function () { .then(function () {
return id; return id;
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!