Skip to content

Commit

Permalink
async blob reading tested
Browse files Browse the repository at this point in the history
  • Loading branch information
xdenser committed Mar 12, 2011
1 parent cd1d0af commit 0b75661
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
59 changes: 59 additions & 0 deletions firebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,65 @@ MakeSafe(Connection,"rollback");
MakeSafe(FBResult,"fetch");


binding.FBblob.prototype._readAll = function(initialSize, chunkSize, callback){
if(initialSize instanceof Function)
{
callback = initialSize;
chunkSize = null;
initialSize = null;
}
else
if(chunkSize instanceof Function)
{
callback = chunkSize;
chunkSize = null;
}

if(!chunkSize) chunkSize = 1024;
if(!initialSize) initialSize = 0;

var chunk = new Buffer(chunkSize);
var res = new Buffer(initialSize);
var cPos = 0;


if(callback)
{
this._openSync();
var blob = this;
this._read(chunk,function receiver(err,b,len){
if(err)
{
blob.Emit('error',err);
}
else
if(len>0)
{

blob.Emit('drain', chunk, len);
if(res.length<=(cPos+len))
{
var nr = new Buffer(cPos+len);
res.copy(nr);
res = nr;
}
chunk.copy(res,cPos,0,len);
cPos = cPos + len;
blob._read(chunk,receiver);
}
else
{
blob.Emit('end',res,cPos);
callback(null, res, cPos);
}
});

this._closeSync();
}


}


exports.createConnection = function () {
var c = new Connection;
Expand Down
25 changes: 18 additions & 7 deletions src/fb-bindings-blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,29 @@ 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[2];
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) };

argv[0] = bufferConstructor->NewInstance(3,cArgs);
argv[1] = Integer::New(r_req->res);

r_req->callback->Call(global, 2, argv);
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("While query - "),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();
Expand All @@ -119,8 +131,7 @@ int FBblob::EIO_After_Read(eio_req *req)
int FBblob::EIO_Read(eio_req *req)
{
struct read_request *r_req = (struct read_request *)(req->data);
ISC_STATUS_ARRAY status;
r_req->res = r_req->blob->read(status,r_req->buffer,(unsigned short) r_req->length);
r_req->res = r_req->blob->read(r_req->status,r_req->buffer,(unsigned short) r_req->length);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/fb-bindings-blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class FBblob : public FBEventEmitter {
char* buffer;
size_t length;
int res;
ISC_STATUS_ARRAY status;
};

static int EIO_After_Read(eio_req *req);
Expand Down
13 changes: 13 additions & 0 deletions tests/def/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,18 @@ module.exports = testCase({
res._closeSync();
test.equal(buf.toString('utf8',0,len),Data,'blob ');
test.done();
},
asyncRead: function(test){
test.expect(2);
Data = 'some data for async blob';
var res = util.getDataTypeResult.call(this,'BLOB SUB_TYPE 1',util.quote(Data));
var buf = new Buffer(1024);
res._openSync();
res._read(buf,function(err,b,len){
test.ifError(err);
res._closeSync();
test.equal(b.toString('utf8',0,len),Data,'blob ');
test.done();
});
}
});

0 comments on commit 0b75661

Please sign in to comment.