Commit dc356c98 authored by Bryan Kaperick's avatar Bryan Kaperick

Replaced _revision counter with a _timestamp identifier which allows parallel put calls to work.

parent 30b0e964
...@@ -25,58 +25,20 @@ ...@@ -25,58 +25,20 @@
}; };
BryanStorage.prototype.post = function (metadata) { BryanStorage.prototype.post = function (metadata) {
// Uses UuidStorage post
return this._sub_storage.post(metadata); return this._sub_storage.post(metadata);
}; };
BryanStorage.prototype.put = function (id, new_metadata) { BryanStorage.prototype.put = function (id, metadata) {
var storage = this, var storage = this;
substorage = this._sub_storage,
previous_data;
return this._sub_storage.get(id) return this._sub_storage.put(id, metadata)
.push(function (latest_data) {
// Prepare to post the current doc as a deprecated version
previous_data = latest_data;
previous_data._deprecated = "true";
previous_data._doc_id = id;
// Get most recent deprecated version's _revision attribute
var options = {
query: '(_doc_id: "' + id + '")',
sort_on: [['_revision', 'descending']],
limit: [0, 1]
};
//return substorage.buildQuery(options);
return substorage.allDocs(options);
})
.push(function (query_results) {
if (query_results.data.rows.length > 0) {
var doc_id = query_results.data.rows[0].id;
return substorage.get(doc_id);
}
throw new jIO.util.jIOError(
"bryanstorage: query returned no results.'",
404
);
})
.push(function (doc) {
previous_data._revision = doc._revision + 1;
return storage.post(previous_data);
},
function () {
// If the query turned up no results,
// there was exactly 1 version previously.
if (previous_data !== undefined) {
previous_data._revision = 0;
return storage.post(previous_data);
}
})
// No matter what happened, need to put new document in
.push(function () { .push(function () {
return substorage.put(id, new_metadata);
// Also push a metadata document recording the posting time
metadata._deprecated = "true";
metadata._doc_id = id;
metadata._timestamp = Date.now();
return storage.post(metadata);
}); });
}; };
...@@ -151,6 +113,7 @@ ...@@ -151,6 +113,7 @@
if (options.query !== "") { if (options.query !== "") {
options.query = "(" + options.query + ") AND "; options.query = "(" + options.query + ") AND ";
} }
options.query = options.query + 'NOT (_deprecated: "true")'; options.query = options.query + 'NOT (_deprecated: "true")';
return this._sub_storage.buildQuery(options); return this._sub_storage.buildQuery(options);
}; };
......
...@@ -11,23 +11,26 @@ ...@@ -11,23 +11,26 @@
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module; module = QUnit.module;
/**
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// _revision parameter updating with RSVP all // _revision parameter updating with RSVP all
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("bryanStorage _revision with RSVP all"); module("bryanStorage revision with RSVP all");
test("verifying _revision updates correctly when puts are done in parallel", test("verifying updates correctly when puts are done in parallel",
function () { function () {
stop(); stop();
expect(1); expect(3);
// create storage of type "bryan" with memory as substorage // create storage of type "bryan" with memory as substorage
var jio = jIO.createJIO({ var dbname = "rsvp_db_" + Date.now(),
jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
type: "uuid", type: "uuid",
sub_storage: { sub_storage: {
type: "memory" //type: "memory"
type: "indexeddb",
database: dbname
} }
} }
}), }),
...@@ -36,47 +39,66 @@ ...@@ -36,47 +39,66 @@
sub_storage: { sub_storage: {
type: "uuid", type: "uuid",
sub_storage: { sub_storage: {
type: "memory" //type: "memory"
type: "indexeddb",
database: dbname
} }
} }
}); });
jio.put("bar", {"title": "foo0"}); jio.put("bar", {"title": "foo0"})
RSVP.all([ .push(function () {
return RSVP.all([
jio.put("bar", {"title": "foo1"}), jio.put("bar", {"title": "foo1"}),
jio.put("bar", {"title": "foo2"}), jio.put("bar", {"title": "foo2"}),
jio.put("bar", {"title": "foo3"}), jio.put("bar", {"title": "foo3"}),
jio.put("bar", {"title": "foo4"}) jio.put("bar", {"title": "foo4"})
]) ]);
})
.push(function () {return jio.get("bar"); }) .push(function () {return jio.get("bar"); })
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "foo4" "title": "foo4"
}); });
}) })
.push(function () {return not_bryan.allDocs({ .push(function () {
query: "_revision: 0" return not_bryan.allDocs({
query: "",
sort_on: [["_timestamp", "ascending"]]
}); });
}) })
.push(function (results) { .push(function (results) {
equal(results.data.rows.length, equal(results.data.rows.length,
1, 6,
"Only one document with _revision = 0"); "Storage contains all 5 revisions plus the most recent one.");
return not_bryan.get(results.data.rows[1].id);
})
.push(function (result) {
deepEqual(result, {
title: "foo0",
_doc_id: "bar",
_timestamp: result._timestamp,
_deprecated: "true"
},
"Query returns the first edition of the document");
})
.fail(function (error) {
//console.log(error);
ok(false, error);
}) })
.fail(function (error) {ok(false, error); })
.always(function () {start(); }); .always(function () {start(); });
}); });
**/
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// bryanStorage revision history // bryanStorage revision history
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("bryanStorage.revision_history"); module("bryanStorage.revision_history");
test("put and get the correct version", function () { test("put and get the correct version", function () {
stop(); stop();
expect(7); expect(7);
var dbname = "rev_hist_db0", var dbname = "rev_hist_db" + Date.now(),
jio = jIO.createJIO({ jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
...@@ -102,12 +124,12 @@ ...@@ -102,12 +124,12 @@
query_input = query_input =
{ {
query: 'NOT (_deprecated: "true")', query: 'NOT (_deprecated: "true")',
sort_on: [['_revision', 'descending']] sort_on: [['_timestamp', 'descending']]
}, },
query_input2 = query_input2 =
{ {
query: 'title: "rev1"', query: 'title: "rev1"',
sort_on: [['_revision', 'descending']] sort_on: [['_timestamp', 'descending']]
}; };
jio.put("doc1", { jio.put("doc1", {
...@@ -164,7 +186,7 @@ ...@@ -164,7 +186,7 @@
"title": "rev1", "title": "rev1",
"subtitle": "subrev1", "subtitle": "subrev1",
"_deprecated": "true", "_deprecated": "true",
"_revision": 1, "_timestamp": result._timestamp,
"_doc_id": "doc1" "_doc_id": "doc1"
}, },
"Retrieve 1st edit by querying for title: 'rev1' with other storage"); "Retrieve 1st edit by querying for title: 'rev1' with other storage");
...@@ -199,12 +221,11 @@ ...@@ -199,12 +221,11 @@
// bryanStorage.revision_history_multiple_edits // bryanStorage.revision_history_multiple_edits
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("bryanStorage.revision_history_multiple_edits"); module("bryanStorage.revision_history_multiple_edits");
test("modify first version but save both", function () { test("modify first version but save both", function () {
stop(); stop();
expect(11); expect(10);
var dbname = "testdb20", var dbname = "rev_hist_mult_db" + Date.now(),
jio = jIO.createJIO({ jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
...@@ -310,14 +331,6 @@ ...@@ -310,14 +331,6 @@
.push(function (result) { .push(function (result) {
equal(result.data.rows.length, 0, "Correct number of results returned"); equal(result.data.rows.length, 0, "Correct number of results returned");
}) })
.push(function () {
return jio.allDocs({
query: '(_revision: 0)'
});
})
.push(function (result) {
equal(result.data.rows.length, 0, "Correct number of results returned");
})
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: '' query: ''
...@@ -331,14 +344,14 @@ ...@@ -331,14 +344,14 @@
.push(function () { .push(function () {
var options = { var options = {
query: "_doc_id: main_doc", query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]] sort_on: [["_timestamp", "ascending"]]
}; };
return not_bryan.allDocs(options); return not_bryan.allDocs(options);
}) })
.push(function (results) { .push(function (results) {
equal(results.data.rows.length, equal(results.data.rows.length,
3, 4,
"should get all 3 deprecated versions."); "should get all 3 deprecated versions plus one copy of the latest.");
return not_bryan.get(results.data.rows[0].id); return not_bryan.get(results.data.rows[0].id);
}) })
.push(function (results) { .push(function (results) {
...@@ -346,7 +359,7 @@ ...@@ -346,7 +359,7 @@
"title": "rev0", "title": "rev0",
"subtitle": "subrev0", "subtitle": "subrev0",
"_doc_id": "main_doc", "_doc_id": "main_doc",
"_revision": 0, "_timestamp": results._timestamp,
"_deprecated": "true" "_deprecated": "true"
}, },
"Get the earliest copy of the doc with all metadata."); "Get the earliest copy of the doc with all metadata.");
...@@ -356,7 +369,7 @@ ...@@ -356,7 +369,7 @@
.push(function () { .push(function () {
var options = { var options = {
query: "_doc_id: main_doc", query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]] sort_on: [["_timestamp", "ascending"]]
}; };
return not_bryan.allDocs(options); return not_bryan.allDocs(options);
}) })
...@@ -368,7 +381,7 @@ ...@@ -368,7 +381,7 @@
"title": "rev1", "title": "rev1",
"subtitle": "subrev1", "subtitle": "subrev1",
"_doc_id": "main_doc", "_doc_id": "main_doc",
"_revision": 1, "_timestamp": results._timestamp,
"_deprecated": "true" "_deprecated": "true"
}, },
"Get the earliest copy of the doc with all metadata."); "Get the earliest copy of the doc with all metadata.");
......
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