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: 4 additions & 2 deletions cpp/examples/c_examples/demo_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ ERRNO write_tsfile() {
for (int row = 0; row < 5; row++) {
Timestamp timestamp = row;
tablet_add_timestamp(tablet, row, timestamp);
tablet_add_value_by_name_string(tablet, row, "id1", "id_field_1");
tablet_add_value_by_name_string(tablet, row, "id2", "id_field_2");
tablet_add_value_by_name_string_with_len(
tablet, row, "id1", "id_field_1", strlen("id_field_1"));
tablet_add_value_by_name_string_with_len(
tablet, row, "id2", "id_field_2", strlen("id_field_2"));
tablet_add_value_by_name_int32_t(tablet, row, "s1", row);
}

Expand Down
49 changes: 28 additions & 21 deletions cpp/src/common/allocator/my_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@ namespace common {

// String that use PageArena
struct String {
char *buf_;
char* buf_;
uint32_t len_;

String() : buf_(nullptr), len_(0) {}
String(char *buf, uint32_t len) : buf_(buf), len_(len) {}
String(const std::string &str, common::PageArena &pa)
String(char* buf, uint32_t len) : buf_(buf), len_(len) {}

// NOTE:
// This constructor accepts a const char* to preserve constness for possible
// future copying. The internal buf_ must be considered read-only.
String(const char* buf, uint32_t len)
: buf_(const_cast<char*>(buf)), len_(len) {}

String(const std::string& str, common::PageArena& pa)
: buf_(nullptr), len_(0) {
dup_from(str, pa);
}
String(const std::string &str) {
buf_ = (char *)str.c_str();
String(const std::string& str) {
buf_ = (char*)str.c_str();
len_ = str.size();
}
FORCE_INLINE bool is_null() const { return buf_ == nullptr && len_ == 0; }
FORCE_INLINE void reset() {
len_ = 0;
buf_ = nullptr;
}
FORCE_INLINE int dup_from(const std::string &str, common::PageArena &pa) {
FORCE_INLINE int dup_from(const std::string& str, common::PageArena& pa) {
len_ = str.size();
if (UNLIKELY(len_ == 0)) {
return common::E_OK;
Expand All @@ -61,11 +68,11 @@ struct String {
return common::E_OK;
}

FORCE_INLINE bool operator==(const String &other) const {
FORCE_INLINE bool operator==(const String& other) const {
return equal_to(other);
}

FORCE_INLINE int dup_from(const String &str, common::PageArena &pa) {
FORCE_INLINE int dup_from(const String& str, common::PageArena& pa) {
len_ = str.len_;
if (UNLIKELY(len_ == 0)) {
buf_ = nullptr;
Expand All @@ -79,8 +86,8 @@ struct String {
return common::E_OK;
}

FORCE_INLINE int dup_from(const char *str, uint32_t len,
common::PageArena &pa) {
FORCE_INLINE int dup_from(const char* str, uint32_t len,
common::PageArena& pa) {
len_ = len;
if (UNLIKELY(len_ == 0)) {
return common::E_OK;
Expand All @@ -93,8 +100,8 @@ struct String {
return common::E_OK;
}

FORCE_INLINE int build_from(const String &s1, const String &s2,
common::PageArena &pa) {
FORCE_INLINE int build_from(const String& s1, const String& s2,
common::PageArena& pa) {
len_ = s1.len_ + s2.len_;
buf_ = pa.alloc(len_);
if (IS_NULL(buf_)) {
Expand All @@ -104,11 +111,11 @@ struct String {
memcpy(buf_ + s1.len_, s2.buf_, s2.len_);
return common::E_OK;
}
FORCE_INLINE void shallow_copy_from(const String &src) {
FORCE_INLINE void shallow_copy_from(const String& src) {
buf_ = src.buf_;
len_ = src.len_;
}
FORCE_INLINE bool equal_to(const String &that) const {
FORCE_INLINE bool equal_to(const String& that) const {
return (len_ == 0 && that.len_ == 0) ||
((len_ == that.len_) && (0 == memcmp(buf_, that.buf_, len_)));
}
Expand All @@ -121,7 +128,7 @@ struct String {
// }

// strict less than. If @this equals to @that, return false.
FORCE_INLINE bool less_than(const String &that) const {
FORCE_INLINE bool less_than(const String& that) const {
if (len_ == 0 || that.len_ == 0) {
return false;
}
Expand All @@ -139,7 +146,7 @@ struct String {
// return = 0, if this = that
// return < 0, if this < that
// return > 0, if this > that
FORCE_INLINE int compare(const String &that) const {
FORCE_INLINE int compare(const String& that) const {
if (len_ == 0 && that.len_ == 0) {
return 0;
}
Expand All @@ -158,19 +165,19 @@ struct String {
}
}

FORCE_INLINE void max(const String &that, common::PageArena &pa) {
FORCE_INLINE void max(const String& that, common::PageArena& pa) {
if (compare(that) < 0) {
this->dup_from(that, pa);
}
}

FORCE_INLINE void min(const String &that, common::PageArena &pa) {
FORCE_INLINE void min(const String& that, common::PageArena& pa) {
if (compare(that) > 0) {
this->dup_from(that, pa);
}
}

bool operator<(const String &other) const {
bool operator<(const String& other) const {
if (this->is_null() && other.is_null()) {
return false;
}
Expand All @@ -192,7 +199,7 @@ struct String {
std::string to_std_string() const { return std::string(buf_, len_); }

#ifndef NDEBUG
friend std::ostream &operator<<(std::ostream &os, const String &s) {
friend std::ostream& operator<<(std::ostream& os, const String& s) {
os << s.len_ << "@";
for (uint32_t i = 0; i < s.len_; i++) {
os << s.buf_[i];
Expand All @@ -203,7 +210,7 @@ struct String {
};

struct StringLessThan {
FORCE_INLINE bool operator()(const String &s1, const String &s2) const {
FORCE_INLINE bool operator()(const String& s1, const String& s2) const {
return s1.less_than(s2);
}
};
Expand Down
Loading
Loading