0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

checkpoint for sharding object stuff

This commit is contained in:
Eliot Horowitz 2009-02-17 15:34:52 -05:00
parent 73c8977fab
commit e74408dc7a
4 changed files with 72 additions and 14 deletions

View File

@ -22,6 +22,9 @@
namespace mongo {
Shard::Shard( ShardInfo * info , BSONObj data ) : _info( info ) , _data( data ){
}
string ShardInfo::modelServer() {
// TODO: this could move around?
return configServer.modelServer();
@ -29,30 +32,69 @@ namespace mongo {
void ShardInfo::serialize(BSONObjBuilder& to) {
to.append( "ns", _ns );
to.append( "key" , _key.key() );
BSONObjBuilder shards;
int num=0;
for ( vector<Shard>::iterator i=_shards.begin(); i != _shards.end(); i++ ){
string s = shards.numStr( num++ );
shards.append( s.c_str() , i->_data );
}
to.append( "shards" , shards.obj() );
}
void ShardInfo::unserialize(BSONObj& from) {
_ns = from.getStringField("ns");
uassert("bad config.shards.name", !_ns.empty());
_key.init( from.getObjectField( "key" ) );
_shards.clear();
}
BSONObj shards = from.getObjectField( "shards" );
if ( shards.isEmpty() ){
BSONObjBuilder all;
// TODO: server
all.append( "min" , _key.globalMin() );
all.append( "max" , _key.globalMax() );
_shards.push_back( Shard( this , all.obj() ) );
}
else {
int num=0;
while ( true ){
string s = BSONObjBuilder::numStr( num++ );
BSONObj next = shards.getObjectField( s.c_str() );
if ( next.isEmpty() )
break;
_shards.push_back( Shard( this , next ) );
}
}
}
void shardObjTest(){
string ns = "alleyinsider.blog.posts";
BSONObj o = BSON( "ns" << ns );
BSONObj o = BSON( "ns" << ns << "key" << BSON( "num" << 1 ) );
ShardInfo si;
si.unserialize( o );
assert( si.getns() == ns );
BSONObjBuilder b;
si.serialize( b );
BSONObj a = b.obj();
assert( ns == a.getStringField( "ns" ) );
assert( 1 == a.getObjectField( "key" )["num"].number() );
log(2) << a << endl;
{
ShardInfo si2;
si2.unserialize( a );
BSONObjBuilder b2;
si2.serialize( b2 );
assert( b2.obj().jsonString() == a.jsonString() );
}
log(1) << "shardObjTest passed" << endl;
}

View File

@ -47,17 +47,17 @@ namespace mongo {
private:
Shard( ShardInfo * info , BSONObj data );
ShardInfo * _info;
BSONObj _data;
void _split( BSONObj& middle );
BSONObj _data;
ShardInfo * _info;
friend class ShardInfo;
};
/* config.sharding
{ ns: 'alleyinsider.fs.chunks' ,
shardType: { ts : 1 } ,
key: { ts : 1 } ,
shards: [ { min: 1, max: 100, server: a } , { min: 101, max: 200 , server : b } ]
}
*/

View File

@ -26,17 +26,27 @@
*/
namespace mongo {
ShardKey::ShardKey( BSONObj fieldsAndOrder ) : _fieldsAndOrder( fieldsAndOrder ){
if ( _fieldsAndOrder.nFields() > 0 ){
_fieldName = fieldsAndOrder.firstElement().fieldName();
uassert( "shard key only supports 1 field right now" , 1 == fieldsAndOrder.nFields() );
uassert( "shard key has to be a number right now" , fieldsAndOrder.firstElement().isNumber() );
_init();
}
else {
_fieldName = 0;
}
}
void ShardKey::init( BSONObj fieldsAndOrder ){
_fieldsAndOrder = fieldsAndOrder;
_init();
}
void ShardKey::_init(){
_fieldName = _fieldsAndOrder.firstElement().fieldName();
uassert( "shard key only supports 1 field right now" , 1 == _fieldsAndOrder.nFields() );
uassert( "shard key has to be a number right now" , _fieldsAndOrder.firstElement().isNumber() );
}
void ShardKey::globalMin( BSONObjBuilder& b ){
uassert( "not valid yet" , _fieldName );

View File

@ -25,10 +25,11 @@ namespace mongo {
class ShardKey {
public:
ShardKey( BSONObj fieldsAndOrder = emptyObj );
void init( BSONObj fieldsAndOrder );
void globalMin( BSONObjBuilder & b );
BSONObj globalMin(){ BSONObjBuilder b; globalMin( b ); return b.obj(); }
void globalMax( BSONObjBuilder & b );
BSONObj globalMax(){ BSONObjBuilder b; globalMax( b ); return b.obj(); }
@ -37,7 +38,12 @@ namespace mongo {
int compare( BSONObj& l , BSONObj& r );
BSONObj& key(){
return _fieldsAndOrder;
}
private:
void _init();
BSONObj _fieldsAndOrder;
const char * _fieldName;
};