Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2020 The Bitcoin Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef BITCOIN_FLATFILE_H 7 : #define BITCOIN_FLATFILE_H 8 : 9 : #include <string> 10 : 11 : #include <fs.h> 12 : #include <serialize.h> 13 : 14 : struct FlatFilePos 15 : { 16 145624 : int nFile{-1}; 17 145624 : unsigned int nPos{0}; 18 : 19 118395 : SERIALIZE_METHODS(FlatFilePos, obj) 20 : { 21 39465 : READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); 22 39465 : } 23 : 24 436481 : FlatFilePos() {} 25 : 26 23955 : FlatFilePos(int nFileIn, unsigned int nPosIn) : 27 23829 : nFile(nFileIn), 28 23829 : nPos(nPosIn) 29 23955 : {} 30 : 31 : friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) { 32 : return (a.nFile == b.nFile && a.nPos == b.nPos); 33 : } 34 : 35 : friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) { 36 : return !(a == b); 37 : } 38 : 39 145929 : bool IsNull() const { return (nFile == -1); } 40 : 41 : std::string ToString() const; 42 : }; 43 : 44 : /** 45 : * FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates 46 : * access to and efficient management of these files. 47 : */ 48 : class FlatFileSeq 49 : { 50 : private: 51 : const fs::path m_dir; 52 : const char* const m_prefix; 53 : const size_t m_chunk_size; 54 : 55 : public: 56 : /** 57 : * Constructor 58 : * 59 : * @param dir The base directory that all files live in. 60 : * @param prefix A short prefix given to all file names. 61 : * @param chunk_size Disk space is pre-allocated in multiples of this amount. 62 : */ 63 : FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size); 64 : 65 : /** Get the name of the file at the given position. */ 66 : fs::path FileName(const FlatFilePos& pos) const; 67 : 68 : /** Open a handle to the file at the given position. */ 69 : FILE* Open(const FlatFilePos& pos, bool read_only = false); 70 : 71 : /** 72 : * Allocate additional space in a file after the given starting position. The amount allocated 73 : * will be the minimum multiple of the sequence chunk size greater than add_size. 74 : * 75 : * @param[in] pos The starting position that bytes will be allocated after. 76 : * @param[in] add_size The minimum number of bytes to be allocated. 77 : * @param[out] out_of_space Whether the allocation failed due to insufficient disk space. 78 : * @return The number of bytes successfully allocated. 79 : */ 80 : size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space); 81 : 82 : /** 83 : * Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final. 84 : * 85 : * @param[in] pos The first unwritten position in the file to be flushed. 86 : * @param[in] finalize True if no more data will be written to this file. 87 : * @return true on success, false on failure. 88 : */ 89 : bool Flush(const FlatFilePos& pos, bool finalize = false); 90 : }; 91 : 92 : #endif // BITCOIN_FLATFILE_H