Ext/gdbm and multiple readers at a time

I sent this to ruby-core but got no answer. I guess I generated too
much noise in the past there (Jenkins & hashes) :stuck_out_tongue:

However, I’d like to know if the following is sensible. I need
concurrent access to a GDBM database and the following seems to make
the trick.

···

man gdbm says

A process that opens a gdbm file is designated as a “reader” or a
“writer”. Only one writer may open a gdbm file and many readers
============
may open the file. Readers and writers can not open the gdbm
file at the same time.

However the gdbm extension didn’t seem to allow that.
I ended up doing the following:

— ruby-1.8.0.orig/ext/gdbm/gdbm.c Tue Jun 3 13:25:17 2003
+++ ruby-1.8.0/ext/gdbm/gdbm.c Tue Jun 3 13:27:17 2003
@@ -97,6 +97,8 @@

 if (!NIL_P(vflags))
     flags = NUM2INT(vflags);
  • else

  •    flags = GDBM_READER;
    

    SafeStringValue(file);

@@ -106,10 +108,7 @@
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
if (!dbm)
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,

  •                   GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
    
  • if (!dbm)
  •   dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
    
  •                   GDBM_READER|flags, 0, MY_FATAL_FUNC);
    
  •                   flags, 0, MY_FATAL_FUNC);
    

    if (!dbm) {
    if (mode == -1) return Qnil;
    @@ -974,12 +973,12 @@
    rb_define_method(rb_cGDBM, “to_hash”, fgdbm_to_hash, 0);

    /* flags for gdbm_opn() */

  • /*
  • rb_define_const(rb_cGDBM, “READER”, INT2FIX(GDBM_READER));
    rb_define_const(rb_cGDBM, “WRITER”, INT2FIX(GDBM_WRITER));
    rb_define_const(rb_cGDBM, “WRCREAT”, INT2FIX(GDBM_WRCREAT));
    rb_define_const(rb_cGDBM, “NEWDB”, INT2FIX(GDBM_NEWDB));
  • */
  • rb_define_const(rb_cGDBM, “FAST”, INT2FIX(GDBM_FAST));
    /* this flag is obsolete in gdbm 1.8.
    On gdbm 1.8, fast mode is default behavior. */

Now I can do
require ‘gdbm’
GDBM.open(“something.db”, GDBM::READER) do |db|
… = db[“bla”]
end

in several processes/threads at a time.

My patch is so simple there must be something wrong about it, otherwise
it’d be in Ruby already :stuck_out_tongue:


Comments appreciated.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

…[Linux’s] capacity to talk via any medium except smoke signals.
– Dr. Greg Wettstein, Roger Maris Cancer Center