2008-12-06 20:49:27 +01:00
|
|
|
// dbhelpers.h
|
|
|
|
|
2008-12-05 18:10:03 +01:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-12-05 18:10:03 +01: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-29 02:28:49 +01:00
|
|
|
*
|
2008-12-05 18:10:03 +01: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-29 02:28:49 +01:00
|
|
|
*
|
2008-12-05 18:10:03 +01: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-29 02:28:49 +01:00
|
|
|
/* db helpers are helper functions and classes that let us easily manipulate the local
|
2008-12-05 18:10:03 +01:00
|
|
|
database instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-12-04 15:46:11 +01:00
|
|
|
#include "../stdafx.h"
|
2009-12-03 23:24:22 +01:00
|
|
|
#include "client.h"
|
|
|
|
#include "db.h"
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-12-03 23:24:22 +01:00
|
|
|
class Cursor;
|
2009-12-21 23:43:06 +01:00
|
|
|
class CoveredIndexMatcher;
|
2009-12-03 23:24:22 +01:00
|
|
|
|
2009-12-03 23:11:24 +01:00
|
|
|
class CursorIterator {
|
|
|
|
public:
|
2009-12-04 07:25:50 +01:00
|
|
|
CursorIterator( auto_ptr<Cursor> c , BSONObj filter = BSONObj() );
|
|
|
|
BSONObj next();
|
|
|
|
bool hasNext();
|
2009-12-03 23:11:24 +01:00
|
|
|
|
|
|
|
private:
|
2009-12-04 07:25:50 +01:00
|
|
|
void _advance();
|
|
|
|
|
2009-12-03 23:11:24 +01:00
|
|
|
auto_ptr<Cursor> _cursor;
|
2009-12-21 23:43:06 +01:00
|
|
|
auto_ptr<CoveredIndexMatcher> _matcher;
|
2009-12-04 07:25:50 +01:00
|
|
|
BSONObj _o;
|
2009-12-03 23:11:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
all helpers assume locking is handled above them
|
|
|
|
*/
|
2009-01-18 23:48:44 +01:00
|
|
|
struct Helpers {
|
2008-12-05 18:10:03 +01:00
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
/* ensure the specified index exists.
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
@param keyPattern key pattern, e.g., { ts : 1 }
|
|
|
|
@param name index name, e.g., "name_1"
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
This method can be a little (not much) cpu-slow, so you may wish to use
|
|
|
|
OCCASIONALLY ensureIndex(...);
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
Note: use ensureHaveIdIndex() for the _id index: it is faster.
|
|
|
|
Note: does nothing if collection does not yet exist.
|
|
|
|
*/
|
2009-04-24 17:36:42 +02:00
|
|
|
static void ensureIndex(const char *ns, BSONObj keyPattern, bool unique, const char *name);
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-24 00:24:15 +01:00
|
|
|
/* fetch a single object from collection ns that matches query.
|
2009-10-09 20:59:44 +02:00
|
|
|
set your db SavedContext first.
|
2009-01-18 23:48:44 +01:00
|
|
|
|
2009-01-24 00:24:15 +01:00
|
|
|
@param requireIndex if true, complain if no index for the query. a way to guard against
|
|
|
|
writing a slow query.
|
2009-01-18 23:48:44 +01:00
|
|
|
|
2009-01-24 00:24:15 +01:00
|
|
|
@return true if object found
|
2009-01-18 23:48:44 +01:00
|
|
|
*/
|
2009-02-25 21:01:07 +01:00
|
|
|
static bool findOne(const char *ns, BSONObj query, BSONObj& result, bool requireIndex = false);
|
2009-11-25 16:09:59 +01:00
|
|
|
|
2010-01-14 19:07:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param foundIndex if passed in will be set to 1 if ns and index found
|
|
|
|
* @return true if object found
|
|
|
|
*/
|
|
|
|
static bool findById(Client&, const char *ns, BSONObj query, BSONObj& result ,
|
|
|
|
bool * nsFound = 0 , bool * indexFound = 0 );
|
2009-01-18 23:48:44 +01:00
|
|
|
|
2009-12-03 23:11:24 +01:00
|
|
|
static auto_ptr<CursorIterator> find( const char *ns , BSONObj query = BSONObj() , bool requireIndex = false );
|
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
/* Get/put the first object from a collection. Generally only useful if the collection
|
2009-01-21 23:26:16 +01:00
|
|
|
only ever has a single object -- which is a "singleton collection".
|
2009-01-18 23:48:44 +01:00
|
|
|
|
2009-01-21 23:26:16 +01:00
|
|
|
You do not need to set the database before calling.
|
|
|
|
|
|
|
|
Returns: true if object exists.
|
2009-01-18 23:48:44 +01:00
|
|
|
*/
|
|
|
|
static bool getSingleton(const char *ns, BSONObj& result);
|
|
|
|
static void putSingleton(const char *ns, BSONObj obj);
|
|
|
|
|
|
|
|
|
|
|
|
/* Remove all objects from a collection.
|
|
|
|
You do not need to set the database before calling.
|
|
|
|
*/
|
|
|
|
static void emptyCollection(const char *ns);
|
|
|
|
|
|
|
|
};
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-10-14 20:34:38 +02:00
|
|
|
class Database;
|
|
|
|
|
2009-05-28 21:22:24 +02:00
|
|
|
// manage a set using collection backed storage
|
2009-05-07 02:24:01 +02:00
|
|
|
class DbSet {
|
|
|
|
public:
|
2009-05-11 16:46:54 +02:00
|
|
|
DbSet( const string &name = "", const BSONObj &key = BSONObj() ) :
|
2009-12-04 07:25:50 +01:00
|
|
|
name_( name ),
|
|
|
|
key_( key.getOwned() ) {
|
2009-05-07 02:24:01 +02:00
|
|
|
}
|
2009-05-11 16:46:54 +02:00
|
|
|
~DbSet();
|
|
|
|
void reset( const string &name = "", const BSONObj &key = BSONObj() );
|
|
|
|
bool get( const BSONObj &obj ) const;
|
2009-05-07 02:24:01 +02:00
|
|
|
void set( const BSONObj &obj, bool val );
|
|
|
|
private:
|
|
|
|
string name_;
|
|
|
|
BSONObj key_;
|
|
|
|
};
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
} // namespace mongo
|