Class: LargeObjectStore::RailsWrapper
- Inherits:
-
Object
- Object
- LargeObjectStore::RailsWrapper
- Defined in:
- lib/large_object_store.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key, options = {}) ⇒ Object
-
#initialize(store) ⇒ RailsWrapper
constructor
A new instance of RailsWrapper.
- #read(key) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(store) ⇒ RailsWrapper
Returns a new instance of RailsWrapper.
24 25 26 |
# File 'lib/large_object_store.rb', line 24 def initialize(store) @store = store end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
22 23 24 |
# File 'lib/large_object_store.rb', line 22 def store @store end |
Instance Method Details
#delete(key) ⇒ Object
98 99 100 |
# File 'lib/large_object_store.rb', line 98 def delete(key) @store.delete(key(key, 0)) end |
#exist?(key) ⇒ Boolean
94 95 96 |
# File 'lib/large_object_store.rb', line 94 def exist?(key) @store.exist?(key(key, 0)) end |
#fetch(key, options = {}) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/large_object_store.rb', line 86 def fetch(key, ={}) value = read(key) return value unless value.nil? value = yield write(key, value, ) value end |
#read(key) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/large_object_store.rb', line 59 def read(key) # read pages pages, uuid = @store.read(key(key, 0)) return if pages.nil? data = if pages.is_a?(Fixnum) # read sliced data keys = (1..pages).map { |i| key(key, i) } # use values_at to enforce key order because read_multi doesn't guarantee a return order slices = @store.read_multi(*keys).values_at(*keys) return nil if slices.compact.size != pages slices = slices.map do |slice| s = slice.dup [s.slice!(0, UUID_SIZE), s] end return nil unless slices.map(&:first).uniq == [uuid] slices.map!(&:last).join("") else pages end deserialize(data) end |
#write(key, value, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/large_object_store.rb', line 28 def write(key, value, = {}) = .dup value = serialize(value, ) # calculate slice size; note that key length is a factor because # the key is stored on the same slab page as the value slice_size = MAX_OBJECT_SIZE - ITEM_HEADER_SIZE - UUID_SIZE - key.bytesize # store number of pages pages = (value.size / slice_size.to_f).ceil if pages == 1 !!@store.write(key(key, 0), value, ) else # store meta uuid = SecureRandom.hex(UUID_BYTES) return false unless @store.write(key(key, 0), [pages, uuid], ) # invalidates the old cache # store object page = 1 loop do slice = value.slice!(0, slice_size) break if slice.size == 0 return false unless @store.write(key(key, page), slice.prepend(uuid), .merge(raw: true)) page += 1 end true end end |