Bitcoin Core  0.19.99
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 <string>
10 
11 #include <fs.h>
12 #include <serialize.h>
13 
15 {
16  int nFile;
17  unsigned int nPos;
18 
20 
21  template <typename Stream, typename Operation>
22  inline void SerializationOp(Stream& s, Operation ser_action) {
24  READWRITE(VARINT(nPos));
25  }
26 
27  FlatFilePos() : nFile(-1), nPos(0) {}
28 
29  FlatFilePos(int nFileIn, unsigned int nPosIn) :
30  nFile(nFileIn),
31  nPos(nPosIn)
32  {}
33 
34  friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
35  return (a.nFile == b.nFile && a.nPos == b.nPos);
36  }
37 
38  friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
39  return !(a == b);
40  }
41 
42  void SetNull() { nFile = -1; nPos = 0; }
43  bool IsNull() const { return (nFile == -1); }
44 
45  std::string ToString() const;
46 };
47 
53 {
54 private:
55  const fs::path m_dir;
56  const char* const m_prefix;
57  const size_t m_chunk_size;
58 
59 public:
67  FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
68 
70  fs::path FileName(const FlatFilePos& pos) const;
71 
73  FILE* Open(const FlatFilePos& pos, bool read_only = false);
74 
84  size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space);
85 
93  bool Flush(const FlatFilePos& pos, bool finalize = false);
94 };
95 
96 #endif // BITCOIN_FLATFILE_H
#define VARINT(obj)
Definition: serialize.h:497
const fs::path m_dir
Definition: flatfile.h:55
void SerializationOp(Stream &s, Operation ser_action)
Definition: flatfile.h:22
bool IsNull() const
Definition: flatfile.h:43
const char * prefix
Definition: rest.cpp:650
int nFile
Definition: flatfile.h:16
ADD_SERIALIZE_METHODS
Definition: flatfile.h:19
#define VARINT_MODE(obj, mode)
Definition: serialize.h:496
const char *const m_prefix
Definition: flatfile.h:56
void SetNull()
Definition: flatfile.h:42
FlatFilePos(int nFileIn, unsigned int nPosIn)
Definition: flatfile.h:29
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:38
std::string ToString() const
Definition: flatfile.cpp:23
FlatFilePos()
Definition: flatfile.h:27
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:34
const size_t m_chunk_size
Definition: flatfile.h:57
#define READWRITE(...)
Definition: serialize.h:186
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:52
unsigned int nPos
Definition: flatfile.h:17