Bitcoin ABC  0.22.13
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 
19 
20  template <typename Stream, typename Operation>
21  inline void SerializationOp(Stream &s, Operation ser_action) {
23  READWRITE(VARINT(nPos));
24  }
25 
26  FlatFilePos() : nFile(-1), nPos(0) {}
27 
28  FlatFilePos(int nFileIn, unsigned int nPosIn)
29  : nFile(nFileIn), nPos(nPosIn) {}
30 
31  friend bool operator==(const FlatFilePos &a, const FlatFilePos &b) {
32  return (a.nFile == b.nFile && a.nPos == b.nPos);
33  }
34 
35  friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b) {
36  return !(a == b);
37  }
38 
39  void SetNull() {
40  nFile = -1;
41  nPos = 0;
42  }
43  bool IsNull() const { return (nFile == -1); }
44 
45  std::string ToString() const;
46 };
47 
52 class FlatFileSeq {
53 private:
54  const fs::path m_dir;
55  const char *const m_prefix;
56  const size_t m_chunk_size;
57 
58 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 
86  size_t Allocate(const FlatFilePos &pos, size_t add_size,
87  bool &out_of_space);
88 
97  bool Flush(const FlatFilePos &pos, bool finalize = false);
98 };
99 
100 #endif // BITCOIN_FLATFILE_H
#define VARINT(obj)
Definition: serialize.h:608
const fs::path m_dir
Definition: flatfile.h:54
void SerializationOp(Stream &s, Operation ser_action)
Definition: flatfile.h:21
bool IsNull() const
Definition: flatfile.h:43
const char * prefix
Definition: rest.cpp:754
int nFile
Definition: flatfile.h:15
ADD_SERIALIZE_METHODS
Definition: flatfile.h:18
#define VARINT_MODE(obj, mode)
Definition: serialize.h:607
const char *const m_prefix
Definition: flatfile.h:55
void SetNull()
Definition: flatfile.h:39
FlatFilePos(int nFileIn, unsigned int nPosIn)
Definition: flatfile.h:28
friend bool operator!=(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:35
std::string ToString() const
Definition: flatfile.cpp:20
FlatFilePos()
Definition: flatfile.h:26
friend bool operator==(const FlatFilePos &a, const FlatFilePos &b)
Definition: flatfile.h:31
const size_t m_chunk_size
Definition: flatfile.h:56
#define READWRITE(...)
Definition: serialize.h:191
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:52
unsigned int nPos
Definition: flatfile.h:16