1
0
Fork 0

fixed firstExample with _id & _key attributes

This commit is contained in:
Jan Steemann 2013-03-28 16:22:29 +01:00
parent 007cae87e6
commit 03fcdf1aa0
2 changed files with 105 additions and 8 deletions

View File

@ -311,6 +311,86 @@ function SimpleQueryByExampleSuite () {
collection.drop();
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample, using _key
////////////////////////////////////////////////////////////////////////////////
testByExampleKey : function () {
var d, s;
d = collection.save({ _key: "meow" });
s = collection.firstExample({ _key: "foo" });
assertEqual(null, s);
s = collection.firstExample({ _key: "meow" });
assertEqual(d._id, s._id);
assertEqual(d._key, s._key);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample, using _key & others
////////////////////////////////////////////////////////////////////////////////
testByExampleKeyMore : function () {
var d, s;
d = collection.save({ _key: "meow" });
s = collection.firstExample({ _key: "meow", a: 1 });
assertEqual(null, s);
d = collection.save({ _key: "foo", a: 2 });
s = collection.firstExample({ _key: "foo", a: 2 });
assertEqual(d._id, s._id);
assertEqual(d._key, s._key);
assertEqual(2, s.a);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample, using _id
////////////////////////////////////////////////////////////////////////////////
testByExampleId : function () {
var d, s;
d = collection.save({ _key: "meow" });
s = collection.firstExample({ _id: cn + "/foo" });
assertEqual(null, s);
s = collection.firstExample({ _id: cn + "/meow" });
assertEqual(d._id, s._id);
assertEqual(d._key, s._key);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample, using _id & others
////////////////////////////////////////////////////////////////////////////////
testByExampleIdMore : function () {
var d, s;
d = collection.save({ _key: "meow" });
s = collection.firstExample({ _id: cn + "/meow", a: 1 });
assertEqual(null, s);
d = collection.save({ _key: "foo", a: 2 });
s = collection.firstExample({ _id: cn + "/foo", a: 2 });
assertEqual(d._id, s._id);
assertEqual(d._key, s._key);
assertEqual(2, s.a);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample, using non existing system attribute
////////////////////////////////////////////////////////////////////////////////
testByExampleNonExisting : function () {
var d, s;
d = collection.save({ _key: "meow" });
s = collection.firstExample({ _foo: "foo" });
assertEqual(null, s);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test: byExample
////////////////////////////////////////////////////////////////////////////////

View File

@ -112,7 +112,7 @@ function byExample (collection, example, skip, limit) {
if (example[k] === null) {
unique = false;
}
else if (k === '_id') {
else if (k === '_id' || k === '_key') {
// example contains the document id in attribute "_id"
documentId = example[k];
break;
@ -126,23 +126,40 @@ function byExample (collection, example, skip, limit) {
try {
// look up document by id
doc = collection.document(documentId);
// we have used the primary index to look up the document
// now we need to post-filter because non-indexed values might not have matched
for (k in example) {
if (example.hasOwnProperty(k)) {
if (doc[k] !== example[k]) {
doc = null;
break;
}
}
}
}
catch (e) {
}
return { "total" : doc ? 1 : 0, "count" : doc ? 1 : 0, "documents" : doc ? [ doc ] : [ ] };
}
var idx = collection.lookupHashIndex.apply(collection, attributes);
var idx = null;
if (idx === null && unique) {
idx = collection.lookupUniqueConstraint.apply(collection, attributes);
try {
idx = collection.lookupHashIndex.apply(collection, attributes);
if (idx === null && unique) {
idx = collection.lookupUniqueConstraint.apply(collection, attributes);
if (idx !== null) {
console.debug("found unique constraint %s", idx.id);
if (idx !== null) {
console.debug("found unique constraint %s", idx.id);
}
}
else if (idx !== null) {
console.debug("found hash index %s", idx.id);
}
}
else if (idx !== null) {
console.debug("found hash index %s", idx.id);
catch (err) {
}
if (idx !== null) {