Skip to content

Commit 1b0cf90

Browse files
committed
fix uv_async usage
1 parent 229faca commit 1b0cf90

4 files changed

Lines changed: 45 additions & 51 deletions

File tree

lib/sqlite3.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ var path = require('path');
33
var util = require('util');
44
var EventEmitter = require('events').EventEmitter;
55

6-
var Database = sqlite3.Database;
7-
var Statement = sqlite3.Statement;
8-
96
function errorCallback(args) {
107
if (typeof args[args.length - 1] === 'function') {
118
var callback = args[args.length - 1];
129
return function(err) { if (err) callback(err); };
1310
}
1411
}
1512

13+
function inherits(target, source) {
14+
for (var k in source.prototype)
15+
target.prototype[k] = source.prototype[k];
16+
}
17+
1618
sqlite3.cached = {
1719
Database: function(file, a, b) {
1820
if (file === '' || file === ':memory:') {
@@ -43,6 +45,13 @@ sqlite3.cached = {
4345
objects: {}
4446
};
4547

48+
49+
var Database = sqlite3.Database;
50+
var Statement = sqlite3.Statement;
51+
52+
inherits(Database, EventEmitter);
53+
inherits(Statement, EventEmitter);
54+
4655
// Database#prepare(sql, [bind1, bind2, ...], [callback])
4756
Database.prototype.prepare = function(sql) {
4857
var params = Array.prototype.slice.call(arguments, 1);
@@ -149,12 +158,6 @@ Database.prototype.removeAllListeners = function(type) {
149158
return val;
150159
};
151160

152-
Database.prototype.emit = EventEmitter.prototype.emit;
153-
Database.prototype.once = EventEmitter.prototype.once;
154-
155-
// util.inherits(Database, EventEmitter);
156-
// util.inherits(Statement, EventEmitter);
157-
158161
// Save the stack trace over EIO callbacks.
159162
sqlite3.verbose = function() {
160163
if (!isVerbose) {

src/statement.cc

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ void Statement::EIO_Each(eio_req *req) {
607607
STATEMENT_INIT(EachBaton);
608608

609609
Async* async = baton->async;
610-
fprintf(stderr, "async:%p\n", async);
611610

612611
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
613612

@@ -620,41 +619,39 @@ void Statement::EIO_Each(eio_req *req) {
620619

621620
if (stmt->Bind(baton->parameters)) {
622621
while (true) {
623-
fprintf(stderr, "before mutex\n");
624622
sqlite3_mutex_enter(mtx);
625623
stmt->status = sqlite3_step(stmt->handle);
626624
if (stmt->status == SQLITE_ROW) {
627625
sqlite3_mutex_leave(mtx);
628626
Row* row = new Row();
629627
GetRow(row, stmt->handle);
630628

631-
// pthread_mutex_lock(&async->mutex);
629+
pthread_mutex_lock(&async->mutex);
632630
async->data.push_back(row);
633631
retrieved++;
634-
// pthread_mutex_unlock(&async->mutex);
635-
636-
fprintf(stderr, "retrieved:%d\n", retrieved);
637-
// uv_async_send(&async->watcher);
632+
pthread_mutex_unlock(&async->mutex);
633+
634+
uv_async_send(&async->watcher);
638635
}
639636
else {
640637
if (stmt->status != SQLITE_DONE) {
641638
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
642639
}
643640
sqlite3_mutex_leave(mtx);
644-
fprintf(stderr, "done\n");
645641
break;
646642
}
647643
}
648644
}
649-
fprintf(stderr, "retrieved:%d\n", retrieved);
650645

651646
async->completed = true;
652-
// uv_async_send(&async->watcher);
647+
uv_async_send(&async->watcher);
653648
}
654649

655650
void Statement::CloseCallback(uv_handle_t* handle) {
656651
assert(handle != NULL);
657-
fprintf(stderr, "close callback\n");
652+
Async* async = static_cast<Async*>(handle->data);
653+
delete async;
654+
handle->data = NULL;
658655
}
659656

660657
void Statement::AsyncEach(uv_async_t* handle, int status) {
@@ -680,16 +677,15 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
680677
Rows::const_iterator it = rows.begin();
681678
Rows::const_iterator end = rows.end();
682679
for (int i = 0; it < end; it++, i++) {
683-
// argv[1] = RowToJS(*it);
680+
argv[1] = RowToJS(*it);
684681
async->retrieved++;
685-
// TRY_CATCH_CALL(async->stmt->handle_, baton->callback, 2, argv);
686-
// delete *it;
682+
TRY_CATCH_CALL(async->stmt->handle_, baton->callback, 2, argv);
683+
delete *it;
687684
}
688685
}
689686
}
690687

691688
if (async->completed) {
692-
fprintf(stderr, "completed\n");
693689
if (!baton->completed.IsEmpty() &&
694690
baton->completed->IsFunction()) {
695691
Local<Value> argv[] = {
@@ -698,9 +694,7 @@ void Statement::AsyncEach(uv_async_t* handle, int status) {
698694
};
699695
TRY_CATCH_CALL(async->stmt->handle_, baton->completed, 2, argv);
700696
}
701-
// uv_close((uv_handle_t*)handle, CloseCallback);
702-
delete async;
703-
handle->data = NULL;
697+
uv_close((uv_handle_t*)handle, CloseCallback);
704698
}
705699
}
706700

src/statement.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,8 @@ class Statement : public ObjectWrap {
165165
stmt(st), baton(eb), completed(false), retrieved(0) {
166166
watcher.data = this;
167167
pthread_mutex_init(&mutex, NULL);
168-
fprintf(stderr, "initialized mutex\n");
169168
stmt->Ref();
170-
fprintf(stderr, "referenced stmt\n");
171169
uv_async_init(uv_default_loop(), &watcher, async_cb);
172-
fprintf(stderr, "started async\n");
173170
}
174171

175172
~Async() {

test/each.test.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ exports['test Statement#each'] = function(beforeExit) {
1919
assert.equal(retrieved, total, "Only retrieved " + retrieved + " out of " + total + " rows.");
2020
});
2121
};
22-
//
23-
// exports['test Statement#each with complete callback'] = function(beforeExit) {
24-
// var db = new sqlite3.Database('test/support/big.db', sqlite3.OPEN_READONLY);
25-
//
26-
// var total = 10000;
27-
// var retrieved = 0;
28-
// var completed = false;
29-
//
30-
// db.each('SELECT id, txt FROM foo LIMIT 0, ?', total, function(err, row) {
31-
// if (err) throw err;
32-
// retrieved++;
33-
// }, function(err, num) {
34-
// assert.equal(retrieved, num);
35-
// completed = true;
36-
// });
37-
//
38-
// beforeExit(function() {
39-
// assert.ok(completed);
40-
// assert.equal(retrieved, total, "Only retrieved " + retrieved + " out of " + total + " rows.");
41-
// });
42-
// };
22+
23+
exports['test Statement#each with complete callback'] = function(beforeExit) {
24+
var db = new sqlite3.Database('test/support/big.db', sqlite3.OPEN_READONLY);
25+
26+
var total = 10000;
27+
var retrieved = 0;
28+
var completed = false;
29+
30+
db.each('SELECT id, txt FROM foo LIMIT 0, ?', total, function(err, row) {
31+
if (err) throw err;
32+
retrieved++;
33+
}, function(err, num) {
34+
assert.equal(retrieved, num);
35+
completed = true;
36+
});
37+
38+
beforeExit(function() {
39+
assert.ok(completed);
40+
assert.equal(retrieved, total, "Only retrieved " + retrieved + " out of " + total + " rows.");
41+
});
42+
};

0 commit comments

Comments
 (0)