2019-01-06 20:43:38 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
2019-01-06 20:06:31 +01:00
|
|
|
// 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
|
|
|
|
|
2019-01-06 20:43:38 +01:00
|
|
|
#include <string>
|
|
|
|
|
2019-01-06 20:06:31 +01:00
|
|
|
#include <fs.h>
|
2019-01-06 20:43:38 +01:00
|
|
|
#include <serialize.h>
|
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
struct FlatFilePos
|
2019-01-06 20:43:38 +01:00
|
|
|
{
|
|
|
|
int nFile;
|
|
|
|
unsigned int nPos;
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
|
|
READWRITE(VARINT(nFile, VarIntMode::NONNEGATIVE_SIGNED));
|
|
|
|
READWRITE(VARINT(nPos));
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:20:57 +01:00
|
|
|
FlatFilePos() : nFile(-1), nPos(0) {}
|
2019-01-06 20:43:38 +01:00
|
|
|
|
2019-01-24 20:20:57 +01:00
|
|
|
FlatFilePos(int nFileIn, unsigned int nPosIn) :
|
|
|
|
nFile(nFileIn),
|
|
|
|
nPos(nPosIn)
|
|
|
|
{}
|
2019-01-06 20:43:38 +01:00
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
|
2019-01-06 20:43:38 +01:00
|
|
|
return (a.nFile == b.nFile && a.nPos == b.nPos);
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:46:30 +01:00
|
|
|
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
|
2019-01-06 20:43:38 +01:00
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() { nFile = -1; nPos = 0; }
|
|
|
|
bool IsNull() const { return (nFile == -1); }
|
|
|
|
|
|
|
|
std::string ToString() const;
|
|
|
|
};
|
2019-01-06 20:06:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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. */
|
2019-01-06 20:46:30 +01:00
|
|
|
fs::path FileName(const FlatFilePos& pos) const;
|
2019-01-06 20:14:22 +01:00
|
|
|
|
|
|
|
/** Open a handle to the file at the given position. */
|
2019-01-24 20:20:57 +01:00
|
|
|
FILE* Open(const FlatFilePos& pos, bool read_only = false);
|
2019-01-06 20:27:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-01-06 20:46:30 +01:00
|
|
|
size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space);
|
2019-01-06 19:14:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-01-06 20:46:30 +01:00
|
|
|
bool Flush(const FlatFilePos& pos, bool finalize = false);
|
2019-01-06 20:06:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BITCOIN_FLATFILE_H
|