Class: LibCDB::CDB::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/libcdb/ruby_cdb_reader.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(io) ⇒ aReader

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



25
26
27
28
29
30
# File 'ext/libcdb/ruby_cdb_reader.c', line 25

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;
}

Instance Attribute Details

#encodingObject

Instance Method Details

#closenil

Closes reader, as well as the underlying IO object.

Returns:

  • (nil)


487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/libcdb/ruby_cdb_reader.c', line 487

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?Boolean

Returns:

  • (Boolean)

#dumpaString

Returns a dump of the database.

Returns:

  • (aString)


449
450
451
452
# File 'ext/libcdb/ruby_cdb_reader.c', line 449

static VALUE
rcdb_reader_dump(VALUE self) {
  RCDB_READER_ITERATE(each_dump, iter_dump, rb_str_new2(""))
}

#each {|key, val| ... } ⇒ Object #each(key) {|val| ... } ⇒ Object #each([key]) ⇒ Enumerator

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.

Overloads:

  • #each {|key, val| ... } ⇒ Object

    Yields:

  • #each(key) {|val| ... } ⇒ Object

    Yields:

    • (val)
  • #each([key]) ⇒ Enumerator

    Returns:

    • (Enumerator)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/libcdb/ruby_cdb_reader.c', line 139

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| ... } ⇒ Object #each_dump(key) {|dump| ... } ⇒ Object #each_dump([key]) ⇒ Enumerator

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.

Overloads:

  • #each_dump {|dump| ... } ⇒ Object

    Yields:

  • #each_dump(key) {|dump| ... } ⇒ Object

    Yields:

  • #each_dump([key]) ⇒ Enumerator

    Returns:

    • (Enumerator)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'ext/libcdb/ruby_cdb_reader.c', line 182

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| ... } ⇒ Object #each_keyEnumerator

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

Overloads:

  • #each_key {|key| ... } ⇒ Object

    Yields:

  • #each_keyEnumerator

    Returns:

    • (Enumerator)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'ext/libcdb/ruby_cdb_reader.c', line 208

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| ... } ⇒ Object #each_valueEnumerator

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

Overloads:

  • #each_value {|val| ... } ⇒ Object

    Yields:

    • (val)
  • #each_valueEnumerator

    Returns:

    • (Enumerator)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/libcdb/ruby_cdb_reader.c', line 236

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?Boolean

Whether the database is empty.

Returns:

  • (Boolean)


402
403
404
405
406
# File 'ext/libcdb/ruby_cdb_reader.c', line 402

static VALUE
rcdb_reader_empty_p(VALUE self) {
  RCDB_READER_ITERATE_ARY(each_key, break_shift,
    rb_ary_new3(2, Qtrue, Qfalse))
}

#fetch(key) ⇒ Array Also known as: fetch_all

Fetch all values for key.

Returns:

  • (Array)


258
259
260
261
262
263
# File 'ext/libcdb/ruby_cdb_reader.c', line 258

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(key) ⇒ aString | nil Also known as: [], get

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

Returns:

  • (aString | nil)


271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'ext/libcdb/ruby_cdb_reader.c', line 271

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(key) ⇒ aString | nil Also known as: rget

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

Returns:

  • (aString | nil)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/libcdb/ruby_cdb_reader.c', line 292

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;
}

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Whether key key exists in the database.

Returns:

  • (Boolean)


365
366
367
368
369
# File 'ext/libcdb/ruby_cdb_reader.c', line 365

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))
}

#has_value?(val) ⇒ Boolean Also known as: value?

Whether value val exists in the database.

Returns:

  • (Boolean)


377
378
379
380
381
# File 'ext/libcdb/ruby_cdb_reader.c', line 377

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))
}

#inspectObject

#key(val) ⇒ aString | nil

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

Returns:

  • (aString | nil)


390
391
392
393
394
# File 'ext/libcdb/ruby_cdb_reader.c', line 390

static VALUE
rcdb_reader_key(VALUE self, VALUE val) {
  RCDB_READER_ITERATE_ARY(each, break_equal2,
    rb_ary_new3(2, Qnil, val))
}

#keysArray

Returns an array of all unique keys.

Returns:

  • (Array)


325
326
327
328
# File 'ext/libcdb/ruby_cdb_reader.c', line 325

static VALUE
rcdb_reader_keys(VALUE self) {
  RCDB_READER_ITERATE(each_key, iter_push, rb_ary_new())
}

#sizeanInteger Also known as: length

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

Returns:

  • (anInteger)


414
415
416
417
418
419
# File 'ext/libcdb/ruby_cdb_reader.c', line 414

static VALUE
rcdb_reader_size(VALUE self) {
  long i = 0;
  RCDB_READER_ITERATE1(each_key, iter_inc, (VALUE)&i)
  return LONG2NUM(i);
}

#to_aArray

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

reader.to_a.size == reader.total

Returns:

  • (Array)


476
477
478
479
# File 'ext/libcdb/ruby_cdb_reader.c', line 476

static VALUE
rcdb_reader_to_a(VALUE self) {
  RCDB_READER_ITERATE(each, iter_push, rb_ary_new())
}

#to_hHash

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

Returns:

  • (Hash)


463
464
465
466
# File 'ext/libcdb/ruby_cdb_reader.c', line 463

static VALUE
rcdb_reader_to_h(VALUE self) {
  RCDB_READER_ITERATE(each, iter_aset, rb_hash_new())
}

#totalanInteger

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

Returns:

  • (anInteger)


427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'ext/libcdb/ruby_cdb_reader.c', line 427

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);
}

#valuesArray

Returns an array of all values.

Returns:

  • (Array)


336
337
338
339
# File 'ext/libcdb/ruby_cdb_reader.c', line 336

static VALUE
rcdb_reader_values(VALUE self) {
  RCDB_READER_ITERATE(each_value, iter_push, rb_ary_new())
}

#values_at(key, ...) ⇒ Array

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

Returns:

  • (Array)


347
348
349
350
351
352
353
354
355
356
357
# File 'ext/libcdb/ruby_cdb_reader.c', line 347

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;
}