class LibCDB::CDB::Reader

The reader for reading CDB files. See Writer for creating them.

Attributes

encoding[RW]

Public Class Methods

new(io) → aReader click to toggle source

Creates a new Reader instance to interface with io. io must be opened for reading (r).

static VALUE
rcdb_reader_initialize(VALUE self, VALUE io) {
  RCDB_INITIALIZE(read, READ, cdb, init)
  rb_iv_set(self, "@encoding", rb_enc_default_external());
  return self;
}

Public Instance Methods

[](p1)
Alias for: fetch_first
close → nil click to toggle source

Closes reader, as well as the underlying IO object.

static VALUE
rcdb_reader_close(VALUE self) {
  struct cdb *cdb = NULL;

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

  RCDB_READER_GET(self, cdb);
  rb_iv_set(self, "closed", Qtrue);

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

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

Whether reader is closed.

static VALUE
rcdb_reader_closed_p(VALUE self) {
  return rb_attr_get(self, rb_intern("closed"));
}
dump → aString click to toggle source

Returns a dump of the database.

static VALUE
rcdb_reader_dump(VALUE self) {
  RCDB_READER_ITERATE(each_dump, iter_dump, rb_str_new2(""))
}
each { |key, val| ... } → reader click to toggle source
each(key) { |val| ... } → reader
each([key]) → anEnumerator

Iterates over each key/value pair, or, if key is given, each value for key. Returns reader, or, if no block is given, an enumerator.

static VALUE
rcdb_reader_each(int argc, VALUE *argv, VALUE self) {
  struct cdb *cdb = NULL;
  struct cdb_find cdbf;
  unsigned cdbp;
  VALUE key;

  RCDB_RETURN_ENUMERATOR(1);
  RCDB_READER_GET(self, cdb);

  if (rb_scan_args(argc, argv, "01", &key) == 1 && !NIL_P(key)) {
    StringValue(key);

    if (cdb_findinit(&cdbf, cdb, RSTRING_PTR(key), RSTRING_LEN(key)) == -1) {
      rb_sys_fail(0);
    }

    while (cdb_findnext(&cdbf) > 0) {
      rb_yield(RCDB_READER_READ(data));
    }
  }
  else {
    cdb_seqinit(&cdbp, cdb);

    while (cdb_seqnext(&cdbp, cdb) > 0) {
      rb_yield(rb_ary_new3(2,
        RCDB_READER_READ(key),
        RCDB_READER_READ(data)));
    }
  }

  return self;
}
each_dump { |dump| ... } → reader click to toggle source
each_dump(key) { |dump| ... } → reader
each_dump([key]) → anEnumerator

Iterates over each record dump, or, if key is given, each record dump for key. Returns reader, or, if no block is given, an enumerator.

static VALUE
rcdb_reader_each_dump(int argc, VALUE *argv, VALUE self) {
  VALUE key;

  RCDB_RETURN_ENUMERATOR(1);

  if (rb_scan_args(argc, argv, "01", &key) == 1 && !NIL_P(key)) {
    StringValue(key);

    RCDB_READER_ITERATE0(each, yield_dump2, rb_ary_new3(1, key))
  }
  else {
    RCDB_READER_ITERATE1(each, yield_dump, rb_ary_new())
  }

  return self;
}
each_key { |key| ... } → reader click to toggle source
each_key → anEnumerator

Iterates over each unique key. Returns reader, or, if no block is given, an enumerator.

static VALUE
rcdb_reader_each_key(VALUE self) {
  struct cdb *cdb = NULL;
  unsigned cdbp;
  VALUE key, hash = rb_hash_new();

  RCDB_RETURN_ENUMERATOR_NONE;
  RCDB_READER_GET(self, cdb);
  cdb_seqinit(&cdbp, cdb);

  while (cdb_seqnext(&cdbp, cdb) > 0) {
    if (NIL_P(rb_hash_lookup(hash, key = RCDB_READER_READ(key)))) {
      rb_hash_aset(hash, key, Qtrue);
      rb_yield(key);
    }
  }

  return self;
}
each_value { |val| ... } → reader click to toggle source
each_value → anEnumerator

Iterates over each value. Returns reader, or, if no block is given, an enumerator.

static VALUE
rcdb_reader_each_value(VALUE self) {
  struct cdb *cdb = NULL;
  unsigned cdbp;

  RCDB_RETURN_ENUMERATOR_NONE;
  RCDB_READER_GET(self, cdb);
  cdb_seqinit(&cdbp, cdb);

  while (cdb_seqnext(&cdbp, cdb) > 0) {
    rb_yield(RCDB_READER_READ(data));
  }

  return self;
}
empty? → true | false click to toggle source

Whether the database is empty.

static VALUE
rcdb_reader_empty_p(VALUE self) {
  RCDB_READER_ITERATE_ARY(each_key, break_shift,
    rb_ary_new3(2, Qtrue, Qfalse))
}
fetch(key) → anArray click to toggle source

Fetch all values for key.

static VALUE
rcdb_reader_fetch(VALUE self, VALUE key) {
  VALUE ary = rb_ary_new();
  RCDB_READER_ITERATE0(each, iter_push, ary)
  return ary;
}
Also aliased as: fetch_all
fetch_all(p1)
Alias for: fetch
fetch_first(key) → aString | nil click to toggle source

Fetch first value for key. Returns nil if key was not found.

static VALUE
rcdb_reader_fetch_first(VALUE self, VALUE key) {
  struct cdb *cdb = NULL;
  VALUE val = Qnil;

  StringValue(key);
  RCDB_READER_GET(self, cdb);

  if (cdb_find(cdb, RSTRING_PTR(key), RSTRING_LEN(key)) > 0) {
    val = RCDB_READER_READ(data);
  }

  return val;
}
Also aliased as: [], get
fetch_last(key) → aString | nil click to toggle source

Fetch last value for key. Returns nil if key was not found.

static VALUE
rcdb_reader_fetch_last(VALUE self, VALUE key) {
  struct cdb *cdb = NULL;
  struct cdb_find cdbf;
  VALUE val = Qnil;
  unsigned pos = 0;
  size_t len = 0;

  StringValue(key);
  RCDB_READER_GET(self, cdb);

  if (cdb_findinit(&cdbf, cdb, RSTRING_PTR(key), RSTRING_LEN(key)) == -1) {
    rb_sys_fail(0);
  }

  while (cdb_findnext(&cdbf) > 0) {
    pos = cdb_datapos(cdb);
    len = cdb_datalen(cdb);
  }

  if (pos > 0) {
    RCDB_READER_READ_POS(pos)
  }

  return val;
}
Also aliased as: rget
get(p1)
Alias for: fetch_first
has_key?(key) → true | false click to toggle source

Whether key key exists in the database.

static VALUE
rcdb_reader_has_key_p(VALUE self, VALUE key) {
  RCDB_READER_ITERATE_ARY(each_key, break_equal,
    rb_ary_new3(2, Qfalse, key))
}
Also aliased as: include?, key?, member?
has_value?(val) → true | false click to toggle source

Whether value val exists in the database.

static VALUE
rcdb_reader_has_value_p(VALUE self, VALUE val) {
  RCDB_READER_ITERATE_ARY(each_value, break_equal,
    rb_ary_new3(2, Qfalse, val))
}
Also aliased as: value?
include?(p1)
Alias for: has_key?
key(val) → aString | nil click to toggle source

Returns the first key associated with value val, or nil if val was not found.

static VALUE
rcdb_reader_key(VALUE self, VALUE val) {
  RCDB_READER_ITERATE_ARY(each, break_equal2,
    rb_ary_new3(2, Qnil, val))
}
key?(p1)
Alias for: has_key?
keys → anArray click to toggle source

Returns an array of all unique keys.

static VALUE
rcdb_reader_keys(VALUE self) {
  RCDB_READER_ITERATE(each_key, iter_push, rb_ary_new())
}
length()
Alias for: size
member?(p1)
Alias for: has_key?
rget(p1)
Alias for: fetch_last
size → anInteger click to toggle source

The number of unique records in the database. Cf. total.

static VALUE
rcdb_reader_size(VALUE self) {
  long i = 0;
  RCDB_READER_ITERATE1(each_key, iter_inc, (VALUE)&i)
  return LONG2NUM(i);
}
Also aliased as: length
to_a → anArray click to toggle source

Converts the database into an array of total key/value pairs.

reader.to_a.size == reader.total
static VALUE
rcdb_reader_to_a(VALUE self) {
  RCDB_READER_ITERATE(each, iter_push, rb_ary_new())
}
to_h → aHash click to toggle source

Converts the database into a hash of size keys associated with their value, or, if there are multiple, an array of their values.

reader.to_h.size == reader.size
static VALUE
rcdb_reader_to_h(VALUE self) {
  RCDB_READER_ITERATE(each, iter_aset, rb_hash_new())
}
total → anInteger click to toggle source

The number of total records in the database. Cf. size.

static VALUE
rcdb_reader_total(VALUE self) {
  struct cdb *cdb = NULL;
  unsigned cdbp;
  long i = 0;

  RCDB_READER_GET(self, cdb);
  cdb_seqinit(&cdbp, cdb);

  while (cdb_seqnext(&cdbp, cdb) > 0) {
    ++i;
  }

  return LONG2NUM(i);
}
value?(p1)
Alias for: has_value?
values → anArray click to toggle source

Returns an array of all values.

static VALUE
rcdb_reader_values(VALUE self) {
  RCDB_READER_ITERATE(each_value, iter_push, rb_ary_new())
}
values_at(key, ...) → anArray click to toggle source

Returns an array containing the values associated with the given keys.

static VALUE
rcdb_reader_values_at(int argc, VALUE *argv, VALUE self) {
  VALUE ary = rb_ary_new();
  int i;

  for (i = 0; i < argc; i++) {
    rb_ary_push(ary, rcdb_reader_fetch(self, argv[i]));
  }

  return ary;
}