From c708367d0b8e47f76cd7f10dd2d09ed336b7b236 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Thu, 26 Oct 2023 16:09:30 +0000 Subject: [PATCH] refactor: iterator & table handler --- .../toydb/src/tablet/tablet_catalog.cc | 8 - .../toydb/src/tablet/tablet_catalog.h | 34 ++--- hybridse/include/codec/row_list.h | 8 +- hybridse/include/vm/catalog.h | 55 +++---- hybridse/include/vm/mem_catalog.h | 132 ++++++----------- hybridse/include/vm/simple_catalog.h | 1 - hybridse/src/vm/catalog_wrapper.cc | 77 +++------- hybridse/src/vm/catalog_wrapper.h | 137 +++--------------- hybridse/src/vm/mem_catalog.cc | 25 +--- 9 files changed, 132 insertions(+), 345 deletions(-) diff --git a/hybridse/examples/toydb/src/tablet/tablet_catalog.cc b/hybridse/examples/toydb/src/tablet/tablet_catalog.cc index 71c2f34f407..81764df9da6 100644 --- a/hybridse/examples/toydb/src/tablet/tablet_catalog.cc +++ b/hybridse/examples/toydb/src/tablet/tablet_catalog.cc @@ -19,7 +19,6 @@ #include #include #include -#include "codec/list_iterator_codec.h" #include "glog/logging.h" #include "storage/table_iterator.h" @@ -99,13 +98,6 @@ bool TabletTableHandler::Init() { return true; } -std::unique_ptr TabletTableHandler::GetIterator() { - std::unique_ptr it( - new storage::FullTableIterator(table_->GetSegments(), - table_->GetSegCnt(), table_)); - return std::move(it); -} - std::unique_ptr TabletTableHandler::GetWindowIterator( const std::string& idx_name) { auto iter = index_hint_.find(idx_name); diff --git a/hybridse/examples/toydb/src/tablet/tablet_catalog.h b/hybridse/examples/toydb/src/tablet/tablet_catalog.h index dd5bea22c51..7302699c5cf 100644 --- a/hybridse/examples/toydb/src/tablet/tablet_catalog.h +++ b/hybridse/examples/toydb/src/tablet/tablet_catalog.h @@ -21,7 +21,6 @@ #include #include #include -#include "base/spin_lock.h" #include "storage/table_impl.h" #include "vm/catalog.h" @@ -77,7 +76,7 @@ class TabletSegmentHandler : public TableHandler { std::string key_; }; -class TabletPartitionHandler +class TabletPartitionHandler final : public PartitionHandler, public std::enable_shared_from_this { public: @@ -89,6 +88,8 @@ class TabletPartitionHandler ~TabletPartitionHandler() {} + RowIterator* GetRawIterator() override { return nullptr; } + const OrderType GetOrderType() const override { return OrderType::kDescOrder; } const vm::Schema* GetSchema() override { return table_handler_->GetSchema(); } @@ -118,7 +119,7 @@ class TabletPartitionHandler vm::IndexHint index_hint_; }; -class TabletTableHandler +class TabletTableHandler final : public vm::TableHandler, public std::enable_shared_from_this { public: @@ -134,26 +135,23 @@ class TabletTableHandler bool Init(); - inline const vm::Schema* GetSchema() { return &schema_; } + const vm::Schema* GetSchema() override { return &schema_; } - inline const std::string& GetName() { return name_; } + const std::string& GetName() override { return name_; } - inline const std::string& GetDatabase() { return db_; } + const std::string& GetDatabase() override { return db_; } - inline const vm::Types& GetTypes() { return types_; } + const vm::Types& GetTypes() override { return types_; } - inline const vm::IndexHint& GetIndex() { return index_hint_; } + const vm::IndexHint& GetIndex() override { return index_hint_; } const Row Get(int32_t pos); - inline std::shared_ptr GetTable() { return table_; } - std::unique_ptr GetIterator(); + std::shared_ptr GetTable() { return table_; } RowIterator* GetRawIterator() override; - std::unique_ptr GetWindowIterator( - const std::string& idx_name); + std::unique_ptr GetWindowIterator(const std::string& idx_name) override; - virtual std::shared_ptr GetPartition( - const std::string& index_name) { + std::shared_ptr GetPartition(const std::string& index_name) override { if (index_hint_.find(index_name) == index_hint_.cend()) { LOG(WARNING) << "fail to get partition for tablet table handler, index name " @@ -166,12 +164,12 @@ class TabletTableHandler const std::string GetHandlerTypeName() override { return "TabletTableHandler"; } - virtual std::shared_ptr GetTablet( - const std::string& index_name, const std::string& pk) { + std::shared_ptr GetTablet(const std::string& index_name, + const std::string& pk) override { return tablet_; } - virtual std::shared_ptr GetTablet( - const std::string& index_name, const std::vector& pks) { + std::shared_ptr GetTablet(const std::string& index_name, + const std::vector& pks) override { return tablet_; } diff --git a/hybridse/include/codec/row_list.h b/hybridse/include/codec/row_list.h index cfc83fae6a1..f601b207b9c 100644 --- a/hybridse/include/codec/row_list.h +++ b/hybridse/include/codec/row_list.h @@ -65,7 +65,13 @@ class ListV { ListV() {} virtual ~ListV() {} /// \brief Return the const iterator - virtual std::unique_ptr> GetIterator() = 0; + virtual std::unique_ptr> GetIterator() { + auto raw = GetRawIterator(); + if (raw == nullptr) { + return {}; + } + return std::unique_ptr>(raw); + } /// \brief Return the const iterator raw pointer virtual ConstIterator *GetRawIterator() = 0; diff --git a/hybridse/include/vm/catalog.h b/hybridse/include/vm/catalog.h index 70a422f8924..68139d1d930 100644 --- a/hybridse/include/vm/catalog.h +++ b/hybridse/include/vm/catalog.h @@ -225,8 +225,7 @@ class TableHandler : public DataHandler { /// Return WindowIterator /// so that user can use it to iterate datasets segment by segment. - virtual std::unique_ptr GetWindowIterator( - const std::string& idx_name) = 0; + virtual std::unique_ptr GetWindowIterator(const std::string& idx_name) { return nullptr; } /// Return the HandlerType of the dataset. /// Return HandlerType::kTableHandler by default @@ -255,8 +254,7 @@ class TableHandler : public DataHandler { /// Return Tablet binding to specify index and keys. /// Return `null` by default. - virtual std::shared_ptr GetTablet( - const std::string& index_name, const std::vector& pks) { + virtual std::shared_ptr GetTablet(const std::string& index_name, const std::vector& pks) { return std::shared_ptr(); } }; @@ -287,27 +285,19 @@ class ErrorTableHandler : public TableHandler { /// Return empty column Types. const Types& GetTypes() override { return types_; } /// Return empty table Schema. - inline const Schema* GetSchema() override { return schema_; } + const Schema* GetSchema() override { return schema_; } /// Return empty table name - inline const std::string& GetName() override { return table_name_; } + const std::string& GetName() override { return table_name_; } /// Return empty indexn information - inline const IndexHint& GetIndex() override { return index_hint_; } + const IndexHint& GetIndex() override { return index_hint_; } /// Return name of database - inline const std::string& GetDatabase() override { return db_; } + const std::string& GetDatabase() override { return db_; } /// Return null iterator - std::unique_ptr GetIterator() { - return std::unique_ptr(); - } - /// Return null iterator - RowIterator* GetRawIterator() { return nullptr; } - /// Return null window iterator - std::unique_ptr GetWindowIterator( - const std::string& idx_name) { - return std::unique_ptr(); - } + RowIterator* GetRawIterator() override { return nullptr; } + /// Return empty row - virtual Row At(uint64_t pos) { return Row(); } + Row At(uint64_t pos) override { return Row(); } /// Return 0 const uint64_t GetCount() override { return 0; } @@ -318,7 +308,7 @@ class ErrorTableHandler : public TableHandler { } /// Return status - virtual base::Status GetStatus() { return status_; } + base::Status GetStatus() override { return status_; } protected: base::Status status_; @@ -341,16 +331,11 @@ class PartitionHandler : public TableHandler { PartitionHandler() : TableHandler() {} ~PartitionHandler() {} - /// Return the iterator of row iterator. - /// Return null by default - virtual std::unique_ptr GetIterator() { - return std::unique_ptr(); - } - /// Return the iterator of row iterator - /// Return null by default + // Return the iterator of row iterator + // Return null by default RowIterator* GetRawIterator() { return nullptr; } - virtual std::unique_ptr GetWindowIterator( - const std::string& idx_name) { + + std::unique_ptr GetWindowIterator(const std::string& idx_name) override { return std::unique_ptr(); } @@ -362,18 +347,15 @@ class PartitionHandler : public TableHandler { const HandlerType GetHandlerType() override { return kPartitionHandler; } /// Return empty row, cause partition dataset does not support At operation. - virtual Row At(uint64_t pos) { return Row(); } + // virtual Row At(uint64_t pos) { return Row(); } /// Return Return table handler of specific segment binding to given key. /// Return `null` by default. - virtual std::shared_ptr GetSegment(const std::string& key) { - return std::shared_ptr(); - } + virtual std::shared_ptr GetSegment(const std::string& key) = 0; /// Return a sequence of table handles of specify segments binding to given /// keys set. - virtual std::vector> GetSegments( - const std::vector& keys) { + virtual std::vector> GetSegments(const std::vector& keys) { std::vector> segments; for (auto key : keys) { segments.push_back(GetSegment(key)); @@ -384,9 +366,6 @@ class PartitionHandler : public TableHandler { const std::string GetHandlerTypeName() override { return "PartitionHandler"; } - /// Return order type of the dataset, - /// and return kNoneOrder by default. - const OrderType GetOrderType() const { return kNoneOrder; } }; /// \brief A wrapper of table handler which is used as a asynchronous row diff --git a/hybridse/include/vm/mem_catalog.h b/hybridse/include/vm/mem_catalog.h index dffb17a8af1..6237edd1d43 100644 --- a/hybridse/include/vm/mem_catalog.h +++ b/hybridse/include/vm/mem_catalog.h @@ -64,11 +64,11 @@ class MemTimeTableIterator : public RowIterator { MemTimeTableIterator(const MemTimeTable* table, const vm::Schema* schema, int32_t start, int32_t end); ~MemTimeTableIterator(); - void Seek(const uint64_t& ts); - void SeekToFirst(); - const uint64_t& GetKey() const; - void Next(); - bool Valid() const; + void Seek(const uint64_t& ts) override; + void SeekToFirst() override; + const uint64_t& GetKey() const override; + void Next() override; + bool Valid() const override; const Row& GetValue() override; bool IsSeekable() const override; @@ -86,12 +86,12 @@ class MemTableIterator : public RowIterator { MemTableIterator(const MemTable* table, const vm::Schema* schema, int32_t start, int32_t end); ~MemTableIterator(); - void Seek(const uint64_t& ts); - void SeekToFirst(); - const uint64_t& GetKey() const; - const Row& GetValue(); - void Next(); - bool Valid() const; + void Seek(const uint64_t& ts) override; + void SeekToFirst() override; + const uint64_t& GetKey() const override; + const Row& GetValue() override; + void Next() override; + bool Valid() const override; bool IsSeekable() const override; private: @@ -113,7 +113,6 @@ class MemWindowIterator : public WindowIterator { void SeekToFirst(); void Next(); bool Valid(); - std::unique_ptr GetValue(); RowIterator* GetRawValue(); const Row GetKey(); @@ -155,24 +154,21 @@ class MemTableHandler : public TableHandler { ~MemTableHandler() override; const Types& GetTypes() override { return types_; } - inline const Schema* GetSchema() { return schema_; } - inline const std::string& GetName() { return table_name_; } - inline const IndexHint& GetIndex() { return index_hint_; } - inline const std::string& GetDatabase() { return db_; } + const Schema* GetSchema() override { return schema_; } + const std::string& GetName() override { return table_name_; } + const IndexHint& GetIndex() override { return index_hint_; } + const std::string& GetDatabase() override { return db_; } - std::unique_ptr GetIterator() override; RowIterator* GetRawIterator() override; - std::unique_ptr GetWindowIterator( - const std::string& idx_name); void AddRow(const Row& row); void Reverse(); - virtual const uint64_t GetCount() { return table_.size(); } - virtual Row At(uint64_t pos) { + const uint64_t GetCount() override { return table_.size(); } + Row At(uint64_t pos) override { return pos < table_.size() ? table_.at(pos) : Row(); } - const OrderType GetOrderType() const { return order_type_; } + const OrderType GetOrderType() const override { return order_type_; } void SetOrderType(const OrderType order_type) { order_type_ = order_type; } const std::string GetHandlerTypeName() override { return "MemTableHandler"; @@ -198,14 +194,11 @@ class MemTimeTableHandler : public TableHandler { const Schema* schema); const Types& GetTypes() override; ~MemTimeTableHandler() override; - inline const Schema* GetSchema() { return schema_; } - inline const std::string& GetName() { return table_name_; } - inline const IndexHint& GetIndex() { return index_hint_; } - std::unique_ptr GetIterator(); - RowIterator* GetRawIterator(); - inline const std::string& GetDatabase() { return db_; } - std::unique_ptr GetWindowIterator( - const std::string& idx_name); + const Schema* GetSchema() override { return schema_; } + const std::string& GetName() override { return table_name_; } + const IndexHint& GetIndex() override { return index_hint_; } + RowIterator* GetRawIterator() override; + const std::string& GetDatabase() override { return db_; } void AddRow(const uint64_t key, const Row& v); void AddFrontRow(const uint64_t key, const Row& v); void PopBackRow(); @@ -218,12 +211,12 @@ class MemTimeTableHandler : public TableHandler { } void Sort(const bool is_asc); void Reverse(); - virtual const uint64_t GetCount() { return table_.size(); } - virtual Row At(uint64_t pos) { + const uint64_t GetCount() override { return table_.size(); } + Row At(uint64_t pos) override { return pos < table_.size() ? table_.at(pos).second : Row(); } void SetOrderType(const OrderType order_type) { order_type_ = order_type; } - const OrderType GetOrderType() const { return order_type_; } + const OrderType GetOrderType() const override { return order_type_; } const std::string GetHandlerTypeName() override { return "MemTimeTableHandler"; } @@ -252,21 +245,11 @@ class Window : public MemTimeTableHandler { return std::make_unique(&table_, schema_); } - RowIterator* GetRawIterator() { - return new vm::MemTimeTableIterator(&table_, schema_); - } + RowIterator* GetRawIterator() override { return new vm::MemTimeTableIterator(&table_, schema_); } virtual bool BufferData(uint64_t key, const Row& row) = 0; virtual void PopBackData() { PopBackRow(); } virtual void PopFrontData() = 0; - virtual const uint64_t GetCount() { return table_.size(); } - virtual Row At(uint64_t pos) { - if (pos >= table_.size()) { - return Row(); - } else { - return table_[pos].second; - } - } const std::string GetHandlerTypeName() override { return "Window"; } bool instance_not_in_window() const { return instance_not_in_window_; } @@ -320,7 +303,7 @@ class WindowRange { return WindowRange(Window::kFrameRowsMergeRowsRange, start_offset, 0, rows_preceding, max_size); } - inline const WindowPositionStatus GetWindowPositionStatus( + const WindowPositionStatus GetWindowPositionStatus( bool out_of_rows, bool before_window, bool exceed_window) const { switch (frame_type_) { case Window::WindowFrameType::kFrameRows: @@ -529,7 +512,7 @@ class CurrentHistoryWindow : public HistoryWindow { void PopFrontData() override { PopFrontRow(); } - bool BufferData(uint64_t key, const Row& row) { + bool BufferData(uint64_t key, const Row& row) override { if (!table_.empty() && GetFrontRow().first > key) { DLOG(WARNING) << "Fail BufferData: buffer key less than latest key"; return false; @@ -558,34 +541,25 @@ class MemSegmentHandler : public TableHandler { virtual ~MemSegmentHandler() {} - inline const vm::Schema* GetSchema() { + const vm::Schema* GetSchema() override { return partition_hander_->GetSchema(); } - inline const std::string& GetName() { return partition_hander_->GetName(); } + const std::string& GetName() override { return partition_hander_->GetName(); } - inline const std::string& GetDatabase() { + const std::string& GetDatabase() override { return partition_hander_->GetDatabase(); } - inline const vm::Types& GetTypes() { return partition_hander_->GetTypes(); } + const vm::Types& GetTypes() override { return partition_hander_->GetTypes(); } - inline const vm::IndexHint& GetIndex() { + const vm::IndexHint& GetIndex() override { return partition_hander_->GetIndex(); } - const OrderType GetOrderType() const { + const OrderType GetOrderType() const override { return partition_hander_->GetOrderType(); } - std::unique_ptr GetIterator() { - auto iter = partition_hander_->GetWindowIterator(); - if (iter) { - iter->Seek(key_); - return iter->Valid() ? iter->GetValue() - : std::unique_ptr(); - } - return std::unique_ptr(); - } RowIterator* GetRawIterator() override { auto iter = partition_hander_->GetWindowIterator(); if (iter) { @@ -594,12 +568,11 @@ class MemSegmentHandler : public TableHandler { } return nullptr; } - std::unique_ptr GetWindowIterator( - const std::string& idx_name) { + std::unique_ptr GetWindowIterator(const std::string& idx_name) override { LOG(WARNING) << "SegmentHandler can't support window iterator"; return std::unique_ptr(); } - virtual const uint64_t GetCount() { + const uint64_t GetCount() override { auto iter = GetIterator(); if (!iter) { return 0; @@ -632,9 +605,7 @@ class MemSegmentHandler : public TableHandler { std::string key_; }; -class MemPartitionHandler - : public PartitionHandler, - public std::enable_shared_from_this { +class MemPartitionHandler : public PartitionHandler, public std::enable_shared_from_this { public: MemPartitionHandler(); explicit MemPartitionHandler(const Schema* schema); @@ -647,18 +618,19 @@ class MemPartitionHandler const Schema* GetSchema() override; const std::string& GetName() override; const std::string& GetDatabase() override; - virtual std::unique_ptr GetWindowIterator(); + RowIterator* GetRawIterator() override { return nullptr; } + std::unique_ptr GetWindowIterator() override; bool AddRow(const std::string& key, uint64_t ts, const Row& row); void Sort(const bool is_asc); void Reverse(); void Print(); - virtual const uint64_t GetCount() { return partitions_.size(); } - virtual std::shared_ptr GetSegment(const std::string& key) { + const uint64_t GetCount() override { return partitions_.size(); } + std::shared_ptr GetSegment(const std::string& key) override { return std::shared_ptr( new MemSegmentHandler(shared_from_this(), key)); } void SetOrderType(const OrderType order_type) { order_type_ = order_type; } - const OrderType GetOrderType() const { return order_type_; } + const OrderType GetOrderType() const override { return order_type_; } const std::string GetHandlerTypeName() override { return "MemPartitionHandler"; } @@ -691,12 +663,6 @@ class ConcatTableHandler : public MemTimeTableHandler { status_ = SyncValue(); return MemTimeTableHandler::At(pos); } - std::unique_ptr GetIterator() override { - if (status_.isRunning()) { - status_ = SyncValue(); - } - return MemTimeTableHandler::GetIterator(); - } RowIterator* GetRawIterator() override { if (status_.isRunning()) { status_ = SyncValue(); @@ -756,11 +722,11 @@ class MemCatalog : public Catalog { bool Init(); - std::shared_ptr GetDatabase(const std::string& db) { + std::shared_ptr GetDatabase(const std::string& db) override { return dbs_[db]; } std::shared_ptr GetTable(const std::string& db, - const std::string& table_name) { + const std::string& table_name) override { return tables_[db][table_name]; } bool IndexSupport() override { return true; } @@ -782,17 +748,11 @@ class RequestUnionTableHandler : public TableHandler { : request_ts_(request_ts), request_row_(request_row), window_(window) {} ~RequestUnionTableHandler() {} - std::unique_ptr GetIterator() override { - return std::unique_ptr(GetRawIterator()); - } RowIterator* GetRawIterator() override; const Types& GetTypes() override { return window_->GetTypes(); } const IndexHint& GetIndex() override { return window_->GetIndex(); } - std::unique_ptr GetWindowIterator(const std::string&) { - return nullptr; - } - const OrderType GetOrderType() const { return window_->GetOrderType(); } + const OrderType GetOrderType() const override { return window_->GetOrderType(); } const Schema* GetSchema() override { return window_->GetSchema(); } const std::string& GetName() override { return window_->GetName(); } const std::string& GetDatabase() override { return window_->GetDatabase(); } diff --git a/hybridse/include/vm/simple_catalog.h b/hybridse/include/vm/simple_catalog.h index 1e1cd78a2f6..fd7c2f3b952 100644 --- a/hybridse/include/vm/simple_catalog.h +++ b/hybridse/include/vm/simple_catalog.h @@ -22,7 +22,6 @@ #include #include -#include "glog/logging.h" #include "proto/fe_type.pb.h" #include "vm/catalog.h" #include "vm/mem_catalog.h" diff --git a/hybridse/src/vm/catalog_wrapper.cc b/hybridse/src/vm/catalog_wrapper.cc index b10c6f1c55b..109ba0edcf5 100644 --- a/hybridse/src/vm/catalog_wrapper.cc +++ b/hybridse/src/vm/catalog_wrapper.cc @@ -110,20 +110,12 @@ std::shared_ptr LazyLastJoinTableHandler::GetPartition(const s new LazyLastJoinPartitionHandler(left_->GetPartition(index_name), right_, parameter_, join_)); } -std::unique_ptr LazyLastJoinTableHandler::GetIterator() { +codec::RowIterator* LazyLastJoinPartitionHandler::GetRawIterator() { auto iter = left_->GetIterator(); if (!iter) { - return std::unique_ptr(); - } - - return std::unique_ptr(new LazyLastJoinIterator(std::move(iter), right_, parameter_, join_)); -} -std::unique_ptr LazyLastJoinPartitionHandler::GetIterator() { - auto iter = left_->GetIterator(); - if (!iter) { - return std::unique_ptr(); + return nullptr; } - return std::unique_ptr(new LazyLastJoinIterator(std::move(iter), right_, parameter_, join_)); + return new LazyLastJoinIterator(std::move(iter), right_, parameter_, join_); } std::unique_ptr LazyLastJoinPartitionHandler::GetWindowIterator() { @@ -140,23 +132,21 @@ const Row& LazyLastJoinIterator::GetValue() { return value_; } -std::unique_ptr LazyLastJoinTableHandler::GetWindowIterator(const std::string& idx_name) { - return nullptr; +codec::RowIterator* LazyLastJoinTableHandler::GetRawIterator() { + auto iter = left_->GetIterator(); + if (!iter) { + return nullptr; + } + + return new LazyLastJoinIterator(std::move(iter), right_, parameter_, join_); } LazyLastJoinWindowIterator::LazyLastJoinWindowIterator(std::unique_ptr&& iter, std::shared_ptr right, const Row& param, std::shared_ptr join) : left_(std::move(iter)), right_(right), parameter_(param), join_(join) {} -std::unique_ptr LazyLastJoinWindowIterator::GetValue() { - auto iter = left_->GetValue(); - if (!iter) { - return std::unique_ptr(); - } - return std::unique_ptr(new LazyLastJoinIterator(std::move(iter), right_, parameter_, join_)); -} -RowIterator* LazyLastJoinWindowIterator::GetRawValue() { +codec::RowIterator* LazyLastJoinWindowIterator::GetRawValue() { auto iter = left_->GetValue(); if (!iter) { return nullptr; @@ -181,14 +171,6 @@ RowIterator* ConcatPartitionHandler::GetRawIterator() { return new ConcatIterator(std::move(li), left_slices_, std::move(ri), right_slices_); } -std::unique_ptr ConcatPartitionHandler::GetIterator() { - auto p = GetRawIterator(); - if (p == nullptr) { - return {}; - } - return std::unique_ptr(p); -} - std::unique_ptr LazyRequestUnionPartitionHandler::GetWindowIterator() { auto w = left_->GetWindowIterator(); if (!w) { @@ -202,14 +184,12 @@ std::shared_ptr LazyRequestUnionPartitionHandler::GetSegment(const return nullptr; } -std::unique_ptr LazyRequestUnionPartitionHandler::GetIterator() { - return std::unique_ptr(GetRawIterator()); -} const IndexHint& LazyRequestUnionPartitionHandler::GetIndex() { return left_->GetIndex(); } const Types& LazyRequestUnionPartitionHandler::GetTypes() { return left_->GetTypes(); } -base::ConstIterator* LazyRequestUnionPartitionHandler::GetRawIterator() { return nullptr; } +codec::RowIterator* LazyRequestUnionPartitionHandler::GetRawIterator() { return nullptr; } + bool LazyAggIterator::Valid() const { return it_->Valid(); } void LazyAggIterator::Next() { it_->Next(); } const uint64_t& LazyAggIterator::GetKey() const { return it_->GetKey(); } @@ -229,14 +209,7 @@ const Row& LazyAggIterator::GetValue() { void LazyAggIterator::Seek(const uint64_t& key) { it_->Seek(key); } void LazyAggIterator::SeekToFirst() { it_->SeekToFirst(); } -std::unique_ptr LazyAggTableHandler::GetIterator() { - auto* it = GetRawIterator(); - if (it == nullptr) { - return {}; - } - return std::unique_ptr(it); -} -std::unique_ptr LazyAggTableHandler::GetWindowIterator(const std::string& idx_name) { return nullptr; } + base::ConstIterator* LazyAggTableHandler::GetRawIterator() { auto it = left_->GetIterator(); if (!it) { @@ -244,7 +217,7 @@ base::ConstIterator* LazyAggTableHandler::GetRawIterator() { } return new LazyAggIterator(std::move(it), func_, agg_gen_, parameter_); } -std::shared_ptr LazyAggTableHandler::GetPartition(const std::string& index_name) { return nullptr; } + const Types& LazyAggTableHandler::GetTypes() { return left_->GetTypes(); } const IndexHint& LazyAggTableHandler::GetIndex() { return left_->GetIndex(); } const Schema* LazyAggTableHandler::GetSchema() { return nullptr; } @@ -255,11 +228,12 @@ std::shared_ptr LazyAggPartitionHandler::GetSegment(const std::str return std::shared_ptr(new LazyAggTableHandler(seg, input_->Func(), agg_gen_, parameter_)); } const std::string LazyAggPartitionHandler::GetHandlerTypeName() { return "LazyLastJoinPartitionHandler"; } -std::unique_ptr LazyAggPartitionHandler::GetIterator() { + +base::ConstIterator* LazyAggPartitionHandler::GetRawIterator() { auto it = input_->Left()->GetIterator(); - return std::unique_ptr(new LazyAggIterator(std::move(it), input_->Func(), agg_gen_, parameter_)); + return new LazyAggIterator(std::move(it), input_->Func(), agg_gen_, parameter_); } -base::ConstIterator* LazyAggPartitionHandler::GetRawIterator() { return nullptr; } + bool ConcatIterator::Valid() const { return left_ && left_->Valid(); } void ConcatIterator::Next() { left_->Next(); @@ -288,13 +262,6 @@ void ConcatIterator::SeekToFirst() { right_->SeekToFirst(); } } -std::unique_ptr SimpleConcatTableHandler::GetIterator() { - auto p = GetRawIterator(); - if (p == nullptr) { - return {}; - } - return std::unique_ptr(p); -} RowIterator* SimpleConcatTableHandler::GetRawIterator() { auto li = left_->GetIterator(); if (!li) { @@ -303,13 +270,7 @@ RowIterator* SimpleConcatTableHandler::GetRawIterator() { auto ri = right_->GetIterator(); return new ConcatIterator(std::move(li), left_slices_, std::move(ri), right_slices_); } -std::unique_ptr SimpleConcatTableHandler::GetWindowIterator(const std::string& idx_name) { - return nullptr; -} std::unique_ptr ConcatPartitionHandler::GetWindowIterator() { return nullptr; } -std::unique_ptr ConcatPartitionHandler::GetWindowIterator(const std::string& idx_name) { - return nullptr; -} std::unique_ptr LazyAggPartitionHandler::GetWindowIterator() { auto w = input_->Left()->GetWindowIterator(); diff --git a/hybridse/src/vm/catalog_wrapper.h b/hybridse/src/vm/catalog_wrapper.h index 855eb1f703a..422e52f58f3 100644 --- a/hybridse/src/vm/catalog_wrapper.h +++ b/hybridse/src/vm/catalog_wrapper.h @@ -144,15 +144,6 @@ class WindowIteratorProjectWrapper : public WindowIterator { const ProjectFun* fun) : WindowIterator(), iter_(std::move(iter)), parameter_(parameter), fun_(fun) {} virtual ~WindowIteratorProjectWrapper() {} - std::unique_ptr GetValue() override { - auto iter = iter_->GetValue(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::unique_ptr( - new IteratorProjectWrapper(std::move(iter), parameter_, fun_)); - } - } RowIterator* GetRawValue() override { auto iter = iter_->GetValue(); if (!iter) { @@ -178,15 +169,6 @@ class WindowIteratorFilterWrapper : public WindowIterator { const PredicateFun* fun) : WindowIterator(), iter_(std::move(iter)), parameter_(parameter), fun_(fun) {} virtual ~WindowIteratorFilterWrapper() {} - std::unique_ptr GetValue() override { - auto iter = iter_->GetValue(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::unique_ptr( - new IteratorFilterWrapper(std::move(iter), parameter_, fun_)); - } - } RowIterator* GetRawValue() override { auto iter = iter_->GetValue(); if (!iter) { @@ -242,16 +224,7 @@ class PartitionProjectWrapper : public PartitionHandler { const std::string& GetDatabase() override { return partition_handler_->GetDatabase(); } - std::unique_ptr> GetIterator() override { - auto iter = partition_handler_->GetIterator(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::unique_ptr( - new IteratorProjectWrapper(std::move(iter), parameter_, fun_)); - } - } - base::ConstIterator* GetRawIterator() override; + codec::RowIterator* GetRawIterator() override; Row At(uint64_t pos) override { value_ = fun_->operator()(partition_handler_->At(pos), parameter_); return value_; @@ -305,15 +278,7 @@ class PartitionFilterWrapper : public PartitionHandler { const std::string& GetDatabase() override { return partition_handler_->GetDatabase(); } - std::unique_ptr> GetIterator() override { - auto iter = partition_handler_->GetIterator(); - if (!iter) { - return std::unique_ptr>(); - } else { - return std::unique_ptr( - new IteratorFilterWrapper(std::move(iter), parameter_, fun_)); - } - } + base::ConstIterator* GetRawIterator() override; std::shared_ptr GetSegment(const std::string& key) override; @@ -336,15 +301,6 @@ class TableProjectWrapper : public TableHandler { : TableHandler(), table_hander_(table_handler), parameter_(parameter), value_(), fun_(fun) {} virtual ~TableProjectWrapper() {} - std::unique_ptr GetIterator() override { - auto iter = table_hander_->GetIterator(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::unique_ptr( - new IteratorProjectWrapper(std::move(iter), parameter_, fun_)); - } - } const Types& GetTypes() override { return table_hander_->GetTypes(); } const IndexHint& GetIndex() override { return table_hander_->GetIndex(); } std::unique_ptr GetWindowIterator( @@ -391,14 +347,6 @@ class TableFilterWrapper : public TableHandler { : TableHandler(), table_hander_(table_handler), parameter_(parameter), fun_(fun) {} virtual ~TableFilterWrapper() {} - std::unique_ptr GetIterator() override { - auto iter = table_hander_->GetIterator(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::make_unique(std::move(iter), parameter_, fun_); - } - } const Types& GetTypes() override { return table_hander_->GetTypes(); } const IndexHint& GetIndex() override { return table_hander_->GetIndex(); } @@ -414,9 +362,13 @@ class TableFilterWrapper : public TableHandler { const Schema* GetSchema() override { return table_hander_->GetSchema(); } const std::string& GetName() override { return table_hander_->GetName(); } const std::string& GetDatabase() override { return table_hander_->GetDatabase(); } - base::ConstIterator* GetRawIterator() override { - return new IteratorFilterWrapper(static_cast>(table_hander_->GetRawIterator()), - parameter_, fun_); + codec::RowIterator* GetRawIterator() override { + auto iter = table_hander_->GetIterator(); + if (!iter) { + return nullptr; + } else { + return new IteratorFilterWrapper(std::move(iter), parameter_, fun_); + } } std::shared_ptr GetPartition(const std::string& index_name) override; const OrderType GetOrderType() const override { return table_hander_->GetOrderType(); } @@ -428,21 +380,12 @@ class TableFilterWrapper : public TableHandler { const PredicateFun* fun_; }; -class LimitTableHandler : public TableHandler { +class LimitTableHandler final : public TableHandler { public: explicit LimitTableHandler(std::shared_ptr table, int32_t limit) : TableHandler(), table_hander_(table), limit_(limit) {} virtual ~LimitTableHandler() {} - std::unique_ptr GetIterator() override { - auto iter = table_hander_->GetIterator(); - if (!iter) { - return std::unique_ptr(); - } else { - return std::make_unique(std::move(iter), limit_); - } - } - // FIXME(ace): do not use this, not implemented std::unique_ptr GetWindowIterator(const std::string& idx_name) override { LOG(ERROR) << "window iterator for LimitTableHandler is not implemented, don't use"; @@ -450,7 +393,12 @@ class LimitTableHandler : public TableHandler { } base::ConstIterator* GetRawIterator() override { - return new LimitIterator(static_cast>(table_hander_->GetRawIterator()), limit_); + auto iter = table_hander_->GetIterator(); + if (!iter) { + return nullptr; + } else { + return new LimitIterator(std::move(iter), limit_); + } } const Types& GetTypes() override { return table_hander_->GetTypes(); } @@ -604,10 +552,10 @@ class LazyLastJoinPartitionHandler final : public PartitionHandler { return "LazyLastJoinPartitionHandler"; } - std::unique_ptr GetIterator() override; - std::unique_ptr GetWindowIterator() override; + codec::RowIterator* GetRawIterator() override; + const IndexHint& GetIndex() override { return left_->GetIndex(); } // unimplemented @@ -618,11 +566,6 @@ class LazyLastJoinPartitionHandler final : public PartitionHandler { const std::string& GetName() override { return name_; } const std::string& GetDatabase() override { return db_; } - // unimplemented - base::ConstIterator* GetRawIterator() override { - return nullptr; - } - private: std::shared_ptr left_; std::shared_ptr right_; @@ -639,30 +582,17 @@ class LazyLastJoinTableHandler final : public TableHandler { const Row& param, std::shared_ptr join); ~LazyLastJoinTableHandler() override {} - std::unique_ptr GetIterator() override; - // unimplemented const Types& GetTypes() override { return left_->GetTypes(); } const IndexHint& GetIndex() override { return left_->GetIndex(); } - // unimplemented - std::unique_ptr GetWindowIterator(const std::string& idx_name) override; - // unimplemented const Schema* GetSchema() override { return nullptr; } const std::string& GetName() override { return name_; } const std::string& GetDatabase() override { return db_; } - base::ConstIterator* GetRawIterator() override { - // unimplemented - return nullptr; - } - - Row At(uint64_t pos) override { - // unimplemented - return value_; - } + codec::RowIterator* GetRawIterator() override; const uint64_t GetCount() override { return left_->GetCount(); } @@ -670,9 +600,7 @@ class LazyLastJoinTableHandler final : public TableHandler { const OrderType GetOrderType() const override { return left_->GetOrderType(); } - const std::string GetHandlerTypeName() override { - return "LazyLastJoinTableHandler"; - } + const std::string GetHandlerTypeName() override { return "LazyLastJoinTableHandler"; } private: std::shared_ptr left_; @@ -692,8 +620,7 @@ class LazyLastJoinWindowIterator final : public codec::WindowIterator { ~LazyLastJoinWindowIterator() override {} - std::unique_ptr GetValue() override; - RowIterator* GetRawValue() override; + codec::RowIterator* GetRawValue() override; void Seek(const std::string& key) override { left_->Seek(key); } void SeekToFirst() override { left_->SeekToFirst(); } @@ -772,7 +699,7 @@ class LazyRequestUnionPartitionHandler final : public PartitionHandler { const std::string GetHandlerTypeName() override { return "LazyRequestUnionPartitiontHandler"; } - std::unique_ptr GetIterator() override; + codec::RowIterator* GetRawIterator() override; const IndexHint& GetIndex() override; @@ -784,8 +711,6 @@ class LazyRequestUnionPartitionHandler final : public PartitionHandler { const std::string& GetName() override { return left_->GetName(); } const std::string& GetDatabase() override { return left_->GetDatabase(); } - base::ConstIterator* GetRawIterator() override; - auto Left() const { return left_; } auto Func() const { return func_; } @@ -832,20 +757,15 @@ class LazyAggTableHandler final : public TableHandler { } ~LazyAggTableHandler() override {} - std::unique_ptr GetIterator() override; + RowIterator* GetRawIterator() override; // unimplemented const Types& GetTypes() override; const IndexHint& GetIndex() override; - std::unique_ptr GetWindowIterator(const std::string& idx_name) override; const Schema* GetSchema() override; const std::string& GetName() override; const std::string& GetDatabase() override; - base::ConstIterator* GetRawIterator() override; - - std::shared_ptr GetPartition(const std::string& index_name) override; - private: std::shared_ptr left_; std::function(const Row&)> func_; @@ -887,7 +807,7 @@ class LazyAggPartitionHandler final : public PartitionHandler { const std::string GetHandlerTypeName() override; - std::unique_ptr GetIterator() override; + base::ConstIterator* GetRawIterator() override; std::unique_ptr GetWindowIterator() override; @@ -898,7 +818,6 @@ class LazyAggPartitionHandler final : public PartitionHandler { const Schema* GetSchema() override { return nullptr; } const std::string& GetName() override { return input_->GetName(); } const std::string& GetDatabase() override { return input_->GetDatabase(); } - base::ConstIterator* GetRawIterator() override; private: std::shared_ptr input_; @@ -942,12 +861,8 @@ class SimpleConcatTableHandler final : public TableHandler { : left_(left), left_slices_(left_slices), right_(right), right_slices_(right_slices) {} ~SimpleConcatTableHandler() override {} - std::unique_ptr GetIterator() override; - RowIterator* GetRawIterator() override; - std::unique_ptr GetWindowIterator(const std::string& idx_name) override; - const Types& GetTypes() override { return left_->GetTypes(); } const IndexHint& GetIndex() override { return left_->GetIndex(); } @@ -971,12 +886,8 @@ class ConcatPartitionHandler final : public PartitionHandler { : left_(left), left_slices_(left_slices), right_(right), right_slices_(right_slices) {} ~ConcatPartitionHandler() override {} - std::unique_ptr GetIterator() override; - RowIterator* GetRawIterator() override; - std::unique_ptr GetWindowIterator(const std::string& idx_name) override; - std::unique_ptr GetWindowIterator() override; std::shared_ptr GetSegment(const std::string& key) override; diff --git a/hybridse/src/vm/mem_catalog.cc b/hybridse/src/vm/mem_catalog.cc index 29a2e2791e4..f4f5897f10f 100644 --- a/hybridse/src/vm/mem_catalog.cc +++ b/hybridse/src/vm/mem_catalog.cc @@ -72,10 +72,6 @@ void MemWindowIterator::Seek(const std::string& key) { void MemWindowIterator::SeekToFirst() { iter_ = start_iter_; } void MemWindowIterator::Next() { iter_++; } bool MemWindowIterator::Valid() { return end_iter_ != iter_; } -std::unique_ptr MemWindowIterator::GetValue() { - return std::unique_ptr( - new MemTimeTableIterator(&(iter_->second), schema_)); -} RowIterator* MemWindowIterator::GetRawValue() { return new MemTimeTableIterator(&(iter_->second), schema_); @@ -114,12 +110,9 @@ MemTimeTableHandler::MemTimeTableHandler(const std::string& table_name, order_type_(kNoneOrder) {} MemTimeTableHandler::~MemTimeTableHandler() {} -std::unique_ptr MemTimeTableHandler::GetIterator() { - return std::make_unique(&table_, schema_); -} -std::unique_ptr MemTimeTableHandler::GetWindowIterator( - const std::string& idx_name) { - return std::unique_ptr(); + +RowIterator* MemTimeTableHandler::GetRawIterator() { + return new MemTimeTableIterator(&table_, schema_); } void MemTimeTableHandler::AddRow(const uint64_t key, const Row& row) { @@ -152,9 +145,6 @@ void MemTimeTableHandler::Reverse() { ? kDescOrder : kDescOrder == order_type_ ? kAscOrder : kNoneOrder; } -RowIterator* MemTimeTableHandler::GetRawIterator() { - return new MemTimeTableIterator(&table_, schema_); -} MemPartitionHandler::MemPartitionHandler() : PartitionHandler(), @@ -232,15 +222,6 @@ void MemPartitionHandler::Print() { } } -std::unique_ptr MemTableHandler::GetWindowIterator( - const std::string& idx_name) { - return std::unique_ptr(); -} -std::unique_ptr MemTableHandler::GetIterator() { - std::unique_ptr it( - new MemTableIterator(&table_, schema_)); - return std::move(it); -} RowIterator* MemTableHandler::GetRawIterator() { return new MemTableIterator(&table_, schema_); }