Branch data 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 : : int nFile; # 17 : : unsigned int nPos; # 18 : : # 19 : 18163 : SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); } # 20 : : # 21 : 367056 : FlatFilePos() : nFile(-1), nPos(0) {} # 22 : : # 23 : : FlatFilePos(int nFileIn, unsigned int nPosIn) : # 24 : : nFile(nFileIn), # 25 : : nPos(nPosIn) # 26 : 8737 : {} # 27 : : # 28 : 0 : friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) { # 29 : 0 : return (a.nFile == b.nFile && a.nPos == b.nPos); # 30 : 0 : } # 31 : : # 32 : 0 : friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) { # 33 : 0 : return !(a == b); # 34 : 0 : } # 35 : : # 36 : 444 : void SetNull() { nFile = -1; nPos = 0; } # 37 : 438004 : bool IsNull() const { return (nFile == -1); } # 38 : : # 39 : : std::string ToString() const; # 40 : : }; # 41 : : # 42 : : /** # 43 : : * FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates # 44 : : * access to and efficient management of these files. # 45 : : */ # 46 : : class FlatFileSeq # 47 : : { # 48 : : private: # 49 : : const fs::path m_dir; # 50 : : const char* const m_prefix; # 51 : : const size_t m_chunk_size; # 52 : : # 53 : : public: # 54 : : /** # 55 : : * Constructor # 56 : : * # 57 : : * @param dir The base directory that all files live in. # 58 : : * @param prefix A short prefix given to all file names. # 59 : : * @param chunk_size Disk space is pre-allocated in multiples of this amount. # 60 : : */ # 61 : : FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size); # 62 : : # 63 : : /** Get the name of the file at the given position. */ # 64 : : fs::path FileName(const FlatFilePos& pos) const; # 65 : : # 66 : : /** Open a handle to the file at the given position. */ # 67 : : FILE* Open(const FlatFilePos& pos, bool read_only = false); # 68 : : # 69 : : /** # 70 : : * Allocate additional space in a file after the given starting position. The amount allocated # 71 : : * will be the minimum multiple of the sequence chunk size greater than add_size. # 72 : : * # 73 : : * @param[in] pos The starting position that bytes will be allocated after. # 74 : : * @param[in] add_size The minimum number of bytes to be allocated. # 75 : : * @param[out] out_of_space Whether the allocation failed due to insufficient disk space. # 76 : : * @return The number of bytes successfully allocated. # 77 : : */ # 78 : : size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space); # 79 : : # 80 : : /** # 81 : : * Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final. # 82 : : * # 83 : : * @param[in] pos The first unwritten position in the file to be flushed. # 84 : : * @param[in] finalize True if no more data will be written to this file. # 85 : : * @return true on success, false on failure. # 86 : : */ # 87 : : bool Flush(const FlatFilePos& pos, bool finalize = false); # 88 : : }; # 89 : : # 90 : : #endif // BITCOIN_FLATFILE_H