Files
mongo/db/clientcursor.h

163 lines
5.3 KiB
C
Raw Normal View History

2009-03-24 14:19:03 -04:00
/* clientcursor.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/>.
*/
2009-03-24 14:19:03 -04:00
/* Cursor -- and its derived classes -- are our internal cursors.
2008-06-06 14:59:58 -04:00
2008-12-28 20:28:49 -05:00
ClientCursor is a wrapper that represents a cursorid from our database
2008-06-06 14:59:58 -04:00
application's perspective.
*/
#pragma once
#include "../stdafx.h"
2009-05-11 10:44:27 -04:00
#include "cursor.h"
#include "jsobj.h"
#include "../util/message.h"
#include "storage.h"
#include "dbhelpers.h"
2009-05-11 15:09:30 -04:00
#include "matcher.h"
2008-06-06 14:59:58 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-03-24 14:19:03 -04:00
typedef long long CursorId; /* passed to the client so it can send back on getMore */
class Cursor; /* internal server cursor base class */
class ClientCursor;
typedef map<CursorId, ClientCursor*> CCById;
extern CCById clientCursorsById;
2008-06-06 14:59:58 -04:00
2009-05-11 10:44:27 -04:00
extern BSONObj id_obj;
2009-05-28 15:22:24 -04:00
// utility class for de duping ids
class IdSet {
public:
2009-05-11 10:44:27 -04:00
IdSet() : mySize_(), inMem_( true ) {}
~IdSet() {
2009-05-11 10:44:27 -04:00
size_ -= mySize_;
}
2009-05-06 20:24:01 -04:00
bool get( const BSONObj &id ) const {
2009-05-11 10:44:27 -04:00
if ( inMem_ )
return mem_.count( id );
else
return db_.get( id );
}
void put( const BSONObj &id ) {
2009-05-11 10:44:27 -04:00
if ( inMem_ ) {
if ( mem_.insert( id.getOwned() ).second ) {
int sizeInc = id.objsize() + sizeof( BSONObj );
mySize_ += sizeInc;
size_ += sizeInc;
}
} else {
db_.set( id, true );
2009-05-06 20:24:01 -04:00
}
}
void mayUpgradeStorage( const string &name ) {
2009-05-11 10:44:27 -04:00
if ( size_ > maxSize_ || mySize_ > maxSizePerSet() ) {
if ( !inMem_ )
return;
log() << "upgrading id set to collection backed storage, cursorid: " << name << endl;
inMem_ = false;
db_.reset( "local.temp.clientcursor." + name, id_obj );
for( BSONObjSetDefaultOrder::const_iterator i = mem_.begin(); i != mem_.end(); ++i )
db_.set( *i, true );
mem_.clear();
size_ -= mySize_;
mySize_ = 0;
2009-05-06 20:24:01 -04:00
}
}
2009-05-11 10:44:27 -04:00
bool inMem() const { return inMem_; }
long long mySize() { return mySize_; }
static long long aggregateSize() { return size_; }
static long long maxSize_;
private:
string name_;
BSONObjSetDefaultOrder mem_;
2009-05-11 10:44:27 -04:00
DbSet db_;
2009-05-06 20:24:01 -04:00
long long mySize_;
static long long size_;
2009-05-11 10:44:27 -04:00
long long maxSizePerSet() const { return maxSize_ / 2; }
bool inMem_;
};
class ClientCursor {
DiskLoc _lastLoc; // use getter and setter not this.
static CursorId allocCursorId();
public:
2009-03-27 16:27:35 -04:00
ClientCursor() : cursorid( allocCursorId() ), pos(0), idleAgeMillis(0) {
clientCursorsById.insert( make_pair(cursorid, this) );
}
~ClientCursor();
const CursorId cursorid;
string ns;
2009-04-07 10:40:10 -04:00
auto_ptr<KeyValJSMatcher> matcher;
auto_ptr<Cursor> c;
auto_ptr<IdSet> ids_;
2009-03-24 14:19:03 -04:00
int pos; /* # objects into the cursor so far */
DiskLoc lastLoc() const {
return _lastLoc;
}
void setLastLoc(DiskLoc);
auto_ptr< set<string> > filter; // which fields query wants returned
2009-03-27 16:27:35 -04:00
Message originalMessage; // this is effectively an auto ptr for data the matcher points to
unsigned idleAgeMillis; // how long has the cursor been around, relative to server idle time
2008-06-06 14:59:58 -04:00
/* Get rid of cursors for namespaces that begin with nsprefix.
Used by drop, deleteIndexes, dropDatabase.
*/
static void invalidate(const char *nsPrefix);
2008-07-24 16:07:18 -04:00
static bool erase(CursorId id) {
ClientCursor *cc = find(id);
if ( cc ) {
delete cc;
return true;
}
return false;
2008-12-28 20:28:49 -05:00
}
2008-06-06 14:59:58 -04:00
static ClientCursor* find(CursorId id, bool warn = true) {
CCById::iterator it = clientCursorsById.find(id);
if ( it == clientCursorsById.end() ) {
if ( warn )
OCCASIONALLY out() << "ClientCursor::find(): cursor not found in map " << id << " (ok after a drop)\n";
return 0;
}
return it->second;
2008-12-28 20:28:49 -05:00
}
2008-06-06 14:59:58 -04:00
/* call when cursor's location changes so that we can update the
cursorsbylocation map. if you are locked and internally iterating, only
need to call when you are ready to "unlock".
*/
void updateLocation();
2008-06-06 14:59:58 -04:00
void cleanupByLocation(DiskLoc loc);
2009-05-11 10:44:27 -04:00
void mayUpgradeStorage() {
if ( !ids_.get() )
return;
2009-05-11 10:44:27 -04:00
stringstream ss;
ss << ns << "." << cursorid;
2009-05-11 10:44:27 -04:00
ids_->mayUpgradeStorage( ss.str() );
}
};
2009-01-14 17:09:51 -05:00
} // namespace mongo