Class: ArrayIO
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#io_index ⇒ Object
readonly
Returns the value of attribute io_index.
Class Method Summary collapse
Instance Method Summary collapse
- #[](input, length = nil) ⇒ Object
- #[]=(input, entry) ⇒ Object
- #at(index) ⇒ Object
-
#cached? ⇒ Boolean
arrayio functionality.
- #close ⇒ Object
- #default_index_file ⇒ Object
- #dump_index(index_file = default_index_file) ⇒ Object
- #each(&block) ⇒ Object
- #each_index(&block) ⇒ Object
- #empty? ⇒ Boolean
- #entry_to_str(entry) ⇒ Object
- #fetch(index, default = nil, &block) ⇒ Object
- #first(n = nil) ⇒ Object
-
#initialize(fd, mode = 'r', io_index = nil) ⇒ ArrayIO
constructor
A new instance of ArrayIO.
- #last(n = nil) ⇒ Object
- #length ⇒ Object
- #load_index(index_file) ⇒ Object
- #range(input, length = nil) ⇒ Object
- #reindex(options = {}, &block) ⇒ Object
-
#size ⇒ Object
array functionality.
- #str_to_entry(string) ⇒ Object
- #strio? ⇒ Boolean
- #values_at(*selectors) ⇒ Object
Constructor Details
#initialize(fd, mode = 'r', io_index = nil) ⇒ ArrayIO
Returns a new instance of ArrayIO.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/array_io.rb', line 22 def initialize(fd, mode='r', io_index=nil) if mode =~ /u/i mode = mode.delete('u') @cached = false else @cached = true end if mode =~ /s/i mode = mode.delete('s') @strio = true else @strio = false end if strio? mode = mode.delete('s') @io = StringIO.open(fd, mode) io_index = '' if io_index.nil? else @io = File.open(fd, mode) set_binmode(io) io_index = default_index_file if io_index.nil? end self.io_index = io_index end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
4 5 6 |
# File 'lib/array_io.rb', line 4 def io @io end |
#io_index ⇒ Object
Returns the value of attribute io_index.
4 5 6 |
# File 'lib/array_io.rb', line 4 def io_index @io_index end |
Class Method Details
.open(fd, mode = 'r', index_file = nil, auto_index = true, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/array_io.rb', line 9 def open(fd, mode='r', index_file=nil, auto_index=true, &block) aio = self.new(fd, mode, index_file) if block_given? aio.reindex if auto_index && aio.empty? yield(aio) aio.close else aio end end |
Instance Method Details
#[](input, length = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/array_io.rb', line 74 def [](input, length=nil) range = range(input, length) case range when Array range.collect { |r| read_entry(r) } else read_entry(range) end end |
#[]=(input, entry) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/array_io.rb', line 84 def []=(input, entry) string = entry_to_str(entry) range_begin = strio? ? io.string.length : io.stat.size range_end = range_begin + string.length range = format_range(range_begin, range_end) # do this first in case io is not open for writing. set_pos(io, range_begin) io << string if cached? io_index[input] = range else pos = index_pos(input) if strio? io_index.string[pos...pos+RANGE_SIZE] = range else #io_index.close end end end |
#at(index) ⇒ Object
70 71 72 |
# File 'lib/array_io.rb', line 70 def at(index) self[index] end |
#cached? ⇒ Boolean
arrayio functionality
159 160 161 |
# File 'lib/array_io.rb', line 159 def cached? @cached end |
#close ⇒ Object
191 192 193 194 |
# File 'lib/array_io.rb', line 191 def close io.close io_index.close unless cached? end |
#default_index_file ⇒ Object
50 51 52 |
# File 'lib/array_io.rb', line 50 def default_index_file io.path.chomp(File.extname(io.path)) + '.index' end |
#dump_index(index_file = default_index_file) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/array_io.rb', line 172 def dump_index(index_file=default_index_file) dumping_to_io = io_index.respond_to?(:path) ? File.(io_index.path) != File.(index_file) : false File.open(index_file, 'w') do |file| set_binmode(file) if cached? io_index.each do |range| file << [range.begin, range.end].pack(FORMAT) end else file << io_index.read end end unless dumping_to_io index_file end |
#each(&block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/array_io.rb', line 123 def each(&block) raise LocalJumpError("no block given") unless block_given? if cached? io_index.each do |range| yield( read_entry(range) ) end else io_index.pos = 0 while !io_index.eof? begin_index, end_index = io_index.read(RANGE_SIZE).unpack(FORMAT) yield( read_entry(begin_index..end_index) ) end end end |
#each_index(&block) ⇒ Object
117 118 119 120 121 |
# File 'lib/array_io.rb', line 117 def each_index(&block) raise LocalJumpError("no block given") unless block_given? 0.upto(length-1, &block) end |
#empty? ⇒ Boolean
139 140 141 |
# File 'lib/array_io.rb', line 139 def empty? length == 0 end |
#entry_to_str(entry) ⇒ Object
200 201 202 |
# File 'lib/array_io.rb', line 200 def entry_to_str(entry) entry.to_s end |
#fetch(index, default = nil, &block) ⇒ Object
107 108 109 110 111 |
# File 'lib/array_io.rb', line 107 def fetch(index, default=nil, &block) index += index_length if index < 0 val = (index >= length ? default : self[index]) block_given? ? yield(val) : val end |
#first(n = nil) ⇒ Object
113 114 115 |
# File 'lib/array_io.rb', line 113 def first(n=nil) n.nil? ? self[0] : self[0,n] end |
#last(n = nil) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/array_io.rb', line 143 def last(n=nil) return self[-1] if n.nil? start = length-n start = 0 if start < 0 self[start, n] end |
#length ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/array_io.rb', line 62 def length if cached? io_index.length else index_length/RANGE_SIZE end end |
#load_index(index_file) ⇒ Object
167 168 169 170 |
# File 'lib/array_io.rb', line 167 def load_index(index_file) input = strio? ? index_file : (File.exists?(index_file) ? File.read(index_file) : '') @io_index = parse_index(input) end |
#range(input, length = nil) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/array_io.rb', line 204 def range(input, length=nil) if cached? if length.nil? return io_index[input] else return io_index[input, length] end end unless length.nil? return nil if length < 0 input = input...(input + length) end if input.kind_of?(Range) begin_pos = index_pos(input.begin) return nil if begin_pos < 0 || begin_pos >= index_length end_pos = index_pos(input.end) + (input.exclude_end? ? 0 : RANGE_SIZE) read_length = end_pos-begin_pos return [] if read_length <= 0 set_pos(io_index, begin_pos) parse_index(io_index.read(read_length)) else begin_pos = index_pos(input) return nil if begin_pos < 0 || begin_pos >= index_length set_pos(io_index, begin_pos) array = io_index.read(RANGE_SIZE).unpack(FORMAT) array.first...array.last end end |
#reindex(options = {}, &block) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/array_io.rb', line 238 def reindex(={}, &block) io.rewind = { :sep_string => $/, :break_before => false, :exclude_break => false, :limit => nil }.merge() sep_string = [:sep_string] break_before = [:break_before] exclude_break = [:exclude_break] limit = [:limit] total_count = 0 if cached? self.io_index = [] else io_index.close if strio? self.io_index = '' else self.io_index = io_index.path end end last_pos = 0 current_pos = 0 range_begin = 0 io.each_line(sep_string) do |line| # Note positions MUST be built up using line.length # io.pos cannot return positions greater than ~2.1e9 last_pos = current_pos current_pos += line.length if (block_given? ? yield(line) : true) range_end = (break_before || exclude_break) ? last_pos : current_pos unless range_end == range_begin io_index << format_range(range_begin, range_end) total_count += 1 end range_begin = (break_before && !exclude_break) ? last_pos : current_pos break unless limit.nil? || total_count < limit end end if limit.nil? || total_count < limit range_end = current_pos unless range_end == range_begin io_index << format_range(range_begin, range_end) end end # this must be done to re-stat Files, assuring index_length is the length of the io_index file. unless cached? || strio? io_index.close self.io_index = io_index.path end self end |
#size ⇒ Object
array functionality
58 59 60 |
# File 'lib/array_io.rb', line 58 def size length end |
#str_to_entry(string) ⇒ Object
196 197 198 |
# File 'lib/array_io.rb', line 196 def str_to_entry(string) string end |
#strio? ⇒ Boolean
163 164 165 |
# File 'lib/array_io.rb', line 163 def strio? @strio end |
#values_at(*selectors) ⇒ Object
151 152 153 |
# File 'lib/array_io.rb', line 151 def values_at(*selectors) selectors.collect {|s| self[s]}.flatten end |