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

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

Constant Summary

Constants included from SmartTransaction

SmartTransaction::INIT_CONNECTION_ARG_ERROR_MSG, SmartTransaction::PQTRANS_INTRANS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SmartTransaction

#ensure_in_transaction, #smart_transaction, #with_connection

Constructor Details

#initialize(connection_or_proc, 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_or_proc, oid)
  @connection_or_proc = connection_or_proc
  @oid = oid.to_s.to_i
  @closed = false
  @pos = 0
end

Instance Attribute Details

#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



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

def close
  @closed = true
end

#eof?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/refile/postgres/backend/reader.rb', line 38

def eof?
  with_connection do |connection|
    smart_transaction(connection) do |descriptor|
      @pos == size
    end
  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
33
34
35
36
# File 'lib/refile/postgres/backend/reader.rb', line 16

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

#sizeObject



46
47
48
# File 'lib/refile/postgres/backend/reader.rb', line 46

def size
  @size ||= fetch_size
end