Commit 05bea40b authored by preetwinder's avatar preetwinder

Change version change behavior

parent 4f8d9ed2
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
this._use_sub_storage_query_partial = this._use_sub_storage_query_partial =
description.use_sub_storage_query_partial; description.use_sub_storage_query_partial;
} }
this._version = description.version || undefined;
} }
IndexStorage2.prototype.hasCapacity = function (name) { IndexStorage2.prototype.hasCapacity = function (name) {
...@@ -59,21 +60,6 @@ ...@@ -59,21 +60,6 @@
} }
}; };
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 compareSortOn(value1, value2, sort_on) { function compareSortOn(value1, value2, sort_on) {
var current_compare = sort_on.slice(0, 1)[0], var current_compare = sort_on.slice(0, 1)[0],
remaining_compare = sort_on.slice(1); remaining_compare = sort_on.slice(1);
...@@ -148,10 +134,10 @@ ...@@ -148,10 +134,10 @@
reject("Connection to: " + db_name + " timeout"); reject("Connection to: " + db_name + " timeout");
}; };
request.onblocked = function () { // request.onblocked = function () {
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) {
...@@ -181,35 +167,6 @@ ...@@ -181,35 +167,6 @@
return new RSVP.Promise(resolver); return new RSVP.Promise(resolver);
} }
function waitForUpdatedOpenIndexedDB(db_name, index_keys, callback) {
function resolver(resolve, reject) {
var db_version, store, current_indices, required_indices;
required_indices = index_keys.map(
function (value) {return 'Index-' + value; }
);
waitForOpenIndexedDB(db_name, undefined, index_keys, function (db) {
db_version = db.version;
store = db.transaction('index-store').objectStore('index-store');
current_indices = store.indexNames;
if (checkArrayEquality(required_indices,
Array.from(current_indices))) {
resolve(callback(db));
} else {
store.transaction.oncomplete = function () {
waitForOpenIndexedDB(db_name, db_version + 1, index_keys,
function (db) {
resolve(callback(db));
});
};
}
})
.fail(function (error) {
reject(error);
});
}
return new RSVP.Promise(resolver);
}
function waitForTransaction(db, stores, flag, callback) { function waitForTransaction(db, stores, flag, callback) {
var tx = db.transaction(stores, flag); var tx = db.transaction(stores, flag);
function canceller() { function canceller() {
...@@ -295,7 +252,7 @@ ...@@ -295,7 +252,7 @@
"' key and checking the substorage for partial queries is not set", "' key and checking the substorage for partial queries is not set",
404); 404);
} }
return waitForUpdatedOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._version,
context._index_keys, function (db) { context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readonly", return waitForTransaction(db, ["index-store"], "readonly",
function (tx) { function (tx) {
...@@ -397,7 +354,7 @@ ...@@ -397,7 +354,7 @@
}); });
}); });
} }
return waitForUpdatedOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._version,
context._index_keys, function (db) { context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readonly", return waitForTransaction(db, ["index-store"], "readonly",
function (tx) { function (tx) {
...@@ -428,11 +385,9 @@ ...@@ -428,11 +385,9 @@
return this._sub_storage.get.apply(this._sub_storage, arguments); return this._sub_storage.get.apply(this._sub_storage, arguments);
}; };
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 waitForOpenIndexedDB(context._database_name, context._version,
.push(function () {
return waitForUpdatedOpenIndexedDB(context._database_name,
context._index_keys, function (db) { context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite", return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) { function (tx) {
...@@ -442,6 +397,13 @@ ...@@ -442,6 +397,13 @@
})); }));
}); });
}); });
};
IndexStorage2.prototype.put = function (id, value) {
var context = this;
return context._sub_storage.put(id, value)
.push(function () {
return context._put(id, value);
}); });
}; };
...@@ -449,19 +411,7 @@ ...@@ -449,19 +411,7 @@
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 waitForUpdatedOpenIndexedDB(context._database_name, context._put(id, value);
context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) {
return waitForIDBRequest(tx.objectStore("index-store").put({
"id": id,
"doc": filterDocValues(value, context._index_keys)
}))
.then(function () {
return id;
});
});
});
}); });
}; };
...@@ -469,7 +419,7 @@ ...@@ -469,7 +419,7 @@
var context = this; var context = this;
return context._sub_storage.remove(id) return context._sub_storage.remove(id)
.push(function () { .push(function () {
return waitForUpdatedOpenIndexedDB(context._database_name, return waitForOpenIndexedDB(context._database_name, context._version,
context._index_keys, function (db) { context._index_keys, function (db) {
return waitForTransaction(db, ["index-store"], "readwrite", return waitForTransaction(db, ["index-store"], "readwrite",
function (tx) { function (tx) {
......
...@@ -1112,6 +1112,7 @@ ...@@ -1112,6 +1112,7 @@
context.jio = jIO.createJIO({ context.jio = jIO.createJIO({
type: "index2", type: "index2",
database: "index2_test", database: "index2_test",
version: 1,
index_keys: ["a"], index_keys: ["a"],
sub_storage: { sub_storage: {
type: "dummystorage3" type: "dummystorage3"
...@@ -1143,6 +1144,7 @@ ...@@ -1143,6 +1144,7 @@
context.jio = jIO.createJIO({ context.jio = jIO.createJIO({
type: "index2", type: "index2",
database: "index2_test", database: "index2_test",
version: 2,
index_keys: ["a", "b", "c"], index_keys: ["a", "b", "c"],
sub_storage: { sub_storage: {
type: "dummystorage3" type: "dummystorage3"
...@@ -1179,6 +1181,7 @@ ...@@ -1179,6 +1181,7 @@
type: "index2", type: "index2",
database: "index2_test", database: "index2_test",
index_keys: ["a", "c"], index_keys: ["a", "c"],
version: 3,
sub_storage: { sub_storage: {
type: "dummystorage3" type: "dummystorage3"
} }
...@@ -1214,6 +1217,63 @@ ...@@ -1214,6 +1217,63 @@
}); });
}); });
test("Parallel test", function () {
var context = this, i, promise_list = [], jio_list = [],
index_keys_combinations;
stop();
expect(1);
DummyStorage3.prototype.put = function (id) {
return id;
};
DummyStorage3.prototype.hasCapacity = function () {
return false;
};
function random(limit) {
return Math.floor(Math.random() * Math.floor(limit));
}
index_keys_combinations = [[], ['a'], ['b'], ['c'], ['d'], ['e'],
['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'],
['b', 'e'], ['c', 'd'], ['c', 'e'], ['a', 'b', 'c'], ['b', 'c', 'd'],
['c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['b', 'c', 'd', 'e'],
['a', 'b', 'c', 'd', 'e']];
for (i = 0; i < 100; i += 1) {
jio_list[i] = jIO.createJIO({
type: "index2",
database: "index2_test",
index_keys:
index_keys_combinations[random(index_keys_combinations.length - 1)],
version: i + 1,
sub_storage: {
type: "dummystorage3"
}
});
}
for (i = 0; i < 100; i += 1) {
promise_list.push(jio_list[i].put("43", {"a": "obscure", "b": "cr",
"c": "34", "d": "5454", "e": 34}));
}
RSVP.all(promise_list)
.then(function () {
return jio_list[99].allDocs();
})
.then(function (result) {
context.jio = jio_list[99];
equal(result.data.total_rows, 1);
})
.fail(function (error) {
console.log(error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// IndexStorage2.getAttachment // IndexStorage2.getAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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