Class: LevelDB::Batch

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(db, write_opts) ⇒ Batch

Returns a new instance of Batch.


6
7
8
9
10
11
12
# File 'lib/leveldb/batch.rb', line 6

def initialize(db, write_opts)
  @_db = db
  @_write_opts = write_opts
  @_err = C::Pointer.malloc(C::SIZEOF_VOIDP)
  @_err.free = C[:free]
  @_batch = C.writebatch_create
end

Instance Method Details

#[]=(key, val) ⇒ Object Also known as: put


14
15
16
17
18
19
# File 'lib/leveldb/batch.rb', line 14

def []=(key, val)
  key, val = key.to_s, val.to_s
  C.writebatch_put(@_batch, key, key.size, val, val.size)

  val
end

#delete(key) ⇒ Object


22
23
24
25
26
27
# File 'lib/leveldb/batch.rb', line 22

def delete(key)
  key = key.to_s
  C.writebatch_delete(@_batch, key, key.size)

  key
end

#error_messageObject Also known as: clear_errors!


49
50
51
52
53
54
55
56
57
# File 'lib/leveldb/batch.rb', line 49

def error_message
  return unless errors?
  @_err.ptr.to_s
ensure
  if errors?
    @_err = C::Pointer.malloc(C::SIZEOF_VOIDP)
    @_err.free = C[:free]
  end
end

#errors?Boolean

Returns:

  • (Boolean)

44
45
46
47
# File 'lib/leveldb/batch.rb', line 44

def errors?
  return unless @_err
  !@_err.ptr.null?
end

#inspectObject Also known as: to_s


60
61
62
# File 'lib/leveldb/batch.rb', line 60

def inspect
  "#<LevelDB::Batch:#{'0x%x' % object_id}>"
end

#write!Object

true end alias clear! clear

Raises:


36
37
38
39
40
41
42
# File 'lib/leveldb/batch.rb', line 36

def write!
  C.write(@_db, @_write_opts, @_batch, @_err)

  raise Error, error_message if errors?

  true
end