Class: OpenAssets::Cache::OutputCache

Inherits:
SQLiteBase
  • Object
show all
Defined in:
lib/openassets/cache/output_cache.rb

Overview

An object that can be used for caching coloring transaction output in a Sqlite database.

Instance Attribute Summary

Attributes inherited from SQLiteBase

#db

Instance Method Summary collapse

Methods inherited from SQLiteBase

#initialize

Constructor Details

This class inherits a constructor from OpenAssets::Cache::SQLiteBase

Instance Method Details

#get(txid, index) ⇒ Object

Get a cached transaction output @param txid The transaction id. @param index The index of the output in the transaction. @return The output for the txid and index provided if it is found in the cache, or nil otherwise.



26
27
28
29
30
31
# File 'lib/openassets/cache/output_cache.rb', line 26

def get(txid, index)
  rows = db.execute('SELECT Value,Script,AssetId,AssetQuantity,OutputType,Metadata FROM Output WHERE TransactionHash = ? AND OutputIndex = ?', [txid, index])
  return nil if rows.empty?
  script = Bitcoin::Script.from_string(rows[0][1])
  OpenAssets::Protocol::TransactionOutput.new(rows[0][0], script, rows[0][2], rows[0][3], rows[0][4], rows[0][5])
end

#put(txid, index, output) ⇒ Object

Put a transaction output @param txid The transaction id. @param index The index of the output in the transaction. @param output The output to save.



37
38
39
40
# File 'lib/openassets/cache/output_cache.rb', line 37

def put(txid, index, output)
  db.execute('INSERT INTO Output (TransactionHash, OutputIndex, Value,Script,AssetId,AssetQuantity,OutputType,Metadata) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
             [txid, index, output.value, output.script.to_string, output.asset_id, output.asset_quantity, output.output_type, output.])
end

#setupObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/openassets/cache/output_cache.rb', line 7

def setup
  db.execute <<-SQL
    CREATE TABLE IF NOT EXISTS Output(
            TransactionHash BLOB,
            OutputIndex INT,
            Value BigInt,
            Script BLOB,
            AssetId BLOB,
            AssetQuantity INT,
            OutputType INT,
            Metadata BLOB,
            PRIMARY KEY (TransactionHash, OutputIndex))
  SQL
end