/
fb-bindings-blob.cc
331 lines (263 loc) · 9.24 KB
/
fb-bindings-blob.cc
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*!
* Copyright by Denys Khanzhiyev aka xdenser
*
* See license text in LICENSE file
*/
#include <stdlib.h>
#include <v8.h>
#include <node.h>
#include "./fb-bindings-blob.h"
v8::Persistent<v8::FunctionTemplate> FBblob::constructor_template;
char FBblob::err_message[MAX_ERR_MSG_LEN];
void FBblob::Initialize (v8::Handle<v8::Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->Inherit(FBEventEmitter::constructor_template);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->Inherit(FBEventEmitter::constructor_template);
constructor_template->SetClassName(String::NewSymbol("FBblob"));
Local<ObjectTemplate> instance_template =
constructor_template->InstanceTemplate();
NODE_SET_PROTOTYPE_METHOD(t, "_readSync", ReadSync);
NODE_SET_PROTOTYPE_METHOD(t, "_read", Read);
NODE_SET_PROTOTYPE_METHOD(t, "_openSync", OpenSync);
NODE_SET_PROTOTYPE_METHOD(t, "_closeSync", CloseSync);
NODE_SET_PROTOTYPE_METHOD(t, "_writeSync", WriteSync);
instance_template->SetInternalFieldCount(1);
Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
instance_t->SetAccessor(String::NewSymbol("inAsyncCall"),InAsyncGetter);
instance_t->SetAccessor(String::NewSymbol("isReadable"),IsReadGetter);
target->Set(String::NewSymbol("FBblob"), t->GetFunction());
}
bool FBblob::HasInstance(v8::Handle<v8::Value> val)
{
if (!val->IsObject()) return false;
v8::Local<v8::Object> obj = val->ToObject();
/*
if (obj->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray)
return true;
*/
if (constructor_template->HasInstance(obj))
return true;
return false;
}
void FBblob::getId(ISC_QUAD* Idp)
{
*Idp = blob_id;
}
Handle<Value> FBblob::New(const Arguments& args)
{
HandleScope scope;
ISC_QUAD *quad = NULL;
Connection *conn = NULL;
ISC_STATUS_ARRAY status;
if((args.Length() > 0) && !args[0]->IsNull()){
REQ_EXT_ARG(0, js_quad);
quad = static_cast<ISC_QUAD *>(js_quad->Value());
}
if(args.Length() > 1) {
REQ_EXT_ARG(1, js_connection);
conn = static_cast<Connection *>(js_connection->Value());
}
status[1] = 0;
FBblob *res = new FBblob(quad, conn, status);
if(status[1])
return ThrowException(Exception::Error(
String::Concat(String::New("In FBblob::New - "),ERR_MSG_STAT(status, FBblob))));
res->Wrap(args.This());
return args.This();
}
Handle<Value> FBblob::ReadSync(const Arguments& args)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(args.This());
if (!Buffer::HasInstance(args[0])) {
return ThrowException(Exception::Error(
String::New("First argument needs to be a buffer")));
}
Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
//printf("buffer len %d\n",buffer_length);
ISC_STATUS_ARRAY status;
int res = blob->read(status, buffer_data, (unsigned short) buffer_length);
if(res==-1) {
return ThrowException(Exception::Error(
String::Concat(String::New("In FBblob::_readSync - "),ERR_MSG_STAT(status, FBblob))));
}
return scope.Close(Integer::New(res));
}
int FBblob::EIO_After_Read(eio_req *req)
{
ev_unref(EV_DEFAULT_UC);
HandleScope scope;
struct read_request *r_req = (struct read_request *)(req->data);
Local<Value> argv[3];
int argc;
Buffer *slowBuffer = Buffer::New(r_req->buffer, (size_t) r_req->length);
Local<Object> global = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(global->Get(String::New("Buffer")));
Handle<Value> cArgs[3] = {slowBuffer->handle_, Integer::New(r_req->length), Integer::New(0) };
if(r_req->res!=-1)
{
argv[0] = Local<Value>::New(Null());
argv[1] = bufferConstructor->NewInstance(3,cArgs);
argv[2] = Integer::New(r_req->res);
argc = 3;
}
else
{
argv[0] = Exception::Error(
String::Concat(String::New("FBblob::EIO_After_Read - "),ERR_MSG_STAT(r_req->status, FBblob)));
argc = 1;
}
r_req->callback->Call(global, argc, argv);
r_req->callback.Dispose();
r_req->blob->stop_async();
r_req->blob->Unref();
free(r_req);
return 0;
return 0;
}
int FBblob::EIO_Read(eio_req *req)
{
struct read_request *r_req = (struct read_request *)(req->data);
r_req->res = r_req->blob->read(r_req->status,r_req->buffer,(unsigned short) r_req->length);
return 0;
}
Handle<Value> FBblob::Read(const Arguments& args)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(args.This());
if (args.Length() != 2){
return ThrowException(Exception::Error(
String::New("Expecting 4 arguments")));
}
if (!Buffer::HasInstance(args[0])) {
return ThrowException(Exception::Error(
String::New("First argument needs to be a buffer")));
}
Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
if(!args[1]->IsFunction()) {
return ThrowException(Exception::Error(
String::New("Expecting Function as second argument")));
}
struct read_request *r_req =
(struct read_request *)calloc(1, sizeof(struct read_request));
if (!r_req) {
V8::LowMemoryNotification();
return ThrowException(Exception::Error(
String::New("Could not allocate memory.")));
}
r_req->blob = blob;
r_req->callback = Persistent<Function>::New(Local<Function>::Cast(args[1]));
r_req->buffer = buffer_data;
r_req->length = buffer_length;
r_req->res = 0;
blob->start_async();
eio_custom(EIO_Read, EIO_PRI_DEFAULT, EIO_After_Read, r_req);
ev_ref(EV_DEFAULT_UC);
blob->Ref();
return Undefined();
}
Handle<Value>
FBblob::OpenSync(const Arguments& args)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(args.This());
ISC_STATUS_ARRAY status;
if(!blob->open(status)){
return ThrowException(Exception::Error(
String::Concat(String::New("In FBblob::_openSync - "),ERR_MSG_STAT(status, FBblob))));
}
return Undefined();
}
Handle<Value>
FBblob::CloseSync(const Arguments& args)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(args.This());
ISC_STATUS_ARRAY status;
status[1] = 0;
blob->close(status);
if(status[1]){
return ThrowException(Exception::Error(
String::Concat(String::New("In FBblob::_closeSync - "),ERR_MSG_STAT(status, FBblob))));
}
return Undefined();
}
Handle<Value>
FBblob::WriteSync(const Arguments& args)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(args.This());
ISC_STATUS_ARRAY status;
if( (args.Length() > 0) && !Buffer::HasInstance(args[0])) {
return ThrowException(Exception::Error(
String::New("First argument needs to be a buffer")));
}
Local<Object> buffer_obj = args[0]->ToObject();
char *buf = Buffer::Data(buffer_obj);
size_t len = Buffer::Length(buffer_obj);
if( (args.Length() > 1) && args[1]->IsInt32() )
{
int16_t alen = (int16_t) args[1]->IntegerValue();
if(alen < len) len = alen;
}
if(isc_put_segment(status, &blob->handle, len, buf))
return ThrowException(Exception::Error(
String::Concat(String::New("In FBblob::_writeSync - "),ERR_MSG_STAT(status, FBblob))));
return scope.Close(Integer::New(len));
}
Handle<Value>
FBblob::IsReadGetter(Local<String> property,
const AccessorInfo &info)
{
HandleScope scope;
FBblob *blob = ObjectWrap::Unwrap<FBblob>(info.Holder());
return scope.Close(Boolean::New(blob->is_read));
}
FBblob::FBblob(ISC_QUAD *id, Connection *conn, ISC_STATUS *status): FBEventEmitter ()
{
if(id) blob_id = *id;
connection = conn;
is_read = true;
if((id == 0) && (connection != 0))
{
handle = 0;
//blob_id = 0;
isc_create_blob2(status, &(connection->db), &(connection->trans), &handle, &blob_id, 0, NULL);
is_read = false;
}
else handle = NULL;
}
FBblob::~FBblob()
{
if(handle!=0) {
ISC_STATUS_ARRAY status;
close(status);
}
}
bool FBblob::open(ISC_STATUS *status)
{
if(isc_open_blob2(status, &(connection->db), &(connection->trans), &handle, &blob_id, 0, NULL)) return false;
return true;
}
int FBblob::read(ISC_STATUS *status,char *buf, unsigned short len)
{
unsigned short a_read;
int res = isc_get_segment(status, &handle, &a_read, len, buf);
if(res == isc_segstr_eof) return 0;
if(res != isc_segment && status[1] != 0) return -1;
return (int)a_read;
}
bool FBblob::close(ISC_STATUS *status)
{
if( handle == 0 ) return true;
isc_close_blob(status, &handle);
handle = 0;
return true;
}