Class: FixWidthTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/fix_width_table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, value_size = nil, range = nil, update = false, in_memory = true) ⇒ FixWidthTable

Returns a new instance of FixWidthTable.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rbbt/fix_width_table.rb', line 4

def initialize(filename, value_size = nil, range = nil, update = false, in_memory = true)
  @filename = filename

  if update || %w(memory stringio).include?(filename.to_s.downcase) || ! File.exist?(filename)
    Log.debug "FixWidthTable create: #{ filename }"
    @value_size  = value_size
    @range       = range
    @record_size = @value_size + (@range ? 16 : 8)
    @write = true

    if %w(memory stringio).include?(filename.to_s.downcase)
      @filename = :memory
      @file     = StringIO.new
    else
      FileUtils.rm @filename if File.exist? @filename
      FileUtils.mkdir_p File.dirname(@filename) unless File.exist? @filename
      #@file = File.open(@filename, 'wb')
      @file = File.open(@filename, 'w:ASCII-8BIT')
    end

    @file.write [value_size].pack("L")
    @file.write [@range ? 1 : 0 ].pack("C")

    @size = 0
  else
    Log.debug "FixWidthTable up-to-date: #{ filename } - (in_memory:#{in_memory})"
    if in_memory
      @file        = Open.open(@filename, :mode => 'r:ASCII-8BIT'){|f| StringIO.new f.read}
    else
      @file        = File.open(@filename, 'r:ASCII-8BIT')
    end
    @value_size  = @file.read(4).unpack("L").first
    @range       = @file.read(1).unpack("C").first == 1
    @record_size = @value_size + (@range ? 16 : 8)
    @write = false

    @size        = (File.size(@filename) - 5) / (@record_size)
  end

  @mask = "a#{@value_size}"
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def filename
  @filename
end

#maskObject

Returns the value of attribute mask.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def mask
  @mask
end

#rangeObject

Returns the value of attribute range.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def range
  @range
end

#record_sizeObject

Returns the value of attribute record_size.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def record_size
  @record_size
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def size
  @size
end

#value_sizeObject

Returns the value of attribute value_size.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def value_size
  @value_size
end

#writeObject

Returns the value of attribute write.



3
4
5
# File 'lib/rbbt/fix_width_table.rb', line 3

def write
  @write
end

Class Method Details

.get(filename, value_size = nil, range = nil, update = false) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/rbbt/fix_width_table.rb', line 58

def self.get(filename, value_size = nil, range = nil, update = false)
  return self.new(filename, value_size, range, update) if filename == :memory
  case
  when (!File.exist?(filename) or update or not Persist::CONNECTIONS.include?(filename))
    Persist::CONNECTIONS[filename] = self.new(filename, value_size, range, update)
  end

  Persist::CONNECTIONS[filename] 
end

Instance Method Details

#[](pos) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/rbbt/fix_width_table.rb', line 278

def [](pos)
  return [] if size == 0
  self.read
  if @range
    get_range(pos)
  else
    get_point(pos)
  end
end

#add(pos, value) ⇒ Object



77
78
79
80
81
82
# File 'lib/rbbt/fix_width_table.rb', line 77

def add(pos, value)
  format = format(pos, value)
  @file.write format

  @size += 1
end

#add_point(data) ⇒ Object

