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

159 lines
4.3 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-22 20:20:29 +02:00
2010-07-16 18:28:26 +02:00
struct ShardChunkVersion {
union {
struct {
int _minor;
int _major;
};
unsigned long long _combined;
};
2011-01-04 06:40:41 +01:00
2010-07-16 18:28:26 +02:00
ShardChunkVersion( int major=0, int minor=0 )
2011-01-04 06:40:41 +01:00
: _minor(minor),_major(major) {
2010-07-16 18:28:26 +02:00
}
2011-01-04 06:40:41 +01:00
2010-07-16 18:28:26 +02:00
ShardChunkVersion( unsigned long long ll )
2011-01-04 06:40:41 +01:00
: _combined( ll ) {
2010-07-16 18:28:26 +02:00
}
2011-01-04 06:40:41 +01:00
ShardChunkVersion( const BSONElement& e ) {
if ( e.type() == Date || e.type() == Timestamp ) {
2010-07-16 18:28:26 +02:00
_combined = e._numberLong();
}
2011-01-04 06:40:41 +01:00
else if ( e.eoo() ) {
2010-07-16 18:28:26 +02:00
_combined = 0;
}
else {
_combined = 0;
2010-07-16 18:28:26 +02:00
log() << "ShardChunkVersion can't handle type (" << (int)(e.type()) << ") " << e << endl;
assert(0);
}
}
2011-01-04 06:40:41 +01:00
void inc( bool major ) {
if ( major )
incMajor();
else
incMinor();
2010-07-16 18:28:26 +02:00
}
void incMajor() {
_major++;
_minor = 0;
}
2011-01-04 06:40:41 +01:00
void incMinor() {
2010-07-16 18:28:26 +02:00
_minor++;
}
unsigned long long toLong() const {
return _combined;
}
bool isSet() const {
return _combined > 0;
}
2011-01-04 06:40:41 +01:00
string toString() const {
stringstream ss;
ss << _major << "|" << _minor;
return ss.str();
2010-07-16 18:28:26 +02:00
}
2010-08-25 15:52:06 +02:00
int majorVersion() const { return _major; }
int minorVersion() const { return _minor; }
2011-01-04 06:40:41 +01:00
2010-07-16 18:28:26 +02:00
operator unsigned long long() const { return _combined; }
2011-01-04 06:40:41 +01:00
ShardChunkVersion& operator=( const BSONElement& elem ) {
switch ( elem.type() ) {
2010-07-16 18:28:26 +02:00
case Timestamp:
case NumberLong:
case Date:
_combined = elem._numberLong();
break;
case EOO:
_combined = 0;
break;
default:
assert(0);
}
return *this;
}
};
2011-01-04 06:40:41 +01:00
inline ostream& operator<<( ostream &s , const ShardChunkVersion& v) {
2010-07-16 18:28:26 +02:00
s << v._major << "|" << v._minor;
return s;
}
2011-01-04 06:40:41 +01:00
/**
* your config info for a given shard/chunk is out of date
2010-05-20 19:36:29 +02:00
*/
class StaleConfigException : public AssertionException {
2009-11-03 16:35:48 +01:00
public:
StaleConfigException( const string& ns , const string& raw , bool justConnection = false )
2011-01-04 06:40:41 +01:00
: AssertionException( (string)"ns: " + ns + " " + raw , 9996 ) ,
_justConnection(justConnection) ,
2011-01-04 06:40:41 +01:00
_ns(ns) {
2009-11-03 16:35:48 +01:00
}
2011-01-04 06:40:41 +01:00
virtual ~StaleConfigException() throw() {}
2010-06-21 19:41:34 +02:00
virtual void appendPrefix( stringstream& ss ) const { ss << "StaleConfigException: "; }
2011-01-04 06:40:41 +01:00
bool justConnection() const { return _justConnection; }
2011-01-04 06:40:41 +01:00
string getns() const { return _ns; }
2011-01-04 06:40:41 +01:00
static bool parse( const string& big , string& ns , string& raw ) {
string::size_type start = big.find( '[' );
if ( start == string::npos )
return false;
string::size_type end = big.find( ']' ,start );
if ( end == string::npos )
return false;
2011-01-04 06:40:41 +01:00
ns = big.substr( start + 1 , ( end - start ) - 1 );
raw = big.substr( end + 1 );
return true;
}
2009-11-03 16:35:48 +01:00
private:
bool _justConnection;
string _ns;
2009-11-03 16:35:48 +01:00
};
extern boost::function4<bool, DBClientBase&, const string&, bool, int> checkShardVersionCB;
extern boost::function1<void, DBClientBase*> resetShardVersionCB;
2009-11-03 16:35:48 +01:00
}