class Ethereum::DB::LevelDB

Constants

EMPTY_STRING

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

Public Class Methods

new(dbfile) click to toggle source
# File lib/ethereum/db/level_db.rb, line 12
def initialize(dbfile)
  logger.info "opening LevelDB", path: dbfile

  @dbfile = dbfile
  reopen

  @commit_counter = 0
  @uncommited = {}
end

Public Instance Methods

==(other) click to toggle source
# File lib/ethereum/db/level_db.rb, line 78
def ==(other)
  other.instance_of?(self.class) && db == other.db
end
cleanup(epoch) click to toggle source
# File lib/ethereum/db/level_db.rb, line 103
def cleanup(epoch)
  # do nothing
end
commit() click to toggle source
# File lib/ethereum/db/level_db.rb, line 49
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) click to toggle source
# File lib/ethereum/db/level_db.rb, line 99
def commit_refcount_changes(epoch)
  # do nothing
end
dec_refcount(k) click to toggle source
# File lib/ethereum/db/level_db.rb, line 91
def dec_refcount(k)
  # do nothing
end
delete(k) click to toggle source
# File lib/ethereum/db/level_db.rb, line 65
def delete(k)
  logger.debug 'deleting entry', key: key
  @uncommited[k] = nil
end
get(k) click to toggle source
# File lib/ethereum/db/level_db.rb, line 26
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) click to toggle source
# File lib/ethereum/db/level_db.rb, line 70
def has_key?(k)
  get(k)
  true
rescue KeyError
  false
end
Also aliased as: include?
inc_refcount(k, v) click to toggle source
# File lib/ethereum/db/level_db.rb, line 87
def inc_refcount(k, v)
  put k, v
end
include?(k)
Alias for: has_key?
inspect()
Alias for: to_s
put(k, v) click to toggle source
# File lib/ethereum/db/level_db.rb, line 44
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) click to toggle source
# File lib/ethereum/db/level_db.rb, line 107
def put_temporarily(k, v)
  inc_refcount(k, v)
  dec_refcount(k)
end
reopen() click to toggle source
# File lib/ethereum/db/level_db.rb, line 22
def reopen
  @db = ::LevelDB::DB.new @dbfile
end
revert_refcount_changes(epoch) click to toggle source
# File lib/ethereum/db/level_db.rb, line 95
def revert_refcount_changes(epoch)
  # do nothing
end
to_s() click to toggle source
# File lib/ethereum/db/level_db.rb, line 82
def to_s
  "<DB at #{@db} uncommited=#{@uncommited.size}>"
end
Also aliased as: inspect

Private Instance Methods

compress(x) click to toggle source
# File lib/ethereum/db/level_db.rb, line 122
def compress(x)
  x.empty? ? EMPTY_STRING : x
end
decompress(x) click to toggle source
# File lib/ethereum/db/level_db.rb, line 118
def decompress(x)
  x == EMPTY_STRING ? '' : x
end
logger() click to toggle source
# File lib/ethereum/db/level_db.rb, line 114
def logger
  @logger ||= Logger.new 'db.leveldb'
end