add all hashes method--the speed is good about 0.58 versus 0.68 if you do it the old Ruby way

This commit is contained in:
Roger Pack 2008-08-29 18:02:50 -06:00
parent b9e2abaa70
commit 266ef93d28
2 changed files with 122 additions and 66 deletions

View File

@ -4,73 +4,59 @@ SHELL = /bin/sh
#### Start of system configuration section. ####
srcdir = .
topdir = /usr/local/include/ruby-1.9.0
hdrdir = /usr/local/include/ruby-1.9.0
arch_hdrdir = /usr/local/include/ruby-1.9.0/$(arch)
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
prefix = $(DESTDIR)/usr/local
topdir = /Users/rogerpack/dev/ruby_186_installed/lib/ruby/1.8/i686-darwin9.4.0
hdrdir = $(topdir)
VPATH = $(srcdir):$(topdir):$(hdrdir)
prefix = $(DESTDIR)/Users/rogerpack/dev/ruby_186_installed
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
sbindir = $(exec_prefix)/sbin
libexecdir = $(exec_prefix)/libexec
datarootdir = $(prefix)/share
datadir = $(datarootdir)
sysconfdir = $(prefix)/etc
sharedstatedir = $(prefix)/com
localstatedir = $(prefix)/var
includedir = $(prefix)/include
oldincludedir = $(DESTDIR)/usr/include
docdir = $(datarootdir)/doc/$(PACKAGE)
infodir = $(datarootdir)/info
htmldir = $(docdir)
dvidir = $(docdir)
pdfdir = $(docdir)
psdir = $(docdir)
libdir = $(exec_prefix)/lib
localedir = $(datarootdir)/locale
mandir = $(datarootdir)/man
sitedir = $(prefix)/lib/ruby/site_ruby
vendordir = $(prefix)/lib/ruby/vendor_ruby
rubyhdrdir = $(includedir)/ruby-$(MAJOR).$(MINOR).$(TEENY)
sitehdrdir = $(rubyhdrdir)/site_ruby
vendorhdrdir = $(rubyhdrdir)/vendor_ruby
rubylibdir = $(libdir)/ruby/$(ruby_version)
docdir = $(datarootdir)/doc/$(PACKAGE)
dvidir = $(docdir)
datarootdir = $(prefix)/share
archdir = $(rubylibdir)/$(arch)
sitelibdir = $(sitedir)/$(ruby_version)
sbindir = $(exec_prefix)/sbin
psdir = $(docdir)
localedir = $(datarootdir)/locale
htmldir = $(docdir)
datadir = $(datarootdir)
includedir = $(prefix)/include
infodir = $(datarootdir)/info
sysconfdir = $(prefix)/etc
mandir = $(datarootdir)/man
libdir = $(exec_prefix)/lib
sharedstatedir = $(prefix)/com
oldincludedir = $(DESTDIR)/usr/include
pdfdir = $(docdir)
sitearchdir = $(sitelibdir)/$(sitearch)
vendorlibdir = $(vendordir)/$(ruby_version)
vendorarchdir = $(vendorlibdir)/$(sitearch)
bindir = $(exec_prefix)/bin
localstatedir = $(prefix)/var
sitelibdir = $(sitedir)/$(ruby_version)
libexecdir = $(exec_prefix)/libexec
CC = gcc
CXX = g++
LIBRUBY = $(LIBRUBY_A)
LIBRUBY = $(LIBRUBY_SO)
LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir)
LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
OUTFLAG = -o
COUTFLAG = -o
RUBY_EXTCONF_H =
CFLAGS = -fPIC -g -O2 $(cflags)
INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir)
DEFS = -D_FILE_OFFSET_BITS=64
CPPFLAGS = -DHAVE_MYSQL_SSL_SET -DHAVE_RB_STR_SET_LEN -DHAVE_MYSQL_MYSQL_H -I/usr/local/include $(DEFS) $(cppflags)
CXXFLAGS = $(CFLAGS) -g -O2
ldflags = -L. -rdynamic -Wl,-export-dynamic
dldflags =
archflag =
DLDFLAGS = $(ldflags) $(dldflags) $(archflag)
LDSHARED = $(CC) -shared
LDSHAREDXX = $(CXX) -shared
CFLAGS = -fno-common -g -O2 -fno-common -pipe -fno-common
INCFLAGS = -I. -I. -I/Users/rogerpack/dev/ruby_186_installed/lib/ruby/1.8/i686-darwin9.4.0 -I.
DEFS =
CPPFLAGS = -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H -I/usr/local/mysql/include -Os -arch i386 -fno-common
CXXFLAGS = $(CFLAGS)
DLDFLAGS = -L.
LDSHARED = cc -dynamic -bundle -undefined suppress -flat_namespace
AR = ar
EXEEXT =
RUBY_INSTALL_NAME = ruby
RUBY_SO_NAME = ruby
arch = i686-linux
sitearch = i686-linux
ruby_version = 1.9.0
ruby = /usr/local/bin/ruby
arch = i686-darwin9.4.0
sitearch = i686-darwin9.4.0
ruby_version = 1.8
ruby = /Users/rogerpack/dev/ruby_186_installed/bin/ruby
RUBY = $(ruby)
RM = rm -f
MAKEDIRS = mkdir -p
@ -83,8 +69,8 @@ COPY = cp
preload =
libpath = . $(libdir) /usr/local/lib
LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir) -L/usr/local/lib -Wl,-R/usr/local/lib
libpath = . $(libdir)
LIBPATH = -L. -L$(libdir)
DEFFILE =
CLEANFILES = mkmf.log
@ -94,22 +80,20 @@ extout =
extout_prefix =
target_prefix =
LOCAL_LIBS =
LIBS = -lmysqlclient -lpthread -lrt -ldl -lcrypt -lm -lc
LIBS = $(LIBRUBYARG_SHARED) -L/usr/local/mysql/lib -lmysqlclient -lz -lm -ldl -lobjc
SRCS = mysql.c
OBJS = mysql.o
TARGET = mysqlplus
DLLIB = $(TARGET).so
TARGET = mysql
DLLIB = $(TARGET).bundle
EXTSTATIC =
STATIC_LIB =
RUBYCOMMONDIR = $(sitedir)$(target_prefix)
RUBYLIBDIR = $(sitelibdir)$(target_prefix)
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
TARGET_SO = $(DLLIB)
CLEANLIBS = $(TARGET).so $(TARGET).il? $(TARGET).tds $(TARGET).map
CLEANLIBS = $(TARGET).bundle $(TARGET).il? $(TARGET).tds $(TARGET).map
CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak
all: $(DLLIB)
@ -143,22 +127,22 @@ site-install-rb: install-rb
.SUFFIXES: .c .m .cc .cxx .cpp .C .o
.cc.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.cxx.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.cpp.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.C.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.c.o:
$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) -c $<
$(DLLIB): $(OBJS)
@-$(RM) $(@)
@-$(RM) $@
$(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)

