cache

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2019 License: ISC Imports: 7 Imported by: 2

Documentation

Overview

Package cache provides a number of types and functions for caching Decred address data, and filtering AddressRow slices. The type AddressCache may store the following data for an address: balance (see db/types.AddressBalance), address table row data (see db/types.AddressRow), merged address table row data, UTXOs (see api/types.AddressTxnOutput), and "metrics" (see db/types.AddressMetrics).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllCreditAddressRows

func AllCreditAddressRows(rows []*dbtypes.AddressRow) []*dbtypes.AddressRow

AllCreditAddressRows returns all of the credit (funding) address rows from the given AddressRow slice.

func AllDebitAddressRows

func AllDebitAddressRows(rows []*dbtypes.AddressRow) []*dbtypes.AddressRow

AllDebitAddressRows returns all of the debit (!funding) address rows from the given AddressRow slice.

func CountCreditDebitRows

func CountCreditDebitRows(rows []*dbtypes.AddressRow) (numCredit, numDebit int)

CountCreditDebitRows returns the numbers of credit (funding) and debit (!funding) address rows in a []*dbtypes.AddressRow.

func CountCreditDebitRowsCompact

func CountCreditDebitRowsCompact(rows []dbtypes.AddressRowCompact) (numCredit, numDebit int)

CountCreditDebitRowsCompact returns the numbers of credit (funding) and debit (!funding) address rows in a []dbtypes.AddressRowCompact.

func CountCreditDebitRowsMerged

func CountCreditDebitRowsMerged(rows []dbtypes.AddressRowMerged) (numCredit, numDebit int)

CountCreditDebitRowsMerged returns the numbers of credit (funding) and debit (!funding) address rows in a []dbtypes.AddressRowMerged.

func CreditAddressRows

func CreditAddressRows(rows interface{}, N, offset int) interface{}

CreditAddressRows returns up to N credit (funding) address rows from the given AddressRow slice, starting after skipping offset rows. The input rows may only be of type []dbtypes.AddressRowCompact or []dbtypes.AddressRowMerged. The same type is returned, unless the input type is unrecognized, in which case a nil interface is returned.

func DebitAddressRows

func DebitAddressRows(rows interface{}, N, offset int) interface{}

DebitAddressRows returns up to N debit (!funding) address rows from the given AddressRow slice, starting after skipping offset rows. The input rows may only be of type []dbtypes.AddressRowCompact or []dbtypes.AddressRowMerged. The same type is returned, unless the input type is unrecognized, in which case a nil interface is returned.

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type AddressCache

type AddressCache struct {
	ProjectAddress string
	// contains filtered or unexported fields
}

AddressCache maintains a store of address data. Use NewAddressCache to create a new AddressCache with initialized internal data structures.

func NewAddressCache

func NewAddressCache(rowCapacity int) *AddressCache

NewAddressCache constructs an AddressCache with capacity for the specified number of address rows.

func (*AddressCache) Balance

func (ac *AddressCache) Balance(addr string) (*dbtypes.AddressBalance, *BlockID)

Balance attempts to retrieve an AddressBalance for the given address. The BlockID for the block at which the cached data is valid is also returned. In the event of a cache miss, both returned pointers will be nil.

func (*AddressCache) BalanceStats

func (ac *AddressCache) BalanceStats() (hits, misses int)

BalanceStats reports the balance hit/miss stats.

func (*AddressCache) Clear

func (ac *AddressCache) Clear(addrs []string) (numCleared int)

Clear purging cached data for the given addresses. If addrs is nil, all data are cleared. If addresses is non-nil empty slice, no data are cleard.

func (*AddressCache) ClearAll

func (ac *AddressCache) ClearAll() (numCleared int)

ClearAll resets AddressCache, purging all cached data.

func (*AddressCache) HistoryChart

func (ac *AddressCache) HistoryChart(addr string, addrChart dbtypes.HistoryChart,
	chartGrouping dbtypes.TimeBasedGrouping) (*dbtypes.ChartsData, *BlockID)

HistoryChart attempts to retrieve ChartsData for the given address, chart type, and grouping inverval. The BlockID for the block at which the cached data is valid is also returned. In the event of a cache miss, both returned pointers will be nil.

func (*AddressCache) HistoryStats

func (ac *AddressCache) HistoryStats() (hits, misses int)

HistoryStats reports the history data hit/miss stats.

func (*AddressCache) Length

func (ac *AddressCache) Length() (numAddrs, numTxns, numUTXOs int)

Length returns the total number of address rows and UTXOs stored in cache.

func (*AddressCache) NumAddresses

func (ac *AddressCache) NumAddresses() int

NumAddresses returns the total number of addresses in the cache.

func (*AddressCache) NumRows

func (ac *AddressCache) NumRows(addr string) (int, *BlockID)

NumRows returns the number of non-merged rows. If the rows are not cached, a count of -1 and *BlockID of nil are returned.

func (*AddressCache) Reporter

func (ac *AddressCache) Reporter()

