本文整理汇总了C++中NamespaceDetails::ensureIndex方法的典型用法代码示例。如果您正苦于以下问题:C++ NamespaceDetails::ensureIndex方法的具体用法?C++ NamespaceDetails::ensureIndex怎么用?C++ NamespaceDetails::ensureIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NamespaceDetails
的用法示例。
在下文中一共展示了NamespaceDetails::ensureIndex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runInsertFromOplog
static void runInsertFromOplog(const char* ns, BSONObj op) {
BSONObj row = op[KEY_STR_ROW].Obj();
// handle add index case
if (mongoutils::str::endsWith(ns, ".system.indexes")) {
// do not build the index if the user has disabled
if (theReplSet->buildIndexes()) {
Client::WriteContext ctx(ns);
NamespaceDetails* nsd = nsdetails(ns);
const string &coll = row["ns"].String();
NamespaceDetails* collNsd = nsdetails(coll);
const bool ok = collNsd->ensureIndex(row);
if (!ok) {
// the index already exists, so this is a no-op
// Note that for create index and drop index, we
// are tolerant of the fact that the operation may
// have already been done
return;
}
// overwrite set to true because we are running on a secondary
insertOneObject(nsd, row, NamespaceDetails::NO_UNIQUE_CHECKS);
}
}
else {
try {
Client::ReadContext ctx(ns);
runNonSystemInsertFromOplogWithLock(ns, row);
}
catch (RetryWithWriteLock &e) {
Client::WriteContext ctx(ns);
runNonSystemInsertFromOplogWithLock(ns, row);
}
}
}
示例2: insertObjects
void insertObjects(const char *ns, const vector<BSONObj> &objs, bool keepGoing, uint64_t flags, bool logop ) {
StringData _ns(ns);
if (NamespaceString::isSystem(_ns)) {
massert(16748, "need transaction to run insertObjects", cc().txnStackSize() > 0);
uassert(10095, "attempt to insert in reserved database name 'system'", nsToDatabaseSubstring(_ns) != "system");
massert(16750, "attempted to insert multiple objects into a system namspace at once", objs.size() == 1);
// Trying to insert into a system collection. Fancy side-effects go here:
if (nsToCollectionSubstring(ns) == "system.indexes") {
BSONObj obj = stripDropDups(objs[0]);
NamespaceDetails *d = getAndMaybeCreateNS(obj["ns"].Stringdata(), logop);
bool ok = d->ensureIndex(obj);
if (!ok) {
// Already had that index
return;
}
// Now we have to actually insert that document into system.indexes, we may have
// modified it with stripDropDups.
vector<BSONObj> newObjs;
newObjs.push_back(obj);
_insertObjects(ns, newObjs, keepGoing, flags, logop);
return;
} else if (!legalClientSystemNS(ns, true)) {
uasserted(16459, str::stream() << "attempt to insert in system namespace '" << ns << "'");
}
}
_insertObjects(ns, objs, keepGoing, flags, logop);
}