Class: UmbrellioUtils::LargeObject

Inherits:
Object
  • Object
show all
Defined in:
lib/umbrellio_utils/large_object.rb,
lib/umbrellio_utils/large_object/writer.rb

Defined Under Namespace

Classes: AlreadyExists, Writer

Constant Summary collapse

FILE_END =
2
PG_PAGE_SIZE =
8 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid = -1)) ⇒ LargeObject

Returns a new instance of LargeObject.



16
17
18
19
# File 'lib/umbrellio_utils/large_object.rb', line 16

def initialize(oid = -1)
  self.oid = oid
  self.str_offset = 0
end

Instance Attribute Details

#oidObject

Returns the value of attribute oid.



13
14
15
# File 'lib/umbrellio_utils/large_object.rb', line 13

def oid
  @oid
end

#str_offsetObject

Returns the value of attribute str_offset.



14
15
16
# File 'lib/umbrellio_utils/large_object.rb', line 14

def str_offset
  @str_offset
end

Instance Method Details

#append!(str) ⇒ Object



34
35
36
37
# File 'lib/umbrellio_utils/large_object.rb', line 34

def append!(str)
  run(:lo_put, oid, str_offset, Sequel.blob(str))
  self.str_offset += str.bytesize
end

#create!Object



21
22
23
24
25
# File 'lib/umbrellio_utils/large_object.rb', line 21

def create!
  run(:lo_create, oid)
rescue Sequel::UniqueConstraintViolation
  raise AlreadyExists.new
end

#delete!Object



43
44
45
46
47
# File 'lib/umbrellio_utils/large_object.rb', line 43

def delete!
  run(:lo_unlink, oid)
rescue PG::UndefinedObject
  # Ignored
end

#exists?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/umbrellio_utils/large_object.rb', line 49

def exists?
  DB[:pg_largeobject_metadata].first(oid:).present?
end

#open_to_write!Object



27
28
29
30
31
32
# File 'lib/umbrellio_utils/large_object.rb', line 27

def open_to_write!
  DB.transaction do
    fd = run(:lo_open, oid, 0x60000)
    self.str_offset = run(:lo_lseek, fd, 0, FILE_END)
  end
end

#readObject



39
40
41
# File 'lib/umbrellio_utils/large_object.rb', line 39

def read(*)
  run(:lo_get, oid, *)
end