mirror of https://gitee.com/bigwinds/arangodb
456 lines
14 KiB
C++
456 lines
14 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
/// DISCLAIMER
|
|
///
|
|
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
|
|
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
|
|
///
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
/// you may not use this file except in compliance with the License.
|
|
/// You may obtain a copy of the License at
|
|
///
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
///
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
/// See the License for the specific language governing permissions and
|
|
/// limitations under the License.
|
|
///
|
|
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
///
|
|
/// @author Dr. Frank Celler
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "RestQueryHandler.h"
|
|
|
|
#include "Aql/Query.h"
|
|
#include "Aql/QueryList.h"
|
|
#include "Basics/conversions.h"
|
|
#include "Basics/json.h"
|
|
#include "Basics/json-utilities.h"
|
|
#include "Basics/StringBuffer.h"
|
|
#include "Basics/StringUtils.h"
|
|
#include "Basics/VelocyPackHelper.h"
|
|
#include "Cluster/ClusterComm.h"
|
|
#include "Cluster/ClusterInfo.h"
|
|
#include "Cluster/ClusterMethods.h"
|
|
#include "Cluster/ServerState.h"
|
|
#include "Rest/HttpRequest.h"
|
|
#include "VocBase/document-collection.h"
|
|
#include "VocBase/vocbase.h"
|
|
|
|
using namespace std;
|
|
using namespace triagens::basics;
|
|
using namespace triagens::rest;
|
|
using namespace triagens::arango;
|
|
using namespace triagens::aql;
|
|
|
|
|
|
|
|
RestQueryHandler::RestQueryHandler(HttpRequest* request,
|
|
ApplicationV8* applicationV8)
|
|
: RestVocbaseBaseHandler(request), _applicationV8(applicationV8) {}
|
|
|
|
|
|
|
|
bool RestQueryHandler::isDirect() const {
|
|
return _request->requestType() != HttpRequest::HTTP_REQUEST_POST;
|
|
}
|
|
|
|
|
|
HttpHandler::status_t RestQueryHandler::execute() {
|
|
// extract the sub-request type
|
|
HttpRequest::HttpRequestType type = _request->requestType();
|
|
|
|
// execute one of the CRUD methods
|
|
try {
|
|
switch (type) {
|
|
case HttpRequest::HTTP_REQUEST_DELETE:
|
|
deleteQuery();
|
|
break;
|
|
case HttpRequest::HTTP_REQUEST_GET:
|
|
readQuery();
|
|
break;
|
|
case HttpRequest::HTTP_REQUEST_PUT:
|
|
replaceProperties();
|
|
break;
|
|
case HttpRequest::HTTP_REQUEST_POST:
|
|
parseQuery();
|
|
break;
|
|
|
|
case HttpRequest::HTTP_REQUEST_HEAD:
|
|
case HttpRequest::HTTP_REQUEST_PATCH:
|
|
case HttpRequest::HTTP_REQUEST_ILLEGAL:
|
|
default: {
|
|
generateNotImplemented("ILLEGAL " + DOCUMENT_PATH);
|
|
break;
|
|
}
|
|
}
|
|
} catch (Exception const& err) {
|
|
handleError(err);
|
|
} catch (std::exception const& ex) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, ex.what(), __FILE__,
|
|
__LINE__);
|
|
handleError(err);
|
|
} catch (...) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, __FILE__, __LINE__);
|
|
handleError(err);
|
|
}
|
|
|
|
// this handler is done
|
|
return status_t(HANDLER_DONE);
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock GetApiQueryProperties
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::readQueryProperties() {
|
|
try {
|
|
auto queryList = static_cast<QueryList*>(_vocbase->_queries);
|
|
|
|
VPackBuilder result;
|
|
result.add(VPackValue(VPackValueType::Object));
|
|
result.add("error", VPackValue(false));
|
|
result.add("code", VPackValue(HttpResponse::OK));
|
|
result.add("enabled", VPackValue(queryList->enabled()));
|
|
result.add("trackSlowQueries", VPackValue(queryList->trackSlowQueries()));
|
|
result.add("maxSlowQueries", VPackValue(queryList->maxSlowQueries()));
|
|
result.add("slowQueryThreshold",
|
|
VPackValue(queryList->slowQueryThreshold()));
|
|
result.add("maxQueryStringLength",
|
|
VPackValue(queryList->maxQueryStringLength()));
|
|
result.close();
|
|
VPackSlice slice = result.slice();
|
|
|
|
generateResult(slice);
|
|
} catch (Exception const& err) {
|
|
handleError(err);
|
|
} catch (std::exception const& ex) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, ex.what(), __FILE__,
|
|
__LINE__);
|
|
handleError(err);
|
|
} catch (...) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, __FILE__, __LINE__);
|
|
handleError(err);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock GetApiQueryCurrent
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock GetApiQuerySlow
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::readQuery(bool slow) {
|
|
try {
|
|
auto queryList = static_cast<QueryList*>(_vocbase->_queries);
|
|
auto queries = slow ? queryList->listSlow() : queryList->listCurrent();
|
|
|
|
VPackBuilder result;
|
|
result.add(VPackValue(VPackValueType::Array));
|
|
|
|
for (auto const& it : queries) {
|
|
auto const& timeString = TRI_StringTimeStamp(it.started);
|
|
auto const& queryString = it.queryString;
|
|
|
|
result.add(VPackValue(VPackValueType::Object));
|
|
result.add("id", VPackValue(StringUtils::itoa(it.id)));
|
|
result.add("query", VPackValue(queryString));
|
|
result.add("started", VPackValue(timeString));
|
|
result.add("runTime", VPackValue(it.runTime));
|
|
result.close();
|
|
}
|
|
result.close();
|
|
VPackSlice s = result.slice();
|
|
|
|
generateResult(s);
|
|
} catch (Exception const& err) {
|
|
handleError(err);
|
|
} catch (std::exception const& ex) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, ex.what(), __FILE__,
|
|
__LINE__);
|
|
handleError(err);
|
|
} catch (...) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, __FILE__, __LINE__);
|
|
handleError(err);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief returns AQL query tracking
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::readQuery() {
|
|
const auto& suffix = _request->suffix();
|
|
|
|
if (suffix.size() != 1) {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting GET /_api/query/<type>");
|
|
return true;
|
|
}
|
|
|
|
auto const& name = suffix[0];
|
|
|
|
if (name == "slow") {
|
|
return readQuery(true);
|
|
} else if (name == "current") {
|
|
return readQuery(false);
|
|
} else if (name == "properties") {
|
|
return readQueryProperties();
|
|
}
|
|
|
|
generateError(HttpResponse::NOT_FOUND, TRI_ERROR_HTTP_NOT_FOUND,
|
|
"unknown type '" + name +
|
|
"', expecting 'slow', 'current', or 'properties'");
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock DeleteApiQuerySlow
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::deleteQuerySlow() {
|
|
auto queryList = static_cast<triagens::aql::QueryList*>(_vocbase->_queries);
|
|
queryList->clearSlow();
|
|
|
|
VPackBuilder result;
|
|
result.add(VPackValue(VPackValueType::Object));
|
|
result.add("error", VPackValue(false));
|
|
result.add("code", VPackValue(HttpResponse::OK));
|
|
result.close();
|
|
VPackSlice slice = result.slice();
|
|
generateResult(slice);
|
|
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock DeleteApiQueryKill
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::deleteQuery(std::string const& name) {
|
|
auto id = StringUtils::uint64(name);
|
|
auto queryList = static_cast<triagens::aql::QueryList*>(_vocbase->_queries);
|
|
TRI_ASSERT(queryList != nullptr);
|
|
|
|
auto res = queryList->kill(id);
|
|
|
|
if (res == TRI_ERROR_NO_ERROR) {
|
|
VPackBuilder result;
|
|
result.add(VPackValue(VPackValueType::Object));
|
|
result.add("error", VPackValue(false));
|
|
result.add("code", VPackValue(HttpResponse::OK));
|
|
result.close();
|
|
VPackSlice slice = result.slice();
|
|
generateResult(slice);
|
|
} else {
|
|
generateError(HttpResponse::BAD, res, "cannot kill query '" + name + "'");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief interrupts a query
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::deleteQuery() {
|
|
const auto& suffix = _request->suffix();
|
|
|
|
if (suffix.size() != 1) {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting DELETE /_api/query/<id> or /_api/query/slow");
|
|
return true;
|
|
}
|
|
|
|
auto const& name = suffix[0];
|
|
|
|
if (name == "slow") {
|
|
return deleteQuerySlow();
|
|
}
|
|
return deleteQuery(name);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock PutApiQueryProperties
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::replaceProperties() {
|
|
const auto& suffix = _request->suffix();
|
|
|
|
if (suffix.size() != 1 || suffix[0] != "properties") {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting PUT /_api/query/properties");
|
|
return true;
|
|
}
|
|
|
|
bool parseSuccess = true;
|
|
VPackOptions options;
|
|
std::shared_ptr<VPackBuilder> parsedBody =
|
|
parseVelocyPackBody(&options, parseSuccess);
|
|
if (!parseSuccess) {
|
|
// error message generated in parseVelocyPackBody
|
|
return true;
|
|
}
|
|
|
|
VPackSlice body = parsedBody.get()->slice();
|
|
if (!body.isObject()) {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting a JSON object as body");
|
|
};
|
|
|
|
auto queryList = static_cast<triagens::aql::QueryList*>(_vocbase->_queries);
|
|
|
|
try {
|
|
bool enabled = queryList->enabled();
|
|
bool trackSlowQueries = queryList->trackSlowQueries();
|
|
size_t maxSlowQueries = queryList->maxSlowQueries();
|
|
double slowQueryThreshold = queryList->slowQueryThreshold();
|
|
size_t maxQueryStringLength = queryList->maxQueryStringLength();
|
|
|
|
VPackSlice attribute;
|
|
attribute = body.get("enabled");
|
|
if (attribute.isBool()) {
|
|
enabled = attribute.getBool();
|
|
}
|
|
|
|
attribute = body.get("trackSlowQueries");
|
|
if (attribute.isBool()) {
|
|
trackSlowQueries = attribute.getBool();
|
|
}
|
|
|
|
attribute = body.get("maxSlowQueries");
|
|
if (attribute.isInteger()) {
|
|
maxSlowQueries = static_cast<size_t>(attribute.getUInt());
|
|
}
|
|
|
|
attribute = body.get("slowQueryThreshold");
|
|
if (attribute.isNumber()) {
|
|
slowQueryThreshold = attribute.getNumber<double>();
|
|
}
|
|
|
|
attribute = body.get("maxQueryStringLength");
|
|
if (attribute.isInteger()) {
|
|
maxQueryStringLength = static_cast<size_t>(attribute.getUInt());
|
|
}
|
|
|
|
queryList->enabled(enabled);
|
|
queryList->trackSlowQueries(trackSlowQueries);
|
|
queryList->maxSlowQueries(maxSlowQueries);
|
|
queryList->slowQueryThreshold(slowQueryThreshold);
|
|
queryList->maxQueryStringLength(maxQueryStringLength);
|
|
|
|
return readQueryProperties();
|
|
} catch (Exception const& err) {
|
|
handleError(err);
|
|
} catch (std::exception const& ex) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, ex.what(), __FILE__,
|
|
__LINE__);
|
|
handleError(err);
|
|
} catch (...) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, __FILE__, __LINE__);
|
|
handleError(err);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief was docuBlock PostApiQueryProperties
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool RestQueryHandler::parseQuery() {
|
|
const auto& suffix = _request->suffix();
|
|
|
|
if (!suffix.empty()) {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting POST /_api/query");
|
|
return true;
|
|
}
|
|
|
|
bool parseSuccess = true;
|
|
VPackOptions options;
|
|
std::shared_ptr<VPackBuilder> parsedBody =
|
|
parseVelocyPackBody(&options, parseSuccess);
|
|
if (!parseSuccess) {
|
|
// error message generated in parseVelocyPackBody
|
|
return true;
|
|
}
|
|
|
|
VPackSlice body = parsedBody.get()->slice();
|
|
|
|
if (!body.isObject()) {
|
|
generateError(HttpResponse::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
|
|
"expecting a JSON object as body");
|
|
};
|
|
|
|
try {
|
|
std::string const&& queryString =
|
|
VelocyPackHelper::checkAndGetStringValue(body, "query");
|
|
|
|
Query query(_applicationV8, true, _vocbase, queryString.c_str(),
|
|
queryString.size(), nullptr, nullptr, PART_MAIN);
|
|
|
|
auto parseResult = query.parse();
|
|
|
|
if (parseResult.code != TRI_ERROR_NO_ERROR) {
|
|
generateError(HttpResponse::BAD, parseResult.code, parseResult.details);
|
|
return true;
|
|
}
|
|
|
|
VPackBuilder result;
|
|
{
|
|
VPackObjectBuilder b(&result);
|
|
result.add("error", VPackValue(false));
|
|
result.add("code", VPackValue(HttpResponse::OK));
|
|
result.add("parsed", VPackValue(true));
|
|
|
|
result.add("collections", VPackValue(VPackValueType::Array));
|
|
for (const auto& it : parseResult.collectionNames) {
|
|
result.add(VPackValue(it));
|
|
}
|
|
result.close(); // Collections
|
|
|
|
result.add("bindVars", VPackValue(VPackValueType::Array));
|
|
for (const auto& it : parseResult.bindParameters) {
|
|
result.add(VPackValue(it));
|
|
}
|
|
result.close(); // bindVars
|
|
|
|
auto tmp = VPackParser::fromJson(
|
|
triagens::basics::JsonHelper::toString(parseResult.json));
|
|
result.add("ast", tmp->slice());
|
|
|
|
if (parseResult.warnings != nullptr) {
|
|
auto tmp = VPackParser::fromJson(
|
|
triagens::basics::JsonHelper::toString(parseResult.warnings));
|
|
result.add("warnings", tmp->slice());
|
|
}
|
|
}
|
|
|
|
VPackSlice slice = result.slice();
|
|
generateResult(slice);
|
|
} catch (Exception const& err) {
|
|
handleError(err);
|
|
} catch (std::exception const& ex) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, ex.what(), __FILE__,
|
|
__LINE__);
|
|
handleError(err);
|
|
} catch (...) {
|
|
triagens::basics::Exception err(TRI_ERROR_INTERNAL, __FILE__, __LINE__);
|
|
handleError(err);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|