1
0
Fork 0

added `type` option for `GET /_api/document?collection=...`

Conflicts:
	CHANGELOG
This commit is contained in:
Jan Steemann 2014-08-30 17:20:00 +02:00
parent 6844433558
commit c0463a1797
6 changed files with 183 additions and 19 deletions

View File

@ -58,7 +58,7 @@ v2.3.0 (XXXX-XX-XX)
* front-end: fetching and filtering of documents, statistics, and query operations are now
handled with asynchronous ajax calls.
* front-end: added process indicator if the front-end is waiting for a server operation.
* front-end: added progress indicator if the front-end is waiting for a server operation.
* front-end: fixed wrong count of documents in the documents view of a collection.
@ -73,6 +73,47 @@ v2.3.0 (XXXX-XX-XX)
storing JavaScript date objects in the database in a sensible manner.
v2.2.3 (2014-XX-XX)
-------------------
* added `type` option for HTTP API `GET /_api/document?collection=...`
This allows controlling the type of results to be returned. By default, paths to
documents will be returned, e.g.
[
`/_api/document/test/mykey1`,
`/_api/document/test/mykey2`,
...
]
To return a list of document ids instead of paths, the `type` URL parameter can be
set to `id`:
[
`test/mykey1`,
`test/mykey2`,
...
]
To return a list of document keys only, the `type` URL parameter can be set to `key`:
[
`mykey1`,
`mykey2`,
...
]
* propery capitalize HTTP response header field names in case the `x-arango-async`
HTTP header was used in a request.
* fixed several documentation issues
* speed up for several general-graph functions, AQL functions starting with `GRAPH_`
and traversals
v2.2.2 (2014-08-08)
-------------------

View File

@ -325,8 +325,6 @@ describe ArangoDB do
end
it "get all documents of an empty collection" do
cmd = "/_api/document?collection=#{@cid}"
# get documents
cmd = "/_api/document?collection=#{@cid}"
doc = ArangoDB.log_get("#{prefix}-all-0", cmd)
@ -341,6 +339,21 @@ describe ArangoDB do
ArangoDB.size_collection(@cid).should eq(0)
end
it "get all documents of an empty collection, using type=id" do
# get documents
cmd = "/_api/document?collection=#{@cid}&type=id"
doc = ArangoDB.log_get("#{prefix}-all-type-id", cmd)
doc.code.should eq(200)
doc.headers['content-type'].should eq("application/json; charset=utf-8")
documents = doc.parsed_response['documents']
documents.should be_kind_of(Array)
documents.length.should eq(0)
ArangoDB.size_collection(@cid).should eq(0)
end
it "create three documents and read them using the collection identifier" do
cmd = "/_api/document?collection=#{@cid}"
@ -414,6 +427,68 @@ describe ArangoDB do
ArangoDB.size_collection(@cid).should eq(0)
end
it "create three documents and read them using the collection name, type=id" do
cmd = "/_api/document?collection=#{@cn}"
location = []
for i in [ 1, 2, 3 ]
body = "{ \"Hallo\" : \"World-#{i}\" }"
doc = ArangoDB.post(cmd, :body => body)
doc.code.should eq(201)
location.push(doc.headers['location'])
end
# get documents
cmd = "/_api/document?collection=#{@cn}&type=id"
doc = ArangoDB.log_get("#{prefix}-all-name", cmd)
doc.code.should eq(200)
doc.headers['content-type'].should eq("application/json; charset=utf-8")
documents = doc.parsed_response['documents']
documents.should be_kind_of(Array)
documents.length.should eq(3)
regex = Regexp.new('^' + @cn + '/\d+$');
documents.each { |document|
document.should match(regex)
}
end
it "create three documents and read them using the collection name, type=key" do
cmd = "/_api/document?collection=#{@cn}"
location = []
for i in [ 1, 2, 3 ]
body = "{ \"Hallo\" : \"World-#{i}\" }"
doc = ArangoDB.post(cmd, :body => body)
doc.code.should eq(201)
location.push(doc.headers['location'])
end
# get documents
cmd = "/_api/document?collection=#{@cn}&type=key"
doc = ArangoDB.log_get("#{prefix}-all-name", cmd)
doc.code.should eq(200)
doc.headers['content-type'].should eq("application/json; charset=utf-8")
documents = doc.parsed_response['documents']
documents.should be_kind_of(Array)
documents.length.should eq(3)
regex = Regexp.new('^\d+$');
documents.each { |document|
document.should match(regex)
}
end
end
################################################################################

View File

