Changeset View
Changeset View
Standalone View
Standalone View
src/flatfile.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_FLATFILE_H | #ifndef BITCOIN_FLATFILE_H | ||||
#define BITCOIN_FLATFILE_H | #define BITCOIN_FLATFILE_H | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <string> | #include <string> | ||||
struct CDiskBlockPos { | struct FlatFilePos { | ||||
int nFile; | int nFile; | ||||
unsigned int nPos; | unsigned int nPos; | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(VARINT(nFile)); | READWRITE(VARINT(nFile)); | ||||
READWRITE(VARINT(nPos)); | READWRITE(VARINT(nPos)); | ||||
} | } | ||||
CDiskBlockPos() { SetNull(); } | FlatFilePos() { SetNull(); } | ||||
CDiskBlockPos(int nFileIn, unsigned int nPosIn) { | FlatFilePos(int nFileIn, unsigned int nPosIn) { | ||||
nFile = nFileIn; | nFile = nFileIn; | ||||
nPos = nPosIn; | nPos = nPosIn; | ||||
} | } | ||||
friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) { | friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) { | ||||
return (a.nFile == b.nFile && a.nPos == b.nPos); | return (a.nFile == b.nFile && a.nPos == b.nPos); | ||||
} | } | ||||
friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) { | friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) { | ||||
return !(a == b); | return !(a == b); | ||||
} | } | ||||
void SetNull() { | void SetNull() { | ||||
nFile = -1; | nFile = -1; | ||||
nPos = 0; | nPos = 0; | ||||
} | } | ||||
bool IsNull() const { return (nFile == -1); } | bool IsNull() const { return (nFile == -1); } | ||||
Show All 18 Lines | public: | ||||
* @param dir The base directory that all files live in. | * @param dir The base directory that all files live in. | ||||
* @param prefix A short prefix given to all file names. | * @param prefix A short prefix given to all file names. | ||||
* @param chunk_size Disk space is pre-allocated in multiples of this | * @param chunk_size Disk space is pre-allocated in multiples of this | ||||
* amount. | * amount. | ||||
*/ | */ | ||||
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size); | FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size); | ||||
/** Get the name of the file at the given position. */ | /** Get the name of the file at the given position. */ | ||||
fs::path FileName(const CDiskBlockPos &pos) const; | fs::path FileName(const FlatFilePos &pos) const; | ||||
/** Open a handle to the file at the given position. */ | /** Open a handle to the file at the given position. */ | ||||
FILE *Open(const CDiskBlockPos &pos, bool fReadOnly = false); | FILE *Open(const FlatFilePos &pos, bool fReadOnly = false); | ||||
/** | /** | ||||
* Allocate additional space in a file after the given starting position. | * Allocate additional space in a file after the given starting position. | ||||
* The amount allocated will be the minimum multiple of the sequence chunk | * The amount allocated will be the minimum multiple of the sequence chunk | ||||
* size greater than add_size. | * size greater than add_size. | ||||
* | * | ||||
* @param[in] pos The starting position that bytes will be allocated after. | * @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[in] add_size The minimum number of bytes to be allocated. | ||||
* @param[out] out_of_space Whether the allocation failed due to | * @param[out] out_of_space Whether the allocation failed due to | ||||
* insufficient disk space. | * insufficient disk space. | ||||
* @return The number of bytes successfully allocated. | * @return The number of bytes successfully allocated. | ||||
*/ | */ | ||||
size_t Allocate(const CDiskBlockPos &pos, size_t add_size, | size_t Allocate(const FlatFilePos &pos, size_t add_size, | ||||
bool &out_of_space); | bool &out_of_space); | ||||
/** | /** | ||||
* Commit a file to disk, and optionally truncate off extra pre-allocated | * Commit a file to disk, and optionally truncate off extra pre-allocated | ||||
* bytes if final. | * bytes if final. | ||||
* | * | ||||
* @param[in] pos The first unwritten position in the file to be flushed. | * @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. | * @param[in] finalize True if no more data will be written to this file. | ||||
* @return true on success, false on failure. | * @return true on success, false on failure. | ||||
*/ | */ | ||||
bool Flush(const CDiskBlockPos &pos, bool finalize = false); | bool Flush(const FlatFilePos &pos, bool finalize = false); | ||||
}; | }; | ||||
#endif // BITCOIN_FLATFILE_H | #endif // BITCOIN_FLATFILE_H |