StatusWith<Shard::QueryResponse> ShardLocal::_exhaustiveFindOnConfig( OperationContext* txn, const ReadPreferenceSetting& readPref, const repl::ReadConcernLevel& readConcernLevel, const NamespaceString& nss, const BSONObj& query, const BSONObj& sort, boost::optional<long long> limit) { auto replCoord = repl::ReplicationCoordinator::get(txn); if (readConcernLevel == repl::ReadConcernLevel::kMajorityReadConcern) { // Set up operation context with majority read snapshot so correct optime can be retrieved. Status status = txn->recoveryUnit()->setReadFromMajorityCommittedSnapshot(); // Wait for any writes performed by this ShardLocal instance to be committed and visible. Status readConcernStatus = replCoord->waitUntilOpTimeForRead( txn, repl::ReadConcernArgs{_getLastOpTime(), readConcernLevel}); if (!readConcernStatus.isOK()) { return readConcernStatus; } // Inform the storage engine to read from the committed snapshot for the rest of this // operation. status = txn->recoveryUnit()->setReadFromMajorityCommittedSnapshot(); if (!status.isOK()) { return status; } } else { invariant(readConcernLevel == repl::ReadConcernLevel::kLocalReadConcern); } DBDirectClient client(txn); Query fullQuery(query); if (!sort.isEmpty()) { fullQuery.sort(sort); } fullQuery.readPref(readPref.pref, BSONArray()); try { std::unique_ptr<DBClientCursor> cursor = client.query(nss.ns().c_str(), fullQuery, limit.get_value_or(0)); if (!cursor) { return {ErrorCodes::OperationFailed, str::stream() << "Failed to establish a cursor for reading " << nss.ns() << " from local storage"}; } std::vector<BSONObj> documentVector; while (cursor->more()) { BSONObj document = cursor->nextSafe().getOwned(); documentVector.push_back(std::move(document)); } return Shard::QueryResponse{std::move(documentVector), replCoord->getCurrentCommittedSnapshotOpTime()}; } catch (const DBException& ex) { return ex.toStatus(); } }
StatusWith<Shard::QueryResponse> ShardLocal::_exhaustiveFindOnConfig( OperationContext* txn, const ReadPreferenceSetting& readPref, const NamespaceString& nss, const BSONObj& query, const BSONObj& sort, boost::optional<long long> limit) { // Set up operation context with majority read snapshot so correct optime can be retrieved. Status status = txn->recoveryUnit()->setReadFromMajorityCommittedSnapshot(); auto replCoord = repl::ReplicationCoordinator::get(txn); // Ensure timeout is set on the txn so we don't wait forever for the snapshot. // TODO (SERVER-18277): Remove this CurOp::get(txn)->ensureStarted(); // Wait until a snapshot is available. while (status == ErrorCodes::ReadConcernMajorityNotAvailableYet) { LOG(1) << "Waiting for ReadFromMajorityCommittedSnapshot to become available"; replCoord->waitUntilSnapshotCommitted(txn, SnapshotName::min()); status = txn->recoveryUnit()->setReadFromMajorityCommittedSnapshot(); } if (!status.isOK()) { return status; } DBDirectClient client(txn); Query fullQuery(query); if (!sort.isEmpty()) { fullQuery.sort(sort); } fullQuery.readPref(readPref.pref, BSONArray()); try { std::unique_ptr<DBClientCursor> cursor = client.query(nss.ns().c_str(), fullQuery, limit.get_value_or(0)); if (!cursor) { return {ErrorCodes::OperationFailed, str::stream() << "Failed to establish a cursor for reading " << nss.ns() << " from local storage"}; } std::vector<BSONObj> documentVector; while (cursor->more()) { BSONObj document = cursor->nextSafe().getOwned(); documentVector.push_back(std::move(document)); } return Shard::QueryResponse{std::move(documentVector), replCoord->getCurrentCommittedSnapshotOpTime()}; } catch (const DBException& ex) { return ex.toStatus(); } }
/** * Searches certificates in all opened system keychains. Takes an optional * \p query that defines filter attributes to be searched for. That query * is amended by generic attributes for "certificate filtering". * * \param query a list of key-value pairs used for filtering * \returns an array with the resulting certificates or nullptr if * no matching certificate was found */ scoped_CFType<CFArrayRef> search(Query query = Query()) const { scoped_CFType<CFDictionaryRef> fullQuery( prepareQuery(std::move(query))); check_notnull(fullQuery, "create search query"); scoped_CFType<CFArrayRef> result(nullptr); auto status = SecItemCopyMatching(fullQuery.get(), (CFTypeRef*)&result.get()); if(errSecItemNotFound == status) { return scoped_CFType<CFArrayRef>(nullptr); // no matches } check_success(status, "look up certificate"); check_notnull(result, "look up certificate (invalid result value)"); return result; }
StatusWith<std::vector<ChunkType>> readShardChunks(OperationContext* opCtx, const NamespaceString& nss, const BSONObj& query, const BSONObj& sort, boost::optional<long long> limit, const OID& epoch) { // Query to retrieve the chunks. Query fullQuery(query); fullQuery.sort(sort); try { DBDirectClient client(opCtx); std::string chunkMetadataNs = ChunkType::ShardNSPrefix + nss.ns(); std::unique_ptr<DBClientCursor> cursor = client.query(chunkMetadataNs, fullQuery, limit.get_value_or(0)); if (!cursor) { return {ErrorCodes::OperationFailed, str::stream() << "Failed to establish a cursor for reading " << chunkMetadataNs << " from local storage"}; } std::vector<ChunkType> chunks; while (cursor->more()) { BSONObj document = cursor->nextSafe().getOwned(); auto statusWithChunk = ChunkType::fromShardBSON(document, epoch); if (!statusWithChunk.isOK()) { return {statusWithChunk.getStatus().code(), str::stream() << "Failed to parse chunk '" << document.toString() << "' due to " << statusWithChunk.getStatus().reason()}; } chunks.push_back(std::move(statusWithChunk.getValue())); } return chunks; } catch (const DBException& ex) { return ex.toStatus(); } }
StatusWith<ShardCollectionType> readShardCollectionsEntry(OperationContext* opCtx, const NamespaceString& nss) { Query fullQuery(BSON(ShardCollectionType::uuid() << nss.ns())); try { DBDirectClient client(opCtx); std::unique_ptr<DBClientCursor> cursor = client.query(ShardCollectionType::ConfigNS.c_str(), fullQuery, 1); if (!cursor) { return Status(ErrorCodes::OperationFailed, str::stream() << "Failed to establish a cursor for reading " << ShardCollectionType::ConfigNS << " from local storage"); } if (!cursor->more()) { // The collection has been dropped. return Status(ErrorCodes::NamespaceNotFound, str::stream() << "collection " << nss.ns() << " not found"); } BSONObj document = cursor->nextSafe(); auto statusWithCollectionEntry = ShardCollectionType::fromBSON(document); if (!statusWithCollectionEntry.isOK()) { return statusWithCollectionEntry.getStatus(); } return statusWithCollectionEntry.getValue(); } catch (const DBException& ex) { return {ex.toStatus().code(), str::stream() << "Failed to read the '" << nss.ns() << "' entry locally from config.collections" << causedBy(ex.toStatus())}; } }