Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite some Tsfile C++ interface #304

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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: 3 additions & 3 deletions cpp/examples/cpp_examples/demo_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ int demo_read() {

std::cout << "begin to query expr" << std::endl;
ASSERT(ret == 0);
storage::QueryDataSet *qds = nullptr;
storage::ResultSet *qds = nullptr;
ret = reader.query(query_expr, qds);

storage::RowRecord *record;
std::cout << "begin to dump data from tsfile ---" << std::endl;
int row_cout = 0;
do {
record = qds->get_next();
if (record) {
if (qds->next()) {
std::cout << "dump QDS : " << record->get_timestamp() << ",";
record = qds->get_row_record();
if (record) {
int size = record->get_fields()->size();
for (int i = 0; i < size; ++i) {
Expand Down
8 changes: 3 additions & 5 deletions cpp/examples/cpp_examples/demo_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,14 @@ int demo_write() {
std::vector<MeasurementSchema> schema_vec[50];
for (int i = 0; i < device_num; i++) {
std::string device_name = "test_device" + std::to_string(i);
schema_vec[i].reserve(measurement_num);
for (int j = 0; j < measurement_num; j++) {
std::string measure_name = "measurement" + std::to_string(j);
schema_vec[i].push_back(
schema_vec[i].emplace_back(
MeasurementSchema(measure_name, common::TSDataType::INT32,
common::TSEncoding::PLAIN,
common::CompressionType::UNCOMPRESSED));
tsfile_writer_->register_timeseries(
device_name, measure_name, common::TSDataType::INT32,
common::TSEncoding::PLAIN,
common::CompressionType::UNCOMPRESSED);
tsfile_writer_->register_timeseries(device_name, schema_vec[i][j]);
}
}

Expand Down
1 change: 1 addition & 0 deletions cpp/src/common/allocator/my_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ struct String {

return this->len_ < other.len_;
}
std::string to_std_string() { return std::string(buf_, len_); }

#ifndef NDEBUG
friend std::ostream &operator<<(std::ostream &os, const String &s) {
Expand Down
16 changes: 9 additions & 7 deletions cpp/src/common/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <vector>

#include "common/db_common.h"

#include "utils/errno_define.h"
namespace storage {

// TODO: use std::move
Expand Down Expand Up @@ -120,22 +120,24 @@ struct DataPoint {

struct TsRecord {
int64_t timestamp_;
std::string device_name_;
std::string device_id_;
std::vector<DataPoint> points_;

TsRecord(const std::string &device_name) : device_name_(device_name) {}
TsRecord(const std::string &device_name) : device_id_(device_name) {}

TsRecord(int64_t timestamp, const std::string &device_name,
int32_t point_count_in_row = 0)
: timestamp_(timestamp), device_name_(device_name), points_() {
: timestamp_(timestamp), device_id_(device_name), points_() {
if (point_count_in_row > 0) {
points_.reserve(point_count_in_row);
}
}

void append_data_point(const DataPoint &point) {
// points_.emplace_back(point); C++11
points_.push_back(point);
template <typename T>
int add_point(const std::string &measurement_name, T val) {
int ret = common::E_OK;
points_.emplace_back(DataPoint(measurement_name, val));
return ret;
}
};

Expand Down
26 changes: 25 additions & 1 deletion cpp/src/common/row_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,31 @@ struct Field {
}
}

template <typename T>
FORCE_INLINE T get_value() {
switch (type_) {
case common::TSDataType::BOOLEAN:
return value_.bval_;
case common::TSDataType::INT32:
return value_.ival_;
case common::TSDataType::INT64:
return value_.lval_;
case common::TSDataType::FLOAT:
return value_.fval_;
case common::TSDataType::DOUBLE:
return value_.dval_;
// case common::TSDataType::TEXT :
// return value_.sval_;
HTHou marked this conversation as resolved.
Show resolved Hide resolved
default:
std::cout << "unknown data type" << std::endl;
break;
}
return -1; // when data type is unknown
}

public:
common::TSDataType type_;

std::string column_name;
union {
bool bval_;
int64_t lval_;
Expand Down Expand Up @@ -182,6 +204,8 @@ class RowRecord {

FORCE_INLINE std::vector<Field *> *get_fields() { return fields_; }

FORCE_INLINE uint32_t get_col_num() { return col_num_; }

private:
int64_t time_; // time value
uint32_t col_num_; // measurement num
Expand Down
9 changes: 9 additions & 0 deletions cpp/src/common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ struct MeasurementSchema {
chunk_writer_(nullptr),
value_chunk_writer_(nullptr) {}

MeasurementSchema(const std::string &measurement_name,
common::TSDataType data_type)
: measurement_name_(measurement_name),
data_type_(data_type),
encoding_(get_default_encoding_for_type(data_type)),
compression_type_(common::LZ4),
chunk_writer_(nullptr),
value_chunk_writer_(nullptr) {}

MeasurementSchema(const std::string &measurement_name,
common::TSDataType data_type, common::TSEncoding encoding,
common::CompressionType compression_type)
Expand Down
134 changes: 57 additions & 77 deletions cpp/src/common/tablet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace storage {

int Tablet::init() {
ASSERT(timestamps_ == NULL);
timestamps_ = (int64_t *)malloc(sizeof(int64_t) * max_rows_);
timestamps_ = (int64_t *)malloc(sizeof(int64_t) * max_row_num_);

size_t schema_count = schema_vec_->size();
std::pair<std::map<std::string, int>::iterator, bool> ins_res;
Expand All @@ -48,12 +48,12 @@ int Tablet::init() {
for (size_t c = 0; c < schema_count; c++) {
const MeasurementSchema &schema = schema_vec_->at(c);
value_matrix_[c] =
malloc(get_data_type_size(schema.data_type_) * max_rows_);
malloc(get_data_type_size(schema.data_type_) * max_row_num_);
}

bitmaps_ = new BitMap[schema_count];
for (size_t c = 0; c < schema_count; c++) {
bitmaps_[c].init(max_rows_, /*init_as_zero=*/true);
bitmaps_[c].init(max_row_num_, /*init_as_zero=*/true);
}
return E_OK;
}
Expand All @@ -75,90 +75,70 @@ void Tablet::destroy() {
}
}

int Tablet::set_timestamp(int row_index, int64_t timestamp) {
int Tablet::add_timestamp(uint32_t row_index, int64_t timestamp) {
ASSERT(timestamps_ != NULL);
if (UNLIKELY(row_index >= max_rows_)) {
if (UNLIKELY(row_index >= max_row_num_)) {
ASSERT(false);
return E_OUT_OF_RANGE;
}
timestamps_[row_index] = timestamp;
return E_OK;
}

#define DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val) \
do { \
SchemaMapIterator find_iter = schema_map_.find(measurement_name); \
if (LIKELY(find_iter == schema_map_.end())) { \
ASSERT(false); \
return E_INVALID_ARG; \
} \
return set_value(row_index, find_iter->second, val); \
} while (false)

#define DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, CppType, val) \
do { \
if (LIKELY(schema_index >= schema_vec_->size())) { \
ASSERT(false); \
return E_OUT_OF_RANGE; \
} \
const MeasurementSchema &schema = schema_vec_->at(schema_index); \
if (LIKELY(GetDataTypeFromTemplateType<CppType>() != \
schema.data_type_)) { \
return E_TYPE_NOT_MATCH; \
} \
CppType *column_values = (CppType *)value_matrix_[schema_index]; \
column_values[row_index] = val; \
bitmaps_[schema_index].set(row_index); /* mark as non-null*/ \
} while (false)

int Tablet::set_value(int row_index, const std::string &measurement_name,
bool val) {
DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val);
}

int Tablet::set_value(int row_index, const std::string &measurement_name,
int32_t val) {
DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val);
}

int Tablet::set_value(int row_index, const std::string &measurement_name,
int64_t val) {
DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val);
}

int Tablet::set_value(int row_index, const std::string &measurement_name,
float val) {
DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val);
}

int Tablet::set_value(int row_index, const std::string &measurement_name,
double val) {
DO_SET_VALUE_BY_COL_NAME(row_index, measurement_name, val);
}

int Tablet::set_value(int row_index, uint32_t schema_index, bool val) {
DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, bool, val);
return E_OK;
}

int Tablet::set_value(int row_index, uint32_t schema_index, int32_t val) {
DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, int32_t, val);
return E_OK;
}

int Tablet::set_value(int row_index, uint32_t schema_index, int64_t val) {
DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, int64_t, val);
return E_OK;
}

int Tablet::set_value(int row_index, uint32_t schema_index, float val) {
DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, float, val);
return E_OK;
template <typename T>
int Tablet::add_value(uint32_t row_index, uint32_t schema_index, T val) {
int ret = common::E_OK;
if (LIKELY(schema_index >= schema_vec_->size())) {
ASSERT(false);
ret = common::E_OUT_OF_RANGE;
} else {
const MeasurementSchema &schema = schema_vec_->at(schema_index);
if (LIKELY(GetDataTypeFromTemplateType<T>() != schema.data_type_)) {
ret = common::E_TYPE_NOT_MATCH;
} else {
T *column_values = (T *)value_matrix_[schema_index];
column_values[row_index] = val;
bitmaps_[schema_index].set(row_index); /* mark as non-null*/
}
}
return ret;
}

int Tablet::set_value(int row_index, uint32_t schema_index, double val) {
DO_SET_VALUE_BY_COL_INDEX(row_index, schema_index, double, val);
return E_OK;
template <typename T>
int Tablet::add_value(uint32_t row_index, const std::string &measurement_name,
T val) {
int ret = common::E_OK;
SchemaMapIterator find_iter = schema_map_.find(measurement_name);
if (LIKELY(find_iter == schema_map_.end())) {
ASSERT(false);
ret = E_INVALID_ARG;
} else {
ret = add_value(row_index, find_iter->second, val);
}
return ret;
}

template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
bool val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
int32_t val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
int64_t val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
float val);
template int Tablet::add_value(uint32_t row_index, uint32_t schema_index,
double val);

template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, bool val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name,
int32_t val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name,
int64_t val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, float val);
template int Tablet::add_value(uint32_t row_index,
const std::string &measurement_name, double val);
} // end namespace storage
Loading
Loading