Venkat
10/4/2007 9:23:00 PM
James Edward Gray II wrote:
>> I wrote a class called "CSVImport" using the default CSV class. Today,
>> I modified it to work with FasterCSV class by changing the requires as
>> below:
>>
>> begin
>> require 'rubygems'
>> require 'faster_csv'
>> FasterCSV.build_csv_interface
>> rescue
>> require 'csv'
>> end
>> # Rest of the code
>
> You have a bug in that. A bare rescue will not catch the LoadError we
> are interested in this case. Therefore, using just the above code, I
> don't believe CSV would ever be loaded. But we know it is loaded because?
I changed that to
rescue LoadError
> My leading guess is that your code is loading CSV with some code you
> didn't show me.
I think I found it.
I am using a module called CSVDBUtils:
# start of csvutils.rb
module CSVDBUtils
#
end
require 'csvexp'
require 'csvimp'
CSVImport = CSVDBUtils::CSVImport
CSVExport = CSVDBUtils::CSVExport
# end of csvutils.rb
The csvdbutils.rb requires 'csvimp' and 'csvexp' and declares, for ease
of use, CSVImport and CSVExport. If I don't do this, users of CSVImport
and CSVExport classes have to instatiate with
CSVDBUtils::CSVImport.new
CSVDBUtils::CSVExport.new
# any other classes added later.
I think the reason the warning was generated was that csvexp.rb
requires 'csv'.
I think *FasterCSV#build_csv_interface* should include a check/test if
it has been done already and not do it if the interface is built. I am
not sure, how it can be done though.
In an application with different ruby source files using CSV and /
FasterCSV, it would be difficult to make sure build_csv_interface is
called only once or that CSV and FasterCSV aren't both used.
I changed the csvutils.rb to following and it doesn't generate any warnings.
# csvutils.rb
begin
require 'rubygems'
require 'faster_csv'
FasterCSV.build_csv_interface
rescue LoadError
require 'csv'
end
require 'csvexp'
require 'csvimp'
.....
.....
# rest of the code
Ofcourse, I had to removed requires from both csvimp and csvexp.
Thanks for quick response and I appreciate your help. I am able to
reduce the time of import of 3000 rows from 90 seconds to less than 9
seconds using batch import (by submitting in bulk instead of row by row)
and FasterCSV.
Inorder to reduce the time further, I think I need to be able to collect
the rows in a single call instead of iterating through rows. Is there a
fill_array or each_n or similar?
-Venkat