MongoDB  2.7.0
syncclusterconnection.h
1 // @file syncclusterconnection.h
2 
3 /*
4  * Copyright 2010 10gen Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 
22 #include "mongo/bson/bsonelement.h"
23 #include "mongo/bson/bsonobj.h"
25 #include "mongo/client/export_macros.h"
26 
27 namespace mongo {
28 
43  class MONGO_CLIENT_API SyncClusterConnection : public DBClientBase {
44  public:
45 
46  using DBClientBase::query;
49 
53  SyncClusterConnection( const list<HostAndPort> &, double socketTimeout = 0);
54  SyncClusterConnection( string commaSeparated, double socketTimeout = 0);
55  SyncClusterConnection( const std::string& a,
56  const std::string& b,
57  const std::string& c,
58  double socketTimeout = 0 );
60 
64  bool prepare( string& errmsg );
65 
69  bool fsync( string& errmsg );
70 
71  // --- from DBClientInterface
72 
73  virtual BSONObj findOne(const string &ns, const Query& query, const BSONObj *fieldsToReturn, int queryOptions);
74 
75  virtual auto_ptr<DBClientCursor> query(const string &ns, Query query, int nToReturn, int nToSkip,
76  const BSONObj *fieldsToReturn, int queryOptions, int batchSize );
77 
78  virtual auto_ptr<DBClientCursor> getMore( const string &ns, long long cursorId, int nToReturn, int options );
79 
80  virtual void insert( const string &ns, BSONObj obj, int flags=0);
81 
82  virtual void insert( const string &ns, const vector< BSONObj >& v, int flags=0);
83 
84  virtual void remove( const string &ns , Query query, int flags );
85 
86  virtual void update( const string &ns , Query query , BSONObj obj , int flags );
87 
88  virtual bool call( Message &toSend, Message &response, bool assertOk , string * actualServer );
89  virtual void say( Message &toSend, bool isRetry = false , string * actualServer = 0 );
90  virtual void sayPiggyBack( Message &toSend );
91 
92  virtual void killCursor( long long cursorID );
93 
94  virtual string getServerAddress() const { return _address; }
95  virtual bool isFailed() const { return false; }
96  virtual bool isStillConnected();
97  virtual string toString() const { return _toString(); }
98 
99  virtual BSONObj getLastErrorDetailed(const std::string& db,
100  bool fsync=false,
101  bool j=false,
102  int w=0,
103  int wtimeout=0);
104  virtual BSONObj getLastErrorDetailed(bool fsync=false, bool j=false, int w=0, int wtimeout=0);
105 
106  virtual bool callRead( Message& toSend , Message& response );
107 
108  virtual ConnectionString::ConnectionType type() const { return ConnectionString::SYNC; }
109 
110  void setAllSoTimeouts( double socketTimeout );
111  double getSoTimeout() const { return _socketTimeout; }
112 
113 
114  virtual bool lazySupported() const { return false; }
115 
116  virtual void setRunCommandHook(DBClientWithCommands::RunCommandHookFunc func);
117  virtual void setPostRunCommandHook(DBClientWithCommands::PostRunCommandHookFunc func);
118 
119  protected:
120  virtual void _auth(const BSONObj& params);
121 
122  private:
123  SyncClusterConnection( SyncClusterConnection& prev, double socketTimeout = 0 );
124  string _toString() const;
125  bool _commandOnActive(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0);
126  auto_ptr<DBClientCursor> _queryOnActive(const string &ns, Query query, int nToReturn, int nToSkip,
127  const BSONObj *fieldsToReturn, int queryOptions, int batchSize );
128  int _lockType( const string& name );
129  void _checkLast();
130  void _connect( const std::string& host );
131 
132  string _address;
133  vector<string> _connAddresses;
134  vector<DBClientConnection*> _conns;
135  map<string,int> _lockTypes;
136  mongo::mutex _mutex;
137 
138  vector<BSONObj> _lastErrors;
139 
140  double _socketTimeout;
141  };
142 
143  class MONGO_CLIENT_API UpdateNotTheSame : public UserException {
144  public:
145  UpdateNotTheSame( int code , const string& msg , const vector<string>& addrs , const vector<BSONObj>& lastErrors )
146  : UserException( code , msg ) , _addrs( addrs ) , _lastErrors( lastErrors ) {
147  verify( _addrs.size() == _lastErrors.size() );
148  }
149 
150  virtual ~UpdateNotTheSame() throw() {
151  }
152 
153  unsigned size() const {
154  return _addrs.size();
155  }
156 
157  pair<string,BSONObj> operator[](unsigned i) const {
158  return make_pair( _addrs[i] , _lastErrors[i] );
159  }
160 
161  private:
162 
163  vector<string> _addrs;
164  vector<BSONObj> _lastErrors;
165  };
166 
167 };
Definition: assert_util.h:149
virtual void update(const string &ns, Query query, BSONObj obj, bool upsert=false, bool multi=false)
updates objects matching query
Definition: dbclient.cpp:1200
Definition: syncclusterconnection.h:143
boost::function< void(BSONObjBuilder *)> RunCommandHookFunc
A function type for runCommand hooking; the function takes a pointer to a BSONObjBuilder and returns ...
Definition: dbclientinterface.h:987
abstract class that implements the core db operations
Definition: dbclientinterface.h:1058
boost::function< void(const BSONObj &, const std::string &)> PostRunCommandHookFunc
Similar to above, but for running a function on a command response after a command has been run...
Definition: dbclientinterface.h:997
Core MongoDB C++ driver interfaces are defined here.
This is a connection to a cluster of servers that operate as one for super high durability.
Definition: syncclusterconnection.h:43
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74
virtual auto_ptr< DBClientCursor > query(const string &ns, Query query, int nToReturn=0, int nToSkip=0, const BSONObj *fieldsToReturn=0, int queryOptions=0, int batchSize=0)
send a query to the database.
Definition: dbclient.cpp:1021
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Represents a Mongo query expression.
Definition: dbclientinterface.h:350
Definition: message.h:153
virtual void remove(const string &ns, Query q, bool justOne=0)
remove matching objects from the database
Definition: dbclient.cpp:1173