LCOV - code coverage report
Current view: top level - src - flatfile.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 9 9 100.0 %
Date: 2020-09-26 01:30:44 Functions: 14 14 100.0 %

          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       37578 :     SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
      20             : 
      21      749593 :     FlatFilePos() : nFile(-1), nPos(0) {}
      22             : 
      23       12615 :     FlatFilePos(int nFileIn, unsigned int nPosIn) :
      24        6789 :         nFile(nFileIn),
      25        6789 :         nPos(nPosIn)
      26       12615 :     {}
      27             : 
      28             :     friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
      29             :         return (a.nFile == b.nFile && a.nPos == b.nPos);
      30             :     }
      31             : 
      32             :     friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
      33             :         return !(a == b);
      34             :     }
      35             : 
      36         219 :     void SetNull() { nFile = -1; nPos = 0; }
      37      318290 :     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      594307 : 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

Generated by: LCOV version 1.15