00001
00002
00019 #pragma once
00020
00021 #include "namespace.h"
00022
00023 namespace mongo {
00024
00025 inline Namespace& Namespace::operator=(const char *ns) {
00026
00027
00028
00029
00030
00031
00032
00033
00034 unsigned len = strlen(ns);
00035 uassert( 10080 , "ns name too long, max size is 128", len < MaxNsLen);
00036 memset(buf, 0, MaxNsLen);
00037 memcpy(buf, ns, len);
00038 return *this;
00039 }
00040
00041 inline string Namespace::extraName(int i) const {
00042 char ex[] = "$extra";
00043 ex[5] += i;
00044 string s = string(buf) + ex;
00045 massert( 10348 , "$extra: ns name too long", s.size() < MaxNsLen);
00046 return s;
00047 }
00048
00049 inline bool Namespace::isExtra() const {
00050 const char *p = strstr(buf, "$extr");
00051 return p && p[5] && p[6] == 0;
00052 }
00053
00054 inline int Namespace::hash() const {
00055 unsigned x = 0;
00056 const char *p = buf;
00057 while ( *p ) {
00058 x = x * 131 + *p;
00059 p++;
00060 }
00061 return (x & 0x7fffffff) | 0x8000000;
00062 }
00063
00064
00065 inline string Namespace::getSisterNS( const char * local ) const {
00066 assert( local && local[0] != '.' );
00067 string old(buf);
00068 if ( old.find( "." ) != string::npos )
00069 old = old.substr( 0 , old.find( "." ) );
00070 return old + "." + local;
00071 }
00072
00073 inline IndexDetails& NamespaceDetails::idx(int idxNo, bool missingExpected ) {
00074 if( idxNo < NIndexesBase )
00075 return _indexes[idxNo];
00076 Extra *e = extra();
00077 if ( ! e ) {
00078 if ( missingExpected )
00079 throw MsgAssertionException( 13283 , "Missing Extra" );
00080 massert(13282, "missing Extra", e);
00081 }
00082 int i = idxNo - NIndexesBase;
00083 if( i >= NIndexesExtra ) {
00084 e = e->next(this);
00085 if ( ! e ) {
00086 if ( missingExpected )
00087 throw MsgAssertionException( 13283 , "missing extra" );
00088 massert(13283, "missing Extra", e);
00089 }
00090 i -= NIndexesExtra;
00091 }
00092 return e->details[i];
00093 }
00094
00095 inline int NamespaceDetails::idxNo(IndexDetails& idx) {
00096 IndexIterator i = ii();
00097 while( i.more() ) {
00098 if( &i.next() == &idx )
00099 return i.pos()-1;
00100 }
00101 massert( 10349 , "E12000 idxNo fails", false);
00102 return -1;
00103 }
00104
00105 inline int NamespaceDetails::findIndexByKeyPattern(const BSONObj& keyPattern) {
00106 IndexIterator i = ii();
00107 while( i.more() ) {
00108 if( i.next().keyPattern() == keyPattern )
00109 return i.pos()-1;
00110 }
00111 return -1;
00112 }
00113
00114
00115 inline int NamespaceDetails::findIndexByName(const char *name) {
00116 IndexIterator i = ii();
00117 while( i.more() ) {
00118 if ( strcmp(i.next().info.obj().getStringField("name"),name) == 0 )
00119 return i.pos()-1;
00120 }
00121 return -1;
00122 }
00123
00124 inline NamespaceDetails::IndexIterator::IndexIterator(NamespaceDetails *_d) {
00125 d = _d;
00126 i = 0;
00127 n = d->nIndexes;
00128 }
00129
00130 }