Storage Engine API
diskloc.h
Go to the documentation of this file.
1 
29 /* @file diskloc.h
30 
31  Storage subsystem management.
32  Lays out our datafiles on disk, manages disk space.
33 */
34 
35 #pragma once
36 
37 #include <boost/functional/hash.hpp>
38 #include <cstdint>
39 
40 #include "mongo/db/jsobj.h"
41 #include "mongo/db/record_id.h"
42 
43 namespace mongo {
44 
45 template <class Version>
47 
48 #pragma pack(1)
49 
53 class DiskLoc {
54  // this will be volume, file #, etc. but is a logical value could be anything depending on
55  // storage engine
56  int _a;
57  int ofs;
58 
59 public:
61  /* note NullOfs is different. todo clean up. see refs to NullOfs in code - use is valid but
62  * outside DiskLoc context so confusing as-is. */
63  NullOfs = -1,
64 
65  // Caps the number of files that may be allocated in a database, allowing about 32TB of
66  // data per db. Note that the DiskLoc and DiskLoc56Bit types supports more files than
67  // this value, as does the data storage format.
68  MaxFiles = 16000,
69 
70  // How invalid DiskLocs are represented in RecordIds.
71  InvalidRepr = -2LL,
72  };
73 
74  DiskLoc(int a, int Ofs) : _a(a), ofs(Ofs) {}
75  DiskLoc() {
76  Null();
77  }
78 
79  // Minimum allowed DiskLoc. No MmapV1RecordHeader may begin at this location because file and
80  // extent headers must precede Records in a file.
81  static DiskLoc min() {
82  return DiskLoc(0, 0);
83  }
84 
85  // Maximum allowed DiskLoc.
86  // No MmapV1RecordHeader may begin at this location because the minimum size of a
87  // MmapV1RecordHeader is larger than one byte. Also, the last bit is not able to be used
88  // because mmapv1 uses that for "used".
89  static DiskLoc max() {
90  return DiskLoc(0x7fffffff, 0x7ffffffe);
91  }
92 
93  bool questionable() const {
94  return ofs < -1 || _a < -1 || _a > 524288;
95  }
96 
97  bool isNull() const {
98  return _a == -1;
99  }
101  _a = -1;
102  /* note NullOfs is different. todo clean up. see refs to NullOfs in code - use is valid but
103  * outside DiskLoc context so confusing as-is. */
104  ofs = 0;
105  return *this;
106  }
107  void assertOk() const {
108  verify(!isNull());
109  }
111  _a = -2;
112  ofs = 0;
113  return *this;
114  }
115  bool isValid() const {
116  return _a != -2;
117  }
118 
119  std::string toString() const {
120  if (isNull())
121  return "null";
122  std::stringstream ss;
123  ss << _a << ':' << std::hex << ofs;
124  return ss.str();
125  }
126 
127  BSONObj toBSONObj() const {
128  return BSON("file" << _a << "offset" << ofs);
129  }
130 
131  int a() const {
132  return _a;
133  }
134 
135  int& GETOFS() {
136  return ofs;
137  }
138  int getOfs() const {
139  return ofs;
140  }
141  void set(int a, int b) {
142  _a = a;
143  ofs = b;
144  }
145 
146  void inc(int amt) {
147  verify(!isNull());
148  ofs += amt;
149  }
150 
151  bool sameFile(DiskLoc b) {
152  return _a == b._a;
153  }
154 
155  bool operator==(const DiskLoc& b) const {
156  return _a == b._a && ofs == b.ofs;
157  }
158  bool operator!=(const DiskLoc& b) const {
159  return !(*this == b);
160  }
161  int compare(const DiskLoc& b) const {
162  int x = _a - b._a;
163  if (x)
164  return x;
165  return ofs - b.ofs;
166  }
167 
168  static DiskLoc fromRecordId(RecordId id) {
169  if (id.isNormal())
170  return DiskLoc((id.repr() >> 32), uint32_t(id.repr()));
171 
172  if (id.isNull())
173  return DiskLoc();
174 
175  if (id == RecordId::max())
176  return DiskLoc::max();
177 
178  if (id == RecordId::min())
179  return DiskLoc::min();
180 
181  dassert(id.repr() == InvalidRepr);
182  return DiskLoc().setInvalid();
183  }
184 
185  RecordId toRecordId() const {
186  if (_a >= 0) {
187  if (*this == DiskLoc::min())
188  return RecordId::min();
189 
190  if (*this == DiskLoc::max())
191  return RecordId::max();
192 
193  return RecordId(uint64_t(_a) << 32 | uint32_t(ofs));
194  }
195 
196  if (isNull())
197  return RecordId();
198 
199  dassert(!isValid());
200  return RecordId(InvalidRepr);
201  }
202 };
203 #pragma pack()
204 
205 inline bool operator<(const DiskLoc& rhs, const DiskLoc& lhs) {
206  return rhs.compare(lhs) < 0;
207 }
208 inline bool operator<=(const DiskLoc& rhs, const DiskLoc& lhs) {
209  return rhs.compare(lhs) <= 0;
210 }
211 inline bool operator>(const DiskLoc& rhs, const DiskLoc& lhs) {
212  return rhs.compare(lhs) > 0;
213 }
214 inline bool operator>=(const DiskLoc& rhs, const DiskLoc& lhs) {
215  return rhs.compare(lhs) >= 0;
216 }
217 
218 inline std::ostream& operator<<(std::ostream& stream, const DiskLoc& loc) {
219  return stream << loc.toString();
220 }
221 
222 } // namespace mongo
bool operator<(const KeyString &lhs, const KeyString &rhs)
Definition: key_string.h:426
bool operator!=(const DiskLoc &b) const
Definition: diskloc.h:158
void inc(int amt)
Definition: diskloc.h:146
int _a
Definition: diskloc.h:56
Definition: diskloc.h:63
bool questionable() const
Definition: diskloc.h:93
void assertOk() const
Definition: diskloc.h:107
bool isNull() const
Definition: diskloc.h:97
DiskLoc & setInvalid()
Definition: diskloc.h:110
int compare(const DiskLoc &b) const
Definition: diskloc.h:161
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
Definition: diskloc.h:71
int & GETOFS()
Definition: diskloc.h:135
static DiskLoc max()
Definition: diskloc.h:89
represents a disk location/offset on disk in a database.
Definition: diskloc.h:53
RecordId toRecordId() const
Definition: diskloc.h:185
bool sameFile(DiskLoc b)
Definition: diskloc.h:151
bool operator>=(const KeyString &lhs, const KeyString &rhs)
Definition: key_string.h:442
int ofs
Definition: diskloc.h:57
static DiskLoc fromRecordId(RecordId id)
Definition: diskloc.h:168
static DiskLoc min()
Definition: diskloc.h:81
std::string toString() const
Definition: diskloc.h:119
std::ostream & operator<<(std::ostream &stream, const IndexKeyEntry &entry)
Definition: index_entry_comparison.cpp:37
bool operator==(const DiskLoc &b) const
Definition: diskloc.h:155
bool operator>(const KeyString &lhs, const KeyString &rhs)
Definition: key_string.h:438
bool isValid() const
Definition: diskloc.h:115
DiskLoc()
Definition: diskloc.h:75
DiskLoc & Null()
Definition: diskloc.h:100
bool operator<=(const KeyString &lhs, const KeyString &rhs)
Definition: key_string.h:430
DiskLoc(int a, int Ofs)
Definition: diskloc.h:74
Definition: diskloc.h:68
BSONObj toBSONObj() const
Definition: diskloc.h:127
SentinelValues
Definition: diskloc.h:60
int a() const
Definition: diskloc.h:131
Definition: diskloc.h:46
int getOfs() const
Definition: diskloc.h:138