class LibCDB::CDB::Writer

The writer for creating CDB files. See Reader for reading them.

Public Class Methods

new(io) → aWriter click to toggle source

Creates a new Writer instance to interface with io. io must be opened for writing (w); in addition, it must be opened for read-write (w+) if insert or replace are to be used.

static VALUE
rcdb_writer_initialize(VALUE self, VALUE io) {
  RCDB_INITIALIZE(writ, WRIT, cdb_make, make_start)

  if (lseek(cdb_fileno(ptr), 0, SEEK_SET) == -1) {
    rb_sys_fail(0);
  }

  return self;
}

Public Instance Methods

<<(*args)
Alias for: store
[]=(*args)
Alias for: replace
add(*args)
Alias for: store
close → nil click to toggle source

Closes writer, as well as the underlying IO object.

static VALUE
rcdb_writer_close(VALUE self) {
  struct cdb_make *cdbm = NULL;

  if (RTEST(rcdb_writer_closed_p(self))) {
    return Qnil;
  }

  RCDB_WRITER_GET(self, cdbm);
  rb_iv_set(self, "closed", Qtrue);

  if (cdb_make_finish(cdbm) == -1) {
    rb_sys_fail(0);
  }

  rb_io_close(rb_iv_get(self, "@io"));

  return Qnil;
}
closed? → true | false click to toggle source

Whether writer is closed.

static VALUE
rcdb_writer_closed_p(VALUE self) {
  return rb_attr_get(self, rb_intern("closed"));
}
insert(key, val) → writer click to toggle source
insert(key, [val, ...]) → writer
insert([key, ...]) → writer
insert({ key => val, ... }) → writer

Stores records in the database and returns writer. Records will only be inserted if they don't already exist in the database.

The arguments are treated the same as in store, so duplicate keys in the arguments will produce multiple records.

static VALUE
rcdb_writer_insert(int argc, VALUE *argv, VALUE self) {
  return rcdb_writer_put(argc, argv, self, CDB_PUT_INSERT);
}
replace(key, val) → writer click to toggle source
replace(key, [val, ...]) → writer
replace([key, ...]) → writer
replace({ key => val, ... }) → writer

Stores records in the database and returns writer. Records with duplicate keys are replaced.

The arguments are treated the same as in store, so duplicate keys in the arguments will produce multiple records.

static VALUE
rcdb_writer_replace(int argc, VALUE *argv, VALUE self) {
  return rcdb_writer_put(argc, argv, self, CDB_PUT_REPLACE);
}
Also aliased as: []=
store(key, val) → writer click to toggle source
store(key, [val, ...]) → writer
store([key, ...]) → writer
store({ key => val, ... }) → writer

Stores records in the database and returns writer. Records are stored unconditionally, so duplicate keys will produce multiple records.

If a single key/value pair is given, a record with key key and value val is created; if val is an array, one record per value is created for key. If an array of keys is given, one record per key with an empty value is created. If a hash of key/value pairs is given, one record per key/value pair is created; the same logic as for a single key/value pair applies.

static VALUE
rcdb_writer_store(int argc, VALUE *argv, VALUE self) {
  return rcdb_writer_put(argc, argv, self, CDB_PUT_ADD);
}
Also aliased as: <<, add