private BsonDocument asGetMoreCommandDocument() { return new BsonDocument(COMMAND_NAME, new BsonInt64(cursorId)) .append("collection", new BsonString(namespace.getCollectionName())) .append("batchSize", new BsonInt32(numberToReturn)); }
private static BsonDocument namespaceToDocument(final MongoNamespace namespace) { Assertions.notNull("namespace", namespace); return new BsonDocument("db", new BsonString(namespace.getDatabaseName())) .append("coll", new BsonString(namespace.getCollectionName())); }
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) { BsonDocument document = new BsonDocument("$query", new BsonDocument("ns", new BsonString(namespace.getFullName()))); if (maxTimeMS > 0) { document.put("$maxTimeMS", new BsonInt64(maxTimeMS)); } if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) { document.put("$readPreference", readPreference.toDocument()); } return document; }
private MongoNamespace getIndexNamespace() { return new MongoNamespace(namespace.getDatabaseName(), "system.indexes"); }
/** * Construct an instance for the given full name. The database name is the string preceding the first {@code "."} character. * * @param fullName the non-null full namespace * @see #checkDatabaseNameValidity(String) * @see #checkCollectionNameValidity(String) */ public MongoNamespace(final String fullName) { notNull("fullName", fullName); this.fullName = fullName; this.databaseName = getDatatabaseNameFromFullName(fullName); this.collectionName = getCollectionNameFullName(fullName); checkDatabaseNameValidity(databaseName); checkCollectionNameValidity(collectionName); }
private BsonDocument getCommand(final ConnectionDescription description) { BsonDocument command = new BsonDocument("dropIndexes", new BsonString(namespace.getCollectionName())); if (indexName != null) { command.put("index", new BsonString(indexName)); } else { command.put("index", indexKeys); } putIfNotZero(command, "maxTimeMS", maxTimeMS); appendWriteConcernToCommand(writeConcern, command, description); return command; } }
private BsonDocument getCommand(final SessionContext sessionContext) { BsonDocument commandDocument = new BsonDocument("mapreduce", new BsonString(namespace.getCollectionName())) .append("map", getMapFunction()) .append("reduce", getReduceFunction()) .append("out", new BsonDocument("inline", new BsonInt32(1))) .append("query", asValueOrNull(getFilter())) .append("sort", asValueOrNull(getSort())) .append("finalize", asValueOrNull(getFinalizeFunction())) .append("scope", asValueOrNull(getScope())) .append("verbose", BsonBoolean.valueOf(isVerbose())); appendReadConcernToCommand(sessionContext, commandDocument); putIfNotZero(commandDocument, "limit", getLimit()); putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS)); putIfTrue(commandDocument, "jsMode", isJsMode()); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } return commandDocument; }
private BsonDocument getCommand() { BsonDocument commandDocument = new BsonDocument("aggregate", new BsonString(namespace.getCollectionName())); commandDocument.put("pipeline", new BsonArray(pipeline)); commandDocument.put("explain", BsonBoolean.TRUE); if (maxTimeMS > 0) { commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS)); } if (allowDiskUse != null) { commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse)); } if (collation != null) { commandDocument.put("collation", collation.asDocument()); } if (hint != null) { commandDocument.put("hint", hint); } return commandDocument; }
@Override public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) { validateCollation(connectionDescription, collation); BsonDocument commandDocument = new BsonDocument("findAndModify", new BsonString(getNamespace().getCollectionName())); putIfNotNull(commandDocument, "query", getFilter()); putIfNotNull(commandDocument, "fields", getProjection()); putIfNotNull(commandDocument, "sort", getSort()); commandDocument.put("new", new BsonBoolean(!isReturnOriginal())); putIfTrue(commandDocument, "upsert", isUpsert()); putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS)); commandDocument.put("update", getReplacement()); if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connectionDescription)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } addWriteConcernToCommand(connectionDescription, commandDocument, sessionContext); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } addTxnNumberToCommand(serverDescription, connectionDescription, commandDocument, sessionContext); return commandDocument; } };
private BsonDocument getCommand(final SessionContext sessionContext) { BsonDocument commandDocument = new BsonDocument("find", new BsonString(namespace.getCollectionName())); for (Map.Entry<String, BsonValue> cur : modifiers.entrySet()) { String commandFieldName = META_OPERATOR_TO_COMMAND_FIELD_MAP.get(cur.getKey()); if (commandFieldName != null) { commandDocument.append(commandFieldName, cur.getValue()); putIfNotNullOrEmpty(commandDocument, "projection", projection); if (skip > 0) { commandDocument.put("skip", new BsonInt32(skip)); commandDocument.put("limit", new BsonInt32(Math.abs(limit))); commandDocument.put("limit", new BsonInt32(Math.abs(batchSize))); } else if (batchSize != 0) { commandDocument.put("batchSize", new BsonInt32(Math.abs(batchSize))); commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS)); commandDocument.put("collation", collation.asDocument()); commandDocument.put("comment", new BsonString(comment)); commandDocument.put("maxScan", new BsonInt64(maxScan));
private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) { List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList(); if (responseBuffers.getReplyHeader().getNumberReturned() != 0) { responseBuffers.reset(); rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers); } BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())) .append("ns", new BsonString(namespace.getFullName())) .append("nextBatch", new BsonArray(rawResultDocuments)); return new BsonDocument("cursor", cursorDocument) .append("ok", new BsonDouble(1)); }
private BsonDocument asFindCommandDocument(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) { BsonDocument command = new BsonDocument(FIND_COMMAND_NAME, new BsonString(namespace.getCollectionName())); String commandFieldName = META_OPERATOR_TO_COMMAND_FIELD_MAP.get(cur.getKey()); if (commandFieldName != null) { command.append(commandFieldName, cur.getValue()); } else if (cur.getKey().equals("$explain")) { isExplain = true; if (command.size() == 1) { command.append("filter", rawQueryDocument); command.append("skip", new BsonInt32(skip)); command.append("limit", new BsonInt32(limit)); command.append("batchSize", new BsonInt32(batchSize)); command.append("tailable", BsonBoolean.valueOf(tailableCursor)); command.append("noCursorTimeout", BsonBoolean.valueOf(noCursorTimeout)); command.append("oplogReplay", BsonBoolean.valueOf(oplogReplay));
protected BsonDocument getBaseCommandDocument(final String commandName) { BsonDocument baseCommandDocument = new BsonDocument(commandName, new BsonString(getNamespace().getCollectionName())) .append("ordered", BsonBoolean.valueOf(isOrdered())); baseCommandDocument.append("writeConcern", WriteConcern.UNACKNOWLEDGED.asDocument()); return baseCommandDocument; }
private BsonDocument getIndex(final IndexRequest request) { BsonDocument index = new BsonDocument(); index.append("key", request.getKeys()); index.append("name", new BsonString(request.getName() != null ? request.getName() : generateIndexName(request.getKeys()))); index.append("ns", new BsonString(namespace.getFullName())); if (request.isBackground()) { index.append("background", BsonBoolean.TRUE); index.append("expireAfterSeconds", new BsonInt64(request.getExpireAfter(TimeUnit.SECONDS))); index.append("v", new BsonInt32(request.getVersion())); index.append("default_language", new BsonString(request.getDefaultLanguage())); index.append("textIndexVersion", new BsonInt32(request.getTextVersion())); index.append("2dsphereIndexVersion", new BsonInt32(request.getSphereVersion())); index.append("collation", request.getCollation().asDocument());
private BsonDocument getCommand(final ConnectionDescription description) { BsonDocument commandDocument = new BsonDocument("renameCollection", new BsonString(originalNamespace.getFullName())) .append("to", new BsonString(newNamespace.getFullName())) .append("dropTarget", BsonBoolean.valueOf(dropTarget)); appendWriteConcernToCommand(writeConcern, commandDocument, description); return commandDocument; } }
this.payload = new SplittablePayload(getPayloadType(batchType), payloadItems); this.sessionContext = sessionContext; this.command = new BsonDocument(); command.put(getCommandName(batchType), new BsonString(namespace.getCollectionName())); command.put("ordered", new BsonBoolean(ordered)); if (!writeConcern.isServerDefault() && !sessionContext.hasActiveTransaction()) { command.put("writeConcern", writeConcern.asDocument()); command.put("bypassDocumentValidation", new BsonBoolean(bypassDocumentValidation)); command.put("txnNumber", new BsonInt64(sessionContext.advanceTransactionNumber()));
private List<BsonElement> getExtraElements(final SessionContext sessionContext) { List<BsonElement> extraElements = new ArrayList<BsonElement>(); extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName()))); if (sessionContext.getClusterTime() != null) { extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime())); } if (sessionContext.hasSession() && responseExpected) { extraElements.add(new BsonElement("lsid", sessionContext.getSessionId())); } boolean firstMessageInTransaction = sessionContext.notifyMessageSent(); if (sessionContext.hasActiveTransaction()) { checkServerVersionForTransactionSupport(); extraElements.add(new BsonElement("txnNumber", new BsonInt64(sessionContext.getTransactionNumber()))); if (firstMessageInTransaction) { extraElements.add(new BsonElement("startTransaction", BsonBoolean.TRUE)); addReadConcernDocument(extraElements, sessionContext); } extraElements.add(new BsonElement("autocommit", BsonBoolean.FALSE)); } if (readPreference != null) { if (!readPreference.equals(primary())) { extraElements.add(new BsonElement("$readPreference", readPreference.toDocument())); } else if (isDirectConnectionToNonShardRouter()) { extraElements.add(new BsonElement("$readPreference", primaryPreferred().toDocument())); } } return extraElements; }
@Override public BsonDocument call(final Connection connection) { if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { return executeWrappedCommandProtocol(binding, "admin", new BsonDocument("currentOp", new BsonInt32(1)), connection); } else { return connection.query(new MongoNamespace("admin", "$cmd.sys.inprog"), new BsonDocument(), null, 0, 1, 0, binding.getReadPreference().isSlaveOk(), false, false, false, false, false, new BsonDocumentCodec()) .getResults().get(0); } } });
private void killCursor(final Connection connection) { if (serverCursor != null) { notNull("connection", connection); if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) { connection.command(namespace.getDatabaseName(), asKillCursorsCommandDocument(), NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(), new BsonDocumentCodec(), connectionSource.getSessionContext()); } else { connection.killCursor(namespace, singletonList(serverCursor.getId())); } serverCursor = null; } }
@Override @SuppressWarnings("deprecation") public ReadOperation<BatchCursor<TResult>> asReadOperation() { BsonValue outCollection = getOutCollection(); if (outCollection != null) { getExecutor().execute(operations.aggregateToCollection(pipeline, maxTimeMS, allowDiskUse, bypassDocumentValidation, collation, hint, comment, aggregationLevel), getReadConcern(), getClientSession()); FindOptions findOptions = new FindOptions().collation(collation); Integer batchSize = getBatchSize(); if (batchSize != null) { findOptions.batchSize(batchSize); } return operations.find(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), new BsonDocument(), resultClass, findOptions); } else { return operations.aggregate(pipeline, resultClass, maxTimeMS, maxAwaitTimeMS, getBatchSize(), collation, hint, comment, allowDiskUse, useCursor, aggregationLevel); } }