This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_vector.cc
More file actions
234 lines (203 loc) · 5.75 KB
/
shared_vector.cc
File metadata and controls
234 lines (203 loc) · 5.75 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
226
227
228
229
230
231
232
233
#include "shared_vector.h"
Napi::FunctionReference SharedVector::constructor;
Napi::Object SharedVector::Init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env,
"Vector",
{
InstanceMethod("push_back", &SharedVector::push_back),
InstanceMethod("at", &SharedVector::at),
InstanceMethod("erase", &SharedVector::erase),
InstanceMethod("empty", &SharedVector::empty),
InstanceMethod("clear", &SharedVector::clear),
//InstanceMethod("getValue", &SharedVector::getValue),
InstanceAccessor("name", &SharedVector::getName, &SharedVector::setName),
InstanceAccessor("value", &SharedVector::getValue, NULL)
}
);
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("Vector", func);
return exports;
}
Napi::Value SharedVector::getName(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::String::New(env, this->name);
}
void SharedVector::setName(const Napi::CallbackInfo &info, const Napi::Value &value)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->name = value.As<Napi::String>();
}
SharedVector::SharedVector(const Napi::CallbackInfo &info)
: Napi::ObjectWrap<SharedVector>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t length = info.Length();
if (length <= 0 || !info[0].IsString())
{
Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
return;
}
Napi::String value = info[0].As<Napi::String>();
this->name = value.Utf8Value();
Napi::Number arg1 = info[1].As<Napi::Number>();
int32_t memorySize = 64 * 1024;
if (arg1.IsNumber()) {
memorySize = arg1.ToNumber().Int32Value();
}
if (memorySize < 1024)
memorySize = 1024;
try
{
pSegment = new managed_mapped_file(open_or_create, this->name.c_str(), memorySize);
pVector = pSegment->find<ShmemVector>("ShmemVector").first;
if (pVector == NULL)
{
const ShmemBufferAllocator shmemBufferAlloc(pSegment->get_segment_manager());
pVector = pSegment->construct<ShmemVector>("ShmemVector")(shmemBufferAlloc);
}
pObj = pSegment->find<ShmemObject>("ShmemVector_ShmemObject").first;
if (pObj == NULL)
{
pObj = pSegment->construct<ShmemObject>("ShmemVector_ShmemObject")();
}
}
catch (std::exception& e)
{
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
return;
}
}
SharedVector::~SharedVector()
{
delete pSegment;
}
static
napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
return reinterpret_cast<napi_value>(*local);
}
static
v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
v8::Local<v8::Value> local;
memcpy(&local, &v, sizeof(v));
return local;
}
typedef struct {
const uint8_t* data;
size_t size;
} napi_serialized_data;
void SharedVector::push_back(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
scoped_lock<interprocess_mutex> lock(pObj->mutex);
Napi::Value value = info[0];
size_t length = info.Length();
if (length <= 0)
{
Napi::TypeError::New(env, "Invalid Parameters, String expected").ThrowAsJavaScriptException();
return;
}
Buffer buffer;
bool b = SharedUtils::serialize(info[0], buffer);
if (b)
{
CharAllocator charAlloc(pSegment->get_segment_manager());
ShmemBuffer sValue(charAlloc);
SharedUtils::copy(buffer, sValue);
try{
pVector->push_back(sValue);
pSegment->flush();
}
catch (std::exception &e)
{
Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
}
}
else
{
Napi::Error::New(env, "Invalid Parameters").ThrowAsJavaScriptException();
}
}
Napi::Value SharedVector::at(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String r = Napi::String::New(env, "");
size_t length = info.Length();
if (length <= 0 || !info[0].IsNumber())
{
Napi::TypeError::New(env, "Invalid Parameters, Number expected").ThrowAsJavaScriptException();
return r;
}
int32_t pos = info[0].As<Napi::Number>().Int32Value();
if (pos >= pVector->size())
{
Napi::RangeError::New(env, "Invalid Parameter, Out of range").ThrowAsJavaScriptException();
return r;
}
ShmemBuffer sValue = pVector->at(pos);
napi_value value;
bool b = SharedUtils::deserialize(sValue, value);
if (b) {
return Napi::Value(env, value);
}
else
{
Napi::Error::New(env, "Invalid Parameter, Out of range").ThrowAsJavaScriptException();
return r;
}
}
void SharedVector::erase(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t length = info.Length();
if (length <= 0 || !info[0].IsNumber())
{
Napi::TypeError::New(env, "Invalid Parameters, Number expected").ThrowAsJavaScriptException();
return;
}
scoped_lock<interprocess_mutex> lock(pObj->mutex);
ShmemVector::iterator pos = pVector->begin();
int32_t num = info[0].As<Napi::Number>().ToNumber().Int32Value();
pos += num;
pVector->erase(pos);
pSegment->flush();
}
Napi::Value SharedVector::empty(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Boolean::New(env, pVector->empty());
}
void SharedVector::clear(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
scoped_lock<interprocess_mutex> lock(pObj->mutex);
pVector->clear();
pSegment->flush();
};
Napi::Value SharedVector::getValue(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Array r = Napi::Array::New(env);
for (int i = 0; i < pVector->size(); i++)
{
napi_value value;
bool b = SharedUtils::deserialize(pVector->at(i), value);
if (b) {
r[i] = value;
}
else
{
Napi::Error::New(env, "Invalid Parameter, Out of range").ThrowAsJavaScriptException();
return r;
}
}
return r;
}