mirror of https://gitee.com/bigwinds/arangodb
fixed firstExample with _id & _key attributes
This commit is contained in:
parent
007cae87e6
commit
03fcdf1aa0
|
@ -311,6 +311,86 @@ function SimpleQueryByExampleSuite () {
|
||||||
collection.drop();
|
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
|
/// @brief test: byExample
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -112,7 +112,7 @@ function byExample (collection, example, skip, limit) {
|
||||||
if (example[k] === null) {
|
if (example[k] === null) {
|
||||||
unique = false;
|
unique = false;
|
||||||
}
|
}
|
||||||
else if (k === '_id') {
|
else if (k === '_id' || k === '_key') {
|
||||||
// example contains the document id in attribute "_id"
|
// example contains the document id in attribute "_id"
|
||||||
documentId = example[k];
|
documentId = example[k];
|
||||||
break;
|
break;
|
||||||
|
@ -126,14 +126,28 @@ function byExample (collection, example, skip, limit) {
|
||||||
try {
|
try {
|
||||||
// look up document by id
|
// look up document by id
|
||||||
doc = collection.document(documentId);
|
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) {
|
catch (e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return { "total" : doc ? 1 : 0, "count" : doc ? 1 : 0, "documents" : doc ? [ doc ] : [ ] };
|
return { "total" : doc ? 1 : 0, "count" : doc ? 1 : 0, "documents" : doc ? [ doc ] : [ ] };
|
||||||
}
|
}
|
||||||
|
|
||||||
var idx = collection.lookupHashIndex.apply(collection, attributes);
|
var idx = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
idx = collection.lookupHashIndex.apply(collection, attributes);
|
||||||
if (idx === null && unique) {
|
if (idx === null && unique) {
|
||||||
idx = collection.lookupUniqueConstraint.apply(collection, attributes);
|
idx = collection.lookupUniqueConstraint.apply(collection, attributes);
|
||||||
|
|
||||||
|
@ -144,6 +158,9 @@ function byExample (collection, example, skip, limit) {
|
||||||
else if (idx !== null) {
|
else if (idx !== null) {
|
||||||
console.debug("found hash index %s", idx.id);
|
console.debug("found hash index %s", idx.id);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
}
|
||||||
|
|
||||||
if (idx !== null) {
|
if (idx !== null) {
|
||||||
// use hash index
|
// use hash index
|
||||||
|
|
Loading…
Reference in New Issue