//////////////////////////////////////////////////////////////////////////////// /// 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 //////////////////////////////////////////////////////////////////////////////// #ifndef ARANGOD_MMFILES_PRIMARY_INDEX_H #define ARANGOD_MMFILES_PRIMARY_INDEX_H 1 #include "Basics/Common.h" #include "Containers/AssocUnique.h" #include "Indexes/IndexIterator.h" #include "MMFiles/MMFilesIndex.h" #include "MMFiles/MMFilesIndexElement.h" #include "MMFiles/MMFilesIndexLookupContext.h" #include "VocBase/voc-types.h" #include "VocBase/vocbase.h" #include #include #include namespace arangodb { class MMFilesPrimaryIndexEqIterator; class MMFilesPrimaryIndexInIterator; class MMFilesAllIndexIterator; class MMFilesAnyIndexIterator; struct MMFilesSimpleIndexElement; namespace transaction { class Methods; } struct MMFilesPrimaryIndexHelper { static inline uint64_t HashKey(uint8_t const* key) { return MMFilesSimpleIndexElement::hash(VPackSlice(key)); } static inline uint64_t HashElement(MMFilesSimpleIndexElement const& element, bool) { return element.hash(); } /// @brief determines if a key corresponds to an element inline bool IsEqualKeyElement(void* userData, uint8_t const* key, MMFilesSimpleIndexElement const& right) const { MMFilesIndexLookupContext* context = static_cast(userData); TRI_ASSERT(context != nullptr); try { VPackSlice tmp = right.slice(context); TRI_ASSERT(tmp.isString()); return VPackSlice(key).binaryEquals(tmp); } catch (...) { return false; } } /// @brief determines if two elements are equal inline bool IsEqualElementElement(void*, MMFilesSimpleIndexElement const& left, MMFilesSimpleIndexElement const& right) const { return (left.localDocumentId() == right.localDocumentId()); } inline bool IsEqualElementElementByKey(void* userData, MMFilesSimpleIndexElement const& left, MMFilesSimpleIndexElement const& right) const { if (left.hash() != right.hash()) { // TODO: check if we have many collisions here return false; } MMFilesIndexLookupContext* context = static_cast(userData); TRI_ASSERT(context != nullptr); VPackSlice l = left.slice(context); VPackSlice r = right.slice(context); TRI_ASSERT(l.isString()); TRI_ASSERT(r.isString()); return l.binaryEquals(r); } }; class MMFilesPrimaryIndex final : public MMFilesIndex { friend class MMFilesPrimaryIndexIterator; public: using ImplType = ::arangodb::containers::AssocUnique; MMFilesPrimaryIndex() = delete; explicit MMFilesPrimaryIndex(arangodb::LogicalCollection& collection); IndexType type() const override { return Index::TRI_IDX_TYPE_PRIMARY_INDEX; } char const* typeName() const override { return "primary"; } bool canBeDropped() const override { return false; } bool isSorted() const override { return false; } bool hasSelectivityEstimate() const override { return true; } double selectivityEstimate(arangodb::velocypack::StringRef const& = arangodb::velocypack::StringRef()) const override { return 1.0; } size_t size() const; size_t memory() const override; void toVelocyPack(VPackBuilder&, std::underlying_type::type) const override; void toVelocyPackFigures(VPackBuilder&) const override; Result insert(transaction::Methods& trx, LocalDocumentId const& documentId, velocypack::Slice const& doc, Index::OperationMode mode) override; Result remove(transaction::Methods& trx, LocalDocumentId const& documentId, arangodb::velocypack::Slice const& doc, Index::OperationMode mode) override; void load() override {} void unload() override; MMFilesSimpleIndexElement lookupKey(transaction::Methods*, VPackSlice const&) const; MMFilesSimpleIndexElement lookupKey(transaction::Methods*, VPackSlice const&, ManagedDocumentResult&) const; MMFilesSimpleIndexElement* lookupKeyRef(transaction::Methods*, VPackSlice const&) const; /// @brief a method to iterate over all elements in the index in /// a sequential order. /// Returns nullptr if all documents have been returned. /// Convention: position === 0 indicates a new start. /// DEPRECATED MMFilesSimpleIndexElement lookupSequential(transaction::Methods*, arangodb::containers::BucketPosition& position, uint64_t& total); /// @brief request an iterator over all elements in the index in /// a sequential order. IndexIterator* allIterator(transaction::Methods*) const; /// @brief request an iterator over all elements in the index in /// a random order. It is guaranteed that each element is found /// exactly once unless the collection is modified. IndexIterator* anyIterator(transaction::Methods*) const; /// @brief a method to iterate over all elements in the index in /// reversed sequential order. /// Returns nullptr if all documents have been returned. /// Convention: position === UINT64_MAX indicates a new start. /// DEPRECATED MMFilesSimpleIndexElement lookupSequentialReverse(transaction::Methods*, arangodb::containers::BucketPosition& position); Result insertKey(transaction::Methods*, LocalDocumentId const& documentId, arangodb::velocypack::Slice const&, OperationMode mode); Result insertKey(transaction::Methods*, LocalDocumentId const& documentId, arangodb::velocypack::Slice const&, ManagedDocumentResult&, OperationMode mode); Result removeKey(transaction::Methods*, LocalDocumentId const& documentId, arangodb::velocypack::Slice const&, OperationMode mode); Result removeKey(transaction::Methods*, LocalDocumentId const& documentId, arangodb::velocypack::Slice const&, ManagedDocumentResult&, OperationMode mode); int resize(transaction::Methods*, size_t); void invokeOnAllElements(std::function); void invokeOnAllElementsForRemoval(std::function); Index::FilterCosts supportsFilterCondition(std::vector> const& allIndexes, arangodb::aql::AstNode const* node, arangodb::aql::Variable const* reference, size_t itemsInIndex) const override; std::unique_ptr iteratorForCondition(transaction::Methods* trx, arangodb::aql::AstNode const* node, arangodb::aql::Variable const* reference, IndexIteratorOptions const& opts) override; arangodb::aql::AstNode* specializeCondition(arangodb::aql::AstNode* node, arangodb::aql::Variable const* reference) const override; private: /// @brief create the iterator, for a single attribute, IN operator std::unique_ptr createInIterator(transaction::Methods*, arangodb::aql::AstNode const*, arangodb::aql::AstNode const*) const; /// @brief create the iterator, for a single attribute, EQ operator std::unique_ptr createEqIterator(transaction::Methods*, arangodb::aql::AstNode const*, arangodb::aql::AstNode const*) const; /// @brief add a single value node to the iterator's keys void handleValNode(transaction::Methods* trx, VPackBuilder* keys, arangodb::aql::AstNode const* valNode, bool isId) const; MMFilesSimpleIndexElement buildKeyElement(LocalDocumentId const& documentId, arangodb::velocypack::Slice const&) const; private: /// @brief the actual index std::unique_ptr _primaryIndex; }; class MMFilesPrimaryIndexEqIterator final : public IndexIterator { public: MMFilesPrimaryIndexEqIterator(LogicalCollection* collection, transaction::Methods* trx, MMFilesPrimaryIndex const* index, std::unique_ptr keys); ~MMFilesPrimaryIndexEqIterator(); char const* typeName() const override { return "primary-index-eq-iterator"; } bool next(LocalDocumentIdCallback const& cb, size_t limit) override; bool nextDocument(DocumentCallback const& cb, size_t limit) override; void reset() override; private: MMFilesPrimaryIndex const* _index; std::unique_ptr _key; bool _done; }; class MMFilesPrimaryIndexInIterator final : public IndexIterator { public: MMFilesPrimaryIndexInIterator(LogicalCollection* collection, transaction::Methods* trx, MMFilesPrimaryIndex const* index, std::unique_ptr keys); ~MMFilesPrimaryIndexInIterator(); char const* typeName() const override { return "primary-index-in-iterator"; } bool next(LocalDocumentIdCallback const& cb, size_t limit) override; void reset() override; private: MMFilesPrimaryIndex const* _index; std::unique_ptr _keys; arangodb::velocypack::ArrayIterator _iterator; }; class MMFilesAllIndexIterator final : public IndexIterator { public: MMFilesAllIndexIterator(LogicalCollection* collection, transaction::Methods* trx, MMFilesPrimaryIndex const* index, MMFilesPrimaryIndex::ImplType const* indexImpl); ~MMFilesAllIndexIterator() = default; char const* typeName() const override { return "all-index-iterator"; } bool next(LocalDocumentIdCallback const& cb, size_t limit) override; bool nextDocument(DocumentCallback const& cb, size_t limit) override; void skip(uint64_t count, uint64_t& skipped) override; void reset() override; private: MMFilesPrimaryIndex::ImplType const* _index; arangodb::containers::BucketPosition _position; std::vector> _documentIds; uint64_t _total; }; class MMFilesAnyIndexIterator final : public IndexIterator { public: MMFilesAnyIndexIterator(LogicalCollection* collection, transaction::Methods* trx, MMFilesPrimaryIndex const* index, MMFilesPrimaryIndex::ImplType const* indexImpl); ~MMFilesAnyIndexIterator() = default; char const* typeName() const override { return "any-index-iterator"; } bool next(LocalDocumentIdCallback const& cb, size_t limit) override; void reset() override; private: MMFilesPrimaryIndex::ImplType const* _index; arangodb::containers::BucketPosition _initial; arangodb::containers::BucketPosition _position; uint64_t _step; uint64_t _total; }; } // namespace arangodb #endif