{{{ Adding data



135
136
137
138
139
# File 'lib/rbbt/fix_width_table.rb', line 135

def add_point(data)
  data.sort_by{|value, pos| pos }.each do |value, pos|
    add pos, value
  end
end

#add_range(data) ⇒ Object



151
152
153
154
155
156
# File 'lib/rbbt/fix_width_table.rb', line 151

def add_range(data)
  @latest = []
  data.sort_by{|value, pos| pos[0] }.each do |value, pos|
    add_range_point(pos, value)
  end
end

#add_range_point(pos, value) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/rbbt/fix_width_table.rb', line 141

def add_range_point(pos, value)
  @latest ||= []
  while @latest.any? and @latest[0] < pos[0]
    @latest.shift
  end
  overlap = @latest.length
  add pos + [overlap], value
  @latest << pos[1]
end

#chunked_values_at(keys, max = 5000) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/rbbt/fix_width_table.rb', line 309

def chunked_values_at(keys, max = 5000)
  Misc.ordered_divide(keys, max).inject([]) do |acc,c|
    new = self.values_at(*c)
    new.annotate acc if new.respond_to? :annotate and acc.empty?
    acc.concat(new)
  end
end

#closeObject



122
123
124
125
# File 'lib/rbbt/fix_width_table.rb', line 122

def close
  @write = false
  @file.close
end

#closest(pos) ⇒ Object

{{{ Searching



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbbt/fix_width_table.rb', line 160

def closest(pos)
  upper = size - 1
  lower = 0

  return -1 if upper < lower

  while(upper >= lower) do
    idx = lower + (upper - lower) / 2
    pos_idx = idx_pos(idx)

    case pos <=> pos_idx
    when 0
      break
    when -1
      upper = idx - 1
    when 1
      lower = idx + 1
    end
  end

  if pos_idx > pos
    idx = idx - 1
  end

  idx.to_i
end

#dumpObject



127
128
129
130
131
# File 'lib/rbbt/fix_width_table.rb', line 127

def dump
  read
  @file.rewind
  @file.read
end

#format(pos, value) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rbbt/fix_width_table.rb', line 68

def format(pos, value)
  padding = value_size - value.length
  if @range
    (pos  + [padding, value + ("\0" * padding)]).pack("llll#{mask}")
  else
    [pos, padding, value + ("\0" * padding)].pack("ll#{mask}")
  end
end

#get_point(pos, return_idx = false) ⇒ Object



234
235
236
237
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
# File 'lib/rbbt/fix_width_table.rb', line 234

def get_point(pos, return_idx = false)
  if Range === pos
    r_start = pos.begin
    r_end   = pos.end
  else
    r_start = pos.to_i
    r_end   = pos.to_i
  end

  idx = closest(r_start)

  return [] if idx >= size
  return [] if idx < 0 and r_start == r_end

  idx = 0 if idx < 0

  idx += 1 unless idx_pos(idx) >= r_start

  return [] if idx >= size

  values = []
  l_start = idx_pos(idx)
  l_end   = idx_pos_end(idx)
  if return_idx 
    while l_start <= r_end
      values << idx
      idx += 1
      break if idx >= size
      l_start = idx_pos(idx)
      l_end   = idx_pos_end(idx)
    end
  else
    while l_start <= r_end
      values << idx_value(idx)
      idx += 1
      break if idx >= size
      l_start = idx_pos(idx)
      l_end   = idx_pos_end(idx)
    end
  end

  values
end

#get_range(pos, return_idx = false) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
# File 'lib/rbbt/fix_width_table.rb', line 187

def get_range(pos, return_idx = false)
  case pos
  when Range
    r_start = pos.begin
    r_end   = pos.end
  when Array
    r_start, r_end = pos
  else
    r_start, r_end = pos, pos
  end

  idx = closest(r_start)

  return [] if idx >= size
  return [] if idx < 0 and r_start == r_end

  idx = 0 if idx < 0

  overlap = idx_overlap(idx)

  idx -= overlap unless overlap.nil?

  values = []
  l_start = idx_pos(idx)
  l_end   = idx_pos_end(idx)
  
  if return_idx
    while l_start <= r_end
      values << idx if l_end >= r_start 
      idx += 1
      break if idx >= size
      l_start = idx_pos(idx)
      l_end   = idx_pos_end(idx)
    end
  else
    while l_start <= r_end
      values << idx_value(idx) if l_end >= r_start 
      idx += 1
      break if idx >= size
      l_start = idx_pos(idx)
      l_end   = idx_pos_end(idx)
    end
  end

  values
end

#idx_overlap(index) ⇒ Object



100
101
102
103
104
# File 'lib/rbbt/fix_width_table.rb', line 100

def idx_overlap(index)
  return nil if index < 0 or index >= size
  @file.seek(13 + (record_size) * index, IO::SEEK_SET)
  @file.read(4).unpack("l").first
end

#idx_pos(index) ⇒ Object



88
89
90
91
92
# File 'lib/rbbt/fix_width_table.rb', line 88

def idx_pos(index)
  return nil if index < 0 or index >= size
  @file.seek(5 + (record_size) * index, IO::SEEK_SET)
  @file.read(4).unpack("l").first
end

#idx_pos_end(index) ⇒ Object



94
95
96
97
98
# File 'lib/rbbt/fix_width_table.rb', line 94

def idx_pos_end(index)
  return nil if index < 0 or index >= size
  @file.seek(9 + (record_size) * index, IO::SEEK_SET)
  @file.read(4).unpack("l").first
end

#idx_value(index) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/rbbt/fix_width_table.rb', line 106

def idx_value(index)
  return nil if index < 0 or index >= size
  @file.seek((@range ? 17 : 9 ) + (record_size) * index, IO::SEEK_SET)
  padding = @file.read(4).unpack("l").first+1
  txt = @file.read(value_size)
  str = txt.unpack(mask).first
  padding > 1 ? str[0..-padding] : str
end

#last_posObject



84
85
86
# File 'lib/rbbt/fix_width_table.rb', line 84

def last_pos
  pos(size - 1)
end

#overlaps(pos, value = false) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rbbt/fix_width_table.rb', line 288

def overlaps(pos, value = false)
  return [] if size == 0
  idxs = if @range
    get_range(pos, true)
  else
    get_point(pos, true)
  end
  if value
    idxs.collect{|idx| [idx_pos(idx), idx_pos_end(idx), idx_value(idx)] * ":"}
  else
    idxs.collect{|idx| [idx_pos(idx), idx_pos_end(idx)] * ":"}
  end
end

#persistence_pathObject



50
51
52
# File 'lib/rbbt/fix_width_table.rb', line 50

def persistence_path
  @filename
end

#persistence_path=(value) ⇒ Object



54
55
56
# File 'lib/rbbt/fix_width_table.rb', line 54

def persistence_path=(value)
  @filename=value
end

#read(force = false) ⇒ Object



115
116
117
118
119
120
# File 'lib/rbbt/fix_width_table.rb', line 115

def read(force = false)
  return if @filename == :memory
  @write = false
  @file.close unless @file.closed?
  @file = File.open(filename, 'r:ASCII-8BIT')
end

#values_at(*list) ⇒ Object



303
304
305
306
307
# File 'lib/rbbt/fix_width_table.rb', line 303

def values_at(*list)
  list.collect{|pos|
    self[pos]
  }
end

#write?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rbbt/fix_width_table.rb', line 46

def write?
  @write
end