Class: Ferret::Store::BufferedIndexOutput

Inherits:
IndexOutput
  • Object
show all
Defined in:
lib/ferret/store/buffered_index_io.rb,
ext/index_io.c

Overview

Base implementation class for a buffered IndexOutput.

Instance Method Summary collapse

Constructor Details

#initializeBufferedIndexOutput

Returns a new instance of BufferedIndexOutput.



8
9
10
11
12
# File 'lib/ferret/store/buffered_index_io.rb', line 8

def initialize
  @buffer = BUFFER.clone
  @buffer_start = 0          # position in file of buffer
  @buffer_position = 0       # position in buffer
end

Instance Method Details

#closeObject

**************************************************************************

BufferIndexInput Methods

**************************************************************************



51
52
53
# File 'lib/ferret/store/buffered_index_io.rb', line 51

def close()
  flush()
end

#flushObject

**************************************************************************

BufferIndexInput Methods

**************************************************************************



44
45
46
47
48
# File 'lib/ferret/store/buffered_index_io.rb', line 44

def flush()
  flush_buffer(@buffer, @buffer_position)
  @buffer_start += @buffer_position
  @buffer_position = 0
end

#lengthObject

The number of bytes in the file.

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/ferret/store/buffered_index_io.rb', line 67

def length
  raise NotImplementedError
end

#posObject

Get the current position in the file, where the next write will occur.



56
57
58
# File 'lib/ferret/store/buffered_index_io.rb', line 56

def pos() 
  return @buffer_start + @buffer_position
end

#seek(pos) ⇒ Object

Set the current position in the file, where the next write will occur.



61
62
63
64
# File 'lib/ferret/store/buffered_index_io.rb', line 61

def seek(pos)
  flush()
  @buffer_start = pos
end

#write_byte(rbyte) ⇒ Object

Writes a single byte.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ferret/store/buffered_index_io.rb', line 15

def write_byte(b)

  # The following code offers a 5% speed improvement over the line
  # below. It relies on the fact that ruby will throw an error if we try
  # and modify a character that is out of range for the string.
  #begin
  #  @buffer[@buffer_position] = b
  #  @buffer_position += 1
  #rescue IndexError
  #  flush
  #  @buffer[@buffer_position] = b
  #  @buffer_position += 1
  #end

  flush if @buffer_position >= BUFFER_SIZE
  @buffer[@buffer_position] = b
  @buffer_position += 1
end

#write_bytes(rbuffer, rlen) ⇒ Object

Writes an array of bytes.

buf

the bytes to write

length

the number of bytes to write



37
38
39
40
41
# File 'lib/ferret/store/buffered_index_io.rb', line 37

def write_bytes(buf, length)
  length.times do |i|
    write_byte(buf[i])
  end
end

#write_chars(rstr, rstart, rlength) ⇒ Object



452
453
454
455
456
457
458
459
# File 'ext/index_io.c', line 452

static VALUE
frt_indexout_write_chars(VALUE self, VALUE rstr, VALUE rstart, VALUE rlength)
{
  int start = FIX2INT(rstart);
  int length = FIX2INT(rlength);

  return frt_write_chars(self, rstr, start, length);
}

#write_int(rint) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
# File 'ext/index_io.c', line 356

static VALUE
frt_indexout_write_int(VALUE self, VALUE rint)
{
  long l = NUM2LONG(rint);
  frt_write_byte(self, (l >> 24) & 0xFF);
  frt_write_byte(self, (l >> 16) & 0xFF);
  frt_write_byte(self, (l >>  8) & 0xFF);
  frt_write_byte(self, l & 0xFF);

  return Qnil;
}

#write_long(rlong) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'ext/index_io.c', line 368

static VALUE
frt_indexout_write_long(VALUE self, VALUE rlong)
{
  long long l = NUM2LL(rlong);
  frt_write_byte(self, (l >> 56) & 0xFF);
  frt_write_byte(self, (l >> 48) & 0xFF);
  frt_write_byte(self, (l >> 40) & 0xFF);
  frt_write_byte(self, (l >> 32) & 0xFF);
  frt_write_byte(self, (l >> 24) & 0xFF);
  frt_write_byte(self, (l >> 16) & 0xFF);
  frt_write_byte(self, (l >>  8) & 0xFF);
  frt_write_byte(self, l & 0xFF);

  return Qnil;
}

#write_string(rstr) ⇒ Object



461
462
463
464
465
466
467
468
469
# File 'ext/index_io.c', line 461

static VALUE
frt_indexout_write_string(VALUE self, VALUE rstr)
{
  int len = RSTRING(StringValue(rstr))->len;
  frt_write_vint(self, len);

  frt_write_chars(self, rstr, 0, len);
  return Qnil;
}

#write_uint(ruint) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
# File 'ext/index_io.c', line 384

static VALUE
frt_indexout_write_uint(VALUE self, VALUE ruint)
{
  unsigned long l = NUM2ULONG(ruint);
  frt_write_byte(self, (l >> 24) & 0xFF);
  frt_write_byte(self, (l >> 16) & 0xFF);
  frt_write_byte(self, (l >>  8) & 0xFF);
  frt_write_byte(self, l & 0xFF);

  return Qnil;
}

#write_ulong(rulong) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/index_io.c', line 396

static VALUE
frt_indexout_write_ulong(VALUE self, VALUE rulong)
{
  unsigned long long l;
  l = rb_num2ull(rulong); /* ruby 1.8 doesn't have NUM2ULL. Added in 1.9 */
  frt_write_byte(self, (l >> 56) & 0xFF);
  frt_write_byte(self, (l >> 48) & 0xFF);
  frt_write_byte(self, (l >> 40) & 0xFF);
  frt_write_byte(self, (l >> 32) & 0xFF);
  frt_write_byte(self, (l >> 24) & 0xFF);
  frt_write_byte(self, (l >> 16) & 0xFF);
  frt_write_byte(self, (l >>  8) & 0xFF);
  frt_write_byte(self, l & 0xFF);

  return Qnil;
}

#write_vint(rulong) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'ext/index_io.c', line 425

static VALUE
frt_indexout_write_vint(VALUE self, VALUE rulong)
{
  register unsigned long long i = rb_num2ull(rulong);

  while (i > 127) {
    frt_write_byte(self, (i & 0x7f) | 0x80);
    i >>= 7;
  }
  frt_write_byte(self, i);

  return Qnil;
}

#write_vlong(rulong) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'ext/index_io.c', line 425

static VALUE
frt_indexout_write_vint(VALUE self, VALUE rulong)
{
  register unsigned long long i = rb_num2ull(rulong);

  while (i > 127) {
    frt_write_byte(self, (i & 0x7f) | 0x80);
    i >>= 7;
  }
  frt_write_byte(self, i);

  return Qnil;
}