Files
bitcoin/src/flatfile.h
merge-script 77248e8496 Merge bitcoin/bitcoin#33771: refactor: C++20 operators
48840bfc2d refactor: Prefer `<=>` over multiple relational operators (Daniel Pfeifer)
5a0f49bd26 refactor: Remove all `operator!=` definitions (Daniel Pfeifer)

Pull request description:

  Remove all `operator!=` definitions and provide `operator<=>` as a replacement where all relational comparison operators were defined before.

  The compiler is able to deduce missing comparison operators from `operator!=` and `operator<=>`. The compiler provided operators have the following advantages:

  1. less code
  2. guaranteed consistency

  Refactoring that changes the implementation, or replaces it with `= default` is left for a separate PR.

ACKs for top commit:
  optout21:
    utACK 48840bfc2d
  Chand-ra:
    tACK [`48840bf`](48840bfc2d). Built the PR and ran unit tests; everything passes.
  maflcko:
    review ACK 48840bfc2d 🌖
  stickies-v:
    utACK 48840bfc2d. Pretty straightforward cleanup taking advantage of C++20 improvements, nice.
  janb84:
    ACK 48840bfc2d
  sipa:
    ACK 48840bfc2d

Tree-SHA512: 7fedc4abc451c7ad611e3a960ff939a35580667222009cb30ca546e564dc9161e3e8d4d1d7d44c538d961cc8f7adba6e6dbcebcd1be370bf33aef294d06f236b
2025-12-08 16:46:03 +00:00

86 lines
2.7 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 <serialize.h>
#include <util/fs.h>
struct FlatFilePos
{
int32_t nFile{-1};
uint32_t nPos{0};
SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
FlatFilePos() = default;
FlatFilePos(int32_t nFileIn, uint32_t nPosIn)
: nFile{nFileIn},
nPos{nPosIn}
{}
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
return (a.nFile == b.nFile && a.nPos == b.nPos);
}
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) const;
/**
* 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) const;
/**
* 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) const;
};
#endif // BITCOIN_FLATFILE_H