@ -963,6 +963,7 @@ int getDocumentOnCoordinator (
int getAllDocumentsOnCoordinator (
string const& dbname,
string const& collname,
string const& returnType,
triagens::rest::HttpResponse::HttpResponseCode& responseCode,
string& contentType,
string& resultBody ) {
@ -988,7 +989,7 @@ int getAllDocumentsOnCoordinator (
res = cc->asyncRequest("", coordTransactionID, "shard:" + it->first,
triagens::rest::HttpRequest::HTTP_REQUEST_GET,
"/_db/" + StringUtils::urlEncode(dbname) + "/_api/document?collection="+
it->first, 0, false, headers, NULL, 3600.0);
it->first + "&type=" + StringUtils::urlEncode(returnType), 0, false, headers, NULL, 3600.0);
delete res;
}
// Now listen to the results:

View File

@ -158,6 +158,7 @@ namespace triagens {
int getAllDocumentsOnCoordinator (
string const& dbname,
string const& collname,
string const& returnType,
triagens::rest::HttpResponse::HttpResponseCode& responseCode,
string& contentType,
string& resultBody);

View File

@ -682,9 +682,20 @@ bool RestDocumentHandler::getDocumentCoordinator (
/// @RESTQUERYPARAM{collection,string,required}
/// The name of the collection.
///
/// @RESTQUERYPARAM{type,string,optional}
/// The type of the result. The following values are allowed:
///
/// - *id*: returns a list of document ids (*_id* attributes)
/// - *key*: returns a list of document keys (*_key* attributes)
/// - *path*: returns a list of document URI paths. This is the default.
///
/// @RESTDESCRIPTION
/// Returns a list of all URI for all documents from the collection identified
/// by *collection*.
/// Returns a list of all keys, ids, or URI paths for all documents in the
/// collection identified by *collection*. The type of the result list is
/// determined by the *type* attribute.
///
/// Note that the results have no defined order and thus the order should
/// not be relied on.
///
/// @RESTRETURNCODES
///
@ -696,9 +707,9 @@ bool RestDocumentHandler::getDocumentCoordinator (
///
/// @EXAMPLES
///
/// Returns a all ids.
/// Returns all document paths
///
/// @EXAMPLE_ARANGOSH_RUN{RestDocumentHandlerReadDocumentAll}
/// @EXAMPLE_ARANGOSH_RUN{RestDocumentHandlerReadDocumentAllPath}
/// var cn = "products";
/// db._drop(cn);
/// db._create(cn);
@ -715,6 +726,25 @@ bool RestDocumentHandler::getDocumentCoordinator (
/// logJsonResponse(response);
/// @END_EXAMPLE_ARANGOSH_RUN
///
/// Returns all document keys
///
/// @EXAMPLE_ARANGOSH_RUN{RestDocumentHandlerReadDocumentAllKey}
/// var cn = "products";
/// db._drop(cn);
/// db._create(cn);
///
/// db.products.save({"hello1":"world1"});
/// db.products.save({"hello2":"world1"});
/// db.products.save({"hello3":"world1"});
/// var url = "/_api/document/?collection=" + cn + "&type=key";
///
/// var response = logCurlRequest('GET', url);
///
/// assert(response.code === 200);
///
/// logJsonResponse(response);
/// @END_EXAMPLE_ARANGOSH_RUN
///
/// Collection does not exist.
///
/// @EXAMPLE_ARANGOSH_RUN{RestDocumentHandlerReadDocumentAllCollectionDoesNotExist}
@ -734,9 +764,14 @@ bool RestDocumentHandler::getDocumentCoordinator (
bool RestDocumentHandler::readAllDocuments () {
bool found;
string collection = _request->value("collection", found);
string returnType = _request->value("type", found);
if (returnType.empty()) {
returnType = "path";
}
if (ServerState::instance()->isCoordinator()) {
return getAllDocumentsCoordinator(collection);
return getAllDocumentsCoordinator(collection, returnType);
}
// find and load collection given by name or identifier
@ -758,7 +793,7 @@ bool RestDocumentHandler::readAllDocuments () {
res = trx.read(ids);
TRI_col_type_e typ = trx.documentCollection()->_info._type;
TRI_col_type_e type = trx.documentCollection()->_info._type;
res = trx.finish(res);
@ -776,17 +811,27 @@ bool RestDocumentHandler::readAllDocuments () {
bool first = true;
string prefix;
if (typ == TRI_COL_TYPE_EDGE) {
prefix = '"' + EDGE_PATH + '/' + trx.resolver()->getCollectionName(cid) + '/';
if (returnType == "key") {
prefix = '"';
}
else if (returnType == "id") {
prefix = '"' + trx.resolver()->getCollectionName(cid) + "\\/";
}
else {
prefix = '"' + DOCUMENT_PATH + '/' + trx.resolver()->getCollectionName(cid) + '/';
// default return type: paths to documents
if (type == TRI_COL_TYPE_EDGE) {
prefix = '"' + EDGE_PATH + '/' + trx.resolver()->getCollectionName(cid) + '/';
}
else {
prefix = '"' + DOCUMENT_PATH + '/' + trx.resolver()->getCollectionName(cid) + '/';
}
}
for (vector<string>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
for (auto id : ids) {
// collection names do not need to be JSON-escaped
// keys do not need to be JSON-escaped
result += prefix + (*i) + '"';
result += prefix + id + '"';
if (first) {
prefix = ",\n" + prefix;
@ -809,8 +854,8 @@ bool RestDocumentHandler::readAllDocuments () {
/// @brief reads a single a document, coordinator case in a cluster
////////////////////////////////////////////////////////////////////////////////
bool RestDocumentHandler::getAllDocumentsCoordinator (
string const& collname ) {
bool RestDocumentHandler::getAllDocumentsCoordinator (string const& collname,
string const& returnType) {
string const& dbname = _request->databaseName();
triagens::rest::HttpResponse::HttpResponseCode responseCode;
@ -818,7 +863,7 @@ bool RestDocumentHandler::getAllDocumentsCoordinator (
string resultBody;
int error = triagens::arango::getAllDocumentsOnCoordinator(
dbname, collname, responseCode, contentType, resultBody);
dbname, collname, returnType, responseCode, contentType, resultBody);
if (error != TRI_ERROR_NO_ERROR) {
generateTransactionError(collname, error);

View File

@ -169,7 +169,8 @@ namespace triagens {
/// @brief read all documents, coordinator case in a cluster
////////////////////////////////////////////////////////////////////////////////
bool getAllDocumentsCoordinator (string const& collname);
bool getAllDocumentsCoordinator (string const& collname,
string const& returnType);
////////////////////////////////////////////////////////////////////////////////
/// @brief read a single document, coordinator case in a cluster