Class: Ethereum::DB::LevelDB

Inherits:
Object
  • Object
show all
Includes:
BaseDB
Defined in:
lib/ethereum/db/level_db.rb

Constant Summary collapse

EMPTY_STRING =

FIXME: workaround, because leveldb gem doesn’t allow put empty string

Utils.keccak256('').freeze

Instance Attribute Summary

Attributes included from BaseDB

#db

Instance Method Summary collapse

Constructor Details

#initialize(dbfile) ⇒ LevelDB

Returns a new instance of LevelDB.



13
14
15
16
17
18
19
20
21
# File 'lib/ethereum/db/level_db.rb', line 13

def initialize(dbfile)
  logger.info "opening LevelDB", path: dbfile

  @dbfile = dbfile
  reopen

  @commit_counter = 0
  @uncommited = {}
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
# File 'lib/ethereum/db/level_db.rb', line 79

def ==(other)
  other.instance_of?(self.class) && db == other.db
end

#cleanup(epoch) ⇒ Object



104
105
106
# File 'lib/ethereum/db/level_db.rb', line 104

def cleanup(epoch)
  # do nothing
end

#commitObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ethereum/db/level_db.rb', line 50

def commit
  logger.debug "committing", db: self

  @db.batch do |b|
    @uncommited.each do |k, v|
      if v
        b.put k, compress(v)
      else
        b.delete k
      end
    end
  end
  logger.debug 'committed', db: self, num: @uncommited.size
  @uncommited = {}
end

#commit_refcount_changes(epoch) ⇒ Object



100
101
102
# File 'lib/ethereum/db/level_db.rb', line 100

def commit_refcount_changes(epoch)
  # do nothing
end

#dec_refcount(k) ⇒ Object



92
93
94
# File 'lib/ethereum/db/level_db.rb', line 92

def dec_refcount(k)
  # do nothing
end

#delete(k) ⇒ Object



66
67
68
69
# File 'lib/ethereum/db/level_db.rb', line 66

def delete(k)
  logger.debug 'deleting entry', key: key
  @uncommited[k] = nil
end

#get(k) ⇒ Object

Raises:

  • (KeyError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ethereum/db/level_db.rb', line 27

def get(k)
  logger.debug 'getting entry', key: Utils.encode_hex(k)[0,8]

  if @uncommited.has_key?(k)
    raise KeyError, 'key not in db' unless @uncommited[k]
    logger.debug "from uncommited"
    return @uncommited[k]
  end

  logger.debug "from db"
  raise KeyError, k.inspect unless @db.exists?(k)
  v = @db.get(k)
  o = decompress v
  @uncommited[k] = o

  o
end

#has_key?(k) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/ethereum/db/level_db.rb', line 71

def has_key?(k)
  get(k)
  true
rescue KeyError
  false
end

#inc_refcount(k, v) ⇒ Object



88
89
90
# File 'lib/ethereum/db/level_db.rb', line 88

def inc_refcount(k, v)
  put k, v
end

#put(k, v) ⇒ Object



45
46
47
48
# File 'lib/ethereum/db/level_db.rb', line 45

def put(k, v)
  logger.debug 'putting entry', key: Utils.encode_hex(k)[0,8], size: v.size
  @uncommited[k] = v
end

#put_temporarily(k, v) ⇒ Object



108
109
110
111
# File 'lib/ethereum/db/level_db.rb', line 108

def put_temporarily(k, v)
  inc_refcount(k, v)
  dec_refcount(k)
end

#reopenObject



23
24
25
# File 'lib/ethereum/db/level_db.rb', line 23

def reopen
  @db = ::LevelDB::DB.new @dbfile
end

#revert_refcount_changes(epoch) ⇒ Object



96
97
98
# File 'lib/ethereum/db/level_db.rb', line 96

def revert_refcount_changes(epoch)
  # do nothing
end

#to_sObject Also known as: inspect



83
84
85
# File 'lib/ethereum/db/level_db.rb', line 83

def to_s
  "<DB at #{@db} uncommited=#{@uncommited.size}>"
end