/
mutationtoken.cpp
133 lines (113 loc) · 4.19 KB
/
mutationtoken.cpp
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
#include "mutationtoken.hpp"
#include "utils.hpp"
#include <sstream>
namespace couchnode
{
struct MutationTokenData {
uint16_t partitionId;
uint64_t partitionUuid;
uint64_t sequenceNumber;
char bucketName[256];
};
void MutationToken::Init(Napi::Env env, Napi::Object exports)
{
Napi::Function func = DefineClass(
env, "MutationToken",
{
InstanceMethod<&MutationToken::jsToString>("toString"),
InstanceMethod<&MutationToken::jsToJSON>("toJSON"),
InstanceMethod<&MutationToken::jsInspect>(
utils::napiGetSymbol(env, "nodejs.util.inspect.custom")),
});
constructor(env) = Napi::Persistent(func);
exports.Set("MutationToken", func);
}
MutationToken::MutationToken(const Napi::CallbackInfo &info)
: Napi::ObjectWrap<MutationToken>(info)
{
if (info[0].IsBuffer()) {
info.This().As<Napi::Object>().Set("raw", info[0]);
return;
}
auto token = MutationToken::parse(info[0]);
auto rawBytesVal = MutationToken::toBuffer(info.Env(), token);
info.This().As<Napi::Object>().Set("raw", rawBytesVal);
}
MutationToken::~MutationToken()
{
}
Napi::Value MutationToken::toBuffer(Napi::Env env,
const couchbase::mutation_token &token)
{
MutationTokenData tokenData;
tokenData.partitionId = token.partition_id();
tokenData.partitionUuid = token.partition_uuid();
tokenData.sequenceNumber = token.sequence_number();
memcpy(tokenData.bucketName, token.bucket_name().c_str(),
token.bucket_name().size() + 1);
return utils::napiDataToBuffer<MutationTokenData>(env, tokenData);
}
couchbase::mutation_token MutationToken::fromBuffer(Napi::Value val)
{
MutationTokenData tokenData =
utils::napiBufferToData<MutationTokenData>(val);
return couchbase::mutation_token{
tokenData.partitionUuid, tokenData.partitionId,
static_cast<uint16_t>(tokenData.sequenceNumber), tokenData.bucketName};
}
Napi::Value MutationToken::create(Napi::Env env,
const couchbase::mutation_token &token)
{
auto rawBytesVal = MutationToken::toBuffer(env, token);
return MutationToken::constructor(env).New({rawBytesVal});
}
couchbase::mutation_token MutationToken::parse(Napi::Value val)
{
if (val.IsNull()) {
return couchbase::mutation_token{};
} else if (val.IsUndefined()) {
return couchbase::mutation_token{};
} else if (val.IsObject()) {
auto objVal = val.As<Napi::Object>();
auto maybeRawVal = objVal.Get("raw");
if (!maybeRawVal.IsEmpty()) {
return MutationToken::fromBuffer(maybeRawVal);
}
} else if (val.IsString()) {
// not currently supported
} else if (val.IsBuffer()) {
return MutationToken::fromBuffer(val);
}
return couchbase::mutation_token{};
}
Napi::Value MutationToken::jsToString(const Napi::CallbackInfo &info)
{
auto token = MutationToken::parse(info.This());
std::stringstream stream;
stream << token.bucket_name() << ":" << token.partition_id() << ":"
<< token.partition_uuid() << ":" << token.sequence_number();
return Napi::String::New(info.Env(), stream.str());
}
Napi::Value MutationToken::jsToJSON(const Napi::CallbackInfo &info)
{
auto env = info.Env();
auto token = MutationToken::parse(info.This());
auto resObj = Napi::Object::New(env);
resObj.Set("bucket_name", Napi::String::New(env, token.bucket_name()));
resObj.Set("partition_id", Napi::Number::New(env, token.partition_id()));
resObj.Set("partition_uuid",
Napi::String::New(env, std::to_string(token.partition_uuid())));
resObj.Set("sequence_number",
Napi::String::New(env, std::to_string(token.sequence_number())));
return resObj;
}
Napi::Value MutationToken::jsInspect(const Napi::CallbackInfo &info)
{
auto token = MutationToken::parse(info.This());
std::stringstream stream;
stream << "MutationToken<" << token.bucket_name() << ":"
<< token.partition_id() << ":" << token.partition_uuid() << ":"
<< token.sequence_number() << ">";
return Napi::String::New(info.Env(), stream.str());
}
} // namespace couchnode