/** @file dbclientinterface.h
Core MongoDB C++ driver interfaces are defined here.
*/
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <boost/thread/locks.hpp>
#include "mongo/config.h"
#include "mongo/base/string_data.h"
#include "mongo/client/bulk_operation_builder.h"
#include "mongo/client/exceptions.h"
#include "mongo/client/export_macros.h"
#include "mongo/client/index_spec.h"
#include "mongo/client/write_concern.h"
#include "mongo/client/write_options.h"
#include "mongo/db/jsobj.h"
#include "mongo/logger/log_severity.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/stdx/functional.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/net/message.h"
#include "mongo/util/net/message_port.h"
namespace mongo {
/** the query field 'options' can have these bits set: */
enum MONGO_CLIENT_API QueryOptions {
/** Tailable means cursor is not closed when the last data is retrieved. rather, the cursor marks
the final object's position. you can resume using the cursor later, from where it was located,
if more data were received. Set on dbQuery and dbGetMore.
like any "latent cursor", the cursor may become invalid at some point -- for example if that
final object it references were deleted. Thus, you should be prepared to requery if you get back
ResultFlag_CursorNotFound.
*/
QueryOption_CursorTailable = 1 << 1,
/** allow query of replica slave. normally these return an error except for namespace "local".
*/
QueryOption_SlaveOk = 1 << 2,
// findingStart mode is used to find the first operation of interest when
// we are scanning through a repl log. For efficiency in the common case,
// where the first operation of interest is closer to the tail than the head,
// we start from the tail of the log and work backwards until we find the
// first operation of interest. Then we scan forward from that first operation,
// actually returning results to the client. During the findingStart phase,
// we release the db mutex occasionally to avoid blocking the db process for
// an extended period of time.
QueryOption_OplogReplay = 1 << 3,
/** The server normally times out idle cursors after an inactivity period to prevent excess memory uses
Set this option to prevent that.
*/
QueryOption_NoCursorTimeout = 1 << 4,
/** Use with QueryOption_CursorTailable. If we are at the end of the data, block for a while rather
than returning no data. After a timeout period, we do return as normal.
*/
QueryOption_AwaitData = 1 << 5,
/** Stream the data down full blast in multiple "more" packages, on the assumption that the client
will fully read all data queried. Faster when you are pulling a lot of data and know you want to
pull it all down. Note: it is not allowed to not read all the data unless you close the connection.
Use the query( stdx::function<void(const BSONObj&)> f, ... ) version of the connection's query()
method, and it will take care of all the details for you.
*/
QueryOption_Exhaust = 1 << 6,
/** When sharded, this means its ok to return partial results
Usually we will fail a query if all required shards aren't up
If this is set, it'll be a partial result set
*/
QueryOption_PartialResults = 1 << 7 ,
QueryOption_AllSupported = QueryOption_CursorTailable | QueryOption_SlaveOk | QueryOption_OplogReplay | QueryOption_NoCursorTimeout | QueryOption_AwaitData | QueryOption_Exhaust | QueryOption_PartialResults
};
/**
* Start from *top* of bits, these are generic write options that apply to all
*/
enum MONGO_CLIENT_API WriteOptions {
/** logical writeback option */
WriteOption_FromWriteback = 1 << 31
};
//
// For legacy reasons, the reserved field pre-namespace of certain types of messages is used
// to store options as opposed to the flags after the namespace. This should be transparent to
// the api user, but we need these constants to disassemble/reassemble the messages correctly.
//
enum MONGO_CLIENT_API ReservedOptions {
Reserved_InsertOption_ContinueOnError = 1 << 0 ,
Reserved_FromWriteback = 1 << 1
};
enum MONGO_CLIENT_API ReadPreference {
/**
* Read from primary only. All operations produce an error (throw an
* exception where applicable) if primary is unavailable. Cannot be
* combined with tags.
*/
ReadPreference_PrimaryOnly = 0,
/**
* Read from primary if available, otherwise a secondary. Tags will
* only be applied in the event that the primary is unavailable and
* a secondary is read from. In this event only secondaries matching
* the tags provided would be read from.
*/
ReadPreference_PrimaryPreferred,
/**
* Read from secondary if available, otherwise error.
*/
ReadPreference_SecondaryOnly,
/**
* Read from a secondary if available, otherwise read from the primary.
*/
ReadPreference_SecondaryPreferred,
/**
* Read from any member.
*/
ReadPreference_Nearest,
};
class MONGO_CLIENT_API DBClientBase;
class MONGO_CLIENT_API DBClientConnection;
/**
* ConnectionString handles parsing different ways to connect to mongo and determining method
* samples:
* server
* server:port
* foo/server:port,server:port SET
*
* tyipcal use
* string errmsg,
* ConnectionString cs = ConnectionString::parse( url , errmsg );
* if ( ! cs.isValid() ) throw "bad: " + errmsg;
* DBClientBase * conn = cs.connect( errmsg );
*/
class MONGO_CLIENT_API ConnectionString {
public:
enum ConnectionType { INVALID , MASTER , PAIR , SET , CUSTOM };
ConnectionString() {
_type = INVALID;
}
// Note: This should only be used for direct connections to a single server. For replica
// set and SyncClusterConnections, use ConnectionString::parse.
ConnectionString( const HostAndPort& server ) {
_type = MASTER;
_servers.push_back( server );
_finishInit();
}
ConnectionString( ConnectionType type , const std::string& s , const std::string& setName = "" ) {
_type = type;
_setName = setName;
_fillServers( s );
switch ( _type ) {
case MASTER:
verify( _servers.size() == 1 );
break;
case SET:
verify( _setName.size() );
verify( _servers.size() >= 1 ); // 1 is ok since we can derive
break;
case PAIR:
verify( _servers.size() == 2 );
break;
default:
verify( _servers.size() > 0 );
}
_finishInit();
}
ConnectionString( const std::string& s , ConnectionType favoredMultipleType ) {
_type = INVALID;
_fillServers( s );
if (
评论1
最新资源