Class: PG::Replication::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/replication/buffer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Buffer

Returns a new instance of Buffer.



9
10
11
12
# File 'lib/pg/replication/buffer.rb', line 9

def initialize(data)
  @data = data
  @pos = 0
end

Class Method Details

.from_string(str) ⇒ Object



14
15
16
# File 'lib/pg/replication/buffer.rb', line 14

def self.from_string(str)
  new(str.b)
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/pg/replication/buffer.rb', line 18

def eof?
  @pos >= @data.bytesize
end

#read(n = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pg/replication/buffer.rb', line 22

def read(n = nil)
  return nil if @pos >= @data.bytesize
  if n.nil?
    result = @data.byteslice(@pos..-1)
    @pos = @data.bytesize
  else
    result = @data.byteslice(@pos, n)
    @pos += result.bytesize
  end
  result
end

#read_boolObject



45
46
47
# File 'lib/pg/replication/buffer.rb', line 45

def read_bool
  readbyte == 1
end

#read_charObject



41
42
43
# File 'lib/pg/replication/buffer.rb', line 41

def read_char
  readbyte.chr
end

#read_cstringObject

Raises:

  • (EOFError)


70
71
72
73
74
75
76
77
# File 'lib/pg/replication/buffer.rb', line 70

def read_cstring
  null_pos = @data.index("\0", @pos)
  raise EOFError, "Unterminated C-string" if null_pos.nil?

  str = @data.byteslice(@pos, null_pos - @pos)
  @pos = null_pos + 1 # Skip past null terminator
  str
end

#read_int16Object



53
54
55
# File 'lib/pg/replication/buffer.rb', line 53

def read_int16
  read_bytes(2).unpack1("n")
end

#read_int32Object



57
58
59
# File 'lib/pg/replication/buffer.rb', line 57

def read_int32
  read_bytes(4).unpack1("N")
end

#read_int64Object



61
62
63
# File 'lib/pg/replication/buffer.rb', line 61

def read_int64
  read_bytes(8).unpack1("Q>")
end

#read_int8Object



49
50
51
# File 'lib/pg/replication/buffer.rb', line 49

def read_int8
  readbyte
end

#read_timestampObject



65
66
67
68
# File 'lib/pg/replication/buffer.rb', line 65

def read_timestamp
  usecs = POSTGRES_EPOCH_USECS + read_int64
  Time.at(usecs / 1_000_000, usecs % 1_000_000, :microsecond, in: "UTC")
end

#readbyteObject

Raises:

  • (EOFError)


34
35
36
37
38
39
# File 'lib/pg/replication/buffer.rb', line 34

def readbyte
  raise EOFError if @pos >= @data.bytesize
  byte = @data.getbyte(@pos)
  @pos += 1
  byte
end