Kùzu C++ API
Loading...
Searching...
No Matches
ku_string.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <string>
6
7#include "api.h"
8
9namespace kuzu {
10namespace common {
11
13
14 static constexpr uint64_t PREFIX_LENGTH = 4;
15 static constexpr uint64_t INLINED_SUFFIX_LENGTH = 8;
16 static constexpr uint64_t SHORT_STR_LENGTH = PREFIX_LENGTH + INLINED_SUFFIX_LENGTH;
17
18 uint32_t len;
19 uint8_t prefix[PREFIX_LENGTH];
20 union {
21 uint8_t data[INLINED_SUFFIX_LENGTH];
22 uint64_t overflowPtr;
23 };
24
25 ku_string_t() : len{0}, overflowPtr{0} {}
26 ku_string_t(const char* value, uint64_t length);
27
28 static bool isShortString(uint32_t len) { return len <= SHORT_STR_LENGTH; }
29
30 const uint8_t* getData() const {
31 return isShortString(len) ? prefix : reinterpret_cast<uint8_t*>(overflowPtr);
32 }
33
34 uint8_t* getDataUnsafe() {
35 return isShortString(len) ? prefix : reinterpret_cast<uint8_t*>(overflowPtr);
36 }
37
38 // These functions do *NOT* allocate/resize the overflow buffer, it only copies the content and
39 // set the length.
40 void set(const std::string& value);
41 void set(const char* value, uint64_t length);
42 void set(const ku_string_t& value);
43 void setShortString(const char* value, uint64_t length) {
44 this->len = length;
45 memcpy(prefix, value, length);
46 }
47 void setLongString(const char* value, uint64_t length) {
48 this->len = length;
49 memcpy(prefix, value, PREFIX_LENGTH);
50 memcpy(reinterpret_cast<char*>(overflowPtr), value, length);
51 }
52 void setShortString(const ku_string_t& value) {
53 this->len = value.len;
54 memcpy(prefix, value.prefix, value.len);
55 }
56 void setLongString(const ku_string_t& value) {
57 this->len = value.len;
58 memcpy(prefix, value.prefix, PREFIX_LENGTH);
59 memcpy(reinterpret_cast<char*>(overflowPtr), reinterpret_cast<char*>(value.overflowPtr),
60 value.len);
61 }
62
63 void setFromRawStr(const char* value, uint64_t length) {
64 this->len = length;
65 if (isShortString(length)) {
66 setShortString(value, length);
67 } else {
68 memcpy(prefix, value, PREFIX_LENGTH);
69 overflowPtr = reinterpret_cast<uint64_t>(value);
70 }
71 }
72
73 std::string getAsShortString() const;
74 std::string getAsString() const;
75 std::string_view getAsStringView() const;
76
77 bool operator==(const ku_string_t& rhs) const;
78
79 inline bool operator!=(const ku_string_t& rhs) const { return !(*this == rhs); }
80
81 bool operator>(const ku_string_t& rhs) const;
82
83 inline bool operator>=(const ku_string_t& rhs) const { return (*this > rhs) || (*this == rhs); }
84
85 inline bool operator<(const ku_string_t& rhs) const { return !(*this >= rhs); }
86
87 inline bool operator<=(const ku_string_t& rhs) const { return !(*this > rhs); }
88};
89
90} // namespace common
91} // namespace kuzu
#define KUZU_API
Definition api.h:25
Definition alter_type.h:5
Definition ku_string.h:12
uint64_t overflowPtr
Definition ku_string.h:22
bool operator<(const ku_string_t &rhs) const
Definition ku_string.h:85
void setShortString(const char *value, uint64_t length)
Definition ku_string.h:43
uint32_t len
Definition ku_string.h:18
const uint8_t * getData() const
Definition ku_string.h:30
uint8_t * getDataUnsafe()
Definition ku_string.h:34
std::string getAsShortString() const
void setLongString(const char *value, uint64_t length)
Definition ku_string.h:47
void set(const std::string &value)
void set(const char *value, uint64_t length)
ku_string_t()
Definition ku_string.h:25
std::string getAsString() const
bool operator>(const ku_string_t &rhs) const
bool operator>=(const ku_string_t &rhs) const
Definition ku_string.h:83
void setFromRawStr(const char *value, uint64_t length)
Definition ku_string.h:63
static bool isShortString(uint32_t len)
Definition ku_string.h:28
void set(const ku_string_t &value)
void setShortString(const ku_string_t &value)
Definition ku_string.h:52
std::string_view getAsStringView() const
ku_string_t(const char *value, uint64_t length)
void setLongString(const ku_string_t &value)
Definition ku_string.h:56
bool operator!=(const ku_string_t &rhs) const
Definition ku_string.h:79
bool operator==(const ku_string_t &rhs) const
bool operator<=(const ku_string_t &rhs) const
Definition ku_string.h:87
uint8_t prefix[PREFIX_LENGTH]
Definition ku_string.h:19