summaryrefslogtreecommitdiff
path: root/db/diskloc.h
blob: 2747abd9d82d23276e238c31b060475785578e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
*    Copyright (C) 2008 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* storage.h

   Storage subsystem management.
   Lays out our datafiles on disk, manages disk space.
*/

#pragma once

#include "jsobj.h"

namespace mongo {


    class Record;
    class DeletedRecord;
    class Extent;
    class BtreeBucket;
    class MongoDataFile;

#pragma pack(1)
    class DiskLoc {
        int fileNo; /* this will be volume, file #, etc. */
        int ofs;
    public:
        // Note: MaxFiles imposes a limit of about 32TB of data per process
        enum SentinelValues { MaxFiles=16000, NullOfs = -1 };

        int a() const {
            return fileNo;
        }

        DiskLoc(int a, int b) : fileNo(a), ofs(b) {
            //assert(ofs!=0);
        }
        DiskLoc() { Null(); }
        DiskLoc(const DiskLoc& l) {
            fileNo=l.fileNo;
            ofs=l.ofs;
        }

        bool questionable() {
            return ofs < -1 ||
                   fileNo < -1 ||
                   fileNo > 524288;
        }

        bool isNull() const {
            return fileNo == -1;
            //            return ofs == NullOfs;
        }
        void Null() {
            fileNo = -1;
            ofs = 0;
        }
        void assertOk() {
            assert(!isNull());
        }
        void setInvalid() {
            fileNo = -2; 
            ofs = 0;
        }
        bool isValid() const {
            return fileNo != -2;
        }

        string toString() const {
            if ( isNull() )
                return "null";
            stringstream ss;
            ss << hex << fileNo << ':' << ofs;
            return ss.str();
        }

        BSONObj toBSONObj() const {
            return BSON( "file" << fileNo << "offset" << ofs );
        }

        int& GETOFS() {
            return ofs;
        }
        int getOfs() const {
            return ofs;
        }
        void set(int a, int b) {
            fileNo=a;
            ofs=b;
        }
        void setOfs(int _fileNo, int _ofs) {
            fileNo = _fileNo;
            ofs = _ofs;
        }

        void inc(int amt) {
            assert( !isNull() );
            ofs += amt;
        }

        bool sameFile(DiskLoc b) {
            return fileNo == b.fileNo;
        }

        bool operator==(const DiskLoc& b) const {
            return fileNo==b.fileNo && ofs == b.ofs;
        }
        bool operator!=(const DiskLoc& b) const {
            return !(*this==b);
        }
        const DiskLoc& operator=(const DiskLoc& b) {
            fileNo=b.fileNo;
            ofs = b.ofs;
            //assert(ofs!=0);
            return *this;
        }
        int compare(const DiskLoc& b) const {
            int x = fileNo - b.fileNo;
            if ( x )
                return x;
            return ofs - b.ofs;
        }
        bool operator<(const DiskLoc& b) const {
            return compare(b) < 0;
        }

        /* get the "thing" associated with this disk location.
           it is assumed the object is what it is -- you must asure that:
           think of this as an unchecked type cast.
        */
        BSONObj obj() const;
        Record* rec() const;
        DeletedRecord* drec() const;
        Extent* ext() const;
        BtreeBucket* btree() const;
        BtreeBucket* btreemod() const; // marks modified / dirty

        MongoDataFile& pdf() const;
    };
#pragma pack()

    const DiskLoc minDiskLoc(0, 1);
    const DiskLoc maxDiskLoc(0x7fffffff, 0x7fffffff);

} // namespace mongo