Commit 87082ec7 authored by preetwinder's avatar preetwinder

Remove unneeded parts

parent 854c1b59
...@@ -41,72 +41,9 @@ ...@@ -41,72 +41,9 @@
this._version = description.version || undefined; this._version = description.version || undefined;
} }
function convertToObject(list, key) {
var i, obj = {};
for (i = 0; i < list.length; i += 1) {
obj[list[i][key]] = list[i];
}
return obj;
}
function keyCount(obj) {
return Object.keys(obj).length;
}
function union(list) {
var result, i, j;
if (list.length === 0) {
return [];
}
result = convertToObject(list[0], 'id');
for (i = 1; i < list.length; i += 1) {
for (j = 0; j < list[i].length; j += 1) {
if (result[list[i][j].id]) {
if (keyCount(result[list[i][j].id].doc) <
keyCount(list[i][j].doc)) {
result[list[i][j].id] = list[i][j];
}
} else {
result[list[i][j].id] = list[i][j];
}
}
}
return Object.values(result);
}
function intersect(list) {
var result, temp = {}, i, j;
if (list.length === 0) {
return [];
}
result = convertToObject(list[0], 'id');
for (i = 1; i < list.length; i += 1) {
for (j = 0; j < list[i].length; j += 1) {
if (result[list[i][j].id]) {
if (keyCount(result[list[i][j].id].doc) <
keyCount(list[i][j].doc)) {
temp[list[i][j].id] = list[i][j];
} else {
temp[list[i][j].id] = result[list[i][j].id];
}
}
}
result = temp;
temp = {};
}
return Object.values(result);
}
IndexStorage2.prototype.hasCapacity = function (name) { IndexStorage2.prototype.hasCapacity = function (name) {
var this_storage_capacity_list = ["select", return (name === 'query') || (name === 'limit') || (name === 'list') ||
"list", this._sub_storage.hasCapacity(name);
"query",
"sort",
"include"];
if (this_storage_capacity_list.indexOf(name) !== -1) {
return true;
}
}; };
function handleUpgradeNeeded(evt, index_keys) { function handleUpgradeNeeded(evt, index_keys) {
...@@ -117,19 +54,20 @@ ...@@ -117,19 +54,20 @@
keyPath: "id", keyPath: "id",
autoIncrement: false autoIncrement: false
}); });
current_indices = new Set();
} else { } else {
store = evt.target.transaction.objectStore('index-store'); store = evt.target.transaction.objectStore('index-store');
} current_indices = new Set(store.indexNames);
current_indices = new Set(store.indexNames); for (i = 0; i < index_keys.length; i += 1) {
for (i = 0; i < index_keys.length; i += 1) { if (!(current_indices.has(index_keys[i]))) {
if (!(current_indices.has(index_keys[i]))) { db.deleteObjectStore("index-store");
db.deleteObjectStore("index-store"); store = db.createObjectStore("index-store", {
store = db.createObjectStore("index-store", { keyPath: "id",
keyPath: "id", autoIncrement: false
autoIncrement: false });
}); current_indices = new Set();
current_indices = new Set(); break;
break; }
} }
} }
for (i = 0; i < index_keys.length; i += 1) { for (i = 0; i < index_keys.length; i += 1) {
...@@ -173,11 +111,10 @@ ...@@ -173,11 +111,10 @@
reject("Connection to: " + db_name + " timeout"); reject("Connection to: " + db_name + " timeout");
}; };
// request.onblocked = function (evt) { request.onblocked = function () {
// console.log("evt", evt); request.result.close();
// request.result.close(); reject("Connection to: " + db_name + " was blocked");
// reject("Connection to: " + db_name + " was blocked"); };
// };
// Create DB if necessary // // Create DB if necessary //
request.onupgradeneeded = function (evt) { request.onupgradeneeded = function (evt) {
...@@ -264,33 +201,7 @@ ...@@ -264,33 +201,7 @@
return doc; return doc;
} }
IndexStorage2.prototype.forceIncludeDocs = function (result) { IndexStorage2.prototype._runQuery = function (key, value, limit) {
var i, get_list = {}, context = this;
return RSVP.Queue()
.push(function () {
for (i = 0; i < result.length; i += 1) {
if (!(result[i].include)) {
get_list[result[i].id] = context._sub_storage.get(result[i].id);
}
}
return RSVP.hash(get_list);
})
.push(function (get_result) {
var keys;
result = convertToObject(result, 'id');
keys = Object.keys(get_result);
for (i = 0; i < keys.length; i += 1) {
result[keys[i]].doc = get_result[keys[i]];
}
result = Object.values(result);
return result;
})
.push(function () {
return result;
});
};
IndexStorage2.prototype._runQuery = function (index, value) {
var context = this; var context = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -299,7 +210,7 @@ ...@@ -299,7 +210,7 @@
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-" + key).getAll(value, limit))
.then(function (evt) { .then(function (evt) {
return evt.target.result; return evt.target.result;
}); });
...@@ -308,70 +219,28 @@ ...@@ -308,70 +219,28 @@
}); });
}; };
IndexStorage2.prototype._processQueryObject = function (object, IndexStorage2.prototype.buildQuery = function (options) {
include_docs) { var context = this, query;
var promise_list = [], context = this, i; if (options.query) {
include_docs = include_docs || false; query = parseStringToObject(options.query);
return RSVP.Queue() if (query.type === 'simple') {
.push(function () { if (context._index_keys.indexOf(query.key) !== -1) {
if (object.type === "simple") { return context._runQuery(query.key, query.value, options.limit)
if ((context._index_keys.indexOf(object.key) === -1)) { .push(function (result) {
return context._sub_storage.allDocs({ return result.map(function (value) {
"query": object.key + ":" + object.value, return {
"include_docs": include_docs id: value.id,
}) value: {}
.push(function (result) { };
if (include_docs) {
result.data.rows.map(function (v) {
v.include = true;
});
}
return result.data.rows;
}); });
}
return context._runQuery(object.key, object.value);
}
if (object.type === "complex") {
for (i = 0; i < object.query_list.length; i += 1) {
promise_list.push(context
._processQueryObject(object.query_list[i], include_docs));
}
return RSVP.all(promise_list)
.then(function (result) {
if (object.operator === "OR") {
return union(result);
}
if (object.operator === "AND") {
return intersect(result);
}
}); });
} }
}) }
};
IndexStorage2.prototype.buildQuery = function (options) {
var context = this;
if (options.query) {
return this._processQueryObject(parseStringToObject(options.query),
options.include_docs)
.push(function (result) {
if (options.include_docs) {
result = context.forceIncludeDocs(result);
options.select_list = undefined;
} else {
options.select_list = options.select_list || [];
}
return result;
})
.push(function (result) {
return result.map(function (value) {
return {
"id": value.id,
"value": filterDocValues(value.doc, options.select_list)
};
});
});
} }
return context._sub_storage.allDocs(options)
.push(function (result) {
return result.data.rows;
});
}; };
IndexStorage2.prototype.get = function () { IndexStorage2.prototype.get = function () {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment