1
0
Fork 0

some small optimizations

This commit is contained in:
jsteemann 2017-06-02 17:34:03 +02:00
parent 7abcfab41a
commit c05421e1aa
5 changed files with 41 additions and 43 deletions

View File

@ -246,9 +246,9 @@ int RestImportHandler::handleSingleDocument(SingleCollectionTransaction& trx,
} }
try { try {
arangodb::basics::VelocyPackHelper::checkAndGetStringValue( arangodb::basics::VelocyPackHelper::ensureStringValue(
slice, StaticStrings::FromString); slice, StaticStrings::FromString);
arangodb::basics::VelocyPackHelper::checkAndGetStringValue( arangodb::basics::VelocyPackHelper::ensureStringValue(
slice, StaticStrings::ToString); slice, StaticStrings::ToString);
} catch (arangodb::basics::Exception const&) { } catch (arangodb::basics::Exception const&) {
std::string part = VPackDumper::toString(slice); std::string part = VPackDumper::toString(slice);
@ -429,20 +429,18 @@ bool RestImportHandler::createFromJson(std::string const& type) {
continue; continue;
} }
TRI_ASSERT(ptr != nullptr);
oldPtr = ptr;
tmpBuilder.clear(); tmpBuilder.clear();
if (pos != nullptr) { if (pos != nullptr) {
// non-empty line // non-empty line
*(const_cast<char*>(pos)) = '\0'; *(const_cast<char*>(pos)) = '\0';
TRI_ASSERT(ptr != nullptr);
oldPtr = ptr;
parseVelocyPackLine(tmpBuilder, ptr, pos, success); parseVelocyPackLine(tmpBuilder, ptr, pos, success);
ptr = pos + 1; ptr = pos + 1;
} else { } else {
// last-line, non-empty // last-line, non-empty
TRI_ASSERT(pos == nullptr); parseVelocyPackLine(tmpBuilder, ptr, end, success);
TRI_ASSERT(ptr != nullptr);
oldPtr = ptr;
parseVelocyPackLine(tmpBuilder, ptr, success);
ptr = end; ptr = end;
} }
@ -492,13 +490,12 @@ bool RestImportHandler::createFromJson(std::string const& type) {
return false; return false;
} }
VPackValueLength const n = documents.length();
VPackBuilder lineBuilder; VPackBuilder lineBuilder;
for (VPackValueLength i = 0; i < n; ++i) { VPackArrayIterator it(documents);
VPackSlice const slice = documents.at(i);
res = handleSingleDocument(trx, lineBuilder, result, babies, slice, isEdgeCollection, while (it.valid()) {
static_cast<size_t>(i + 1)); res = handleSingleDocument(trx, lineBuilder, result, babies, it.value(), isEdgeCollection,
static_cast<size_t>(it.index() + 1));
if (res.fail()) { if (res.fail()) {
if (complete) { if (complete) {
@ -508,6 +505,8 @@ bool RestImportHandler::createFromJson(std::string const& type) {
res = TRI_ERROR_NO_ERROR; res = TRI_ERROR_NO_ERROR;
} }
it.next();
} }
} }
@ -597,14 +596,12 @@ bool RestImportHandler::createFromVPack(std::string const& type) {
return false; return false;
} }
VPackValueLength const n = documents.length();
VPackBuilder lineBuilder; VPackBuilder lineBuilder;
for (VPackValueLength i = 0; i < n; ++i) {
VPackSlice const slice = documents.at(i);
res = handleSingleDocument(trx, lineBuilder, result, babies, slice, isEdgeCollection, VPackArrayIterator it(documents);
static_cast<size_t>(i + 1)); while (it.valid()) {
res = handleSingleDocument(trx, lineBuilder, result, babies, it.value(), isEdgeCollection,
static_cast<size_t>(it.index() + 1));
if (res.fail()) { if (res.fail()) {
if (complete) { if (complete) {
@ -614,6 +611,8 @@ bool RestImportHandler::createFromVPack(std::string const& type) {
res = TRI_ERROR_NO_ERROR; res = TRI_ERROR_NO_ERROR;
} }
it.next();
} }
babies.close(); babies.close();
@ -1042,28 +1041,12 @@ void RestImportHandler::generateDocumentsCreated(
/// @brief parse a single document line /// @brief parse a single document line
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void RestImportHandler::parseVelocyPackLine(
VPackBuilder& builder,
std::string const& line, bool& success) {
try {
success = true;
VPackParser parser(builder);
parser.parse(line);
} catch (VPackException const&) {
success = false;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief parse a single document line
////////////////////////////////////////////////////////////////////////////////
void RestImportHandler::parseVelocyPackLine(VPackBuilder& builder, void RestImportHandler::parseVelocyPackLine(VPackBuilder& builder,
char const* start, char const* end, bool& success) { char const* start, char const* end, bool& success) {
try { try {
success = true; success = true;
VPackParser parser(builder); VPackParser parser(builder);
parser.parse(start,std::distance(start, end)); parser.parse(start, std::distance(start, end));
} catch (std::exception const&) { } catch (std::exception const&) {
// The line is invalid and could not be transformed into a string // The line is invalid and could not be transformed into a string
success = false; success = false;

View File

@ -141,12 +141,6 @@ class RestImportHandler : public RestVocbaseBaseHandler {
/// @brief parses a string /// @brief parses a string
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
void parseVelocyPackLine(VPackBuilder&, std::string const&, bool&);
//////////////////////////////////////////////////////////////////////////////
/// @brief parses a string
//////////////////////////////////////////////////////////////////////////////
void parseVelocyPackLine(VPackBuilder&, char const*, char const*, bool&); void parseVelocyPackLine(VPackBuilder&, char const*, char const*, bool&);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@ -675,7 +675,7 @@ void transaction::Methods::buildDocumentIdentity(
LogicalCollection* collection, VPackBuilder& builder, TRI_voc_cid_t cid, LogicalCollection* collection, VPackBuilder& builder, TRI_voc_cid_t cid,
StringRef const& key, TRI_voc_rid_t rid, TRI_voc_rid_t oldRid, StringRef const& key, TRI_voc_rid_t rid, TRI_voc_rid_t oldRid,
ManagedDocumentResult const* oldDoc, ManagedDocumentResult const* newDoc) { ManagedDocumentResult const* oldDoc, ManagedDocumentResult const* newDoc) {
std::string temp; std::string temp; // TODO: pass a string into this function
temp.reserve(64); temp.reserve(64);
if (_state->isRunningInCluster()) { if (_state->isRunningInCluster()) {

View File

@ -488,6 +488,20 @@ std::string VelocyPackHelper::checkAndGetStringValue(VPackSlice const& slice,
return sub.copyString(); return sub.copyString();
} }
void VelocyPackHelper::ensureStringValue(VPackSlice const& slice,
std::string const& name) {
TRI_ASSERT(slice.isObject());
if (!slice.hasKey(name)) {
std::string msg = "The attribute '" + name + "' was not found.";
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_BAD_PARAMETER, msg);
}
VPackSlice const sub = slice.get(name);
if (!sub.isString()) {
std::string msg = "The attribute '" + name + "' is not a string.";
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_BAD_PARAMETER, msg);
}
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief returns a string value, or the default value if it is not a string /// @brief returns a string value, or the default value if it is not a string
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -282,9 +282,16 @@ class VelocyPackHelper {
static std::string checkAndGetStringValue(VPackSlice const&, char const*); static std::string checkAndGetStringValue(VPackSlice const&, char const*);
//////////////////////////////////////////////////////////////////////////////
/// @brief ensures a sub-element is of type string
//////////////////////////////////////////////////////////////////////////////
static std::string checkAndGetStringValue(VPackSlice const&, static std::string checkAndGetStringValue(VPackSlice const&,
std::string const&); std::string const&);
static void ensureStringValue(VPackSlice const&,
std::string const&);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// @brief returns a Numeric sub-element, or throws if <name> does not exist /// @brief returns a Numeric sub-element, or throws if <name> does not exist
/// or it is not a Number /// or it is not a Number