Bitcoin ABC  0.24.7
P2P Digital Currency
flatfile.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <fs.h>
10 #include <serialize.h>
11 
12 #include <string>
13 
14 struct FlatFilePos {
15  int nFile;
16  unsigned int nPos;
17 
20  VARINT(obj.nPos));
21  }
22 
23  FlatFilePos() : nFile(-1), nPos(0) {}
24 
25  FlatFilePos(int nFileIn, unsigned int nPosIn)
26  : nFile(nFileIn), nPos(nPosIn) {}
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  void SetNull() {
37  nFile = -1;
38  nPos = 0;
39  }
40  bool IsNull() const { return (nFile == -1); }
41 
42  std::string ToString() const;
43 };
44 
49 class FlatFileSeq {
50 private:
51  const fs::path m_dir;
52  const char *const m_prefix;
53  const size_t m_chunk_size;
54 
55 public:
64  FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size);
65 
67  fs::path FileName(const FlatFilePos &pos) const;
68 
70  FILE *Open(const FlatFilePos &pos, bool read_only = false);
71 
83  size_t Allocate(const FlatFilePos &pos, size_t add_size,
84  bool &out_of_space);
85 
94  bool Flush(const FlatFilePos &pos, bool finalize = false);
95 };
96 
97 #endif // BITCOIN_FLATFILE_H
FlatFilePos::nFile
int nFile
Definition: flatfile.h:15
FlatFilePos::operator==
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:28
FlatFilePos::SetNull
void SetNull()
Definition: flatfile.h:36
fs.h
FlatFilePos::ToString
std::string ToString() const
Definition: flatfile.cpp:20
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:595
FlatFileSeq::m_chunk_size
const size_t m_chunk_size
Definition: flatfile.h:53
FlatFileSeq::Allocate
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space)
Allocate additional space in a file after the given starting position.
Definition: flatfile.cpp:51
FlatFilePos::IsNull
bool IsNull() const
Definition: flatfile.h:40
VarIntMode::NONNEGATIVE_SIGNED
@ NONNEGATIVE_SIGNED
FlatFilePos::SERIALIZE_METHODS
SERIALIZE_METHODS(FlatFilePos, obj)
Definition: flatfile.h:18
FlatFileSeq::m_dir
const fs::path m_dir
Definition: flatfile.h:51
FlatFilePos::FlatFilePos
FlatFilePos(int nFileIn, unsigned int nPosIn)
Definition: flatfile.h:25
prefix
const char * prefix
Definition: rest.cpp:772
FlatFileSeq
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:49
FlatFileSeq::FlatFileSeq
FlatFileSeq(fs::path dir, const char *prefix, size_t chunk_size)
Constructor.
Definition: flatfile.cpp:13
FlatFileSeq::Open
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:28
FlatFilePos
Definition: flatfile.h:14
VARINT
#define VARINT(obj)
Definition: serialize.h:596
FlatFilePos::operator!=
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:32
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
FlatFileSeq::FileName
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:24
FlatFilePos::FlatFilePos
FlatFilePos()
Definition: flatfile.h:23
FlatFileSeq::m_prefix
const char *const m_prefix
Definition: flatfile.h:52
serialize.h
FlatFilePos::nPos
unsigned int nPos
Definition: flatfile.h:16
FlatFileSeq::Flush
bool Flush(const FlatFilePos &pos, bool finalize=false)
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
Definition: flatfile.cpp:79