Class: Refile::Postgres::Backend::Reader
- Inherits:
-
Object
- Object
- Refile::Postgres::Backend::Reader
- Includes:
- SmartTransaction
- Defined in:
- lib/refile/postgres/backend/reader.rb
Constant Summary collapse
- STREAM_CHUNK_SIZE =
16384
Constants included from SmartTransaction
SmartTransaction::INIT_CONNECTION_ARG_ERROR_MSG, SmartTransaction::PQTRANS_INTRANS
Instance Attribute Summary collapse
-
#oid ⇒ Object
readonly
Returns the value of attribute oid.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #close ⇒ Object
- #each ⇒ Object
- #eof? ⇒ Boolean
-
#initialize(connection_or_proc, oid) ⇒ Reader
constructor
A new instance of Reader.
- #read(length = nil, buffer = nil) ⇒ Object
- #size ⇒ Object
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.
8 9 10 11 12 13 |
# 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 |
Instance Attribute Details
#oid ⇒ Object (readonly)
Returns the value of attribute oid.
15 16 17 |
# File 'lib/refile/postgres/backend/reader.rb', line 15 def oid @oid end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
15 16 17 |
# File 'lib/refile/postgres/backend/reader.rb', line 15 def pos @pos end |
Instance Method Details
#close ⇒ Object
61 62 63 |
# File 'lib/refile/postgres/backend/reader.rb', line 61 def close @closed = true end |
#each ⇒ Object
47 48 49 50 51 52 53 54 55 |
# 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? ⇒ Boolean
39 40 41 42 43 44 45 |
# 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) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/refile/postgres/backend/reader.rb', line 57 def size @size ||= fetch_size end |