Class: Expanse::BlobMap

Inherits:
Object
  • Object
show all
Defined in:
lib/expanse.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk_size: 0) ⇒ BlobMap

Returns a new instance of BlobMap.



372
373
374
375
# File 'lib/expanse.rb', line 372

def initialize(chunk_size: 0)
  @ptr = Native.expanse_blob_map_new(chunk_size)
  ObjectSpace.define_finalizer(self, self.class.finalize(@ptr))
end

Class Method Details

.finalize(ptr) ⇒ Object



377
378
379
# File 'lib/expanse.rb', line 377

def self.finalize(ptr)
  proc { Native.expanse_blob_map_free(ptr) if ptr && !ptr.null? }
end

Instance Method Details

#clearObject



410
411
412
413
# File 'lib/expanse.rb', line 410

def clear
  Native.expanse_blob_map_clear(@ptr)
  self
end

#delete(key) ⇒ Object



398
399
400
# File 'lib/expanse.rb', line 398

def delete(key)
  Native.expanse_blob_map_remove(@ptr, key) != 0
end

#get(key) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
# File 'lib/expanse.rb', line 386

def get(key)
  # ExpanseBlobView: ptr (8B), len (8B), hot_meta (4B), is_inline (1B) + padding (3B) = 24 bytes
  view_buf = Fiddle::Pointer.malloc(24)
  if Native.expanse_blob_map_get(@ptr, key, view_buf) != 0
    raw_ptr = view_buf[0, 8].unpack1("Q<")
    len = view_buf[8, 8].unpack1("Q<")
    meta = view_buf[16, 4].unpack1("L<")
    val = len.zero? ? "" : Fiddle::Pointer.new(raw_ptr).to_str(len)
    [val, meta]
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


402
403
404
# File 'lib/expanse.rb', line 402

def key?(key)
  Native.expanse_blob_map_contains_key(@ptr, key) != 0
end

#set(key, payload, hot_meta: 0) ⇒ Object



381
382
383
384
# File 'lib/expanse.rb', line 381

def set(key, payload, hot_meta: 0)
  b = payload.b
  Native.expanse_blob_map_insert(@ptr, key, b, b.bytesize, hot_meta) != 0
end

#sizeObject



406
407
408
# File 'lib/expanse.rb', line 406

def size
  Native.expanse_blob_map_len(@ptr)
end