Commit ee3050e4 authored by Bryan Kaperick's avatar Bryan Kaperick

Added new put functionality and tests on put and putAttachment.

parent 3d03aafb
...@@ -15,6 +15,13 @@ ...@@ -15,6 +15,13 @@
return timestamp + "-" + uuid; return timestamp + "-" + uuid;
} }
function isTimestamp(id) {
//A timestamp is of the form
//"[13 digit number]-[4 numbers/lowercase letters]"
var re = /^[0-9]{13}-[a-z0-9]{4}$/;
return re.test(id);
}
function removeOldRevs( function removeOldRevs(
substorage, substorage,
results, results,
...@@ -193,7 +200,8 @@ ...@@ -193,7 +200,8 @@
}; };
HistoryStorage.prototype.put = function (id, data) { HistoryStorage.prototype.put = function (id, data) {
var timestamp = generateUniqueTimestamp(Date.now()), var substorage = this._sub_storage,
timestamp = generateUniqueTimestamp(Date.now()),
metadata = { metadata = {
// XXX: remove this attribute once query can sort_on id // XXX: remove this attribute once query can sort_on id
timestamp: timestamp, timestamp: timestamp,
...@@ -201,6 +209,22 @@ ...@@ -201,6 +209,22 @@
doc: data, doc: data,
op: "put" op: "put"
}; };
if (this._include_revisions && isTimestamp(id)) {
return substorage.get(id)
.push(function (metadata) {
metadata.timestamp = timestamp;
metadata.doc = data;
return substorage.put(timestamp, metadata);
},
function (error) {
if (error.status_code === 404 &&
error instanceof jIO.util.jIOError) {
return substorage.put(timestamp, metadata);
}
throw error;
});
}
return this._sub_storage.put(timestamp, metadata); return this._sub_storage.put(timestamp, metadata);
}; };
...@@ -343,6 +367,20 @@ ...@@ -343,6 +367,20 @@
op: "putAttachment" op: "putAttachment"
}, },
substorage = this._sub_storage; substorage = this._sub_storage;
if (this._include_revisions && isTimestamp(id)) {
return substorage.get(id)
.push(function (metadata) {
metadata.timestamp = timestamp;
metadata.name = name;
},
function (error) {
if (!(error.status_code === 404 &&
error instanceof jIO.util.jIOError)) {
throw error;
}
});
}
return this._sub_storage.put(timestamp, metadata) return this._sub_storage.put(timestamp, metadata)
.push(function () { .push(function () {
return substorage.putAttachment(timestamp, name, blob); return substorage.putAttachment(timestamp, name, blob);
......
...@@ -832,7 +832,7 @@ ...@@ -832,7 +832,7 @@
}) })
.push(function (res) { .push(function (res) {
timestamp = res.data.rows[0].id; timestamp = res.data.rows[0].id;
return history.put(timestamp, {key: "val"}); return jio.put(timestamp, {key: "val"});
}) })
.push(function () { .push(function () {
return jio.get("doc"); return jio.get("doc");
...@@ -1001,6 +1001,119 @@ ...@@ -1001,6 +1001,119 @@
.always(function () {start(); }); .always(function () {start(); });
}); });
test("Updating a document with include revisions",
function () {
stop();
expect(1);
var jio = this.jio,
history = this.history,
not_history = this.not_history,
timestamps,
t_id;
jio.put("doc", {title: "version0"})
.push(function () {
return history.put("doc", {title: "version1"});
})
.push(function () {
return not_history.allDocs({sort_on: [["timestamp", "ascending"]]});
})
.push(function (results) {
t_id = results.data.rows[0].id;
return history.put(t_id, {title: "version0.1"});
})
.push(function () {
return jio.put(t_id, {title: "label0"});
})
.push(function () {
return history.put("1234567891012-abcd", {k: "v"});
})
.push(function () {
return not_history.allDocs({
select_list: ["timestamp"]
});
})
.push(function (results) {
timestamps = results.data.rows.map(function (d) {
return d.value.timestamp;
});
})
.push(function () {
return not_history.allDocs({
sort_on: [["timestamp", "ascending"]],
select_list: ["timestamp", "op", "doc_id", "doc"]
});
})
.push(function (results) {
deepEqual(results.data.rows, [
{
id: timestamps[0],
doc: {},
value: {
timestamp: timestamps[0],
op: "put",
doc_id: "doc",
doc: {
title: "version0"
}
}
},
{
id: timestamps[1],
doc: {},
value: {
timestamp: timestamps[1],
op: "put",
doc_id: "doc",
doc: {
title: "version1"
}
}
},
{
id: timestamps[2],
doc: {},
value: {
timestamp: timestamps[2],
op: "put",
doc_id: "doc",
doc: {
title: "version0.1"
}
}
},
{
id: timestamps[3],
doc: {},
value: {
timestamp: timestamps[3],
op: "put",
doc_id: timestamps[0],
doc: {
title: "label0"
}
}
},
{
id: timestamps[4],
doc: {},
value: {
timestamp: timestamps[4],
op: "put",
doc_id: "1234567891012-abcd",
doc: {
k: "v"
}
}
}
], "Documents stored with correct metadata");
})
.fail(function (error) {
//console.log(error);
ok(false, error);
})
.always(function () {start(); });
});
test("Retrieving older revisions with get", test("Retrieving older revisions with get",
function () { function () {
stop(); stop();
......
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