dash/src/flatfile.h
Konstantin Akimov 4aa197dbdb Merge #18673: scripted-diff: Sort test includes
fa4632c41714dfaa699bacc6a947d72668a4deef test: Move boost/stdlib includes last (MarcoFalke)
fa488f131fd4f5bab0d01376c5a5013306f1abcd scripted-diff: Bump copyright headers (MarcoFalke)
fac5c373006a9e4bcbb56843bb85f1aca4d87599 scripted-diff: Sort test includes (MarcoFalke)

Pull request description:

  When writing tests, often includes need to be added or removed. Currently the list of includes is not sorted, so developers that write tests and have `clang-format` installed will either have an unrelated change (sorting) included in their commit or they will have to manually undo the sort.

  This pull preempts both issues by just sorting all includes in one commit.

  Please be aware that this is **NOT** a change to policy to enforce clang-format or any other developer guideline or process. Developers are free to use whatever tool they want, see also #18651.

  Edit: Also includes a commit to bump the copyright headers, so that the touched files don't need to be touched again for that.

ACKs for top commit:
  practicalswift:
    ACK fa4632c41714dfaa699bacc6a947d72668a4deef
  jonatack:
    ACK fa4632c41714dfaa, light review and sanity checks with gcc build and clang fuzz build

Tree-SHA512: 130a8d073a379ba556b1e64104d37c46b671425c0aef0ed725fd60156a95e8dc83fb6f0b5330b2f8152cf5daaf3983b4aca5e75812598f2626c39fd12b88b180
2023-08-29 22:00:59 -05:00

94 lines
2.9 KiB
C++

// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_FLATFILE_H
#define BITCOIN_FLATFILE_H
#include <string>
#include <fs.h>
#include <serialize.h>
struct FlatFilePos
{
int nFile;
unsigned int nPos;
SERIALIZE_METHODS(FlatFilePos, obj)
{
READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos));
}
FlatFilePos() : nFile(-1), nPos(0) {}
FlatFilePos(int nFileIn, unsigned int nPosIn) :
nFile(nFileIn),
nPos(nPosIn)
{}
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
return (a.nFile == b.nFile && a.nPos == b.nPos);
}
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
return !(a == b);
}
void SetNull() { nFile = -1; nPos = 0; }
bool IsNull() const { return (nFile == -1); }
std::string ToString() const;
};
/**
* FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
* access to and efficient management of these files.
*/
class FlatFileSeq
{
private:
const fs::path m_dir;
const char* const m_prefix;
const size_t m_chunk_size;
public:
/**
* Constructor
*
* @param dir The base directory that all files live in.
* @param prefix A short prefix given to all file names.
* @param chunk_size Disk space is pre-allocated in multiples of this amount.
*/
FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
/** Get the name of the file at the given position. */
fs::path FileName(const FlatFilePos& pos) const;
/** Open a handle to the file at the given position. */
FILE* Open(const FlatFilePos& pos, bool read_only = false);
/**
* Allocate additional space in a file after the given starting position. The amount allocated
* will be the minimum multiple of the sequence chunk size greater than add_size.
*
* @param[in] pos The starting position that bytes will be allocated after.
* @param[in] add_size The minimum number of bytes to be allocated.
* @param[out] out_of_space Whether the allocation failed due to insufficient disk space.
* @return The number of bytes successfully allocated.
*/
size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space);
/**
* Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
*
* @param[in] pos The first unwritten position in the file to be flushed.
* @param[in] finalize True if no more data will be written to this file.
* @return true on success, false on failure.
*/
bool Flush(const FlatFilePos& pos, bool finalize = false);
};
#endif // BITCOIN_FLATFILE_H