Implement a local resultset cache so we can free the mysql C result once we've cached it's rows in ruby land.

This allows the caller to iterate over the Mysql2::Result instance as many times as they want, lazily pulling what rows are needed from the mysql C result.
This commit is contained in:
Brian Lopez 2010-04-21 01:32:54 -07:00
parent efc66b6c06
commit eefa443c2e
3 changed files with 82 additions and 39 deletions

View File

@ -269,28 +269,43 @@ static VALUE rb_mysql_client_affected_rows(VALUE self) {
/* Mysql2::Result */ /* Mysql2::Result */
static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) { static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
VALUE obj; VALUE obj;
obj = Data_Wrap_Struct(cMysql2Result, 0, rb_mysql_result_free, r); mysql2_result_wrapper * wrapper;
obj = Data_Make_Struct(cMysql2Result, mysql2_result_wrapper, rb_mysql_result_mark, rb_mysql_result_free, wrapper);
wrapper->numberOfFields = 0;
wrapper->numberOfRows = 0;
wrapper->lastRow = 0;
wrapper->resultFreed = 0;
wrapper->result = r;
rb_obj_call_init(obj, 0, NULL); rb_obj_call_init(obj, 0, NULL);
return obj; return obj;
} }
void rb_mysql_result_free(void * result) { void rb_mysql_result_free(void * wrapper) {
MYSQL_RES * r = result; mysql2_result_wrapper * w = wrapper;
if (r) { if (w && w->resultFreed != 1) {
mysql_free_result(r); mysql_free_result(w->result);
w->resultFreed = 1;
}
}
void rb_mysql_result_mark(void * wrapper) {
mysql2_result_wrapper * w = wrapper;
if (w) {
rb_gc_mark(w->fieldList);
rb_gc_mark(w->rows);
} }
} }
static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) { static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
VALUE rowHash, opts, block; VALUE rowHash, opts, block;
MYSQL_RES * result; mysql2_result_wrapper * wrapper;
MYSQL_ROW row; MYSQL_ROW row;
MYSQL_FIELD * fields; MYSQL_FIELD * fields;
struct tm parsedTime; struct tm parsedTime;
unsigned int i = 0, numFields = 0, symbolizeKeys = 0; unsigned int i = 0, numFields = 0, symbolizeKeys = 0;
unsigned long * fieldLengths; unsigned long * fieldLengths;
GetMysql2Result(self, result); GetMysql2Result(self, wrapper);
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) { if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
Check_Type(opts, T_HASH); Check_Type(opts, T_HASH);
@ -299,14 +314,14 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
} }
} }
row = mysql_fetch_row(result); row = mysql_fetch_row(wrapper->result);
if (row == NULL) { if (row == NULL) {
return Qnil; return Qnil;
} }
numFields = mysql_num_fields(result); numFields = mysql_num_fields(wrapper->result);
fieldLengths = mysql_fetch_lengths(result); fieldLengths = mysql_fetch_lengths(wrapper->result);
fields = mysql_fetch_fields(result); fields = mysql_fetch_fields(wrapper->result);
rowHash = rb_hash_new(); rowHash = rb_hash_new();
for (i = 0; i < numFields; i++) { for (i = 0; i < numFields; i++) {
@ -402,45 +417,58 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
} }
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) { static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
VALUE dataset, opts, block; VALUE opts, block;
MYSQL_RES * result; mysql2_result_wrapper * wrapper;
unsigned long numRows, i; unsigned long i;
GetMysql2Result(self, result); GetMysql2Result(self, wrapper);
rb_scan_args(argc, argv, "01&", &opts, &block); rb_scan_args(argc, argv, "01&", &opts, &block);
// force-start at the beginning of the result set for proper if (wrapper->lastRow == 0) {
// behavior of #each wrapper->numberOfRows = mysql_num_rows(wrapper->result);
mysql_data_seek(result, 0); if (wrapper->numberOfRows == 0) {
numRows = mysql_num_rows(result);
if (numRows == 0) {
return Qnil; return Qnil;
} }
wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
// TODO: allow yielding datasets of configurable size
// like find_in_batches from AR...
if (block != Qnil) {
for (i = 0; i < numRows; i++) {
VALUE row = rb_mysql_result_fetch_row(argc, argv, self);
if (row == Qnil) {
return Qnil;
} }
rb_yield(row);
if (wrapper->lastRow == wrapper->numberOfRows) {
// we've already read the entire dataset from the C result into our
// internal array. Lets hand that over to the user since it's ready to go
for (i = 0; i < wrapper->numberOfRows; i++) {
rb_yield(rb_ary_entry(wrapper->rows, i));
} }
} else { } else {
dataset = rb_ary_new2(numRows); unsigned long rowsProcessed = 0;
for (i = 0; i < numRows; i++) { rowsProcessed = RARRAY_LEN(wrapper->rows);
VALUE row = rb_mysql_result_fetch_row(argc, argv, self); for (i = 0; i < wrapper->numberOfRows; i++) {
VALUE row;
if (i < rowsProcessed) {
row = rb_ary_entry(wrapper->rows, i);
} else {
row = rb_mysql_result_fetch_row(argc, argv, self);
rb_ary_store(wrapper->rows, i, row);
wrapper->lastRow++;
}
if (row == Qnil) { if (row == Qnil) {
// we don't need the mysql C dataset around anymore, peace it
rb_mysql_result_free(wrapper->result);
return Qnil; return Qnil;
} }
rb_ary_store(dataset, i, row);
if (block != Qnil) {
rb_yield(row);
} }
return dataset;
} }
return Qnil; if (wrapper->lastRow == wrapper->numberOfRows) {
// we don't need the mysql C dataset around anymore, peace it
rb_mysql_result_free(wrapper->result);
}
}
return wrapper->rows;
} }
/* Ruby Extension initializer */ /* Ruby Extension initializer */

View File

@ -43,9 +43,20 @@ static VALUE rb_mysql_client_affected_rows(VALUE self);
void rb_mysql_client_free(void * client); void rb_mysql_client_free(void * client);
/* Mysql2::Result */ /* Mysql2::Result */
#define GetMysql2Result(obj, sval) (sval = (MYSQL_RES*)DATA_PTR(obj)); typedef struct {
VALUE fieldList;
VALUE rows;
unsigned long numberOfFields;
int *types;
unsigned long numberOfRows;
unsigned long lastRow;
int resultFreed;
MYSQL_RES *result;
} mysql2_result_wrapper;
#define GetMysql2Result(obj, sval) (sval = (mysql2_result_wrapper*)DATA_PTR(obj));
VALUE cMysql2Result; VALUE cMysql2Result;
static VALUE rb_mysql_result_to_obj(MYSQL_RES * res); static VALUE rb_mysql_result_to_obj(MYSQL_RES * res);
static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self); static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self);
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self); static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self);
void rb_mysql_result_free(void * result); void rb_mysql_result_free(void * wrapper);
void rb_mysql_result_mark(void * wrapper);

View File

@ -41,6 +41,10 @@ describe Mysql2::Result do
row.keys.first.class.should eql(Symbol) row.keys.first.class.should eql(Symbol)
end end
end end
it "should cache previously yielded results" do
@result.first.should eql(@result.first)
end
end end
context "row data type mapping" do context "row data type mapping" do