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
6 changes: 6 additions & 0 deletions cpp/src/common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,15 @@ struct MeasurementSchemaGroup {
// measurement_name -> MeasurementSchema
MeasurementSchemaMap measurement_schema_map_;
bool is_aligned_ = false;
bool has_last_time_ = false;
TimeChunkWriter* time_chunk_writer_ = nullptr;
int64_t last_time_ = INT64_MIN;

void update_last_time(int64_t timestamp) {
last_time_ = std::max(last_time_, timestamp);
has_last_time_ = true;
}

~MeasurementSchemaGroup() {
if (time_chunk_writer_ != nullptr) {
delete time_chunk_writer_;
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/writer/time_chunk_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include "time_chunk_writer.h"

#include <algorithm>
#include <limits>

#include "common/logger/elog.h"

using namespace common;
Expand Down Expand Up @@ -199,4 +202,17 @@ bool TimeChunkWriter::hasData() {
time_page_writer_.get_statistic()->count_ > 0);
}

int64_t TimeChunkWriter::get_last_time() {
int64_t last = std::numeric_limits<int64_t>::min();
if (chunk_statistic_ != nullptr && chunk_statistic_->get_count() > 0) {
last = chunk_statistic_->get_end_time();
}
if (time_page_writer_.get_statistic() != nullptr &&
time_page_writer_.get_statistic()->get_count() > 0) {
last =
std::max(last, time_page_writer_.get_statistic()->get_end_time());
}
return last;
}

} // end namespace storage
13 changes: 13 additions & 0 deletions cpp/src/writer/time_chunk_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class TimeChunkWriter {

bool hasData();

// Highest timestamp written so far (sealed pages plus the current
// unsealed page). Only meaningful when hasData() is true; returns the
// minimum int64 value when nothing has been written.
int64_t get_last_time();

int64_t estimate_max_series_mem_size();

// Current (unsealed) page point count.
Expand All @@ -122,6 +127,14 @@ class TimeChunkWriter {
}

private:
friend class TsFileWriter;

// The aligned table writer has validated these timestamps and splits
// ranges at the same page boundaries as the value columns.
int write_ordered_page_batch(const int64_t* timestamps, uint32_t count) {
return time_page_writer_.encode_ordered_batch(timestamps, count);
}

FORCE_INLINE bool is_cur_page_full() const {
// FIXME
return (time_page_writer_.get_point_numer() >=
Expand Down
69 changes: 49 additions & 20 deletions cpp/src/writer/time_page_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef WRITER_PAGE_TIME_WRITER_H
#define WRITER_PAGE_TIME_WRITER_H

#include <algorithm>

#include "common/allocator/byte_stream.h"
#include "common/global.h"
#include "common/statistic.h"
Expand Down Expand Up @@ -85,26 +87,32 @@ class TimePageWriter {
return ret;
}

// Reject a timestamp only when it is strictly less than the previous one
// (allow_non_decreasing_timestamps_ enabled), or at most equal to it
// (flag disabled, the historical behavior).
FORCE_INLINE static bool is_out_of_order(int64_t time, int64_t last_time) {
return common::g_config_value_.allow_non_decreasing_timestamps_
? time < last_time
: time <= last_time;
}

static bool is_batch_in_order(const int64_t* times, uint32_t count) {
return common::g_config_value_.allow_non_decreasing_timestamps_
? batch_in_order<true>(times, count)
: batch_in_order<false>(times, count);
}

int write_batch(const int64_t* timestamps, uint32_t count) {
int ret = common::E_OK;
if (count == 0) return ret;
if (count == 0) return common::E_OK;
// Check order: first timestamp vs existing end_time
if (statistic_->count_ != 0 && is_inited_ &&
is_out_of_order(timestamps[0], statistic_->end_time_)) {
return common::E_OUT_OF_ORDER;
}
// Check monotonicity within batch
for (uint32_t i = 1; i < count; i++) {
if (is_out_of_order(timestamps[i], timestamps[i - 1])) {
return common::E_OUT_OF_ORDER;
}
}
if (RET_FAIL(time_encoder_->encode_batch(timestamps, count,
time_out_stream_))) {
} else {
statistic_->update_time_batch(timestamps, count);
if (!is_batch_in_order(timestamps, count)) {
return common::E_OUT_OF_ORDER;
}
return ret;
return encode_ordered_batch(timestamps, count);
}

FORCE_INLINE uint32_t get_point_numer() const { return statistic_->count_; }
Expand Down Expand Up @@ -136,14 +144,35 @@ class TimePageWriter {
void destroy_page_data() { cur_page_data_.destroy(); }

private:
// Reject a timestamp only when it is strictly less than the previous one
// (allow_non_decreasing_timestamps_ enabled), or at most equal to it
// (flag disabled, the historical behavior).
FORCE_INLINE static bool is_out_of_order(int64_t time, int64_t last_time) {
return common::g_config_value_.allow_non_decreasing_timestamps_
? time < last_time
: time <= last_time;
friend class TimeChunkWriter;

int encode_ordered_batch(const int64_t* timestamps, uint32_t count) {
int ret =
time_encoder_->encode_batch(timestamps, count, time_out_stream_);
if (ret == common::E_OK) {
statistic_->update_time_batch(timestamps, count);
}
return ret;
}

template <bool AllowEqual>
static bool batch_in_order(const int64_t* times, uint32_t count) {
// Compare directly to avoid signed overflow. Reduce comparisons in
// blocks so long ordered ranges vectorize and bad input fails early.
while (count > 1) {
const uint32_t pairs = std::min<uint32_t>(count - 1, 256);
bool invalid = false;
for (uint32_t i = 0; i < pairs; ++i) {
invalid |= AllowEqual ? times[i + 1] < times[i]
: times[i + 1] <= times[i];
}
if (invalid) return false;
times += pairs;
count -= pairs;
}
return true;
}

FORCE_INLINE int prepare_end_page() {
int ret = common::E_OK;
if (RET_FAIL(time_encoder_->flush(time_out_stream_))) {
Expand Down
Loading
Loading