Skip to content

Commit ffeba6b

Browse files
committed
encoding
1 parent cec1272 commit ffeba6b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

binding.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct BatchIterator : public rocksdb::WriteBatch::Handler {
165165
return rocksdb::Status::OK();
166166
}
167167

168-
BatchEntry entry = { BatchOp::Put };
168+
BatchEntry entry = {BatchOp::Put};
169169

170170
if (keys_) {
171171
entry.key = key.ToStringView();
@@ -189,7 +189,7 @@ struct BatchIterator : public rocksdb::WriteBatch::Handler {
189189
return rocksdb::Status::OK();
190190
}
191191

192-
BatchEntry entry = { BatchOp::Delete };
192+
BatchEntry entry = {BatchOp::Delete};
193193

194194
if (keys_) {
195195
entry.key = key.ToStringView();
@@ -209,7 +209,7 @@ struct BatchIterator : public rocksdb::WriteBatch::Handler {
209209
return rocksdb::Status::OK();
210210
}
211211

212-
BatchEntry entry = { BatchOp::Merge };
212+
BatchEntry entry = {BatchOp::Merge};
213213

214214
if (keys_) {
215215
entry.key = key.ToStringView();
@@ -233,7 +233,7 @@ struct BatchIterator : public rocksdb::WriteBatch::Handler {
233233
return;
234234
}
235235

236-
BatchEntry entry = { BatchOp::Data };
236+
BatchEntry entry = {BatchOp::Data};
237237

238238
entry.val = data.ToStringView();
239239

util.h

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
} \
2121
}
2222

23-
#define ROCKS_STATUS_THROWS_NAPI(call) \
23+
#define ROCKS_STATUS_THROWS_NAPI(call) \
2424
{ \
2525
auto _status = (call); \
2626
if (!_status.ok()) { \
@@ -147,23 +147,7 @@ static napi_status GetString(napi_env env, napi_value from, rocksdb::PinnableSli
147147
return napi_ok;
148148
}
149149

150-
static napi_status EncodingIsBuffer(napi_env env, napi_value obj, const std::string_view& key, bool& result) {
151-
bool has = false;
152-
NAPI_STATUS_RETURN(napi_has_named_property(env, obj, key.data(), &has));
153-
154-
if (has) {
155-
napi_value value;
156-
NAPI_STATUS_RETURN(napi_get_named_property(env, obj, key.data(), &value));
157-
158-
size_t size;
159-
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, value, nullptr, 0, &size));
160-
161-
// Value is either "buffer" or "utf8" so we can tell them apart just by size
162-
result = size == 6;
163-
}
164-
165-
return napi_ok;
166-
}
150+
enum class Encoding { Invalid, Buffer, String };
167151

168152
static napi_status GetValue(napi_env env, napi_value value, bool& result) {
169153
return napi_get_value_bool(env, value, &result);
@@ -184,20 +168,14 @@ static napi_status GetValue(napi_env env, napi_value value, int64_t& result) {
184168
static napi_status GetValue(napi_env env, napi_value value, uint64_t& result) {
185169
int64_t result2;
186170
NAPI_STATUS_RETURN(napi_get_value_int64(env, value, &result2));
187-
if (result2 < 0) {
188-
return napi_generic_failure;
189-
}
190171
result = static_cast<uint64_t>(result2);
191172
return napi_ok;
192173
}
193174

194175
static napi_status GetValue(napi_env env, napi_value value, unsigned long& result) {
195176
int64_t result2;
196177
NAPI_STATUS_RETURN(napi_get_value_int64(env, value, &result2));
197-
if (result2 < 0) {
198-
return napi_generic_failure;
199-
}
200-
result = static_cast<uint64_t>(result2);
178+
result = static_cast<unsigned long>(result2);
201179
return napi_ok;
202180
}
203181

@@ -213,6 +191,19 @@ static napi_status GetValue(napi_env env, napi_value value, rocksdb::ColumnFamil
213191
return napi_get_value_external(env, value, reinterpret_cast<void**>(&result));
214192
}
215193

194+
static napi_status GetValue(napi_env env, napi_value value, Encoding& result) {
195+
size_t size;
196+
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, value, nullptr, 0, &size));
197+
198+
if (size == 6) {
199+
result = Encoding::Buffer;
200+
} else {
201+
result = Encoding::String;
202+
}
203+
204+
return napi_ok;
205+
}
206+
216207
template <typename T>
217208
static napi_status GetValue(napi_env env, napi_value value, std::optional<T>& result) {
218209
result = T{};
@@ -254,6 +245,13 @@ static napi_status GetProperty(napi_env env,
254245
return GetValue(env, value, result);
255246
}
256247

248+
static napi_status EncodingIsBuffer(napi_env env, napi_value obj, const std::string_view& key, bool& result) {
249+
Encoding encoding;
250+
NAPI_STATUS_RETURN(GetProperty(env, obj, key, encoding));
251+
result = encoding == Encoding::Buffer;
252+
return napi_ok;
253+
}
254+
257255
template <typename T>
258256
napi_status Convert(napi_env env, T&& s, bool asBuffer, napi_value& result) {
259257
if (!s) {

0 commit comments

Comments
 (0)