From 5a5d1de7eaa33bf54ce45e65bf215afeb0cc4588 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 11 Aug 2015 15:18:33 +0200 Subject: [PATCH] Moved the insertion of elements into an indox to the super class. Is only used for Skiplist and Hash. Might probably be used by other indicies later --- arangod/Indexes/HashIndex.cpp | 78 ++++-------------------- arangod/Indexes/Index.cpp | 2 +- arangod/Indexes/Index.h | 96 +++++++++++++++++++++++++++++- arangod/Indexes/SkiplistIndex2.cpp | 92 +--------------------------- arangod/Indexes/SkiplistIndex2.h | 9 --- 5 files changed, 110 insertions(+), 167 deletions(-) diff --git a/arangod/Indexes/HashIndex.cpp b/arangod/Indexes/HashIndex.cpp index 444cab9617..48dd184176 100644 --- a/arangod/Indexes/HashIndex.cpp +++ b/arangod/Indexes/HashIndex.cpp @@ -89,81 +89,25 @@ static void FreeSubObjectsHashIndexElement (T* element) { /// hashed and the shape identifier of each part. //////////////////////////////////////////////////////////////////////////////// -template -static int HashIndexHelper (HashIndex const* hashIndex, - T* hashElement, - TRI_doc_mptr_t const* document) { - TRI_shaped_json_t shapedJson; // the object behind document - - auto shaper = hashIndex->collection()->getShaper(); // ONLY IN INDEX, PROTECTED by RUNTIME - bool const sparse = hashIndex->sparse(); - - // ............................................................................. - // Assign the document to the TRI_hash_index_element_t structure - so that it - // can later be retreived. - // ............................................................................. - - TRI_EXTRACT_SHAPED_JSON_MARKER(shapedJson, document->getDataPtr()); // ONLY IN INDEX, PROTECTED by RUNTIME - - hashElement->_document = const_cast(document); - char const* ptr = document->getShapedJsonPtr(); // ONLY IN INDEX - - // ............................................................................. - // Extract the attribute values - // ............................................................................. - - int res = TRI_ERROR_NO_ERROR; - - auto const& paths = hashIndex->paths(); - size_t const n = paths.size(); - - for (size_t j = 0; j < n; ++j) { - TRI_shape_pid_t path = paths[j]; - - // determine if document has that particular shape - TRI_shape_access_t const* acc = shaper->findAccessor(shapedJson._sid, path); - - // field not part of the object - if (acc == nullptr || acc->_resultSid == TRI_SHAPE_ILLEGAL) { - hashElement->_subObjects[j]._sid = BasicShapes::TRI_SHAPE_SID_NULL; - - res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; - - if (sparse) { - // no need to continue - return res; - } - - continue; - } - - // extract the field - TRI_shaped_json_t shapedObject; - if (! TRI_ExecuteShapeAccessor(acc, &shapedJson, &shapedObject)) { - return TRI_ERROR_INTERNAL; - } - - if (shapedObject._sid == BasicShapes::TRI_SHAPE_SID_NULL) { - res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; - - if (sparse) { - // no need to continue - return res; +/* + // TODO needs to be extracted as a helper function + if ( triagens::basics::TRI_AttributeNamesHaveExpansion(hashIndex->fields()[j]) ) { + TRI_shape_t const* shape = shaper->lookupShapeId(shapedObject._sid); + if (shape->_type >= TRI_SHAPE_LIST && shape->_type <= TRI_SHAPE_HOMOGENEOUS_SIZED_LIST) { + std::cout << "Should expand here" << std::endl; + auto json = triagens::basics::Json(TRI_UNKNOWN_MEM_ZONE, TRI_JsonShapedJson(shaper, &shapedObject)); + std::cout << "Is Array " << json.isArray() << " :: " << json << std::endl; } } - - TRI_FillShapedSub(&hashElement->_subObjects[j], &shapedObject, ptr); - } - - return res; } +*/ //////////////////////////////////////////////////////////////////////////////// /// @brief index helper for hashing with allocation //////////////////////////////////////////////////////////////////////////////// template -static int HashIndexHelperAllocate (HashIndex const* hashIndex, +static int HashIndexHelperAllocate (HashIndex* hashIndex, T* hashElement, TRI_doc_mptr_t const* document) { // ............................................................................. @@ -177,7 +121,7 @@ static int HashIndexHelperAllocate (HashIndex const* hashIndex, return TRI_ERROR_OUT_OF_MEMORY; } - int res = HashIndexHelper(hashIndex, hashElement, document); + int res = hashIndex->fillElement(hashElement, hashElement->_subObjects, document, hashIndex->paths(), hashIndex->sparse()); if (res == TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING) { // if the document does not have the indexed attribute or has it, and it is null, diff --git a/arangod/Indexes/Index.cpp b/arangod/Indexes/Index.cpp index c6015a39c0..9bef11ac8b 100644 --- a/arangod/Indexes/Index.cpp +++ b/arangod/Indexes/Index.cpp @@ -30,8 +30,8 @@ #include "Index.h" #include "Basics/Exceptions.h" #include "Basics/json-utilities.h" -#include "VocBase/document-collection.h" #include "VocBase/server.h" +#include "VocBase/VocShaper.h" using namespace triagens::arango; diff --git a/arangod/Indexes/Index.h b/arangod/Indexes/Index.h index cad209b1f8..9add047c77 100644 --- a/arangod/Indexes/Index.h +++ b/arangod/Indexes/Index.h @@ -33,7 +33,11 @@ #include "Basics/Common.h" #include "Basics/AttributeNameParser.h" #include "Basics/JsonHelper.h" +#include "Basics/logging.h" +#include "VocBase/document-collection.h" +#include "VocBase/shaped-json.h" #include "VocBase/vocbase.h" +#include "VocBase/VocShaper.h" #include "VocBase/voc-types.h" // ----------------------------------------------------------------------------- @@ -208,8 +212,98 @@ namespace triagens { struct TRI_document_collection_t* _collection; - std::vector> const _fields; + std::vector> const _fields; + + +// ----------------------------------------------------------------------------- +// --SECTION-- protected methods +// ----------------------------------------------------------------------------- + + public: + +//////////////////////////////////////////////////////////////////////////////// +/// @brief helper function to insert a document into any index type +//////////////////////////////////////////////////////////////////////////////// + + template + int fillElement(Idx_Element* element, + TRI_shaped_sub_t* subObjects, + TRI_doc_mptr_t const* document, + std::vector const& paths, + bool const sparse) { + TRI_ASSERT(document != nullptr); + TRI_ASSERT_EXPENSIVE(document->getDataPtr() != nullptr); // ONLY IN INDEX, PROTECTED by RUNTIME + + TRI_shaped_json_t shapedJson; + + TRI_EXTRACT_SHAPED_JSON_MARKER(shapedJson, document->getDataPtr()); // ONLY IN INDEX, PROTECTED by RUNTIME + + if (shapedJson._sid == TRI_SHAPE_ILLEGAL) { + LOG_WARNING("encountered invalid marker with shape id 0"); + + return TRI_ERROR_INTERNAL; + } + + int res = TRI_ERROR_NO_ERROR; + + element->_document = const_cast(document); + char const* ptr = element->_document->getShapedJsonPtr(); // ONLY IN INDEX, PROTECTED by RUNTIME + + // auto subObjects = ; + + size_t const n = paths.size(); + + for (size_t j = 0; j < n; ++j) { + TRI_shape_pid_t path = paths[j]; + + // .......................................................................... + // Determine if document has that particular shape + // .......................................................................... + + TRI_shape_access_t const* acc = _collection->getShaper()->findAccessor(shapedJson._sid, path); // ONLY IN INDEX, PROTECTED by RUNTIME + + if (acc == nullptr || acc->_resultSid == TRI_SHAPE_ILLEGAL) { + // OK, the document does not contain the attributed needed by + // the index, are we sparse? + subObjects[j]._sid = BasicShapes::TRI_SHAPE_SID_NULL; + + res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; + + if (sparse) { + // no need to continue + return res; + } + continue; + } + + // .......................................................................... + // Extract the field + // .......................................................................... + + TRI_shaped_json_t shapedObject; + if (! TRI_ExecuteShapeAccessor(acc, &shapedJson, &shapedObject)) { + return TRI_ERROR_INTERNAL; + } + + if (shapedObject._sid == BasicShapes::TRI_SHAPE_SID_NULL) { + res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; + + if (sparse) { + // no need to continue + return res; + } + } + + // ......................................................................... + // Store the field + // ......................................................................... + + TRI_FillShapedSub(&subObjects[j], &shapedObject, ptr); + } + + return res; + } }; } diff --git a/arangod/Indexes/SkiplistIndex2.cpp b/arangod/Indexes/SkiplistIndex2.cpp index fe04870d21..111a11ac53 100644 --- a/arangod/Indexes/SkiplistIndex2.cpp +++ b/arangod/Indexes/SkiplistIndex2.cpp @@ -183,7 +183,8 @@ int SkiplistIndex2::insert (TRI_doc_mptr_t const* doc, return TRI_ERROR_OUT_OF_MEMORY; } - int res = fillElement(skiplistElement, doc); + int res = fillElement(skiplistElement, SkiplistIndex_Subobjects(skiplistElement), doc, _paths, _sparse); + // ........................................................................... // most likely the cause of this error is that the index is sparse // and not all attributes the index needs are set -- so the document @@ -231,7 +232,7 @@ int SkiplistIndex2::remove (TRI_doc_mptr_t const* doc, return TRI_ERROR_OUT_OF_MEMORY; } - int res = fillElement(skiplistElement, doc); + int res = fillElement(skiplistElement, SkiplistIndex_Subobjects(skiplistElement), doc, _paths, _sparse); // .......................................................................... // Error returned generally implies that the document never was part of the @@ -290,93 +291,6 @@ TRI_skiplist_iterator_t* SkiplistIndex2::lookup (TRI_index_operator_t* slOperato reverse); } -// ----------------------------------------------------------------------------- -// --SECTION-- private methods -// ----------------------------------------------------------------------------- - -//////////////////////////////////////////////////////////////////////////////// -/// @brief helper for skiplist methods -//////////////////////////////////////////////////////////////////////////////// - -int SkiplistIndex2::fillElement(TRI_skiplist_index_element_t* skiplistElement, - TRI_doc_mptr_t const* document) { - // .......................................................................... - // Assign the document to the SkiplistIndexElement structure so that it can - // be retrieved later. - // .......................................................................... - - TRI_ASSERT(document != nullptr); - TRI_ASSERT_EXPENSIVE(document->getDataPtr() != nullptr); // ONLY IN INDEX, PROTECTED by RUNTIME - - TRI_shaped_json_t shapedJson; - TRI_EXTRACT_SHAPED_JSON_MARKER(shapedJson, document->getDataPtr()); // ONLY IN INDEX, PROTECTED by RUNTIME - - if (shapedJson._sid == TRI_SHAPE_ILLEGAL) { - LOG_WARNING("encountered invalid marker with shape id 0"); - - return TRI_ERROR_INTERNAL; - } - - int res = TRI_ERROR_NO_ERROR; - - skiplistElement->_document = const_cast(document); - char const* ptr = skiplistElement->_document->getShapedJsonPtr(); // ONLY IN INDEX, PROTECTED by RUNTIME - - auto subObjects = SkiplistIndex_Subobjects(skiplistElement); - - size_t const n = _paths.size(); - - for (size_t j = 0; j < n; ++j) { - TRI_shape_pid_t shape = _paths[j]; - - // .......................................................................... - // Determine if document has that particular shape - // .......................................................................... - - TRI_shape_access_t const* acc = _collection->getShaper()->findAccessor(shapedJson._sid, shape); // ONLY IN INDEX, PROTECTED by RUNTIME - - if (acc == nullptr || acc->_resultSid == TRI_SHAPE_ILLEGAL) { - // OK, the document does not contain the attributed needed by - // the index, are we sparse? - subObjects[j]._sid = BasicShapes::TRI_SHAPE_SID_NULL; - - res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; - - if (_sparse) { - // no need to continue - return res; - } - continue; - } - - // .......................................................................... - // Extract the field - // .......................................................................... - - TRI_shaped_json_t shapedObject; - if (! TRI_ExecuteShapeAccessor(acc, &shapedJson, &shapedObject)) { - return TRI_ERROR_INTERNAL; - } - - if (shapedObject._sid == BasicShapes::TRI_SHAPE_SID_NULL) { - res = TRI_ERROR_ARANGO_INDEX_DOCUMENT_ATTRIBUTE_MISSING; - - if (_sparse) { - // no need to continue - return res; - } - } - - // ......................................................................... - // Store the field - // ......................................................................... - - TRI_FillShapedSub(&subObjects[j], &shapedObject, ptr); - } - - return res; -} - // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- diff --git a/arangod/Indexes/SkiplistIndex2.h b/arangod/Indexes/SkiplistIndex2.h index 0b0e75a30b..4bbb2cf25d 100644 --- a/arangod/Indexes/SkiplistIndex2.h +++ b/arangod/Indexes/SkiplistIndex2.h @@ -109,15 +109,6 @@ namespace triagens { return _unique; } -// ----------------------------------------------------------------------------- -// --SECTION-- private methods -// ----------------------------------------------------------------------------- - - private: - - int fillElement(TRI_skiplist_index_element_t*, - struct TRI_doc_mptr_t const*); - // ----------------------------------------------------------------------------- // --SECTION-- private variables // -----------------------------------------------------------------------------