Skip to content

Commit

Permalink
refactor: iterator & table handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed Oct 27, 2023
1 parent b39cb17 commit c708367
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 345 deletions.
8 changes: 0 additions & 8 deletions hybridse/examples/toydb/src/tablet/tablet_catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <memory>
#include <string>
#include <utility>
#include "codec/list_iterator_codec.h"
#include "glog/logging.h"
#include "storage/table_iterator.h"

Expand Down Expand Up @@ -99,13 +98,6 @@ bool TabletTableHandler::Init() {
return true;
}

std::unique_ptr<RowIterator> TabletTableHandler::GetIterator() {
std::unique_ptr<storage::FullTableIterator> it(
new storage::FullTableIterator(table_->GetSegments(),
table_->GetSegCnt(), table_));
return std::move(it);
}

std::unique_ptr<WindowIterator> TabletTableHandler::GetWindowIterator(
const std::string& idx_name) {
auto iter = index_hint_.find(idx_name);
Expand Down
34 changes: 16 additions & 18 deletions hybridse/examples/toydb/src/tablet/tablet_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <memory>
#include <string>
#include <vector>
#include "base/spin_lock.h"
#include "storage/table_impl.h"
#include "vm/catalog.h"

Expand Down Expand Up @@ -77,7 +76,7 @@ class TabletSegmentHandler : public TableHandler {
std::string key_;
};

class TabletPartitionHandler
class TabletPartitionHandler final
: public PartitionHandler,
public std::enable_shared_from_this<PartitionHandler> {
public:
Expand All @@ -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(); }
Expand Down Expand Up @@ -118,7 +119,7 @@ class TabletPartitionHandler
vm::IndexHint index_hint_;
};

class TabletTableHandler
class TabletTableHandler final
: public vm::TableHandler,
public std::enable_shared_from_this<vm::TableHandler> {
public:
Expand All @@ -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<storage::Table> GetTable() { return table_; }
std::unique_ptr<RowIterator> GetIterator();
std::shared_ptr<storage::Table> GetTable() { return table_; }
RowIterator* GetRawIterator() override;
std::unique_ptr<codec::WindowIterator> GetWindowIterator(
const std::string& idx_name);
std::unique_ptr<codec::WindowIterator> GetWindowIterator(const std::string& idx_name) override;

virtual std::shared_ptr<PartitionHandler> GetPartition(
const std::string& index_name) {
std::shared_ptr<PartitionHandler> 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 "
Expand All @@ -166,12 +164,12 @@ class TabletTableHandler
const std::string GetHandlerTypeName() override {
return "TabletTableHandler";
}
virtual std::shared_ptr<hybridse::vm::Tablet> GetTablet(
const std::string& index_name, const std::string& pk) {
std::shared_ptr<hybridse::vm::Tablet> GetTablet(const std::string& index_name,
const std::string& pk) override {
return tablet_;
}
virtual std::shared_ptr<hybridse::vm::Tablet> GetTablet(
const std::string& index_name, const std::vector<std::string>& pks) {
std::shared_ptr<hybridse::vm::Tablet> GetTablet(const std::string& index_name,
const std::vector<std::string>& pks) override {
return tablet_;
}

Expand Down
8 changes: 7 additions & 1 deletion hybridse/include/codec/row_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ class ListV {
ListV() {}
virtual ~ListV() {}
/// \brief Return the const iterator
virtual std::unique_ptr<ConstIterator<uint64_t, V>> GetIterator() = 0;
virtual std::unique_ptr<ConstIterator<uint64_t, V>> GetIterator() {
auto raw = GetRawIterator();
if (raw == nullptr) {
return {};
}
return std::unique_ptr<ConstIterator<uint64_t, V>>(raw);
}

/// \brief Return the const iterator raw pointer
virtual ConstIterator<uint64_t, V> *GetRawIterator() = 0;
Expand Down
55 changes: 17 additions & 38 deletions hybridse/include/vm/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<WindowIterator> GetWindowIterator(
const std::string& idx_name) = 0;
virtual std::unique_ptr<WindowIterator> GetWindowIterator(const std::string& idx_name) { return nullptr; }

/// Return the HandlerType of the dataset.
/// Return HandlerType::kTableHandler by default
Expand Down Expand Up @@ -255,8 +254,7 @@ class TableHandler : public DataHandler {

/// Return Tablet binding to specify index and keys.
/// Return `null` by default.
virtual std::shared_ptr<Tablet> GetTablet(
const std::string& index_name, const std::vector<std::string>& pks) {
virtual std::shared_ptr<Tablet> GetTablet(const std::string& index_name, const std::vector<std::string>& pks) {
return std::shared_ptr<Tablet>();
}
};
Expand Down Expand Up @@ -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<RowIterator> GetIterator() {
return std::unique_ptr<RowIterator>();
}
/// Return null iterator
RowIterator* GetRawIterator() { return nullptr; }
/// Return null window iterator
std::unique_ptr<WindowIterator> GetWindowIterator(
const std::string& idx_name) {
return std::unique_ptr<WindowIterator>();
}
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; }
Expand All @@ -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_;
Expand All @@ -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<RowIterator> GetIterator() {
return std::unique_ptr<RowIterator>();
}
/// 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<WindowIterator> GetWindowIterator(
const std::string& idx_name) {

std::unique_ptr<WindowIterator> GetWindowIterator(const std::string& idx_name) override {
return std::unique_ptr<WindowIterator>();
}

Expand All @@ -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<TableHandler> GetSegment(const std::string& key) {
return std::shared_ptr<TableHandler>();
}
virtual std::shared_ptr<TableHandler> GetSegment(const std::string& key) = 0;

/// Return a sequence of table handles of specify segments binding to given
/// keys set.
virtual std::vector<std::shared_ptr<TableHandler>> GetSegments(
const std::vector<std::string>& keys) {
virtual std::vector<std::shared_ptr<TableHandler>> GetSegments(const std::vector<std::string>& keys) {
std::vector<std::shared_ptr<TableHandler>> segments;
for (auto key : keys) {
segments.push_back(GetSegment(key));
Expand All @@ -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
Expand Down
Loading

0 comments on commit c708367

Please sign in to comment.