-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstore_util.cpp
More file actions
225 lines (197 loc) · 6.22 KB
/
store_util.cpp
File metadata and controls
225 lines (197 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Copyright (C) 2025 EloqData Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under either of the following two licenses:
* 1. GNU Affero General Public License, version 3, as published by the Free
* Software Foundation.
* 2. GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License or GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* and GNU General Public License V2 along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "store_util.h"
#include <memory>
#include <string>
#include <vector>
namespace EloqShare
{
std::string uint32_str_converter(uint32_t val)
{
return std::to_string(val);
}
std::string uint16_str_converter(uint16_t val)
{
return std::to_string(val);
}
std::string string_str_converter(const std::string &val)
{
return val;
}
std::string bool_str_converter(bool val)
{
return val ? "1" : "0";
}
uint32_t str_uint32_converter(const std::string &s)
{
return std::stoul(s);
}
uint16_t str_uint16_converter(const std::string &s)
{
return std::stoul(s);
}
std::string str_string_converter(const std::string &s)
{
return s;
}
bool str_bool_converter(const std::string &s)
{
return s == "1";
}
template <>
void SerializeVector<std::string>(const std::vector<std::string> &vec,
std::string &str)
{
// The vector size
size_t cnt = vec.size();
size_t val_len = sizeof(cnt);
const char *val_ptr = reinterpret_cast<const char *>(&cnt);
str.append(val_ptr, val_len);
// The vector content
val_len = sizeof(size_t);
for (size_t i = 0; i < vec.size(); ++i)
{
// string size
size_t str_len = vec[i].size();
val_ptr = reinterpret_cast<const char *>(&str_len);
str.append(val_ptr, val_len);
// string content
str.append(vec[i].data(), str_len);
}
}
template <>
void DeserializeToVector<std::string>(const char *str,
size_t length,
std::vector<std::string> &vec)
{
if (!length)
{
return;
}
size_t offset = 0;
// The vector size.
size_t vec_size = *(reinterpret_cast<const size_t *>(str + offset));
offset += sizeof(size_t);
// The vector content
for (size_t i = 0; i < vec_size; ++i)
{
// string size
size_t str_len = *(reinterpret_cast<const size_t *>(str + offset));
offset += sizeof(str_len);
// string content
vec.emplace_back((str + offset), str_len);
offset += str_len;
}
assert(offset == length);
}
// void SerializeFlushRecord(const txservice::FlushRecord &flush_rec,
// std::vector<char> &buf)
// {
// if (flush_rec.payload_status_ != txservice::RecordStatus::Deleted)
// {
// int64_t commit_ts = flush_rec.commit_ts_;
// int8_t deleted = 0;
// const txservice::BlobTxRecord *rec =
// dynamic_cast<const txservice::BlobTxRecord
// *>(flush_rec.Payload());
// assert(rec != nullptr);
// buf.resize(sizeof(int8_t) + sizeof(int64_t) + rec->value_.size());
// char *p = buf.data();
// // encode deleted
// std::memcpy(p, &deleted, sizeof(int8_t));
// p += sizeof(int8_t);
// // encode version
// std::memcpy(p, &commit_ts, sizeof(int64_t));
// p += sizeof(int64_t);
// std::memcpy(p, rec->value_.c_str(), rec->value_.size());
// }
// else
// {
// buf.resize(sizeof(int8_t) + sizeof(int64_t));
// char *p = buf.data();
// int64_t commit_ts = flush_rec.commit_ts_;
// int8_t deleted = 1;
// // encode deleted
// std::memcpy(p, &deleted, sizeof(int8_t));
// p += sizeof(int8_t);
// // encode version
// std::memcpy(p, &commit_ts, sizeof(int64_t));
// p += sizeof(int64_t);
// }
// }
// void SerializeFlushRecord(const txservice::FlushRecord &flush_rec,
// std::string &rec_str)
// {
// if (flush_rec.payload_status_ != txservice::RecordStatus::Deleted)
// {
// const txservice::BlobTxRecord *rec =
// dynamic_cast<const txservice::BlobTxRecord
// *>(flush_rec.Payload());
// assert(rec != nullptr);
// rec_str.resize(sizeof(int8_t) + sizeof(int64_t) +
// rec->value_.size()); char *p = rec_str.data();
// int8_t deleted = 0;
// int64_t commit_ts = flush_rec.commit_ts_;
// std::memcpy(p, &deleted, sizeof(int8_t));
// p += sizeof(int8_t);
// std::memcpy(p, &commit_ts, sizeof(int64_t));
// p += sizeof(int64_t);
// std::memcpy(p, rec->value_.c_str(), rec->value_.size());
// }
// else
// {
// rec_str.resize(sizeof(int8_t) + sizeof(int64_t));
// char *p = rec_str.data();
// int8_t deleted = 1;
// int64_t commit_ts = flush_rec.commit_ts_;
// std::memcpy(p, &deleted, sizeof(int8_t));
// p += sizeof(int8_t);
// std::memcpy(p, &commit_ts, sizeof(int64_t));
// }
// }
void DeserializeRecord(const char *payload,
const size_t payload_size,
std::string &rec_str,
bool &is_deleted,
int64_t &version_ts)
{
assert(payload_size >= (sizeof(int8_t) + sizeof(int64_t)));
const char *p = payload;
int8_t deleted = 0;
std::memcpy(&deleted, p, sizeof(int8_t));
p += sizeof(int8_t);
int64_t version = 0;
std::memcpy(&version, p, sizeof(int64_t));
p += sizeof(int64_t);
version_ts = version;
if (deleted == 0)
{
is_deleted = false;
rec_str =
std::string(p, payload_size - sizeof(int8_t) - sizeof(int64_t));
}
else
{
is_deleted = true;
}
}
} // namespace EloqShare