class LibCDB::CDB::Reader
The reader for reading CDB files. See Writer for creating them.
Attributes
Public Class Methods
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
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;
}
Whether reader is closed.
static VALUE
rcdb_reader_closed_p(VALUE self) {
return rb_attr_get(self, rb_intern("closed"));
}
Returns a dump of the database.
static VALUE
rcdb_reader_dump(VALUE self) {
RCDB_READER_ITERATE(each_dump, iter_dump, rb_str_new2(""))
}
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;
}
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;
}
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;
}
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;
}
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 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;
}
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;
}
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;
}
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))
}
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))
}
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))
}
Returns an array of all unique keys.
static VALUE
rcdb_reader_keys(VALUE self) {
RCDB_READER_ITERATE(each_key, iter_push, rb_ary_new())
}
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);
}
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())
}
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())
}
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);
}
Returns an array of all values.
static VALUE
rcdb_reader_values(VALUE self) {
RCDB_READER_ITERATE(each_value, iter_push, rb_ary_new())
}
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;
}