1
0
Fork 0

added arangovpack binary

This commit is contained in:
jsteemann 2016-07-05 13:53:22 +02:00
parent c5dbfd7c7b
commit b788babcc8
5 changed files with 347 additions and 0 deletions

View File

@ -71,6 +71,7 @@ set(ARANGO_DUMP_FRIENDLY_STRING "arangodump - export")
set(ARANGO_RESTORE_FRIENDLY_STRING "arangrestore - importer")
set(ARANGO_IMP_FRIENDLY_STRING "arangoimp - TSV/CSV/JSON importer")
set(ARANGOSH_FRIENDLY_STRING "arangosh - commandline client")
set(ARANGO_VPACK_FRIENDLY_STRING "arangovpack - vpack printer")
# libraries
set(LIB_ARANGO arango)
@ -83,6 +84,7 @@ set(BIN_ARANGODUMP arangodump)
set(BIN_ARANGOIMP arangoimp)
set(BIN_ARANGORESTORE arangorestore)
set(BIN_ARANGOSH arangosh)
set(BIN_ARANGOVPACK arangovpack)
# test binaries
set(TEST_BASICS_SUITE basics_suite)

View File

@ -234,6 +234,39 @@ else ()
add_dependencies(arangosh zlibstatic)
endif ()
################################################################################
## arangovpack
################################################################################
if (MSVC)
generate_product_version(ProductVersionFiles_arangovpack
NAME arangovpack
FILE_DESCRIPTION ${ARANGO_VPACK_FRIENDLY_STRING}
ICON ${ARANGO_ICON}
VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR}
VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR}
VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH}
VERSION_REVISION ${BUILD_ID}
)
endif ()
add_executable(${BIN_ARANGOVPACK}
${ProductVersionFiles_arangovpack}
${PROJECT_SOURCE_DIR}/lib/Basics/WorkMonitorDummy.cpp
VPack/VPackFeature.cpp
VPack/arangovpack.cpp
)
target_link_libraries(${BIN_ARANGOVPACK}
${LIB_ARANGO}
${MSVC_LIBS}
${SYSTEM_LIBRARIES}
)
install(
TARGETS ${BIN_ARANGOVPACK}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
################################################################################
## foxx-manager
################################################################################

View File

@ -0,0 +1,191 @@
////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2016 ArangoDB 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 Jan Steemann
////////////////////////////////////////////////////////////////////////////////
#include "VPackFeature.h"
#include "ApplicationFeatures/ApplicationServer.h"
#include "Basics/FileUtils.h"
#include "Basics/StringUtils.h"
#include "Basics/VelocyPackHelper.h"
#include "ProgramOptions/ProgramOptions.h"
#include <velocypack/Dumper.h>
#include <velocypack/Options.h>
#include <velocypack/Slice.h>
#include <velocypack/Validator.h>
#include <velocypack/velocypack-aliases.h>
#include <iostream>
using namespace arangodb;
using namespace arangodb::basics;
using namespace arangodb::options;
static std::string ConvertFromHex(std::string const& value) {
std::string result;
result.reserve(value.size());
size_t const n = value.size();
int prev = -1;
for (size_t i = 0; i < n; ++i) {
int current;
if (value[i] >= '0' && value[i] <= '9') {
current = value[i] - '0';
} else if (value[i] >= 'a' && value[i] <= 'f') {
current = 10 + (value[i] - 'a');
} else if (value[i] >= 'A' && value[i] <= 'F') {
current = 10 + (value[i] - 'A');
} else {
prev = -1;
continue;
}
if (prev == -1) {
// first part of two-byte sequence
prev = current;
} else {
// second part of two-byte sequence
result.push_back(static_cast<unsigned char>((prev << 4) + current));
prev = -1;
}
}
return result;
}
VPackFeature::VPackFeature(application_features::ApplicationServer* server,
int* result)
: ApplicationFeature(server, "VPack"),
_result(result),
_prettyPrint(false),
_hexInput(false),
_printNonJson(false) {
requiresElevatedPrivileges(false);
setOptional(false);
}
void VPackFeature::collectOptions(
std::shared_ptr<options::ProgramOptions> options) {
options->addOption(
"--input-file", "input filename",
new StringParameter(&_inputFile));
options->addOption(
"--output-file", "output filename",
new StringParameter(&_outputFile));
options->addOption(
"--pretty", "pretty print result",
new BooleanParameter(&_prettyPrint));
options->addOption(
"--hex", "read hex-encoded input",
new BooleanParameter(&_hexInput));
options->addOption(
"--print-non-json", "print non-JSON types",
new BooleanParameter(&_printNonJson));
}
void VPackFeature::start() {
*_result = EXIT_SUCCESS;
bool toStdOut = false;
#ifdef __linux__
// treat "-" as stdin. quick hack for linux
if (_inputFile.empty() || _inputFile == "-") {
_inputFile = "/proc/self/fd/0";
}
// treat missing outfile as stdout
if (_outputFile.empty() || _outputFile == "+") {
_outputFile = "/proc/self/fd/1";
toStdOut = true;
}
#endif
std::string s = basics::FileUtils::slurp(_inputFile);
if (_hexInput) {
s = ConvertFromHex(s);
}
VPackOptions options;
options.prettyPrint = _prettyPrint;
options.unsupportedTypeBehavior =
(_printNonJson ? VPackOptions::ConvertUnsupportedType : VPackOptions::FailOnUnsupportedType);
try {
VPackValidator validator(&options);
validator.validate(s.c_str(), s.size(), false);
} catch (std::exception const& ex) {
std::cerr << "Invalid VPack input while processing infile '" << _inputFile
<< "': " << ex.what() << std::endl;
*_result = TRI_ERROR_INTERNAL;
return;
}
VPackSlice const slice(s.c_str());
VPackBuffer<char> buffer(4096);
VPackCharBufferSink sink(&buffer);
VPackDumper dumper(&sink, &options);
try {
dumper.dump(slice);
} catch (std::exception const& ex) {
std::cerr << "An exception occurred while processing infile '" << _inputFile
<< "': " << ex.what() << std::endl;
*_result = TRI_ERROR_INTERNAL;
return;
} catch (...) {
std::cerr << "An unknown exception occurred while processing infile '"
<< _inputFile << "'" << std::endl;
*_result = TRI_ERROR_INTERNAL;
return;
}
std::ofstream ofs(_outputFile, std::ofstream::out);
if (!ofs.is_open()) {
std::cerr << "Cannot write outfile '" << _outputFile << "'" << std::endl;
*_result = TRI_ERROR_INTERNAL;
return;
}
// reset stream
if (!toStdOut) {
ofs.seekp(0);
}
// write into stream
char const* start = buffer.data();
ofs.write(start, buffer.size());
ofs.close();
if (!toStdOut) {
std::cout << "Successfully converted JSON infile '" << _inputFile << "'"
<< std::endl;
std::cout << "VPack Infile size: " << s.size() << std::endl;
std::cout << "JSON Outfile size: " << buffer.size() << std::endl;
}
*_result = TRI_ERROR_NO_ERROR;
}

View File

@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2016 ArangoDB 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 Jan Steemann
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_VPACK_VPACK_FEATURE_H
#define ARANGODB_VPACK_VPACK_FEATURE_H 1
#include "ApplicationFeatures/ApplicationFeature.h"
#include "Basics/VelocyPackHelper.h"
namespace arangodb {
class VPackFeature final
: public application_features::ApplicationFeature {
public:
VPackFeature(application_features::ApplicationServer* server,
int* result);
public:
void collectOptions(std::shared_ptr<options::ProgramOptions>) override;
void start() override;
private:
int* _result;
std::string _inputFile;
std::string _outputFile;
bool _prettyPrint;
bool _hexInput;
bool _printNonJson;
};
}
#endif

View File

@ -0,0 +1,69 @@
////////////////////////////////////////////////////////////////////////////////
/// 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 Jan Steemann
////////////////////////////////////////////////////////////////////////////////
#include "Basics/Common.h"
#include "ApplicationFeatures/ConfigFeature.h"
#include "ApplicationFeatures/ShutdownFeature.h"
#include "ApplicationFeatures/VersionFeature.h"
#include "Basics/ArangoGlobalContext.h"
#include "Logger/LoggerFeature.h"
#include "ProgramOptions/ProgramOptions.h"
#include "Random/RandomFeature.h"
#include "VPack/VPackFeature.h"
using namespace arangodb;
using namespace arangodb::application_features;
int main(int argc, char* argv[]) {
ArangoGlobalContext context(argc, argv);
context.installHup();
std::shared_ptr<options::ProgramOptions> options(new options::ProgramOptions(
argv[0], "Usage: arangovpack [<options>]", "For more information use:"));
ApplicationServer server(options);
int ret;
server.addFeature(new ConfigFeature(&server, "arangovpack"));
server.addFeature(new LoggerFeature(&server, false));
server.addFeature(new RandomFeature(&server));
server.addFeature(new ShutdownFeature(&server, {"VPack"}));
server.addFeature(new VPackFeature(&server, &ret));
server.addFeature(new VersionFeature(&server));
try {
server.run(argc, argv);
} catch (std::exception const& ex) {
LOG(ERR) << "arangovpack terminated because of an unhandled exception: "
<< ex.what();
ret = EXIT_FAILURE;
} catch (...) {
LOG(ERR) << "arangovpack terminated because of an unhandled exception of "
"unknown type";
ret = EXIT_FAILURE;
}
return context.exit(ret);
}