Reporter prints the number of cached addresses, rows, and utxos, as well as a table of cache hits and misses.

func (*AddressCache) RowStats

func (ac *AddressCache) RowStats() (hits, misses int)

RowStats reports the row hit/miss stats.

func (*AddressCache) Rows

func (ac *AddressCache) Rows(addr string) ([]dbtypes.AddressRowCompact, *BlockID)

Rows attempts to retrieve an []*AddressRow for the given address. The BlockID for the block at which the cached data is valid is also returned. In the event of a cache miss, the slice and the *BlockID will be nil.

func (*AddressCache) StoreBalance

func (ac *AddressCache) StoreBalance(addr string, balance *dbtypes.AddressBalance, block *BlockID) bool

StoreBalance stores the AddressBalance for the given address in cache. The current best block data is required to determine cache freshness.

func (*AddressCache) StoreHistoryChart

func (ac *AddressCache) StoreHistoryChart(addr string, addrChart dbtypes.HistoryChart,
	chartGrouping dbtypes.TimeBasedGrouping, cd *dbtypes.ChartsData, block *BlockID) bool

StoreHistoryChart stores the charts data for the given address in cache. The current best block data is required to determine cache freshness.

func (*AddressCache) StoreRows

func (ac *AddressCache) StoreRows(addr string, rows []*dbtypes.AddressRow, block *BlockID) bool

StoreRows stores the non-merged AddressRow slice for the given address in cache. The current best block data is required to determine cache freshness.

func (*AddressCache) StoreRowsCompact

func (ac *AddressCache) StoreRowsCompact(addr string, rows []dbtypes.AddressRowCompact, block *BlockID) bool

StoreRowsCompact stores the non-merged AddressRow slice for the given address in cache. The current best block data is required to determine cache freshness.

func (*AddressCache) StoreUTXOs

func (ac *AddressCache) StoreUTXOs(addr string, utxos []apitypes.AddressTxnOutput, block *BlockID) bool

StoreUTXOs stores the AddressTxnOutput slice for the given address in cache. The current best block data is required to determine cache freshness.

func (*AddressCache) Transactions

func (ac *AddressCache) Transactions(addr string, N, offset int64, txnType dbtypes.AddrTxnViewType) ([]*dbtypes.AddressRow, *BlockID, error)

Transactions attempts to retrieve transaction data for the given address and view (merged or not, debit/credit/all). Like the DB queries, the number of transactions to retrieve, N, and the number of transactions to skip, offset, are also specified.

func (*AddressCache) TransactionsCompact

func (ac *AddressCache) TransactionsCompact(addr string, N, offset int64, txnType dbtypes.AddrTxnViewType) ([]dbtypes.AddressRowCompact, *BlockID, error)

TransactionsCompact is like Transactions, but it must be used with a non-merged AddrTxnViewType, and it returns a []dbtypes.AddressRowCompact. A cache miss is indicated by (*BlockID)==nil. The retured rows may be nil or an empty slice for a cache hit if the address has no history.

func (*AddressCache) TransactionsMerged

func (ac *AddressCache) TransactionsMerged(addr string, N, offset int64, txnType dbtypes.AddrTxnViewType) ([]dbtypes.AddressRowMerged, *BlockID, error)

TransactionsMerged is like Transactions, but it must be used with a merged AddrTxnViewType, and it returns a []dbtypes.AddressRowMerged. A cache miss is indicated by (*BlockID)==nil. The retured rows may be nil or an empty slice for a cache hit if the address has no history.

func (*AddressCache) UTXOs

func (ac *AddressCache) UTXOs(addr string) ([]apitypes.AddressTxnOutput, *BlockID)

UTXOs attempts to retrieve an []AddressTxnOutput for the given address. The BlockID for the block at which the cached data is valid is also returned. In the event of a cache miss, the slice and the *BlockID will be nil.

func (*AddressCache) UtxoStats

func (ac *AddressCache) UtxoStats() (hits, misses int)

UtxoStats reports the utxo hit/miss stats.

type AddressCacheItem

type AddressCacheItem struct {
	// contains filtered or unexported fields
}

AddressCacheItem is the unit of cached data pertaining to a certain address. The height and hash of the best block at the time the data was obtained is stored to determine validity of the cache item. Cached data for an address are: balance, all non-merged address table rows, all merged address table rows, all UTXOs, and address metrics.

func (*AddressCacheItem) Balance

func (d *AddressCacheItem) Balance() (*dbtypes.AddressBalance, *BlockID)

Balance is a thread-safe accessor for the *dbtypes.AddressBalance.

func (*AddressCacheItem) BlockHash

func (d *AddressCacheItem) BlockHash() chainhash.Hash

BlockHash is a thread-safe accessor for the block hash.

func (*AddressCacheItem) BlockHeight

func (d *AddressCacheItem) BlockHeight() int64

BlockHeight is a thread-safe accessor for the block height.

func (*AddressCacheItem) HistoryChart

