Class: Ferret::Store::BufferedIndexInput

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

Overview

Base implementation class for buffered IndexInput

Instance Method Summary collapse

Methods inherited from IndexInput

#close, #length

Constructor Details

#initializeBufferedIndexInput

Returns a new instance of BufferedIndexInput.



85
86
87
88
89
90
# File 'lib/ferret/store/buffered_index_io.rb', line 85

def initialize
  @buffer = nil
  @buffer_start = 0
  @buffer_length = 0
  @buffer_position = 0
end

Instance Method Details

#initialize_copy(orig) ⇒ Object

Creates a clone of the BufferedIndexReader. Reading from a BufferedIndexInput should not change the state (read position) in the clone and vice-versa.



143
144
145
146
# File 'lib/ferret/store/buffered_index_io.rb', line 143

def initialize_copy(o)
  super
  @buffer = o.buffer.clone if o.buffer
end

#posObject

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



124
125
126
# File 'lib/ferret/store/buffered_index_io.rb', line 124

def pos()
  return @buffer_start + @buffer_position
end

#read_byteObject

Read and return a single byte from the file



93
94
95
96
97
98
# File 'lib/ferret/store/buffered_index_io.rb', line 93

def read_byte
  refill if (@buffer_position >= @buffer_length)
  byte = @buffer[@buffer_position]
  @buffer_position += 1
  return byte
end

#read_bytes(rbuf, roffset, rlen) ⇒ Object

Read len bytes into buffer starting at position offset in buffer

buffer

The string buffer to read the characters into.

offset

The position in buffer to start writing to.

len

the number of characters to read

returns

the buffer



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ferret/store/buffered_index_io.rb', line 106

def read_bytes(buffer, offset, len)
  if (len < BUFFER_SIZE) 
    offset.upto(offset+len-1) do |i| # read byte-by-byte
      buffer[i] = read_byte
    end
  else                               # read all-at-once
    start = pos()
    seek_internal(start)
    read_internal(buffer, offset, len)

    @buffer_start = start + len      # adjust stream variables
    @buffer_position = 0
    @buffer_length = 0               # trigger refill on read
  end
  return buffer
end

#read_chars(rbuf, roffset, rlen) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'ext/index_io.c', line 152

static VALUE
frt_indexin_read_bytes(VALUE self, VALUE rbuf, VALUE roffset, VALUE rlen)
{
  int len, offset;

  len = FIX2INT(rlen);
  offset = FIX2INT(roffset);

  return frt_read_bytes(self, rbuf, offset, len);
}

#read_intObject



181
182
183
184
185
186
187
188
# File 'ext/index_io.c', line 181

static VALUE
frt_indexin_read_int(VALUE self)
{
  return LONG2NUM(((long)frt_read_byte(self) << 24) |
                  ((long)frt_read_byte(self) << 16) |
                  ((long)frt_read_byte(self) << 8) |
                   (long)frt_read_byte(self));
}

#read_longObject



190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/index_io.c', line 190

static VALUE
frt_indexin_read_long(VALUE self)
{
  return LL2NUM(((long long)frt_read_byte(self) << 56) |
                ((long long)frt_read_byte(self) << 48) |
                ((long long)frt_read_byte(self) << 40) |
                ((long long)frt_read_byte(self) << 32) |
                ((long long)frt_read_byte(self) << 24) |
                ((long long)frt_read_byte(self) << 16) |
                ((long long)frt_read_byte(self) << 8) |
                 (long long)frt_read_byte(self));
}

#read_stringObject



262
263
264
265
266
267
268
269
270
271
# File 'ext/index_io.c', line 262

static VALUE
frt_indexin_read_string(VALUE self)
{
  int length = (int)frt_read_vint(self);
  char *str = (char *)ALLOC_N(char, length);

  frt_read_chars(self, str, 0, length);

  return rb_str_new(str, length);
}

#read_uintObject



203
204
205
206
207
208
209
210
# File 'ext/index_io.c', line 203

static VALUE
frt_indexin_read_uint(VALUE self)
{
  return ULONG2NUM(((unsigned long)frt_read_byte(self) << 24) |
                   ((unsigned long)frt_read_byte(self) << 16) |
                   ((unsigned long)frt_read_byte(self) << 8) |
                    (unsigned long)frt_read_byte(self));
}

#read_ulongObject



212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/index_io.c', line 212

static VALUE
frt_indexin_read_ulong(VALUE self)
{
  return ULL2NUM(((unsigned long long)frt_read_byte(self) << 56) |
                 ((unsigned long long)frt_read_byte(self) << 48) |
                 ((unsigned long long)frt_read_byte(self) << 40) |
                 ((unsigned long long)frt_read_byte(self) << 32) |
                 ((unsigned long long)frt_read_byte(self) << 24) |
                 ((unsigned long long)frt_read_byte(self) << 16) |
                 ((unsigned long long)frt_read_byte(self) << 8) |
                  (unsigned long long)frt_read_byte(self));
}

#read_vintObject



243
244
245
246
247
# File 'ext/index_io.c', line 243

static VALUE
frt_indexin_read_vint(VALUE self)
{
  return ULL2NUM(frt_read_vint(self));
}

#read_vlongObject



243
244
245
246
247
# File 'ext/index_io.c', line 243

static VALUE
frt_indexin_read_vint(VALUE self)
{
  return ULL2NUM(frt_read_vint(self));
}

#refillObject

Refill the buffer from the file.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ferret/store/buffered_index_io.rb', line 170

def refill
  start = @buffer_start + @buffer_position
  last = start + BUFFER_SIZE
  if (last > length())               # don't read past EOF
    last = length()
  end
  @buffer_length = last - start
  if (@buffer_length <= 0)
    raise EOFError
  end

  if (@buffer == nil)
    @buffer = BUFFER.clone # allocate buffer lazily
  end
  read_internal(@buffer, 0, @buffer_length)

  @buffer_start = start
  @buffer_position = 0
end

#seek(rpos) ⇒ Object

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/ferret/store/buffered_index_io.rb', line 129

def seek(pos)
  if (pos >= @buffer_start and pos < (@buffer_start + @buffer_length))
    @buffer_position = pos - @buffer_start  # seek within buffer
  else 
    @buffer_start = pos
    @buffer_position = 0
    @buffer_length = 0               # trigger refill() on read()
    seek_internal(pos)
  end
end