Brian Candler wrote:
> If so, it sounds to me like what you really want is cdb:
> http://cr.yp.to/cdb.html
> 
> You emit key/value records of the form
> 
> +1,50:1->(50 byte record)
> +1,70:2->(70 byte record)
> +1,60:3->(60 byte record)
> ...
> +2,500:10->(500 byte record)
> ... etc
> 
> then pipe it into cdbmake. The resulting file is built, in a single 
> pass, with a hash index, allowing you to jump to record with key 'x' 
> instantly.

I dug out some old CDB-building code; it's actually very simple with 
ruby-cdb and no piping is required. Here's some code which takes 
table-separated key-value pairs file and builds a cdb:

def cdbmake(src, dest=src+".cdb", mode=0444, uid=0, gid=0)
  require 'cdb'
  File.open(src) do |f1|
    File.delete(file+".tmp") rescue nil
    CDBMake.open(src+".tmp", mode) do |f2|
      f1.each_line do |line|
        line.chomp!
        next if line =~ /^#/ or line =~ /^\s*$/
        if line =~ /^([^\t]*)(\t(.*))?$/
          f2.add($1, $3 || "")
        else
          puts "#{src}: Bad line #{line.inspect} ignored"
        end
      end
    end
  end
  File.rename(src+".tmp", dest)
  File.chown(uid, gid, dest)
rescue
  File.delete(src+".tmp") rescue nil
  raise
end

This used http://raa.ruby-lang.org/project/cdb/ which unfortunately is 
not available as a gem, but it does bundle the cdb source for you.

I see there's now a cdb gem. To install this you need the cdb 
development libraries already installed on your system. Under Ubuntu all 
I needed was:

  sudo apt-get install libcdb-dev
  sudo gem install cdb

Strangely I found it installed all files mode 600. The fix:

  find /usr/lib/ruby/gems/1.8/gems/cdb-0.0.2 -type f | xargs sudo chmod 
644

Anyway, the README shows quite clearly how to use it.
-- 
Posted via http://www.ruby-forum.com/.