Files

152 lines
4.1 KiB
C++
Raw Permalink Normal View History

2017-04-03 15:30:03 +02:00
/*
2017-11-13 23:09:33 +01:00
* This file is part of the Flowee project
* Copyright (C) 2017-2024 Tom Zander <tom@flowee.org>
2017-04-03 15:30:03 +02:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
2018-01-16 10:47:52 +00:00
#ifndef BLOCKSDB_P_H
#define BLOCKSDB_P_H
2017-04-03 15:30:03 +02:00
2019-04-06 12:19:13 +02:00
/*
* 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!!
*/
2017-04-03 15:30:03 +02:00
#include "chain.h"
#include "BlocksDB.h"
#include "streaming/ConstBuffer.h"
2017-04-03 15:30:03 +02:00
#include <vector>
#include <mutex>
#include <memory>
2017-04-03 15:30:03 +02:00
#include <list>
2019-04-01 15:34:13 +02:00
#include <boost/unordered_map.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
2017-04-03 15:30:03 +02:00
class CBlockIndex;
2018-09-25 16:24:05 +02:00
class CScheduler;
2017-04-03 15:30:03 +02:00
namespace Blocks {
struct DataFile
{
DataFile() : filesize(0) {}
2017-09-03 21:17:27 +02:00
boost::iostreams::mapped_file file;
std::weak_ptr<char> buffer;
size_t filesize;
};
enum BlockType {
ForwardBlock,
2021-03-08 18:14:47 +01:00
RevertBlock,
MetaDataForBlock
};
struct FileHistoryEntry
{
2018-09-25 16:24:05 +02:00
FileHistoryEntry(const std::shared_ptr<char> &dataFile, int64_t lastAccessed)
: dataFile(dataFile), lastAccessed(lastAccessed) {}
std::shared_ptr<char> 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 <typename Stream, typename Operation>
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<DBPrivate>
{
2017-04-03 15:30:03 +02:00
public:
DBPrivate();
~DBPrivate();
2017-04-03 15:30:03 +02:00
Streaming::ConstBuffer loadBlock(CDiskBlockPos pos, BlockType type);
Streaming::ConstBuffer writeBlock(const std::deque<Streaming::ConstBuffer> &block, CDiskBlockPos &pos, BlockType type);
2018-01-15 15:26:12 +00:00
void unloadIndexMap();
void foundBlockFile(int index, const BlockFileInfo &info);
2018-01-15 15:26:12 +00:00
std::shared_ptr<char> mapFile(int fileIndex, BlockType type, size_t *size_out = 0, bool *isWritable = nullptr);
2018-09-25 16:24:05 +02:00
// close files from filehistory that have been unused for some time.
void setScheduler(CScheduler *scheduler);
2019-02-15 13:31:49 +01:00
void pruneFiles();
2020-02-27 13:35:56 +01:00
/**
* @brief allByHeight Sort and return the blocks by height.
*/
std::vector<std::pair<int, CBlockIndex*> > allByHeight() const;
2018-09-25 16:24:05 +02:00
2017-04-03 15:30:03 +02:00
CChain headersChain;
std::list<CBlockIndex*> headerChainTips;
2017-08-01 12:14:54 +02:00
std::vector<std::string> blocksDataDirs;
2018-01-15 15:26:12 +00:00
std::recursive_mutex lock;
bool nextWriteToNewFile = false; // we detected a writing issue with the last file, go to a new data file.
std::vector<DataFile*> datafiles;
std::vector<DataFile*> revertDatafiles;
2019-04-28 22:58:36 +02:00
std::list<FileHistoryEntry> fileHistory; // keep the last opened ones to avoid opening and closing files all the time.
2018-01-15 15:26:12 +00:00
std::mutex blockIndexLock;
2019-04-01 15:34:13 +02:00
typedef boost::unordered_map<uint256, CBlockIndex*, HashShortener> BlockMap;
2018-01-15 15:26:12 +00:00
BlockMap indexMap;
ReindexingState reindexing = NoReindex;
2017-04-03 15:30:03 +02:00
};
}
// a global owned by main.cpp
extern std::vector<Blocks::BlockFileInfo> vinfoBlockFile;
2017-04-03 15:30:03 +02:00
#endif