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 @@
};
BryanStorage.prototype.post = function (metadata) {
// Uses UuidStorage post
return this._sub_storage.post(metadata);
};
BryanStorage.prototype.put = function (id, new_metadata) {
var storage = this,
substorage = this._sub_storage,
previous_data;
BryanStorage.prototype.put = function (id, metadata) {
var storage = this;
return this._sub_storage.get(id)
.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
return this._sub_storage.put(id, metadata)
.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 @@
if (options.query !== "") {
options.query = "(" + options.query + ") AND ";
}
options.query = options.query + 'NOT (_deprecated: "true")';
return this._sub_storage.buildQuery(options);
};
......
......@@ -11,23 +11,26 @@
equal = QUnit.equal,
module = QUnit.module;
/**
/////////////////////////////////////////////////////////////////
// _revision parameter updating with RSVP all
/////////////////////////////////////////////////////////////////
module("bryanStorage _revision with RSVP all");
test("verifying _revision updates correctly when puts are done in parallel",
module("bryanStorage revision with RSVP all");
test("verifying updates correctly when puts are done in parallel",
function () {
stop();
expect(1);
expect(3);
// create storage of type "bryan" with memory as substorage
var jio = jIO.createJIO({
var dbname = "rsvp_db_" + Date.now(),
jio = jIO.createJIO({
type: "bryan",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
//type: "memory"
type: "indexeddb",
database: dbname
}
}
}),
......@@ -36,47 +39,66 @@
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory"
//type: "memory"
type: "indexeddb",
database: dbname
}
}
});
jio.put("bar", {"title": "foo0"});
RSVP.all([
jio.put("bar", {"title": "foo0"})
.push(function () {
return RSVP.all([
jio.put("bar", {"title": "foo1"}),
jio.put("bar", {"title": "foo2"}),
jio.put("bar", {"title": "foo3"}),
jio.put("bar", {"title": "foo4"})
])
]);
})
.push(function () {return jio.get("bar"); })
.push(function (result) {
deepEqual(result, {
"title": "foo4"
});
})
.push(function () {return not_bryan.allDocs({
query: "_revision: 0"
.push(function () {
return not_bryan.allDocs({
query: "",
sort_on: [["_timestamp", "ascending"]]
});
})
.push(function (results) {
equal(results.data.rows.length,
1,
"Only one document with _revision = 0");
6,
"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(); });
});
**/
/////////////////////////////////////////////////////////////////
// bryanStorage revision history
/////////////////////////////////////////////////////////////////
module("bryanStorage.revision_history");
test("put and get the correct version", function () {
stop();
expect(7);
var dbname = "rev_hist_db0",
var dbname = "rev_hist_db" + Date.now(),
jio = jIO.createJIO({
type: "bryan",
sub_storage: {
......@@ -102,12 +124,12 @@
query_input =
{
query: 'NOT (_deprecated: "true")',
sort_on: [['_revision', 'descending']]
sort_on: [['_timestamp', 'descending']]
},
query_input2 =
{
query: 'title: "rev1"',
sort_on: [['_revision', 'descending']]
sort_on: [['_timestamp', 'descending']]
};
jio.put("doc1", {
......@@ -164,7 +186,7 @@
"title": "rev1",
"subtitle": "subrev1",
"_deprecated": "true",
"_revision": 1,
"_timestamp": result._timestamp,
"_doc_id": "doc1"
},
"Retrieve 1st edit by querying for title: 'rev1' with other storage");
......@@ -199,12 +221,11 @@
// bryanStorage.revision_history_multiple_edits
/////////////////////////////////////////////////////////////////
module("bryanStorage.revision_history_multiple_edits");
test("modify first version but save both", function () {
stop();
expect(11);
var dbname = "testdb20",
expect(10);
var dbname = "rev_hist_mult_db" + Date.now(),
jio = jIO.createJIO({
type: "bryan",
sub_storage: {
......@@ -310,14 +331,6 @@
.push(function (result) {
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 () {
return jio.allDocs({
query: ''
......@@ -331,14 +344,14 @@
.push(function () {
var options = {
query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]]
sort_on: [["_timestamp", "ascending"]]
};
return not_bryan.allDocs(options);
})
.push(function (results) {
equal(results.data.rows.length,
3,
"should get all 3 deprecated versions.");
4,
"should get all 3 deprecated versions plus one copy of the latest.");
return not_bryan.get(results.data.rows[0].id);
})
.push(function (results) {
......@@ -346,7 +359,7 @@
"title": "rev0",
"subtitle": "subrev0",
"_doc_id": "main_doc",
"_revision": 0,
"_timestamp": results._timestamp,
"_deprecated": "true"
},
"Get the earliest copy of the doc with all metadata.");
......@@ -356,7 +369,7 @@
.push(function () {
var options = {
query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]]
sort_on: [["_timestamp", "ascending"]]
};
return not_bryan.allDocs(options);
})
......@@ -368,7 +381,7 @@
"title": "rev1",
"subtitle": "subrev1",
"_doc_id": "main_doc",
"_revision": 1,
"_timestamp": results._timestamp,
"_deprecated": "true"
},
"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