Class: Refile::Postgres::Backend::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/postgres/backend/reader.rb

Constant Summary collapse

PQTRANS_INTRANS =

(idle, within transaction block)

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, oid) ⇒ Reader

Returns a new instance of Reader.



7
8
9
10
11
12
# File 'lib/refile/postgres/backend/reader.rb', line 7

def initialize(connection, oid)
  @connection = connection
  @oid = oid.to_s.to_i
  @closed = false
  @pos = 0
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/refile/postgres/backend/reader.rb', line 14

def connection
  @connection
end

#oidObject (readonly)

Returns the value of attribute oid.



14
15
16
# File 'lib/refile/postgres/backend/reader.rb', line 14

def oid
  @oid
end

#posObject (readonly)

Returns the value of attribute pos.



14
15
16
# File 'lib/refile/postgres/backend/reader.rb', line 14

def pos
  @pos
end

Instance Method Details

#closeObject



49
50
51
# File 'lib/refile/postgres/backend/reader.rb', line 49

def close
  @closed = true
end

#eof?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/refile/postgres/backend/reader.rb', line 34

def eof?
  smart_transaction do |descriptor|
    @pos == size
  end
end

#read(length = nil, buffer = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/refile/postgres/backend/reader.rb', line 16

def read(length = nil, buffer = nil)
  result = if length
    raise "closed" if @closed
    smart_transaction do |descriptor|
      connection.lo_lseek(descriptor, @pos, PG::SEEK_SET)
      data = connection.lo_read(descriptor, length)
      @pos = connection.lo_tell(descriptor)
      data
    end
  else
    smart_transaction do |descriptor|
      connection.lo_read(descriptor, size)
    end
  end
  buffer.replace(result) if buffer and result
  result
end

#sizeObject



40
41
42
43
44
45
46
47
# File 'lib/refile/postgres/backend/reader.rb', line 40

def size
  @size ||= smart_transaction 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