#pragma once #include #include "common/enums/extend_direction.h" #include "common/mask.h" #include "processor/operator/physical_operator.h" namespace lbug { namespace processor { class BaseSemiMasker; struct SemiMaskerLocalState { common::table_id_map_t> localMasksPerTable; common::SemiMask* singleTableRef = nullptr; void maskSingleTable(common::offset_t offset) const { singleTableRef->mask(offset); } void maskMultiTable(common::nodeID_t nodeID) const { DASSERT(localMasksPerTable.contains(nodeID.tableID)); localMasksPerTable.at(nodeID.tableID)->mask(nodeID.offset); } }; class SemiMaskerSharedState { public: explicit SemiMaskerSharedState( common::table_id_map_t> masksPerTable) : masksPerTable{std::move(masksPerTable)} {} SemiMaskerLocalState* appendLocalState(); void mergeToGlobal(); // Re-arm the state for another execution of a cached physical plan. Local states (and the // masks they fill) are recreated by every worker thread via appendLocalState(); keeping // entries from a previous execution would re-merge that execution's node offsets into the // global masks. void resetForReuse() { localInfos.clear(); } private: common::table_id_map_t> masksPerTable; std::vector> localInfos; std::mutex mtx; }; struct SemiMaskerPrintInfo final : OPPrintInfo { std::vector operatorNames; explicit SemiMaskerPrintInfo(std::vector operatorNames) : operatorNames{std::move(operatorNames)} {} std::string toString() const override; std::unique_ptr copy() const override { return std::unique_ptr(new SemiMaskerPrintInfo(*this)); } private: SemiMaskerPrintInfo(const SemiMaskerPrintInfo& other) : OPPrintInfo{other}, operatorNames{other.operatorNames} {} }; class BaseSemiMasker : public PhysicalOperator { static constexpr PhysicalOperatorType type_ = PhysicalOperatorType::SEMI_MASKER; protected: BaseSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : PhysicalOperator{type_, std::move(child), id, std::move(printInfo)}, keyPos{keyPos}, keyVector{nullptr}, sharedState{std::move(sharedState)}, localState{nullptr} {} void initGlobalStateInternal(ExecutionContext* /*context*/) override { // Runs once per execution, before worker threads call initLocalStateInternal(). Drop // local mask states left over from the previous execution of a cached plan. sharedState->resetForReuse(); } void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) override; void finalizeInternal(ExecutionContext* context) final; protected: DataPos keyPos; common::ValueVector* keyVector; std::shared_ptr sharedState; SemiMaskerLocalState* localState; }; class SingleTableSemiMasker final : public BaseSemiMasker { public: SingleTableSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : BaseSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo)} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, sharedState, children[0]->copy(), id, printInfo->copy()); } }; class MultiTableSemiMasker final : public BaseSemiMasker { public: MultiTableSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : BaseSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo)} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, sharedState, children[0]->copy(), id, printInfo->copy()); } }; class NodeIDsSemiMask : public BaseSemiMasker { protected: NodeIDsSemiMask(DataPos keyPos, DataPos srcNodeIDPos, DataPos dstNodeIDPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : BaseSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo)}, srcNodeIDPos{srcNodeIDPos}, dstNodeIDPos{dstNodeIDPos} {} void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) final; protected: DataPos srcNodeIDPos; DataPos dstNodeIDPos; common::ValueVector* srcNodeIDVector = nullptr; common::ValueVector* dstNodeIDVector = nullptr; }; class NodeIDsSingleTableSemiMasker final : public NodeIDsSemiMask { public: NodeIDsSingleTableSemiMasker(DataPos keyPos, DataPos srcNodeIDPos, DataPos dstNodeIDPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : NodeIDsSemiMask{keyPos, srcNodeIDPos, dstNodeIDPos, std::move(sharedState), std::move(child), id, std::move(printInfo)} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, srcNodeIDPos, dstNodeIDPos, sharedState, children[0]->copy(), id, printInfo->copy()); } }; class NodeIDsMultipleTableSemiMasker final : public NodeIDsSemiMask { public: NodeIDsMultipleTableSemiMasker(DataPos keyPos, DataPos srcNodeIDPos, DataPos dstNodeIDPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : NodeIDsSemiMask{keyPos, srcNodeIDPos, dstNodeIDPos, std::move(sharedState), std::move(child), id, std::move(printInfo)} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, srcNodeIDPos, dstNodeIDPos, sharedState, children[0]->copy(), id, printInfo->copy()); } }; class PathSemiMasker : public BaseSemiMasker { protected: PathSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo, common::ExtendDirection direction) : BaseSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo)}, direction{direction} {} void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) final; protected: common::ValueVector* pathRelsVector = nullptr; common::ValueVector* pathRelsSrcIDDataVector = nullptr; common::ValueVector* pathRelsDstIDDataVector = nullptr; common::ExtendDirection direction; }; class PathSingleTableSemiMasker final : public PathSemiMasker { public: PathSingleTableSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo, common::ExtendDirection direction) : PathSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo), direction} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, sharedState, children[0]->copy(), id, printInfo->copy(), direction); } }; class PathMultipleTableSemiMasker final : public PathSemiMasker { public: PathMultipleTableSemiMasker(DataPos keyPos, std::shared_ptr sharedState, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo, common::ExtendDirection direction) : PathSemiMasker{keyPos, std::move(sharedState), std::move(child), id, std::move(printInfo), direction} {} bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return std::make_unique(keyPos, sharedState, children[0]->copy(), id, printInfo->copy(), direction); } }; } // namespace processor } // namespace lbug