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



140
141
142
143
144
145
146
147
148
149
# File 'ext/index_io.c', line 140

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



169
170
171
172
173
174
175
176
177
# File 'ext/index_io.c', line 169

VALUE
frt_indexin_read_int(VALUE self)
{
  GET_MY_BUF;
  return LONG2NUM(((int)frt_read_byte(self, my_buf) << 24) |
                  ((int)frt_read_byte(self, my_buf) << 16) |
                  ((int)frt_read_byte(self, my_buf) << 8) |
                   (int)frt_read_byte(self, my_buf));
}

#read_longObject



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/index_io.c', line 179

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

#read_stringObject



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'ext/index_io.c', line 258

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

  frt_read_chars(self, str, 0, length);

  return rb_str_new(str, length);
}

#read_uintObject



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

static VALUE
frt_indexin_read_uint(VALUE self)
{
  GET_MY_BUF;
  return ULONG2NUM(((unsigned int)frt_read_byte(self, my_buf) << 24) |
                   ((unsigned int)frt_read_byte(self, my_buf) << 16) |
                   ((unsigned int)frt_read_byte(self, my_buf) << 8) |
                    (unsigned int)frt_read_byte(self, my_buf));
}

#read_ulongObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/index_io.c', line 203

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

#read_vintObject



235
236
237
238
239
240
# File 'ext/index_io.c', line 235

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

#read_vlongObject



235
236
237
238
239
240
# File 'ext/index_io.c', line 235

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

#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