00001
00002
00019 #pragma once
00020
00021 #include "../pch.h"
00022 #include "../client/dbclient.h"
00023 #include "../db/jsobj.h"
00024
00029 namespace mongo {
00030
00031 struct ShardChunkVersion {
00032 union {
00033 struct {
00034 int _minor;
00035 int _major;
00036 };
00037 unsigned long long _combined;
00038 };
00039
00040 ShardChunkVersion( int major=0, int minor=0 )
00041 : _minor(minor),_major(major) {
00042 }
00043
00044 ShardChunkVersion( unsigned long long ll )
00045 : _combined( ll ) {
00046 }
00047
00048 ShardChunkVersion( const BSONElement& e ) {
00049 if ( e.type() == Date || e.type() == Timestamp ) {
00050 _combined = e._numberLong();
00051 }
00052 else if ( e.eoo() ) {
00053 _combined = 0;
00054 }
00055 else {
00056 _combined = 0;
00057 log() << "ShardChunkVersion can't handle type (" << (int)(e.type()) << ") " << e << endl;
00058 assert(0);
00059 }
00060 }
00061
00062 void inc( bool major ) {
00063 if ( major )
00064 incMajor();
00065 else
00066 incMinor();
00067 }
00068
00069 void incMajor() {
00070 _major++;
00071 _minor = 0;
00072 }
00073
00074 void incMinor() {
00075 _minor++;
00076 }
00077
00078 unsigned long long toLong() const {
00079 return _combined;
00080 }
00081
00082 bool isSet() const {
00083 return _combined > 0;
00084 }
00085
00086 string toString() const {
00087 stringstream ss;
00088 ss << _major << "|" << _minor;
00089 return ss.str();
00090 }
00091
00092 int majorVersion() const { return _major; }
00093 int minorVersion() const { return _minor; }
00094
00095 operator unsigned long long() const { return _combined; }
00096
00097 ShardChunkVersion& operator=( const BSONElement& elem ) {
00098 switch ( elem.type() ) {
00099 case Timestamp:
00100 case NumberLong:
00101 case Date:
00102 _combined = elem._numberLong();
00103 break;
00104 case EOO:
00105 _combined = 0;
00106 break;
00107 default:
00108 massert( 13657 , str::stream() << "unknown type for ShardChunkVersion: " << elem , 0 );
00109 }
00110 return *this;
00111 }
00112 };
00113
00114 inline ostream& operator<<( ostream &s , const ShardChunkVersion& v) {
00115 s << v._major << "|" << v._minor;
00116 return s;
00117 }
00118
00122 class StaleConfigException : public AssertionException {
00123 public:
00124 StaleConfigException( const string& ns , const string& raw , bool justConnection = false )
00125 : AssertionException( (string)"ns: " + ns + " " + raw , 9996 ) ,
00126 _justConnection(justConnection) ,
00127 _ns(ns) {
00128 }
00129
00130 virtual ~StaleConfigException() throw() {}
00131
00132 virtual void appendPrefix( stringstream& ss ) const { ss << "StaleConfigException: "; }
00133
00134 bool justConnection() const { return _justConnection; }
00135
00136 string getns() const { return _ns; }
00137
00138 static bool parse( const string& big , string& ns , string& raw ) {
00139 string::size_type start = big.find( '[' );
00140 if ( start == string::npos )
00141 return false;
00142 string::size_type end = big.find( ']' ,start );
00143 if ( end == string::npos )
00144 return false;
00145
00146 ns = big.substr( start + 1 , ( end - start ) - 1 );
00147 raw = big.substr( end + 1 );
00148 return true;
00149 }
00150 private:
00151 bool _justConnection;
00152 string _ns;
00153 };
00154
00155 extern boost::function4<bool, DBClientBase&, const string&, bool, int> checkShardVersionCB;
00156 extern boost::function1<void, DBClientBase*> resetShardVersionCB;
00157
00158 }