Files
mongo/db/database.h

137 lines
4.4 KiB
C
Raw Normal View History

// database.h
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
* 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.
2008-12-28 20:28:49 -05:00
*
* 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.
2008-12-28 20:28:49 -05:00
*
* 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/>.
*/
2008-12-05 16:45:10 -05:00
#pragma once
#include "cmdline.h"
2011-11-17 08:04:44 -05:00
#include "namespace.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
2011-11-17 08:04:44 -05:00
class Extent;
class MongoDataFile;
class ClientCursor;
struct ByLocKey;
typedef map<ByLocKey, ClientCursor*> CCByLoc;
/**
* Database represents a database database
* Each database database has its own set of files -- dbname.ns, dbname.0, dbname.1, ...
* NOT memory mapped
*/
class Database {
public:
static bool _openAllFiles;
2011-01-04 00:40:41 -05:00
Database(const char *nm, /*out*/ bool& newDb, const string& _path = dbpath);
2010-08-26 10:42:07 -04:00
private:
~Database();
public:
/* you must use this to close - there is essential code in this method that is not in the ~Database destructor.
thus the destructor is private. this could be cleaned up one day...
*/
static void closeDatabase( const char *db, const string& path );
2010-12-30 13:38:09 -05:00
void openAllFiles();
/**
* tries to make sure that this hasn't been deleted
*/
bool isOk() const { return magic == 781231; }
2011-01-04 00:40:41 -05:00
bool isEmpty() { return ! namespaceIndex.allocated(); }
2010-12-30 13:38:09 -05:00
/**
* total file size of Database in bytes
*/
long long fileSize() const;
2011-01-04 00:40:41 -05:00
2010-12-30 13:38:09 -05:00
int numFiles() const { return (int)files.size(); }
2011-01-04 00:40:41 -05:00
/**
* returns file valid for file number n
2010-12-30 13:38:09 -05:00
*/
boost::filesystem::path fileName( int n ) const;
2011-01-04 00:40:41 -05:00
2010-12-30 13:38:09 -05:00
bool exists(int n) const { return boost::filesystem::exists( fileName( n ) ); }
2011-01-04 00:40:41 -05:00
/**
* return file n. if it doesn't exist, create it
2010-12-30 13:38:09 -05:00
*/
MongoDataFile* getFile( int n, int sizeNeeded = 0, bool preallocateOnly = false );
2011-01-04 00:40:41 -05:00
2010-12-30 13:38:09 -05:00
MongoDataFile* addAFile( int sizeNeeded, bool preallocateNextFile );
2011-01-04 00:40:41 -05:00
2010-12-30 13:38:09 -05:00
/**
* makes sure we have an extra file at the end that is empty
* safe to call this multiple times - the implementation will only preallocate one file
*/
void preallocateAFile() { getFile( numFiles() , 0, true ); }
2011-05-23 20:01:42 -04:00
MongoDataFile* suitableFile( const char *ns, int sizeNeeded, bool preallocate, bool enforceQuota );
2011-05-23 20:01:42 -04:00
Extent* allocExtent( const char *ns, int size, bool capped, bool enforceQuota );
2011-01-04 00:40:41 -05:00
2010-12-30 13:38:09 -05:00
MongoDataFile* newestFile();
2011-01-04 00:40:41 -05:00
/**
* @return true if success. false if bad level or error creating profile ns
*/
bool setProfilingLevel( int newLevel , string& errmsg );
2010-12-30 13:38:09 -05:00
void flushFiles( bool sync ) const;
2011-01-04 00:40:41 -05:00
2010-09-22 14:55:57 -04:00
/**
* @return true if ns is part of the database
* ns=foo.bar, db=foo returns true
*/
bool ownsNS( const string& ns ) const {
if ( ! startsWith( ns , name ) )
return false;
return ns[name.size()] == '.';
}
2010-12-30 13:38:09 -05:00
/**
* @throws DatabaseDifferCaseCode if the name is a duplicate based on
* case insensitive matching.
*/
void checkDuplicateUncasedNames() const;
2010-12-30 13:38:09 -05:00
/**
* @return name of an existing database with same text name but different
* casing, if one exists. Otherwise the empty string is returned. If
* 'duplicates' is specified, it is filled with all duplicate names.
*/
static string duplicateUncasedName( const string &name, const string &path, set< string > *duplicates = 0 );
2010-12-30 13:38:09 -05:00
public: // this should be private later
2011-01-04 00:40:41 -05:00
vector<MongoDataFile*> files;
const string name; // "alleyinsider"
const string path;
NamespaceIndex namespaceIndex;
int profile; // 0=off.
const string profileName; // "alleyinsider.system.profile"
CCByLoc ccByLoc;
2011-01-04 00:40:41 -05:00
int magic; // used for making sure the object is still loaded in memory
2011-11-17 08:04:44 -05:00
2011-11-17 10:54:42 -05:00
RWLockBase dbLock; // see d_concurrency.h
};
2009-01-14 17:09:51 -05:00
} // namespace mongo