Class: FileList

Inherits:
Object show all
Defined in:
lib/file_list.rb

Overview

tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileList

Returns a new instance of FileList.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/file_list.rb', line 16

def initialize()
  @files = []
  @loaded_file_index = nil
  @filelists = []
  @filelist_delays = []
  @child_len = 0
  @file_content_lens=[]
  @loaded_file_data = nil
  @mark_of_death = false
  @to_del=[]
end

Instance Attribute Details

#child_lenObject

I want each child in the fileslist to fill len frames from my start



15
16
17
# File 'lib/file_list.rb', line 15

def child_len
  @child_len
end

#file_content_lensObject

Returns the value of attribute file_content_lens.



6
7
8
# File 'lib/file_list.rb', line 6

def file_content_lens
  @file_content_lens
end

#filelist_delaysObject

delays of children



13
14
15
# File 'lib/file_list.rb', line 13

def filelist_delays
  @filelist_delays
end

#filelistsObject

N CHILDREN



11
12
13
# File 'lib/file_list.rb', line 11

def filelists
  @filelists
end

#filesObject

ONLY WITH NO CHILDREN



5
6
7
# File 'lib/file_list.rb', line 5

def files
  @files
end

#loaded_file_dataObject

Returns the value of attribute loaded_file_data.



8
9
10
# File 'lib/file_list.rb', line 8

def loaded_file_data
  @loaded_file_data
end

#loaded_file_indexObject

Returns the value of attribute loaded_file_index.



7
8
9
# File 'lib/file_list.rb', line 7

def loaded_file_index
  @loaded_file_index
end

#mark_of_deathObject

I want each child in the fileslist to fill len frames from my start



15
16
17
# File 'lib/file_list.rb', line 15

def mark_of_death
  @mark_of_death
end

#to_delObject

I want each child in the fileslist to fill len frames from my start



15
16
17
# File 'lib/file_list.rb', line 15

def to_del
  @to_del
end

Instance Method Details

#addlist(list, delay = 0) ⇒ Object



196
197
198
199
# File 'lib/file_list.rb', line 196

def addlist list, delay=0
  self.filelists.push list
  self.filelist_delays.push delay.to_i
end

#current_index_fits_opended_file?(index) ⇒ Boolean

all that have been opened are tallied

Returns:

  • (Boolean)


165
166
167
# File 'lib/file_list.rb', line 165

def current_index_fits_opended_file?(index) # all that have been opened are tallied
  index < file_content_lens[0..loaded_file_index].reduce(:+)
end

#get(index, size = 1) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/file_list.rb', line 130

def get(index, size=1)
#    out=Array.new(1,0)
out=[]
  # not opened yet
  if loaded_file_index.nil?
    # App.logger.print_and_flush "|t|"
    load_from_file(0)
  end

  #still got data to write in THIS file
  if current_index_fits_opended_file?(index)
    out = (read_data_at index, size)
#      puts "fl-no-child: normal read #{out}"

  #need to load next file (index is higher than current)
  else 
    # theres more files to load
    if files.count > loaded_file_index+1 
      load_from_file(loaded_file_index+1)
      out = (read_data_at index, size)
      # puts "fl-no-child: file #{loaded_file_index} is finished. load next. found val #{out}"
    else # no more files, index is past
      self.mark_of_death =true
#        puts "fl-no-child: marking for dead!!!"
    end
  end
  out
end

#get_content_lenObject

return

total len of this TrackSection

nil is error



161
162
163
# File 'lib/file_list.rb', line 161

def get_content_len
  child_len.nil? ? file_content_lens.reduce(:+) : child_len
end

#get_value(index, size = 1) ⇒ Object

return the value, index frames into your files. recursive.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/file_list.rb', line 37

def get_value(index,size=1)
#    puts "requested #{size}"
  out = []
  
  if files.empty? # has children
     out=lookup_children(index,size)
     # raise "Child filelist failed at index #{index}, size #{size}." if out.count < 1
  else # no children, just files
#      puts "looked something up"
    out = get(index,size)
#      puts "retrieved #{out.count} frames from object files on disk"
#      puts "got #{out.count}"
#      puts "=>> no children, real files found value #{out}"
  end
#    puts "valuse  #{out}"
  out
end

#load_from_file(fi) ⇒ Object

index



184
185
186
187
188
189
190
# File 'lib/file_list.rb', line 184

def load_from_file(fi)
  f=File.new(files[fi], "r")
  data=f.gets(nil)
  f.close
  self.loaded_file_index = fi
  parse_loaded data
end

#lookup_child(index, size, i) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/file_list.rb', line 55

def lookup_child(index,size,i)
  out=[]
  fl=filelists[i]
  App.checks+=1
  delay = filelist_delays[i]          

  # delay = 0 if delay.nil?
  # puts "deciding if we should go into filelist #{i+1} of #{filelists.count},"+
       # " is #{delay} <= #{index} ? "
#        puts "child len #{@child_len}"
  needed_until_in = delay - index
#     puts "needed till in #{needed_until_in}"
  if needed_until_in <= 0
    
    if fl.mark_of_death
#            puts "old count: #{filelists.count}"
      # fl.loaded_file_data=nil
      # GC.start
      self.to_del << i
#        puts "#{i} is dead! new count: #{filelists.count}"
    else
      # normal result
      result =fl.get_value(index-delay,size)
      
      out= result
    end
  else 
     spoof_req_len = size - needed_until_in
#       puts "checking if we can spoof, next move is #{size} and we will be in in #{needed_until_in}."
     if spoof_req_len > 0
#          puts "checking buggy dude spoofed!! #{index}"
       
       # Read a smaller chunk in the futre that will be missed if we increment by size.
       index_to_get_in = needed_until_in + index
       delay_on_future_chunk = Array.new(needed_until_in, 0)
       future_chunk = fl.get_value(index_to_get_in-delay, spoof_req_len)
#             puts future_chunk.count

#          puts "spoofing at #{index} for #{delay} and got #{future_chunk.count} results."
       combo = [] + delay_on_future_chunk + future_chunk
#             puts combo.count
       out= combo # with resizing, but will be chunk len anyway.
     else
       # Will be handled in future chunks.
#          out.add_e Array.new(2,0)
     # puts "not above delay"
#          puts "c #{delay-index}"
     end
  end
  out
end

#lookup_children(index, size) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/file_list.rb', line 107

def lookup_children(index,size)
  out = []
  if filelists.count==0
      #no file lists, so fill with 0s to len
      sil_amount = size
      sil_amount = child_len if child_len < sil_amount && child_len != 0  #limit silence to my length
      sil_amount
      out = Array.new(sil_amount-index,0) if sil_amount-index > 0
      ### puts "set mark of death, returning silence #{sil_amount}, my length: #{child_len}"
      self.mark_of_death = true
      return []
  end
  self.to_del = []
  filelists.count.times do |i|
    out.add_e lookup_child(index,size,i)
  end
  self.to_del.each do |i| 
    self.filelist_delays.delete_at i 
    self.filelists.delete_at i 
  end
  out
end

#parse_loaded(data) ⇒ Object



192
193
194
# File 'lib/file_list.rb', line 192

def parse_loaded data
  self.loaded_file_data=Marshal.load(data)
end

#read_data_at(index, size = 2) ⇒ Object

returning array of size



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/file_list.rb', line 170

def read_data_at (index,size=2)
  lookup=loaded_file_index==0 ? index : index-file_content_lens[0..loaded_file_index-1].reduce(:+)
  #index - all other lengths, so we get a relative index.
  rb = loaded_file_data.count-1 # right bound
#    puts "largest possible  #{rb}, lookup #{lookup}"
  jump = lookup+size-1
  jump = rb if jump > rb
  result=loaded_file_data[lookup..jump]
#    puts "#{lookup }  #{jump} #{result}"
#    puts "giving data size #{result.count}"
  result
end

#root_get_value(index, size = 1) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/file_list.rb', line 28

def root_get_value(index,size=1)
  out=get_value(index,size)
  if out.count < 1
    # raise "Error looking up a value at that index. fatal. index #{index}, size #{size}." 
    out=Array.new(size,0)
  end
  out
end

#write(wave_data, delay = 0) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/file_list.rb', line 201

def write(wave_data,delay=0)
  full=wave_data.dps
  # puts "--> values: #{full.join(', ')}"
  spli=App.split_array(full, App::CHUNK_SIZE)
  spli.each_with_index do |array,i|
    self.files.push "#{App::TMP_DIR}#{object_id}_#{i}"
    self.file_content_lens.push array.count
    File.open(self.files.last, 'w') do |fout|  
      fout.puts Marshal.dump(array)
    end
  end
  # puts "written #{spli.count} tmp files"
end