Storage Engine API
dur_journalformat.h
Go to the documentation of this file.
1 // @file dur_journalformat.h The format of our journal files.
2 
31 #pragma once
32 
33 #include <sstream>
34 #include <string>
35 
36 #include "mongo/util/assert_util.h"
37 
38 namespace mongo {
39 
40 namespace dur {
41 
42 const unsigned Alignment = 8192;
43 
44 #pragma pack(1)
45 
48 struct JHeader {
49  JHeader() {}
50  JHeader(std::string fname);
51 
52  // "j\n". j means journal, then a linefeed, fwiw if you were to run "less" on the file or
53  // something...
54  char magic[2];
55 
56 // x4142 is asci--readable if you look at the file with head/less -- thus the starting values were
57 // near that. simply incrementing the version # is safe on a fwd basis.
58 #if defined(_NOCOMPRESS)
59  enum { CurrentVersion = 0x4148 };
60 #else
61  enum { CurrentVersion = 0x4149 };
62 #endif
63  unsigned short _version;
64 
65  // these are just for diagnostic ease (make header more useful as plain text)
66  char n1; // '\n'
67  char ts[20]; // ascii timestamp of file generation. for user reading, not used by code.
68  char n2; // '\n'
69  char dbpath[128]; // path/filename of this file for human reading and diagnostics. not used
70  // by code.
71  char n3, n4; // '\n', '\n'
72 
73  unsigned long long fileId; // unique identifier that will be in each JSectHeader.
74  // important as we recycle prealloced files
75 
76  char reserved3[8026]; // 8KB total for the file header
77  char txt2[2]; // "\n\n" at the end
78 
79  bool versionOk() const {
80  return _version == CurrentVersion;
81  }
82  bool valid() const {
83  return magic[0] == 'j' && txt2[1] == '\n' && fileId;
84  }
85 };
86 
91 struct JSectHeader {
92 private:
93  unsigned _sectionLen; // unpadded length in bytes of the whole section
94 public:
95  unsigned long long
96  seqNumber; // sequence number that can be used on recovery to not do too much work
97  unsigned long long fileId; // matches JHeader::fileId
98  unsigned sectionLen() const {
99  return _sectionLen;
100  }
101 
102  // we store the unpadded length so we can use that when we uncompress. to
103  // get the true total size this must be rounded up to the Alignment.
104  void setSectionLen(unsigned lenUnpadded) {
105  _sectionLen = lenUnpadded;
106  }
107 
108  unsigned sectionLenWithPadding() const {
109  unsigned x = (sectionLen() + (Alignment - 1)) & (~(Alignment - 1));
110  dassert(x % Alignment == 0);
111  return x;
112  }
113 };
114 
119 struct JEntry {
120  enum OpCodes {
121  OpCode_Footer = 0xffffffff,
122  OpCode_DbContext = 0xfffffffe,
123  OpCode_FileCreated = 0xfffffffd,
124  OpCode_DropDb = 0xfffffffc,
125  OpCode_Min = 0xfffff000
126  };
127  union {
128  unsigned
129  len; // length in bytes of the data of the JEntry. does not include the JEntry header
131  };
132 
133  unsigned ofs; // offset in file
134 
135  // sentinel and masks for _fileNo
136  enum {
137  DotNsSuffix = 0x7fffffff, // ".ns" file
138  LocalDbBit = 0x80000000 // assuming "local" db instead of using the JDbContext
139  };
140  int _fileNo; // high bit is set to indicate it should be the <dbpath>/local database
141  // char data[len] follows
142 
143  const char* srcData() const {
144  const int* i = &_fileNo;
145  return (const char*)(i + 1);
146  }
147 
148  int getFileNo() const {
149  return _fileNo & (~LocalDbBit);
150  }
151  void setFileNo(int f) {
152  _fileNo = f;
153  }
154  bool isNsSuffix() const {
155  return getFileNo() == DotNsSuffix;
156  }
157 
159  _fileNo |= LocalDbBit;
160  }
161  bool isLocalDbContext() const {
162  return _fileNo & LocalDbBit;
163  }
165  _fileNo = getFileNo();
166  }
167 
168  static std::string suffix(int fileno) {
169  if (fileno == DotNsSuffix)
170  return "ns";
171  std::stringstream ss;
172  ss << fileno;
173  return ss.str();
174  }
175 };
176 
178 struct JSectFooter {
179  JSectFooter();
180  JSectFooter(const void* begin, int len); // needs buffer to compute hash
181  unsigned sentinel;
182  unsigned char hash[16];
183  unsigned long long reserved;
184  char magic[4]; // "\n\n\n\n"
185 
191  bool checkHash(const void* begin, int len) const;
192 
193  bool magicOk() const {
194  return *((unsigned*)magic) == 0x0a0a0a0a;
195  }
196 };
197 
199 struct JDbContext {
200  JDbContext() : sentinel(JEntry::OpCode_DbContext) {}
201  const unsigned sentinel; // compare to JEntry::len -- zero is our sentinel
202  // char dbname[];
203 };
204 
206 struct LSNFile {
207  unsigned ver;
208  unsigned reserved2;
209  unsigned long long lsn;
210  unsigned long long checkbytes;
211  unsigned long long reserved[8];
212 
213  void set(unsigned long long lsn);
214  unsigned long long get();
215 };
216 
217 #pragma pack()
218 }
219 }
unsigned len
Definition: dur_journalformat.h:129
unsigned sectionLenWithPadding() const
Definition: dur_journalformat.h:108
void setSectionLen(unsigned lenUnpadded)
Definition: dur_journalformat.h:104
an individual write operation within a group commit section.
Definition: dur_journalformat.h:119
unsigned reserved2
Definition: dur_journalformat.h:208
unsigned long long fileId
Definition: dur_journalformat.h:97
unsigned long long fileId
Definition: dur_journalformat.h:73
char n1
Definition: dur_journalformat.h:66
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
OpCodes opcode
Definition: dur_journalformat.h:130
char txt2[2]
Definition: dur_journalformat.h:77
bool valid() const
Definition: dur_journalformat.h:82
unsigned ver
Definition: dur_journalformat.h:207
bool isNsSuffix() const
Definition: dur_journalformat.h:154
unsigned long long lsn
Definition: dur_journalformat.h:209
void clearLocalDbContextBit()
Definition: dur_journalformat.h:164
void setLocalDbContextBit()
Definition: dur_journalformat.h:158
unsigned sectionLen() const
Definition: dur_journalformat.h:98
JHeader()
Definition: dur_journalformat.h:49
"Section" header.
Definition: dur_journalformat.h:91
char n2
Definition: dur_journalformat.h:68
char n3
Definition: dur_journalformat.h:71
const unsigned sentinel
Definition: dur_journalformat.h:201
OpCodes
Definition: dur_journalformat.h:120
declares "the next entry(s) are for this database / file path prefix"
Definition: dur_journalformat.h:199
"last sequence number"
Definition: dur_journalformat.h:206
char reserved3[8026]
Definition: dur_journalformat.h:76
Definition: dur_journalformat.h:61
static std::string suffix(int fileno)
Definition: dur_journalformat.h:168
const unsigned Alignment
Definition: dur_journalformat.h:42
unsigned long long reserved
Definition: dur_journalformat.h:183
unsigned long long seqNumber
Definition: dur_journalformat.h:96
const char * srcData() const
Definition: dur_journalformat.h:143
bool magicOk() const
Definition: dur_journalformat.h:193
char magic[2]
Definition: dur_journalformat.h:54
char dbpath[128]
Definition: dur_journalformat.h:69
unsigned long long checkbytes
Definition: dur_journalformat.h:210
unsigned _sectionLen
Definition: dur_journalformat.h:93
char ts[20]
Definition: dur_journalformat.h:67
group commit section footer.
Definition: dur_journalformat.h:178
unsigned ofs
Definition: dur_journalformat.h:133
unsigned sentinel
Definition: dur_journalformat.h:181
beginning header for a journal/j._<n> file there is nothing important int this header at this time...
Definition: dur_journalformat.h:48
bool versionOk() const
Definition: dur_journalformat.h:79
int _fileNo
Definition: dur_journalformat.h:140
unsigned short _version
Definition: dur_journalformat.h:63
bool isLocalDbContext() const
Definition: dur_journalformat.h:161
void setFileNo(int f)
Definition: dur_journalformat.h:151
int getFileNo() const
Definition: dur_journalformat.h:148
JDbContext()
Definition: dur_journalformat.h:200
char n4
Definition: dur_journalformat.h:71