Commit 9dfafb7f authored by preetwinder's avatar preetwinder

migrations, additonal capacities and put test

parent 3fecea79
......@@ -34,29 +34,58 @@
this._sub_storage = jIO.createJIO(description.sub_storage);
this._database_name = "jio:" + description.database;
this._index_keys = description.index_keys || [];
this._version = 1;
}
IndexStorage2.prototype.hasCapacity = function (name) {
return ((name === "list") || (name === "query")) || (name === "limit");
return ((name === "list") || (name === "query")) || (name === "limit") ||
(name === "select");
};
function checkArrayEquality(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
var i;
array1 = array1.sort();
array2 = array2.sort();
for (i = 0; i < array1.length; i += 1) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
}
function handleUpgradeNeeded(evt, index_keys) {
var db = evt.target.result, store, i;
var db = evt.target.result, store, i, current_indices;
if (!(db.objectStoreNames[0])) {
store = db.createObjectStore("index-store", {
keyPath: "id",
autoIncrement: false
});
} else {
store = evt.target.transaction.objectStore('index-store');
}
current_indices = new Set(store.indexNames);
for (i = 0; i < index_keys.length; i += 1) {
store.createIndex("Index-" + index_keys[i], "doc." + index_keys[i],
{unique: false});
if (!(current_indices.has('Index-' + index_keys[i]))) {
store.createIndex('Index-' + index_keys[i],
'doc.' + index_keys[i], { unique: false });
}
current_indices.delete('Index-' + index_keys[i]);
}
current_indices = Array.from(current_indices);
for (i = 0; i < current_indices.length; i += 1) {
store.deleteIndex(current_indices[i]);
}
}
function waitForOpenIndexedDB(db_name, index_keys, callback) {
function waitForOpenIndexedDB(db_name, version, index_keys, callback) {
function resolver(resolve, reject) {
// Open DB //
var request = indexedDB.open(db_name);
var request = indexedDB.open(db_name, version);
request.onerror = function (error) {
if (request.result) {
request.result.close();
......@@ -81,11 +110,6 @@
reject("Connection to: " + db_name + " timeout");
};
request.onblocked = function () {
request.result.close();
reject("Connection to: " + db_name + " was blocked");
};
// Create DB if necessary //
request.onupgradeneeded = function (evt) {
handleUpgradeNeeded(evt, index_keys);
......@@ -99,6 +123,20 @@
request.onsuccess = function () {
return new RSVP.Queue()
.push(function () {
var store, current_indices, db_version, required_indices;
store = request.result.transaction('index-store')
.objectStore('index-store');
current_indices = store.indexNames;
db_version = request.result.version;
required_indices = index_keys.map(
function (value) {return 'Index-' + value; }
);
if (!(checkArrayEquality(required_indices,
Array.from(current_indices)))) {
request.result.close();
return waitForOpenIndexedDB(db_name, db_version + 1,
index_keys, callback);
}
return callback(request.result);
})
.push(function (result) {
......@@ -184,15 +222,16 @@
context._sub_storage.hasCapacity("query");
} catch (error) {
throw new jIO.util.jIOError(
"No index for this key and substorage doesn't support queries"
"No index for this key and substorage doesn't support queries",
404
);
}
return context._sub_storage.buildQuery(
{"query": index + ":" + value}
{ "query": index + ":" + value }
);
}
return waitForOpenIndexedDB(context._database_name, context._index_keys,
function (db) {
return waitForOpenIndexedDB(context._database_name, undefined,
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readonly",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store")
......@@ -255,19 +294,27 @@
options.limit)
.push(function (result) {
return result.map(function (value) {
return {"id": value.id, "value": {} };
return {
"id": value.id,
"value": filterDocValues(value.doc,
options.select_list || [])
};
});
});
}
return waitForOpenIndexedDB(context._database_name, context._index_keys,
function (db) {
return waitForOpenIndexedDB(context._database_name, undefined,
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readonly",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store")
.getAll(undefined, options.limit))
.then(function (evt) {
return evt.target.result.map(function (value) {
return {"id": value.id, "value": {} };
return {
"id": value.id,
"value": filterDocValues(value.doc,
options.select_list || [])
};
});
});
});
......@@ -282,8 +329,8 @@
var context = this;
return context._sub_storage.put(id, value)
.push(function () {
return waitForOpenIndexedDB(context._database_name, context._index_keys,
function (db) {
return waitForOpenIndexedDB(context._database_name, undefined,
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").put({
......@@ -299,8 +346,8 @@
var context = this;
return context._sub_storage.post(value)
.push(function (id) {
return waitForOpenIndexedDB(context._database_name, context._index_keys,
function (db) {
return waitForOpenIndexedDB(context._database_name, undefined,
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").put({
......@@ -318,16 +365,13 @@
IndexStorage2.prototype.remove = function (id) {
var context = this;
return context._sub_storage.remove(id)
.push(function (result) {
return waitForOpenIndexedDB(context._database_name, context._index_keys,
function (db) {
.push(function () {
return waitForOpenIndexedDB(context._database_name, undefined,
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store")
.delete(id))
.then(function () {
return result;
});
.delete(id));
});
});
});
......
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