class Refile::Postgres::Backend::Reader

Constants

STREAM_CHUNK_SIZE

Attributes

oid[R]
pos[R]

Public Class Methods

new(connection_or_proc, oid) click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 8
def initialize(connection_or_proc, oid)
  @connection_or_proc = connection_or_proc
  @oid = oid.to_s.to_i
  @closed = false
  @pos = 0
end

Public Instance Methods

close() click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 61
def close
  @closed = true
end
each() { |read(STREAM_CHUNK_SIZE)| ... } click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 47
def each
  if block_given?
    until eof?
      yield(read(STREAM_CHUNK_SIZE))
    end
  else
    to_enum
  end
end
eof?() click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 39
def eof?
  with_connection do |connection|
    smart_transaction(connection) do |descriptor|
      @pos == size
    end
  end
end
read(length = nil, buffer = nil) click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 17
def read(length = nil, buffer = nil)
  result = if length
    raise "closed" if @closed
    with_connection do |connection|
      smart_transaction(connection) do |descriptor|
        connection.lo_lseek(descriptor, @pos, PG::SEEK_SET)
        data = connection.lo_read(descriptor, length)
        @pos = connection.lo_tell(descriptor)
        data
      end
    end
  else
    with_connection do |connection|
      smart_transaction(connection) do |descriptor|
        connection.lo_read(descriptor, size)
      end
    end
  end
  buffer.replace(result) if buffer and result
  result
end
size() click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 57
def size
  @size ||= fetch_size
end

Private Instance Methods

fetch_size() click to toggle source
# File lib/refile/postgres/backend/reader.rb, line 67
def fetch_size
  with_connection do |connection|
    smart_transaction(connection) do |descriptor|
      current_position = connection.lo_tell(descriptor)
      end_position = connection.lo_lseek(descriptor, 0, PG::SEEK_END)
      connection.lo_lseek(descriptor, current_position, PG::SEEK_SET)
      end_position
    end
  end
end