diff --git a/js/common/tests/shell-simple-query.js b/js/common/tests/shell-simple-query.js index 3571b0ebe0..e36abd295c 100644 --- a/js/common/tests/shell-simple-query.js +++ b/js/common/tests/shell-simple-query.js @@ -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 //////////////////////////////////////////////////////////////////////////////// diff --git a/js/server/modules/org/arangodb/simple-query.js b/js/server/modules/org/arangodb/simple-query.js index 34e30e3918..f5307ac0f6 100644 --- a/js/server/modules/org/arangodb/simple-query.js +++ b/js/server/modules/org/arangodb/simple-query.js @@ -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) {