Bitcoin Core 28.99.0
P2P Digital Currency
flatfile.h
Go to the documentation of this file.
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 <serialize.h>
12#include <util/fs.h>
13
15{
16 int nFile{-1};
17 unsigned int nPos{0};
18
20
21 FlatFilePos() = default;
22
23 FlatFilePos(int nFileIn, unsigned int nPosIn) :
24 nFile(nFileIn),
25 nPos(nPosIn)
26 {}
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 bool IsNull() const { return (nFile == -1); }
37
38 std::string ToString() const;
39};
40
46{
47private:
49 const char* const m_prefix;
50 const size_t m_chunk_size;
51
52public:
60 FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
61
63 fs::path FileName(const FlatFilePos& pos) const;
64
66 FILE* Open(const FlatFilePos& pos, bool read_only = false) const;
67
77 size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const;
78
86 bool Flush(const FlatFilePos& pos, bool finalize = false) const;
87};
88
89#endif // BITCOIN_FLATFILE_H
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:46
FILE * Open(const FlatFilePos &pos, bool read_only=false) const
Open a handle to the file at the given position.
Definition: flatfile.cpp:33
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:28
bool Flush(const FlatFilePos &pos, bool finalize=false) const
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
Definition: flatfile.cpp:81
const fs::path m_dir
Definition: flatfile.h:48
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space) const
Allocate additional space in a file after the given starting position.
Definition: flatfile.cpp:55
const size_t m_chunk_size
Definition: flatfile.h:50
const char *const m_prefix
Definition: flatfile.h:49
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
Definition: flatfile.cpp:13
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
const char * prefix
Definition: rest.cpp:1009
#define VARINT(obj)
Definition: serialize.h:498
#define VARINT_MODE(obj, mode)
Definition: serialize.h:497
@ NONNEGATIVE_SIGNED
#define READWRITE(...)
Definition: serialize.h:156
int nFile
Definition: flatfile.h:16
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:28
FlatFilePos()=default
FlatFilePos(int nFileIn, unsigned int nPosIn)
Definition: flatfile.h:23
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:32
std::string ToString() const
Definition: flatfile.cpp:23
unsigned int nPos
Definition: flatfile.h:17
SERIALIZE_METHODS(FlatFilePos, obj)
Definition: flatfile.h:19
bool IsNull() const
Definition: flatfile.h:36