예제 #1
0
    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
0
파일: insert.cpp 프로젝트: aberg001/mongo
    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);
    }