<commit_before>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2015-2018 The Bitcoin Unlimited developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_TXDB_H
#define BITCOIN_TXDB_H

#include "blockstorage/dbabstract.h"
#include "chain.h"
#include "coins.h"
#include "dbwrapper.h"

#include <map>
#include <string>
#include <utility>
#include <vector>

class CBlockFileInfo;
class CBlockIndex;
class uint256;

static const bool DEFAULT_TXINDEX = false;

//! -dbcache default (MiB)
static const int64_t nDefaultDbCache = 500;
//! max. -dbcache in (MiB)
static const int64_t nMaxDbCache = sizeof(void *) > 4 ? 16384 : 2048;
//! min. -dbcache in (MiB)
static const int64_t nMinDbCache = 4;
//! % of available memory to leave unused by dbcache if/when we dynamically size the dbcache.
static const int64_t nDefaultPcntMemUnused = 10;
//! max increase in cache size since the last time we did a full flush
static const uint64_t nMaxCacheIncreaseSinceLastFlush = 512 * 1000 * 1000;
//! the minimum system memory we always keep free when doing automatic dbcache sizing
static const uint64_t nMinMemToKeepAvaialable = 300 * 1000 * 1000;
//! the max size a batch can get before a write to the utxo is made
static const size_t nMaxDBBatchSize = 16 << 20;
//! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
static const int64_t nMaxBlockDBCache = 2;
//! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
//! Max memory allocated to coin DB specific cache (MiB)
static const int64_t nMaxCoinsDBCache = 8;

/** Get the current available memory */
uint64_t GetAvailableMemory();
/** Get the total physical memory */
uint64_t GetTotalSystemMemory();
/** Get the sizes for each of the caches. This is done during init.cpp on startup but also
 *  later, during dynamic sizing of the coins cache, when need to know the initial startup values.
 */
void GetCacheConfiguration(int64_t &_nBlockDBCache,
    int64_t &_nBlockUndoDBcache,
    int64_t &_nBlockTreeDBCache,
    int64_t &_nCoinDBCache,
    int64_t &_nCoinCacheMaxSize,
    bool fDefault = false);
/** Calculate the various cache sizes. This is primarily used in GetCacheConfiguration() however during
 *  dynamic sizing of the coins cache we also need to use this function directly.
 */
void CacheSizeCalculations(int64_t _nTotalCache,
    int64_t &_nBlockDBCache,
    int64_t &_nBlockUndoDBcache,
    int64_t &_nBlockTreeDBCache,
    int64_t &_nCoinDBCache,
    int64_t &_nCoinCacheMaxSize);
/** This function is called during FlushStateToDisk.  The coins cache is dynamically sized before any
 *  checking is done for cache flushing and trimming
 */
void AdjustCoinCacheSize();

struct CDiskTxPos : public CDiskBlockPos
{
    unsigned int nTxOffset; // after header

    ADD_SERIALIZE_METHODS;

    template <typename Stream, typename Operation>
    inline void SerializationOp(Stream &s, Operation ser_action)
    {
        READWRITE(*(CDiskBlockPos *)this);
        READWRITE(VARINT(nTxOffset));
    }

    CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
        : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn)
    {
    }

    CDiskTxPos() { SetNull(); }
    void SetNull()
    {
        CDiskBlockPos::SetNull();
        nTxOffset = 0;
    }
};

class CCoinsViewDBCursor;

