Commit 6b8af9e6 authored by Bryan Kaperick's avatar Bryan Kaperick

Implemented the method of storing every operation done to each document ...

Implemented the method of storing every operation done to each document  instead of the documents explicitely.  The tests which do not pass are labelled NOT IMPLEMENTED due to current limitations on the ability to query sub-attributes of documents.
parent 0d5eba32
...@@ -44,7 +44,10 @@ ...@@ -44,7 +44,10 @@
404 404
); );
}) })
// Decide return based on last edit type
.push(function (result) { .push(function (result) {
// If last edit was a remove, throw a 'not found' error // If last edit was a remove, throw a 'not found' error
if (result.op === "remove") { if (result.op === "remove") {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -52,6 +55,7 @@ ...@@ -52,6 +55,7 @@
404 404
); );
} }
// If last edit was a put, return the document data // If last edit was a put, return the document data
if (result.op === "put") { if (result.op === "put") {
return result.doc; return result.doc;
...@@ -147,7 +151,71 @@ ...@@ -147,7 +151,71 @@
}; };
BryanStorage.prototype.buildQuery = function (options) { BryanStorage.prototype.buildQuery = function (options) {
return this._sub_storage.buildQuery(options); if (options.sort_on === undefined) {
options.sort_on = [];
}
if (options.limit === undefined) {
options.limit = [0, -1];
}
options.sort_on.push(["timestamp", "descending"]);
var meta_options = {
// XXX: I don't believe it's currently possible to query on sub-attributes
// so for now, we just use the inputted query, which obviously will fail
query: options.query,
// XXX: same here, we cannot sort correctly because we cannot access
// attributes of doc
sort_on: options.sort_on
},
substorage = this._sub_storage,
max_num_docs = options.limit[1],
first_doc = options.limit[0];
return this._sub_storage.allDocs(meta_options)
.push(function (results) {
var promises = results.data.rows.map(function (data) {
return substorage.get(data.id);
});
return RSVP.all(promises);
})
.push(function (results_array) {
var clean_data = [],
ind,
seen_docs = [],
current_doc,
counter = 0;
if (max_num_docs === -1) {
max_num_docs = results_array.length;
}
for (ind = 0; ind < results_array.length; ind += 1) {
current_doc = results_array[ind];
// If the latest version of this document has not yet been
// included in query result
if (seen_docs[current_doc.doc_id] !== true) {
// If the latest edit was a put operation, add it to query
// results
if (current_doc.op === "put") {
if (counter >= first_doc) {
clean_data.push({
doc: {},
value: {},
id: current_doc.doc_id
});
if (clean_data.length === max_num_docs) {
return clean_data;
}
}
counter += 1;
}
// Mark document as read so no older edits are considered
seen_docs[current_doc.doc_id] = true;
}
}
// In passing results back to allDocs, formatting of query is handled
return clean_data;
});
}; };
jIO.addStorage('bryan', BryanStorage); jIO.addStorage('bryan', BryanStorage);
......
...@@ -136,7 +136,7 @@ ...@@ -136,7 +136,7 @@
test("verifying the correct results are returned from bryanStorage.allDocs", test("verifying the correct results are returned from bryanStorage.allDocs",
function () { function () {
stop(); stop();
expect(1); expect(10);
// create storage of type "bryan" with memory as substorage // create storage of type "bryan" with memory as substorage
var jio = jIO.createJIO({ var jio = jIO.createJIO({
...@@ -151,21 +151,24 @@ ...@@ -151,21 +151,24 @@
jio.put("bar", {"title": "foo0"}) jio.put("bar", {"title": "foo0"})
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
jio.remove("bar"),
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("barbar", {"title": "attr0"}), jio.put("barbar", {"title": "attr0"}),
jio.put("barbar", {"title": "attr1"}), jio.put("barbar", {"title": "attr1"}),
jio.put("barbar", {"title": "attr2"}) jio.put("barbar", {"title": "attr2"}),
jio.put("barbarbar", {"title": "val0"}),
jio.put("barbarbarbar", {"title": "prop0"})
]); ]);
}) })
// Make two final puts so we know what to expect as the current state of // Make two final puts so we know what to expect as the current state of
// each document. // each document.
.push(function () { .push(function () {
return jio.put("bar", {"title": "foo4"}); return jio.put("barbar", {"title": "attr3"});
}) })
.push(function () { .push(function () {
return jio.put("barbar", {"title": "attr3"}); return jio.put("bar", {"title": "foo4"});
}) })
// Queries should only include information about the final two versions // Queries should only include information about the final two versions
...@@ -177,29 +180,96 @@ ...@@ -177,29 +180,96 @@
}) })
.push(function (results) { .push(function (results) {
equal(results.data.rows.length, equal(results.data.rows.length,
2, 4,
"Empty query yields two results since there are two unique docs"); "Empty query yields four results since there are four unique docs");
return jio.get(results.data.rows[0].id); return jio.get(results.data.rows[0].id);
},
function (error) {
return ok(false, "Query failed: " + error);
}) })
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
title: "attr3" title: "attr3"
}, "Retrieve the first title in the correct format (no metadata)"); },
"NOT IMPLEMENTED: Retrieve correct sort order with no metadata");
}, },
function () { function () {
return ok(false, "Couldn't find document in storage"); return ok(false, "Couldn't find document in storage");
}) })
// Querying with a limit
.push(function () {
return jio.allDocs({
query: "",
sort_on: [["title", "ascending"]],
limit: [0, 1]
});
})
.push(function (results) {
equal(results.data.rows.length,
1,
"Since limit [0,1] was supplied, only 1st document is returned");
return jio.get(results.data.rows[0].id);
})
.push(function (result) {
deepEqual(result, {
title: "attr3"
},
"NOT IMPLEMENTED: retrieving documents in specified sort_on order");
})
// Querying with a more complicated limit
.push(function () {
return jio.allDocs({
query: "",
sort_on: [["title", "ascending"]],
limit: [2, 2]
});
})
.push(function (results) {
equal(results.data.rows.length,
2,
"Retrieving the correct documents when options.limit is specified");
deepEqual(results.data.rows[0].id,
"barbarbarbar",
"NOT IMPLEMENTED: retrieving documents in specified sort_on order");
deepEqual(results.data.rows[1].id,
"barbarbar",
"NOT IMPLEMENTED: retrieving documents in specified sort_on order");
return jio.get(results.data.rows[0].id);
})
.push(function (result) {
deepEqual(result, {
title: "property0"
},
"NOT IMPLEMENTED: retrieving documents in specified sort_on order");
})
// Querying for a specific id // Querying for a specific id
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: "id: bar" query: "id: bar"
}); });
}) })
.push(function (results) {
equal(results.data.rows.length,
1,
"NOT IMPLEMENTED: query involving specific document attributes");
return jio.get(results.data.rows[0].id);
})
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
title: "foo4" title: "foo4"
}, "Retrieve correct document in correct format (no metadata)"); },
"NOT IMPLEMENTED: query involving specific document attributes");
},
function () {
ok(false,
"NOT IMPLEMENTED: query involving specific document attributes"
);
}) })
.fail(function (error) { .fail(function (error) {
......
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