Class: XRBP::NodeStore::Backends::NuDB

Inherits:
DB
  • Object
show all
Includes:
Decompressor
Defined in:
lib/xrbp/nodestore/backends/nudb.rb

Overview

NuDB nodestore backend, faciliates accessing XRP Ledger data in a NuDB database. This module accommodates for compression used in rippled’s NuDB nodestore backend implementation

Examples:

retrieve data from NuDB backend

require 'nodestore/backends/nudb'
nudb = NodeStore::Backends::NuDB.new '/var/lib/rippled/db/nudb'
puts nudb.ledger('B506ADD630CB707044B4BFFCD943C1395966692A13DD618E5BD0978A006B43BD')

Constant Summary collapse

KEY_SIZE =
32

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DB

#account, #inner_node, #ledger, #ledger_entry, #tx

Constructor Details

#initialize(path) ⇒ NuDB

Returns a new instance of NuDB.



26
27
28
29
30
# File 'lib/xrbp/nodestore/backends/nudb.rb', line 26

def initialize(path)
  @path = path
  create!
  open
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/xrbp/nodestore/backends/nudb.rb', line 22

def path
  @path
end

Instance Method Details

#[](key) ⇒ String

Retrieve database value for the specified key

Parameters:

  • key (String)

    binary key to lookup

Returns:

  • (String)

    binary value



36
37
38
39
40
# File 'lib/xrbp/nodestore/backends/nudb.rb', line 36

def [](key)
  fetched = @store.fetch(key)[0]
  return nil if fetched.empty?
  decompress(fetched)
end

#eachObject

Iterate over each database key/value pair, invoking callback. During iteration will emit signals specific to the DB types being parsed

Examples:

iterating over NuDB entries

nudb.each do |iterator|
  puts "Key/Value: #{iterator.key}/#{iterator.value}"
end

handling ledgers via event callback

nudb.on(:ledger) do |hash, ledger|
  puts "Ledger #{hash}"
  pp ledger
end

# Any Enumerable method that invokes #each will
# have intended effect
nudb.to_a


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xrbp/nodestore/backends/nudb.rb', line 61

def each
  dat = File.join(path, "nudb.dat")

  RuDB::each(dat) do |key, val|
    val = decompress(val)
    type, obj = infer_type(val)

    if type
      emit type, key, obj
    else
      emit :unknown, key, val
    end

    # 'mock' iterator
    iterator = OpenStruct.new(:key   => key,
                              :value => val)
    yield iterator
  end

  return self
end