func (d *AddressCacheItem) HistoryChart(addrChart dbtypes.HistoryChart, chartGrouping dbtypes.TimeBasedGrouping) (*dbtypes.ChartsData, *BlockID)

HistoryChart is a thread-safe accessor for the TxHistory.

func (*AddressCacheItem) NumRows

func (d *AddressCacheItem) NumRows() (int, *BlockID)

NumRows returns the number of non-merged rows. If the rows are not cached, a count of -1 and *BlockID of nil are returned.

func (*AddressCacheItem) Rows

Rows is a thread-safe accessor for the []*dbtypes.AddressRow.

func (*AddressCacheItem) SetBalance

func (d *AddressCacheItem) SetBalance(block BlockID, balance *dbtypes.AddressBalance)

SetBalance updates the cache item for the given AddressBalance valid at the given BlockID.

func (*AddressCacheItem) SetRows

func (d *AddressCacheItem) SetRows(block BlockID, rows []dbtypes.AddressRowCompact)

SetRows updates the cache item for the given non-merged AddressRow slice valid at the given BlockID.

func (*AddressCacheItem) SetUTXOs

func (d *AddressCacheItem) SetUTXOs(block BlockID, utxos []apitypes.AddressTxnOutput)

SetUTXOs updates the cache item for the given AddressTxnOutput slice valid at the given BlockID.

func (*AddressCacheItem) Transactions

func (d *AddressCacheItem) Transactions(N, offset int, txnView dbtypes.AddrTxnViewType) (interface{}, *BlockID, error)

Transactions attempts to retrieve transaction data for the given view (merged or not, debit/credit/all). Like the DB queries, the number of transactions to retrieve, N, and the number of transactions to skip, offset, are also specified.

func (*AddressCacheItem) UTXOs

UTXOs is a thread-safe accessor for the []apitypes.AddressTxnOutput.

type BlockID

type BlockID struct {
	Hash   chainhash.Hash
	Height int64
}

BlockID provides basic identifying information about a block.

func NewBlockID

func NewBlockID(hash *chainhash.Hash, height int64) *BlockID

NewBlockID constructs a new BlockID.

type CacheCounts

type CacheCounts struct {
	Hits, Misses int
}

CacheCounts stores cache hits and misses.

type CacheLock

type CacheLock struct {
	// contains filtered or unexported fields
}

CacheLock is a "try lock" for coordinating multiple accessors, while allowing only a single updater. Use NewCacheLock to create a CacheLock.

func NewCacheLock

func NewCacheLock() *CacheLock

NewCacheLock constructs a new CacheLock.

func (*CacheLock) TryLock

func (cl *CacheLock) TryLock(addr string) (busy bool, wait chan struct{}, done func())

TryLock will attempt to obtain an exclusive lock and a function to release the lock. If the lock is already held, the channel returned by TryLock will be closed when/if the holder of the lock calls the done function.

Trylock returns a bool, busy, indicating if another caller has already obtained the lock. When busy is false, the caller has obtained the exclusive lock, and the returned func(), done, should be called when ready to release the lock. When busy is true, the returned channel, wait, should be received from to block until the updater has released the lock.

type CacheMetrics

type CacheMetrics struct {
	// contains filtered or unexported fields
}

CacheMetrics is a collection of CacheCounts for the various cached data.

func (*CacheMetrics) BalanceHit

func (cm *CacheMetrics) BalanceHit()

func (*CacheMetrics) BalanceMiss

func (cm *CacheMetrics) BalanceMiss()

func (*CacheMetrics) BalanceStats

func (cm *CacheMetrics) BalanceStats() (hits, misses int)

func (*CacheMetrics) HistoryHit

func (cm *CacheMetrics) HistoryHit()

func (*CacheMetrics) HistoryMiss

func (cm *CacheMetrics) HistoryMiss()

func (*CacheMetrics) HistoryStats

func (cm *CacheMetrics) HistoryStats() (hits, misses int)

func (*CacheMetrics) RowHit

func (cm *CacheMetrics) RowHit()

func (*CacheMetrics) RowMiss

func (cm *CacheMetrics) RowMiss()

func (*CacheMetrics) RowStats

func (cm *CacheMetrics) RowStats() (hits, misses int)

func (*CacheMetrics) UtxoHit

func (cm *CacheMetrics) UtxoHit()

func (*CacheMetrics) UtxoMiss

func (cm *CacheMetrics) UtxoMiss()

func (*CacheMetrics) UtxoStats

func (cm *CacheMetrics) UtxoStats() (hits, misses int)

type TxHistory

type TxHistory struct {
	TypeByInterval    [dbtypes.NumIntervals]*dbtypes.ChartsData
	AmtFlowByInterval [dbtypes.NumIntervals]*dbtypes.ChartsData
}

TxHistory contains ChartsData for different chart types (tx type and amount flow), each with data at known time intervals (TimeBasedGrouping).

func (*TxHistory) Clear

func (th *TxHistory) Clear()

Clear sets each *ChartsData to nil, effectively clearing the TxHistory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL