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
# 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
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

Instance Method Details

#addlist(list, delay = 0) ⇒ Object



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

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)


153
154
155
# File 'lib/file_list.rb', line 153

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/file_list.rb', line 118

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



149
150
151
# File 'lib/file_list.rb', line 149

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.



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

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



172
173
174
175
176
177
178
# File 'lib/file_list.rb', line 172

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



54
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
# File 'lib/file_list.rb', line 54

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
  if needed_until_in <= 0
#          puts "child #{i}"# #{index-delay}"
    if fl.mark_of_death
#            puts "old count: #{filelists.count}"
      # fl.loaded_file_data=nil
      # GC.start
      self.filelists.delete_at i
      self.filelist_delays.delete_at i
#            puts "#{i} is dead! new count: #{filelists.count}"
    else
      # normal result
      result =fl.get_value(index-delay,size)
#             puts "normal #{result.count}"
      out= result
    end
  else 
     spoof_req_len = size - needed_until_in
     if spoof_req_len > 0
       # 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
       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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/file_list.rb', line 100

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
  filelists.each_with_index do |fl,i|
    out.add_e lookup_child(index,size,i)
  end
  out
end

#parse_loaded(data) ⇒ Object



180
181
182
# File 'lib/file_list.rb', line 180

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

#read_data_at(index, size = 2) ⇒ Object

returning array of size



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/file_list.rb', line 158

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



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

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



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/file_list.rb', line 189

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