/* * This file is part of the Flowee project * Copyright (C) 2017-2024 Tom Zander * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLOCKSDB_P_H #define BLOCKSDB_P_H /* * WARNING USAGE OF THIS HEADER IS RESTRICTED. * This Header file is part of the private API and is meant to be used solely by the server component. * * Usage of this API will likely mean your code will break in interesting ways in the future, * or even stop to compile. * * YOU HAVE BEEN WARNED!! */ #include "chain.h" #include "BlocksDB.h" #include "streaming/ConstBuffer.h" #include #include #include #include #include #include class CBlockIndex; class CScheduler; namespace Blocks { struct DataFile { DataFile() : filesize(0) {} boost::iostreams::mapped_file file; std::weak_ptr buffer; size_t filesize; }; enum BlockType { ForwardBlock, RevertBlock, MetaDataForBlock }; struct FileHistoryEntry { FileHistoryEntry(const std::shared_ptr &dataFile, int64_t lastAccessed) : dataFile(dataFile), lastAccessed(lastAccessed) {} std::shared_ptr dataFile; int64_t lastAccessed = 0; }; class BlockFileInfo { public: unsigned int nBlocks; //! number of blocks stored in file unsigned int nSize; //! number of used bytes of block file unsigned int nUndoSize; //! number of used bytes in the undo file ADD_SERIALIZE_METHODS template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { READWRITE(VARINT(nBlocks)); READWRITE(VARINT(nSize)); READWRITE(VARINT(nUndoSize)); } void SetNull() { nBlocks = 0; nSize = 0; nUndoSize = 0; } BlockFileInfo() { SetNull(); } std::string ToString() const; /** update statistics (does not update nSize) */ void AddBlock() { nBlocks++; } }; class DBPrivate : public std::enable_shared_from_this { public: DBPrivate(); ~DBPrivate(); Streaming::ConstBuffer loadBlock(CDiskBlockPos pos, BlockType type); Streaming::ConstBuffer writeBlock(const std::deque &block, CDiskBlockPos &pos, BlockType type); void unloadIndexMap(); void foundBlockFile(int index, const BlockFileInfo &info); std::shared_ptr mapFile(int fileIndex, BlockType type, size_t *size_out = 0, bool *isWritable = nullptr); // close files from filehistory that have been unused for some time. void setScheduler(CScheduler *scheduler); void pruneFiles(); /** * @brief allByHeight Sort and return the blocks by height. */ std::vector > allByHeight() const; CChain headersChain; std::list headerChainTips; std::vector blocksDataDirs; std::recursive_mutex lock; bool nextWriteToNewFile = false; // we detected a writing issue with the last file, go to a new data file. std::vector datafiles; std::vector revertDatafiles; std::list fileHistory; // keep the last opened ones to avoid opening and closing files all the time. std::mutex blockIndexLock; typedef boost::unordered_map BlockMap; BlockMap indexMap; ReindexingState reindexing = NoReindex; }; } // a global owned by main.cpp extern std::vector vinfoBlockFile; #endif