2008-12-05 18:10:03 +01:00
|
|
|
// dbhelpers.cpp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2008-12-06 20:49:27 +01:00
|
|
|
#include "db.h"
|
|
|
|
#include "dbhelpers.h"
|
|
|
|
#include "query.h"
|
2009-01-18 23:48:44 +01:00
|
|
|
#include "json.h"
|
2009-02-25 21:01:07 +01:00
|
|
|
#include "queryoptimizer.h"
|
2009-11-25 16:09:59 +01:00
|
|
|
#include "btree.h"
|
2009-12-03 23:11:24 +01:00
|
|
|
#include "pdfile.h"
|
2008-12-06 20:49:27 +01:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-12-04 07:25:50 +01:00
|
|
|
CursorIterator::CursorIterator( auto_ptr<Cursor> c , BSONObj filter )
|
|
|
|
: _cursor( c ){
|
2009-12-06 07:08:19 +01:00
|
|
|
if ( ! filter.isEmpty() )
|
2009-12-21 23:43:06 +01:00
|
|
|
_matcher.reset( new CoveredIndexMatcher( filter , BSONObj() ) );
|
2009-12-04 07:25:50 +01:00
|
|
|
_advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
BSONObj CursorIterator::next(){
|
|
|
|
BSONObj o = _o;
|
|
|
|
_advance();
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorIterator::hasNext(){
|
|
|
|
return ! _o.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorIterator::_advance(){
|
|
|
|
if ( ! _cursor->ok() ){
|
|
|
|
_o = BSONObj();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( _cursor->ok() ){
|
|
|
|
_o = _cursor->current();
|
|
|
|
_cursor->advance();
|
2009-12-06 07:08:19 +01:00
|
|
|
if ( _matcher.get() == 0 || _matcher->matches( _o ) )
|
2009-12-04 07:25:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_o = BSONObj();
|
|
|
|
}
|
|
|
|
|
2009-04-24 17:36:42 +02:00
|
|
|
void Helpers::ensureIndex(const char *ns, BSONObj keyPattern, bool unique, const char *name) {
|
2009-01-18 23:48:44 +01:00
|
|
|
NamespaceDetails *d = nsdetails(ns);
|
|
|
|
if( d == 0 )
|
|
|
|
return;
|
|
|
|
|
2009-10-21 22:00:40 +02:00
|
|
|
{
|
|
|
|
NamespaceDetails::IndexIterator i = d->ii();
|
|
|
|
while( i.more() ) {
|
|
|
|
if( i.next().keyPattern().woCompare(keyPattern) == 0 )
|
|
|
|
return;
|
|
|
|
}
|
2009-01-18 23:48:44 +01:00
|
|
|
}
|
|
|
|
|
2009-10-21 22:00:40 +02:00
|
|
|
if( d->nIndexes >= NamespaceDetails::NIndexesMax ) {
|
2009-01-18 23:48:44 +01:00
|
|
|
problem() << "Helper::ensureIndex fails, MaxIndexes exceeded " << ns << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-14 20:34:38 +02:00
|
|
|
string system_indexes = cc().database()->name + ".system.indexes";
|
2009-01-18 23:48:44 +01:00
|
|
|
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append("name", name);
|
|
|
|
b.append("ns", ns);
|
|
|
|
b.append("key", keyPattern);
|
2009-04-24 17:36:42 +02:00
|
|
|
b.appendBool("unique", unique);
|
2009-01-18 23:48:44 +01:00
|
|
|
BSONObj o = b.done();
|
|
|
|
|
|
|
|
theDataFileMgr.insert(system_indexes.c_str(), o.objdata(), o.objsize());
|
|
|
|
}
|
|
|
|
|
2009-02-25 21:01:07 +01:00
|
|
|
class FindOne : public QueryOp {
|
|
|
|
public:
|
|
|
|
FindOne( bool requireIndex ) : requireIndex_( requireIndex ) {}
|
|
|
|
virtual void init() {
|
2009-04-25 00:10:12 +02:00
|
|
|
if ( requireIndex_ && strcmp( qp().indexKey().firstElement().fieldName(), "$natural" ) == 0 )
|
|
|
|
throw MsgAssertionException( "Not an index cursor" );
|
2009-02-25 21:31:31 +01:00
|
|
|
c_ = qp().newCursor();
|
2009-02-25 21:01:07 +01:00
|
|
|
if ( !c_->ok() )
|
|
|
|
setComplete();
|
|
|
|
else
|
2009-12-21 23:43:06 +01:00
|
|
|
matcher_.reset( new CoveredIndexMatcher( qp().query(), qp().indexKey() ) );
|
2009-02-25 21:01:07 +01:00
|
|
|
}
|
|
|
|
virtual void next() {
|
|
|
|
if ( !c_->ok() ) {
|
|
|
|
setComplete();
|
|
|
|
return;
|
|
|
|
}
|
2009-04-07 16:40:10 +02:00
|
|
|
if ( matcher_->matches( c_->currKey(), c_->currLoc() ) ) {
|
2009-03-02 23:12:02 +01:00
|
|
|
one_ = c_->current();
|
2009-02-25 21:01:07 +01:00
|
|
|
setComplete();
|
|
|
|
} else {
|
|
|
|
c_->advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual bool mayRecordPlan() const { return false; }
|
|
|
|
virtual QueryOp *clone() const { return new FindOne( requireIndex_ ); }
|
|
|
|
BSONObj one() const { return one_; }
|
|
|
|
private:
|
|
|
|
bool requireIndex_;
|
|
|
|
auto_ptr< Cursor > c_;
|
2009-12-21 23:43:06 +01:00
|
|
|
auto_ptr< CoveredIndexMatcher > matcher_;
|
2009-02-25 21:01:07 +01:00
|
|
|
BSONObj one_;
|
|
|
|
};
|
|
|
|
|
2009-01-18 23:48:44 +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
|
|
|
*/
|
|
|
|
bool Helpers::findOne(const char *ns, BSONObj query, BSONObj& result, bool requireIndex) {
|
2009-04-25 00:10:12 +02:00
|
|
|
QueryPlanSet s( ns, query, BSONObj(), 0, !requireIndex );
|
2009-02-25 21:01:07 +01:00
|
|
|
FindOne original( requireIndex );
|
|
|
|
shared_ptr< FindOne > res = s.runOp( original );
|
2009-04-25 00:10:12 +02:00
|
|
|
massert( res->exceptionMessage(), res->complete() );
|
2009-02-25 21:01:07 +01:00
|
|
|
if ( res->one().isEmpty() )
|
2009-01-27 21:35:28 +01:00
|
|
|
return false;
|
2009-02-25 21:01:07 +01:00
|
|
|
result = res->one();
|
|
|
|
return true;
|
2009-01-18 23:48:44 +01:00
|
|
|
}
|
|
|
|
|
2009-12-03 23:11:24 +01:00
|
|
|
auto_ptr<CursorIterator> Helpers::find( const char *ns , BSONObj query , bool requireIndex ){
|
|
|
|
uassert( "requireIndex not supported in Helpers::find yet" , ! requireIndex );
|
|
|
|
auto_ptr<CursorIterator> i;
|
2009-12-04 07:25:50 +01:00
|
|
|
i.reset( new CursorIterator( DataFileMgr::findAll( ns ) , query ) );
|
2009-12-03 23:11:24 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 16:09:59 +01:00
|
|
|
bool Helpers::findById(const char *ns, BSONObj query, BSONObj& result ){
|
|
|
|
NamespaceDetails *d = nsdetails(ns);
|
|
|
|
if ( ! d )
|
|
|
|
return false;
|
|
|
|
int idxNo = d->findIdIndex();
|
|
|
|
if ( idxNo < 0 )
|
|
|
|
return false;
|
|
|
|
IndexDetails& i = d->idx( idxNo );
|
|
|
|
|
|
|
|
BSONObj key = i.getKeyFromQuery( query );
|
|
|
|
|
|
|
|
DiskLoc loc = i.head.btree()->findSingle( i , i.head , key );
|
|
|
|
if ( loc.isNull() )
|
|
|
|
return false;
|
|
|
|
result = loc.obj();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* Get the first object from a collection. Generally only useful if the collection
|
|
|
|
only ever has a single object -- which is a "singleton collection.
|
|
|
|
|
|
|
|
Returns: true if object exists.
|
|
|
|
*/
|
2009-01-18 23:48:44 +01:00
|
|
|
bool Helpers::getSingleton(const char *ns, BSONObj& result) {
|
2009-01-15 16:17:11 +01:00
|
|
|
DBContext context(ns);
|
|
|
|
|
|
|
|
auto_ptr<Cursor> c = DataFileMgr::findAll(ns);
|
|
|
|
if ( !c->ok() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
result = c->current();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
void Helpers::putSingleton(const char *ns, BSONObj obj) {
|
2009-01-15 16:17:11 +01:00
|
|
|
DBContext context(ns);
|
|
|
|
stringstream ss;
|
2009-10-21 21:18:21 +02:00
|
|
|
updateObjects(ns, obj, /*pattern=*/BSONObj(), /*upsert=*/true, /*multi=*/false, ss, /*logop=*/true );
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:44 +01:00
|
|
|
void Helpers::emptyCollection(const char *ns) {
|
2009-01-15 16:17:11 +01:00
|
|
|
DBContext context(ns);
|
2009-03-19 21:23:04 +01:00
|
|
|
deleteObjects(ns, BSONObj(), false);
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2009-01-14 23:09:51 +01:00
|
|
|
|
2009-05-11 16:46:54 +02:00
|
|
|
DbSet::~DbSet() {
|
|
|
|
if ( name_.empty() )
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
DBContext c( name_.c_str() );
|
|
|
|
if ( nsdetails( name_.c_str() ) ) {
|
|
|
|
string errmsg;
|
|
|
|
BSONObjBuilder result;
|
|
|
|
dropCollection( name_, errmsg, result );
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
problem() << "exception cleaning up DbSet" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbSet::reset( const string &name, const BSONObj &key ) {
|
|
|
|
if ( !name.empty() )
|
|
|
|
name_ = name;
|
|
|
|
if ( !key.isEmpty() )
|
|
|
|
key_ = key.getOwned();
|
2009-05-07 02:24:01 +02:00
|
|
|
DBContext c( name_.c_str() );
|
|
|
|
if ( nsdetails( name_.c_str() ) ) {
|
|
|
|
Helpers::emptyCollection( name_.c_str() );
|
|
|
|
} else {
|
|
|
|
string err;
|
2009-05-11 16:46:54 +02:00
|
|
|
massert( err, userCreateNS( name_.c_str(), fromjson( "{autoIndexId:false}" ), err, false ) );
|
2009-05-07 02:24:01 +02:00
|
|
|
}
|
|
|
|
Helpers::ensureIndex( name_.c_str(), key_, true, "setIdx" );
|
|
|
|
}
|
|
|
|
|
2009-05-11 16:46:54 +02:00
|
|
|
bool DbSet::get( const BSONObj &obj ) const {
|
2009-05-07 02:24:01 +02:00
|
|
|
DBContext c( name_.c_str() );
|
|
|
|
BSONObj temp;
|
|
|
|
return Helpers::findOne( name_.c_str(), obj, temp, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbSet::set( const BSONObj &obj, bool val ) {
|
|
|
|
DBContext c( name_.c_str() );
|
|
|
|
if ( val ) {
|
|
|
|
try {
|
|
|
|
BSONObj k = obj;
|
2009-08-13 19:26:03 +02:00
|
|
|
theDataFileMgr.insert( name_.c_str(), k, false );
|
2009-05-07 02:24:01 +02:00
|
|
|
} catch ( DBException& ) {
|
|
|
|
// dup key - already in set
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deleteObjects( name_.c_str(), obj, true, false, false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
} // namespace mongo
|