View File

@ -1064,6 +1064,62 @@ static VALUE fetch_row(VALUE obj)
return ary;
}
/* fetch_hashes_array (internal) */
static VALUE fetch_hashes_array(VALUE obj, VALUE with_table)
{
MYSQL_RES* res = GetMysqlRes(obj);
unsigned int n = mysql_num_fields(res);
MYSQL_ROW row = mysql_fetch_row(res); // need one early to determine the columns
if (row == NULL)
return Qnil;
unsigned long* lengths = mysql_fetch_lengths(res);
MYSQL_FIELD* fields = mysql_fetch_fields(res);
unsigned int i;
VALUE hash;
VALUE colname;
if (with_table == Qnil || with_table == Qfalse) {
colname = rb_iv_get(obj, "colname");
if (colname == Qnil) {
colname = rb_ary_new2(n);
for (i=0; i<n; i++) {
VALUE s = rb_tainted_str_new2(fields[i].name);
rb_obj_freeze(s);
rb_ary_store(colname, i, s);
}
rb_obj_freeze(colname);
rb_iv_set(obj, "colname", colname);
}
} else {
colname = rb_iv_get(obj, "tblcolname");
if (colname == Qnil) {
colname = rb_ary_new2(n);
for (i=0; i<n; i++) {
int len = strlen(fields[i].table)+strlen(fields[i].name)+1;
VALUE s = rb_tainted_str_new(NULL, len);
snprintf(RSTRING_PTR(s), len+1, "%s.%s", fields[i].table, fields[i].name);
rb_obj_freeze(s);
rb_ary_store(colname, i, s);
}
rb_obj_freeze(colname);
rb_iv_set(obj, "tblcolname", colname);
}
}
VALUE ary;
ary = rb_ary_new();
while(row != NULL)
{
hash = rb_hash_new();
for (i=0; i<n; i++) {
rb_hash_aset(hash, rb_ary_entry(colname, i), row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
}
rb_ary_push(ary, hash);
row = mysql_fetch_row(res);
}
return ary;
}
/* fetch_hash2 (internal) */
static VALUE fetch_hash2(VALUE obj, VALUE with_table)
{
@ -1206,6 +1262,21 @@ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
return obj;
}
/* all_hashes(with_table=false) -- returns an array of hashes, one hash per row */
static VALUE all_hashes(int argc, VALUE* argv, VALUE obj)
{
VALUE with_table;
VALUE field_names;
VALUE res;
unsigned int n,i;
check_free(obj);
rb_scan_args(argc, argv, "01", &with_table);
if (with_table == Qnil)
with_table = Qfalse;
return fetch_hashes_array(obj, with_table);
}
/*-------------------------------
* Mysql::Field object method
*/
@ -2155,6 +2226,7 @@ void Init_mysql(void)
rb_define_method(cMysqlRes, "row_tell", row_tell, 0);
rb_define_method(cMysqlRes, "each", each, 0);
rb_define_method(cMysqlRes, "each_hash", each_hash, -1);
rb_define_method(cMysqlRes, "all_hashes", all_hashes, -1);
/* MysqlField object method */
rb_define_method(cMysqlField, "name", field_name, 0);