0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/s/util.h

127 lines
3.5 KiB
C
Raw Normal View History

2009-11-03 16:35:48 +01:00
// util.h
/**
* Copyright (C) 2008 10gen Inc.
*
* 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.
*
* 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.
*
* 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-11-03 18:13:42 +01:00
#pragma once
2010-04-27 21:33:27 +02:00
#include "../pch.h"
2009-11-03 18:13:42 +01:00
#include "../client/dbclient.h"
2010-07-16 18:28:26 +02:00
#include "../db/jsobj.h"
2009-11-03 16:35:48 +01:00
/**
some generic sharding utils that can be used in mongod or mongos
*/
namespace mongo {
2010-07-16 18:28:26 +02:00
struct ShardChunkVersion {
union {
struct {
int _minor;
int _major;
};
unsigned long long _combined;
};
ShardChunkVersion( int major=0, int minor=0 )
: _minor(minor),_major(major){
}
ShardChunkVersion( unsigned long long ll )
: _combined( ll ){
}
ShardChunkVersion( const BSONElement& e ){
if ( e.type() == Date || e.type() == Timestamp ){
_combined = e._numberLong();
}
else if ( e.eoo() ){
_combined = 0;
}
else {
log() << "ShardChunkVersion can't handle type (" << (int)(e.type()) << ") " << e << endl;
assert(0);
}
}
ShardChunkVersion incMajor() const {
return ShardChunkVersion( _major + 1 , 0 );
}
void operator++(){
_minor++;
}
unsigned long long toLong() const {
return _combined;
}
bool isSet() const {
return _combined > 0;
}
string toString() const {
stringstream ss;
ss << _major << "|" << _minor;
return ss.str();
}
operator unsigned long long() const { return _combined; }
ShardChunkVersion& operator=( const BSONElement& elem ){
switch ( elem.type() ){
case Timestamp:
case NumberLong:
case Date:
_combined = elem._numberLong();
break;
case EOO:
_combined = 0;
break;
default:
assert(0);
}
return *this;
}
};
inline ostream& operator<<( ostream &s , const ShardChunkVersion& v){
s << v._major << "|" << v._minor;
return s;
}
2009-11-03 16:35:48 +01:00
/**
2010-05-20 19:36:29 +02:00
* your config info for a given shard/chunk is out of date
*/
class StaleConfigException : public AssertionException {
2009-11-03 16:35:48 +01:00
public:
StaleConfigException( const string& ns , const string& raw , bool justConnection = false )
: AssertionException( (string)"ns: " + ns + " " + raw , 9996 ) , _justConnection(justConnection){
2009-11-03 16:35:48 +01:00
}
2010-05-20 19:36:29 +02:00
virtual ~StaleConfigException() throw(){}
2010-06-21 19:41:34 +02:00
virtual void appendPrefix( stringstream& ss ) const { ss << "StaleConfigException: "; }
bool justConnection() const { return _justConnection; }
2009-11-03 16:35:48 +01:00
private:
bool _justConnection;
2009-11-03 16:35:48 +01:00
};
bool checkShardVersion( DBClientBase & conn , const string& ns , bool authoritative = false , int tryNumber = 1 );
2009-11-03 16:35:48 +01:00
}