/** CCoinsView backed by the coin database (chainstate/) */
class CCoinsViewDB : public CCoinsView
{
protected:
    CDBWrapper db;

public:
    CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);

    bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
    bool HaveCoin(const COutPoint &outpoint) const override;
    uint256 GetBestBlock() const;
    uint256 _GetBestBlock() const;
    uint256 GetBestBlock(BlockDBMode mode) const;
    uint256 _GetBestBlock(BlockDBMode mode) const;
    void WriteBestBlock(const uint256 &hashBlock);
    void _WriteBestBlock(const uint256 &hashBlock);
    void WriteBestBlock(const uint256 &hashBlock, BlockDBMode mode);
    void _WriteBestBlock(const uint256 &hashBlock, BlockDBMode mode);
    bool BatchWrite(CCoinsMap &mapCoins,<commit_msg>Add missing override attribute to _GetBestBlock()<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2015-2018 The Bitcoin Unlimited developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_TXDB_H
#define BITCOIN_TXDB_H

#include "blockstorage/dbabstract.h"
#include "chain.h"
#include "coins.h"
#include "dbwrapper.h"

#include <map>
#include <string>
#include <utility>
#include <vector>

class CBlockFileInfo;
class CBlockIndex;
class uint256;

static const bool DEFAULT_TXINDEX = false;

//! -dbcache default (MiB)
static const int64_t nDefaultDbCache = 500;
//! max. -dbcache in (MiB)
static const int64_t nMaxDbCache = sizeof(void *) > 4 ? 16384 : 2048;
//! min. -dbcache in (MiB)
static const int64_t nMinDbCache = 4;
//! % of available memory to leave unused by dbcache if/when we dynamically size the dbcache.
static const int64_t nDefaultPcntMemUnused = 10;
//! max increase in cache size since the last time we did a full flush
static const uint64_t nMaxCacheIncreaseSinceLastFlush = 512 * 1000 * 1000;
//! the minimum system memory we always keep free when doing automatic dbcache sizing
static const uint64_t nMinMemToKeepAvaialable = 300 * 1000 * 1000;
//! the max size a batch can get before a write to the utxo is made
static const size_t nMaxDBBatchSize = 16 << 20;
//! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
static const int64_t nMaxBlockDBCache = 2;
//! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
//! Max memory allocated to coin DB specific cache (MiB)
static const int64_t nMaxCoinsDBCache = 8;

/** Get the current available memory */
uint64_t GetAvailableMemory();
/** Get the total physical memory */
uint64_t GetTotalSystemMemory();
/** Get the sizes for each of the caches. This is done during init.cpp on startup but also
 *  later, during dynamic sizing of the coins cache, when need to know the initial startup values.
 */
void GetCacheConfiguration(int64_t &_nBlockDBCache,
    int64_t &_nBlockUndoDBcache,
    int64_t &_nBlockTreeDBCache,
    int64_t &_nCoinDBCache,
    int64_t &_nCoinCacheMaxSize,
    bool fDefault = false);
/** Calculate the various cache sizes. This is primarily used in GetCacheConfiguration() however during
 *  dynamic sizing of the coins cache we also need to use this function directly.
 */
void CacheSizeCalculations(int64_t _nTotalCache,
    int64_t &_nBlockDBCache,
    int64_t &_nBlockUndoDBcache,
    int64_t &_nBlockTreeDBCache,
    int64_t &_nCoinDBCache,
    int64_t &_nCoinCacheMaxSize);
/** This function is called during FlushStateToDisk.  The coins cache is dynamically sized before any
 *  checking is done for cache flushing and trimming
 */
void AdjustCoinCacheSize();

struct CDiskTxPos : public CDiskBlockPos
{
    unsigned int nTxOffset; // after header

    ADD_SERIALIZE_METHODS;

    template <typename Stream, typename Operation>
    inline void SerializationOp(Stream &s, Operation ser_action)
    {
        READWRITE(*(CDiskBlockPos *)this);
        READWRITE(VARINT(nTxOffset));
    }

    CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
        : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn)
    {
    }

    CDiskTxPos() { SetNull(); }
    void SetNull()
    {
        CDiskBlockPos::SetNull();
        nTxOffset = 0;
    }
};

class CCoinsViewDBCursor;

/** CCoinsView backed by the coin database (chainstate/) */
class CCoinsViewDB : public CCoinsView
{
protected:
    CDBWrapper db;

public:
    CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);

    bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
    bool HaveCoin(const COutPoint &outpoint) const override;
    uint256 GetBestBlock() const;
    uint256 _GetBestBlock() const override;
    uint256 GetBestBlock(BlockDBMode mode) const;
    uint256 _GetBestBlock(BlockDBMode mode) const;
    void WriteBestBlock(const uint256 &hashBlock);
    void _WriteBestBlock(const uint256 &hashBlock);
    void WriteBestBlock(const uint256 &hashBlock, BlockDBMode mode);
    void _WriteBestBlock(const uint256 &hashBlock, BlockDBMode mode);
    bool BatchWrite(CCoinsMap &mapCoins,
{'fraction_non_alphanumeric': 0.08222490931076179, 'fraction_numerical': 0.034516873694624604, 'mean_word_length': 4.323581041544763, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6830327', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2824, 'n_words': 965}