@SuppressWarnings("unchecked") private BsonArray getCursorDocuments(final BsonDocument result) { return result.getArray("cursors"); }
@SuppressWarnings("unchecked") static <T> List<T> toList(final BsonDocument result, final String fieldContainingWrappedArray) { return ((BsonArrayWrapper<T>) result.getArray(fieldContainingWrappedArray)).getWrappedArray(); }
private static List<String> getCompressors(final BsonDocument isMasterResult) { List<String> compressorList = new ArrayList<String>(); for (BsonValue compressor : isMasterResult.getArray("compression", new BsonArray())) { compressorList.add(compressor.asString().getValue()); } return compressorList; }
@Override public Boolean apply(final BsonDocument result, final ServerAddress serverAddress) { return result.get("users").isArray() && !result.getArray("users").isEmpty(); } };
@SuppressWarnings("unchecked") private List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) { BsonArray upsertedValue = result.getArray("upserted", new BsonArray()); List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>(); for (BsonValue upsertedItem : upsertedValue) { BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem; bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id"))); } return bulkWriteUpsertList; }
static ServerVersion getVersion(final BsonDocument buildInfoResult) { List<BsonValue> versionArray = buildInfoResult.getArray("versionArray").subList(0, 3); return new ServerVersion(asList(versionArray.get(0).asInt32().getValue(), versionArray.get(1).asInt32().getValue(), versionArray.get(2).asInt32().getValue())); }
Authenticator getAuthenticatorFromIsMasterResult(final BsonDocument isMasterResult, final ConnectionDescription connectionDescription) { if (isMasterResult.containsKey("saslSupportedMechs")) { BsonArray saslSupportedMechs = isMasterResult.getArray("saslSupportedMechs"); AuthenticationMechanism mechanism = saslSupportedMechs.contains(DEFAULT_MECHANISM_NAME) ? SCRAM_SHA_256 : SCRAM_SHA_1; return new ScramShaAuthenticator(getMongoCredentialWithCache().withMechanism(mechanism)); } else { return getLegacyDefaultAuthenticator(connectionDescription); } }
/** * Construct a new instance with the CommandResult from a failed command * * @param response the command response * @param address the address of the server that generated the response */ public MongoCommandException(final BsonDocument response, final ServerAddress address) { super(extractErrorCode(response), format("Command failed with error %s: '%s' on server %s. The full response is %s", extractErrorCodeAndName(response), extractErrorMessage(response), address, getResponseAsJson(response)), address); this.response = response; for (BsonValue curErrorLabel : response.getArray("errorLabels", new BsonArray())) { addLabel(curErrorLabel.asString().getValue()); } }
/** * For internal use only: extract the error code from the response to a write command. * @param response the response * @return the code, or -1 if there is none */ public static int extractErrorCode(final BsonDocument response) { // mongos may set an err field containing duplicate key error information String errorMessage = extractErrorMessage(response); if (errorMessage != null) { if (response.containsKey("err")) { if (errorMessage.contains("E11000 duplicate key error")) { return 11000; } } // mongos may return a list of documents representing write command responses from each shard. Return the one with a matching // "err" field, so that it can be used to get the error code if (!response.containsKey("code") && response.containsKey("errObjects")) { for (BsonValue curErrorDocument : response.getArray("errObjects")) { if (errorMessage.equals(extractErrorMessage(curErrorDocument.asDocument()))) { return curErrorDocument.asDocument().getNumber("code").intValue(); } } } } return response.getNumber("code", new BsonInt32(-1)).intValue(); }
public static ServerDescription createServerDescription(final ServerAddress serverAddress, final BsonDocument isMasterResult, final ServerVersion serverVersion, final long roundTripTime) { return ServerDescription.builder() .state(CONNECTED) .version(serverVersion) .address(serverAddress) .type(getServerType(isMasterResult)) .canonicalAddress(isMasterResult.containsKey("me") ? isMasterResult.getString("me").getValue() : null) .hosts(listToSet(isMasterResult.getArray("hosts", new BsonArray()))) .passives(listToSet(isMasterResult.getArray("passives", new BsonArray()))) .arbiters(listToSet(isMasterResult.getArray("arbiters", new BsonArray()))) .primary(getString(isMasterResult, "primary")) .maxDocumentSize(getMaxBsonObjectSize(isMasterResult)) .tagSet(getTagSetFromDocument(isMasterResult.getDocument("tags", new BsonDocument()))) .setName(getString(isMasterResult, "setName")) .minWireVersion(getMinWireVersion(isMasterResult)) .maxWireVersion(getMaxWireVersion(isMasterResult)) .electionId(getElectionId(isMasterResult)) .setVersion(getSetVersion(isMasterResult)) .lastWriteDate(getLastWriteDate(isMasterResult)) .roundTripTime(roundTripTime, NANOSECONDS) .logicalSessionTimeoutMinutes(getLogicalSessionTimeoutMinutes(isMasterResult)) .ok(CommandHelper.isCommandOk(isMasterResult)).build(); }
@SuppressWarnings("unchecked") private BsonArray getCursorDocuments(final BsonDocument result) { return result.getArray("cursors"); }
private static List<String> getCompressors(final BsonDocument isMasterResult) { List<String> compressorList = new ArrayList<String>(); for (BsonValue compressor : isMasterResult.getArray("compression", new BsonArray())) { compressorList.add(compressor.asString().getValue()); } return compressorList; }
private static List<ServerAddress> hostsFrom(ServerHeartbeatSucceededEvent event) { return event.getReply().getArray("hosts", new BsonArray()).stream() .map(bsonValue -> new ServerAddress(bsonValue.asString().getValue())) .collect(Collectors.toList()); }
private static List<ServerAddress> hostsFrom(ServerHeartbeatSucceededEvent event) { return event.getReply().getArray("hosts", new BsonArray()).stream() .map(bsonValue -> new ServerAddress(bsonValue.asString().getValue())) .collect(Collectors.toList()); }
/** * * @param linkArrayRef * @return the created or existing link array */ public BsonArray addLinkArray(String linkArrayRef) { if (!links.containsKey(linkArrayRef)) { links.append(linkArrayRef, new BsonArray()); } BsonArray linkArray = links.getArray(linkArrayRef); return linkArray; }
@SuppressWarnings("unchecked") private List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) { BsonArray upsertedValue = result.getArray("upserted", new BsonArray()); List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>(); for (BsonValue upsertedItem : upsertedValue) { BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem; bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id"))); } return bulkWriteUpsertList; }
@Override public Boolean apply(final BsonDocument result, final ServerAddress serverAddress) { return result.get("users").isArray() && !result.getArray("users").isEmpty(); } };
static ServerVersion getVersion(final BsonDocument buildInfoResult) { List<BsonValue> versionArray = buildInfoResult.getArray("versionArray").subList(0, 3); return new ServerVersion(asList(versionArray.get(0).asInt32().getValue(), versionArray.get(1).asInt32().getValue(), versionArray.get(2).asInt32().getValue())); }
/** * * @param rel * @param rep */ public void addRepresentation(String rel, Representation rep) { if (!embedded.containsKey(rel)) { embedded.append(rel, new BsonArray()); } BsonArray repArray = embedded.getArray(rel); repArray.add(rep.asBsonDocument()); }
Authenticator getAuthenticatorFromIsMasterResult(final BsonDocument isMasterResult, final ConnectionDescription connectionDescription) { if (isMasterResult.containsKey("saslSupportedMechs")) { BsonArray saslSupportedMechs = isMasterResult.getArray("saslSupportedMechs"); AuthenticationMechanism mechanism = saslSupportedMechs.contains(DEFAULT_MECHANISM_NAME) ? SCRAM_SHA_256 : SCRAM_SHA_1; return new ScramShaAuthenticator(getMongoCredentialWithCache().withMechanism(mechanism)); } else { return getLegacyDefaultAuthenticator(connectionDescription); } }