Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cpp/src/common/tsblock/tuple_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class TupleDesc {
return column_list_[index];
}

FORCE_INLINE const ColumnSchema& operator[](uint32_t index) const {
ASSERT(index < column_list_.size());
return column_list_[index];
}

FORCE_INLINE ColumnSchema& get_column_schema(uint32_t index) {
ASSERT(index < column_list_.size());
return column_list_[index];
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/reader/block/device_ordered_tsblock_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ int DeviceOrderedTsBlockReader::has_next(bool& has_next) {
current_reader_ = new SingleDeviceTsBlockReader(
task, block_size_, metadata_querier_, tsfile_io_reader_,
time_filter_, field_filter_);
current_reader_->set_reusable_block(&reusable_block_);
if (current_reader_ == nullptr) {
return common::E_OOM;
}
current_reader_->set_reusable_block(&reusable_block_);
if (RET_FAIL(current_reader_->init(task, block_size_, time_filter_,
field_filter_, remaining_offset_,
remaining_limit_))) {
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/reader/block/single_device_tsblock_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace storage {
namespace {
const char* kTimeOnlyContextName = "__time_only_aligned_context__";

common::TsBlock* reusable_block_factory(void* ctx, common::TupleDesc& desc,
common::TsBlock* reusable_block_factory(void* ctx,
const common::TupleDesc& desc,
uint32_t max_rows) {
return static_cast<ReusableTsBlock*>(ctx)->acquire(desc, max_rows);
}
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/reader/block/single_device_tsblock_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class ReusableTsBlock {

// Returns a block matching `wanted` (time + value columns), reusing the
// previous buffers when the shape matches. The caller resets it before use.
common::TsBlock* acquire(common::TupleDesc& wanted, uint32_t max_rows) {
common::TsBlock* acquire(const common::TupleDesc& wanted,
uint32_t max_rows) {
bool rebuild = block_ == nullptr || max_rows_ != max_rows ||
desc_.get_column_count() != wanted.get_column_count();
if (!rebuild) {
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/reader/tsfile_series_scan_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class TsFileSeriesScanIterator {
// block plus all its per-column buffers for every device. The factory is
// handed the exact layout the block must have and may return a matching
// block that is reused across series; returning nullptr makes the iterator
// allocate a new one. The returned block must already be reset().
// allocate a new one. The iterator resets a returned block before use.
using TsBlockFactory = common::TsBlock* (*)(void* ctx,
common::TupleDesc& desc,
const common::TupleDesc& desc,
uint32_t max_block_rows);
void set_tsblock_factory(TsBlockFactory factory, void* ctx) {
tsblock_factory_ = factory;
Expand Down
62 changes: 47 additions & 15 deletions cpp/test/reader/block/reusable_tsblock_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ TEST(ReusableTsBlockTest, ReusesTheBlockForTheSameLayout) {
RowAppender rows(first);
for (int i = 0; i < 5; i++) {
ASSERT_TRUE(rows.add_row());
// Append every column of the layout: a partially written row would
// leave the block in a state the reader never produces.
int64_t t = i;
int64_t value = i * 2;
rows.append(0, reinterpret_cast<char*>(&t), sizeof(t));
rows.append(1, reinterpret_cast<char*>(&value), sizeof(value));
}
first->reset();
EXPECT_EQ(first->get_row_count(), 0u);
Expand Down Expand Up @@ -167,7 +171,8 @@ class ReusableTsBlockScanTest : public ::testing::Test {

// Reads every row of the table once, exactly as a device-ordered scan does
// for one device: the series scan iterator asks @reusable for its output
// block.
// block, and the shared multi-value context bulk-copies both value columns
// into appenders on a destination block.
std::vector<int64_t> scan(ReusableTsBlock& reusable) {
PageArena arena;
arena.init(512, MOD_TSFILE_READER);
Expand All @@ -181,22 +186,49 @@ class ReusableTsBlockScanTest : public ::testing::Test {
EXPECT_EQ(context.init(&task, names, nullptr, positions, arena, nullptr,
0, -1, &reusable),
common::E_OK);

TupleDesc tuple;
tuple.push_back(ColumnSchema("time", INT64, UNCOMPRESSED, PLAIN));
tuple.push_back(ColumnSchema("v", INT64, UNCOMPRESSED, PLAIN));
tuple.push_back(ColumnSchema("w", INT64, UNCOMPRESSED, PLAIN));
TsBlock output(&tuple, kRows);
EXPECT_EQ(output.init(), E_OK);
RowAppender rows(&output);
std::vector<std::unique_ptr<ColAppender>> owned;
std::vector<ColAppender*> appenders;
for (uint32_t c = 0; c < tuple.get_column_count(); ++c) {
owned.emplace_back(new ColAppender(c, &output));
appenders.push_back(owned.back().get());
}

std::vector<int64_t> times;
while (context.available_rows() > 0) {
int64_t time = 0;
EXPECT_EQ(context.get_current_time(time), common::E_OK);
char* value = nullptr;
uint32_t len = 0;
EXPECT_EQ(context.get_current_value(value, len), common::E_OK);
int64_t v = 0;
std::memcpy(&v, value, sizeof(v));
EXPECT_EQ(v, time - kBaseTime);
times.push_back(time);
const int moved = context.move_iter();
if (moved == common::E_NO_MORE_DATA) {
break;
while (true) {
const uint32_t available = context.available_rows();
if (available == 0) break;
output.reset();
for (auto* appender : appenders) appender->reset();
const int ret = context.bulk_copy_into(appenders, appenders[0],
&rows, available);
EXPECT_TRUE(ret == E_OK || ret == E_NO_MORE_DATA);
RowIterator iter(&output);
while (iter.has_next()) {
uint32_t len = 0;
bool is_null = false;
int64_t time = 0;
std::memcpy(&time, iter.read(0, &len, &is_null), sizeof(time));
EXPECT_FALSE(is_null);
int64_t v = 0;
std::memcpy(&v, iter.read(1, &len, &is_null), sizeof(v));
EXPECT_FALSE(is_null);
EXPECT_EQ(v, time - kBaseTime);
int64_t w = 0;
std::memcpy(&w, iter.read(2, &len, &is_null), sizeof(w));
EXPECT_FALSE(is_null);
EXPECT_EQ(w, (time - kBaseTime) * 2);
times.push_back(time);
iter.next();
}
EXPECT_EQ(moved, common::E_OK);
if (ret == E_NO_MORE_DATA) break;
}
return times;
}
Expand Down
Loading