Class: Amp::FakeFileAppender

Inherits:
Object
  • Object
show all
Defined in:
lib/amp/revlogs/changelog.rb

Overview

This is used for faking data writing, until we’re totally done accumulating data.

Instance Method Summary collapse

Constructor Details

#initialize(fp, buffer, size) ⇒ FakeFileAppender

Initializes the fake file with the pointer to the real deal, and also the current data



11
12
13
14
15
16
# File 'lib/amp/revlogs/changelog.rb', line 11

def initialize(fp, buffer, size)
  @data = buffer
  @fp = fp
  @offset = fp.tell
  @size = size
end

Instance Method Details

#closeObject

Closes the real file pointer for this fake file



36
37
38
# File 'lib/amp/revlogs/changelog.rb', line 36

def close
  @fp.close
end

#endptObject

Returns the endpoint of the data in the (fake) file



20
21
22
# File 'lib/amp/revlogs/changelog.rb', line 20

def endpt
  @size + @data.join.size
end

#flushObject

Nothing, since we don’t flush, we just sit here



32
# File 'lib/amp/revlogs/changelog.rb', line 32

def flush; end

#read(count = -1)) ⇒ String

Reads from the fake file, for count bytes. This is a little sketchy because we might be reading some real data and some fake data, or all real, or all fake.

Parameters:

  • count (Integer) (defaults to: -1))

    how much to read. -1 will read it all.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/amp/revlogs/changelog.rb', line 68

def read(count=-1)
  ret = ""
  
  if @offset < @size
    s = @fp.read(count)
    ret = s
    @offset += s.size
    if count > 0
      count -= s.size
    end
  end
  
  unless count.zero?
    doff = @offset - @size
    @data.unshift @data.join
    @data.delete_range(1..-1)
    s = @data[0][doff..(doff+count-1)]
    @offset += s.size
    ret += s
  end
  
  ret
end

#seek(offset, whence) ⇒ Object

Seeks to the position requested. We’re faking this, but it’s a little tougher, because if we break the fake file down like this:

actual file size = 10 | pending data size = 10

If the user seeks to 15, we need to make sure we don’t try to seek to 15 in the file, as that would cause errors.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/amp/revlogs/changelog.rb', line 48

def seek(offset, whence)
  if whence == IO::SEEK_SET
    @offset = offset
  elsif whence == IO::SEEK_CUR
    @offset += offset
  elsif whence == IO::SEEK_END
    @offset = self.endpt + offset
  end
  if @offset < @size
    @fp.seek @offset
  end
end

#tellObject

Returns the current offset of the (fake) file



26
27
28
# File 'lib/amp/revlogs/changelog.rb', line 26

def tell
  @offset
end

#write(s) ⇒ Object

Writes to the fake file. Notice - this will only work because we only append to the file - we don’t write in the middle anywhere, or this scheme would fail.

Parameters:

  • s (#to_s)

    data to write



98
99
100
101
# File 'lib/amp/revlogs/changelog.rb', line 98

def write(s)
  @data << s.to_s
  @offset += s.size
end