00001 // cursors.h 00002 /* 00003 * Copyright (C) 2010 10gen Inc. 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU Affero General Public License, version 3, 00007 * as published by the Free Software Foundation. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Affero General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Affero General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 00019 #pragma once 00020 00021 #include "../pch.h" 00022 00023 #include "../db/jsobj.h" 00024 #include "../db/dbmessage.h" 00025 #include "../client/dbclient.h" 00026 #include "../client/parallel.h" 00027 00028 #include "request.h" 00029 00030 namespace mongo { 00031 00032 class ShardedClientCursor : boost::noncopyable { 00033 public: 00034 ShardedClientCursor( QueryMessage& q , ClusteredCursor * cursor ); 00035 virtual ~ShardedClientCursor(); 00036 00037 long long getId(); 00038 00042 bool sendNextBatch( Request& r ) { return sendNextBatch( r , _ntoreturn ); } 00043 bool sendNextBatch( Request& r , int ntoreturn ); 00044 00045 void accessed(); 00047 long long idleTime( long long now ); 00048 00049 protected: 00050 00051 ClusteredCursor * _cursor; 00052 00053 int _skip; 00054 int _ntoreturn; 00055 00056 int _totalSent; 00057 bool _done; 00058 00059 long long _id; 00060 long long _lastAccessMillis; // 0 means no timeout 00061 00062 }; 00063 00064 typedef boost::shared_ptr<ShardedClientCursor> ShardedClientCursorPtr; 00065 00066 class CursorCache { 00067 public: 00068 00069 static long long TIMEOUT; 00070 00071 typedef map<long long,ShardedClientCursorPtr> MapSharded; 00072 typedef map<long long,string> MapNormal; 00073 00074 CursorCache(); 00075 ~CursorCache(); 00076 00077 ShardedClientCursorPtr get( long long id ) const; 00078 void store( ShardedClientCursorPtr cursor ); 00079 void remove( long long id ); 00080 00081 void storeRef( const string& server , long long id ); 00082 00084 string getRef( long long id ) const ; 00085 00086 void gotKillCursors(Message& m ); 00087 00088 void appendInfo( BSONObjBuilder& result ) const ; 00089 00090 long long genId(); 00091 00092 void doTimeouts(); 00093 void startTimeoutThread(); 00094 private: 00095 mutable mongo::mutex _mutex; 00096 00097 MapSharded _cursors; 00098 MapNormal _refs; 00099 00100 long long _shardedTotal; 00101 00102 static int _myLogLevel; 00103 }; 00104 00105 extern CursorCache cursorCache; 00106 }