Class: LS4::FileUpdateLog::LogFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ls4/service/ulog_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, atime) ⇒ LogFile

Returns a new instance of LogFile.



184
185
186
187
188
189
190
191
192
# File 'lib/ls4/service/ulog_file.rb', line 184

def initialize(path, atime)
  @path = path
  @file = File.open(@path, File::RDWR|File::CREAT)
  if @file.stat.size == 0
    init_file(atime)
  else
    read_file
  end
end

Instance Method Details

#append(atime, data, &block) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ls4/service/ulog_file.rb', line 198

def append(atime, data, &block)
  reltime = atime - @atime
  if reltime <= @last_reltime
    reltime = @last_reltime+1
  end
  @last_reltime = reltime

  pos = get_offset

  r = Record.new(reltime, data)
  ref = RecordRef.new(reltime, pos)

  @file.pos = pos
  r.write(@file)
  noffset = @file.pos

  block.call

  @index << ref
  set_offset(noffset)

  reltime + @atime
end

#closeObject



194
195
196
# File 'lib/ls4/service/ulog_file.rb', line 194

def close
  @file.close
end

#get(time) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ls4/service/ulog_file.rb', line 222

def get(time)
  reltime = time - @atime

  while true
    # FIXME binary search
    ref = @index.find {|ref|
      ref.reltime > reltime
    }

    unless ref
      break
    end

    r = ref.read_body(@file)

    return r.data, r.reltime + @atime
  end

  return nil, reltime + @atime
end