Class: Bitcoin::Store::DB::LevelDB

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/store/db/level_db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "#{Bitcoin.base_dir}/db/spv") ⇒ LevelDB

Returns a new instance of LevelDB.



12
13
14
15
16
17
# File 'lib/bitcoin/store/db/level_db.rb', line 12

def initialize(path = "#{Bitcoin.base_dir}/db/spv")
  # @logger = Bitcoin::Logger.create(:debug)
  FileUtils.mkdir_p(path)
  @db = ::LevelDBNative::DB.new(path)
  # logger.debug 'Opened LevelDB successfully.'
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/bitcoin/store/db/level_db.rb', line 9

def db
  @db
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/bitcoin/store/db/level_db.rb', line 10

def logger
  @logger
end

Instance Method Details

#best_hashObject

get best block hash.



35
36
37
# File 'lib/bitcoin/store/db/level_db.rb', line 35

def best_hash
  db.get(KEY_PREFIX[:best])
end

#closeObject



69
70
71
# File 'lib/bitcoin/store/db/level_db.rb', line 69

def close
  db.close
end

#delete(key) ⇒ Object

delete specified key data.



40
41
42
# File 'lib/bitcoin/store/db/level_db.rb', line 40

def delete(key)
  db.delete(key)
end

#get(key) ⇒ Object

get value from specified key. @return the stored value.

Parameters:

  • key (Object)

    a key.



30
31
32
# File 'lib/bitcoin/store/db/level_db.rb', line 30

def get(key)
  db.get(key)
end

#get_entry_payload_from_hash(hash) ⇒ String

get entry payload

Parameters:

  • hash (String)

    the hash with hex format.

Returns:

  • (String)

    the ChainEntry payload.



57
58
59
# File 'lib/bitcoin/store/db/level_db.rb', line 57

def get_entry_payload_from_hash(hash)
  db.get(KEY_PREFIX[:entry] + hash)
end

#get_hash_from_height(height) ⇒ Object

get block hash specified height



45
46
47
# File 'lib/bitcoin/store/db/level_db.rb', line 45

def get_hash_from_height(height)
  db.get(height_key(height))
end

#next_hash(hash) ⇒ Object

get next block hash specified hash



50
51
52
# File 'lib/bitcoin/store/db/level_db.rb', line 50

def next_hash(hash)
  db.get(KEY_PREFIX[:next] + hash)
end

#put(key, value) ⇒ Object

put data into LevelDB.

Parameters:

  • key (Object)

    a key.

  • value (Object)

    a value.



22
23
24
25
# File 'lib/bitcoin/store/db/level_db.rb', line 22

def put(key, value)
  # logger.debug "put #{key} data"
  db.put(key, value)
end

#save_entry(entry) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/bitcoin/store/db/level_db.rb', line 61

def save_entry(entry)
  db.batch do
    db.put(entry.key ,entry.to_payload)
    db.put(height_key(entry.height), entry.block_hash)
    connect_entry(entry)
  end
end