private BsonDocument asGetMoreCommandDocument() { return new BsonDocument(COMMAND_NAME, new BsonInt64(cursorId)) .append("collection", new BsonString(namespace.getCollectionName())) .append("batchSize", new BsonInt32(numberToReturn)); }
@Override public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) { BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext); BsonValue id = bsonDocument.get("_id"); String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue(); long length = bsonDocument.getNumber("length").longValue(); int chunkSize = bsonDocument.getNumber("chunkSize").intValue(); Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue()); String md5 = bsonDocument.containsKey("md5") ? bsonDocument.getString("md5").getValue() : null; BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument()); Document optionalMetadata = asDocumentOrNull(metadataBsonDocument); for (String key : VALID_FIELDS) { bsonDocument.remove(key); } Document deprecatedExtraElements = asDocumentOrNull(bsonDocument); return new GridFSFile(id, filename, length, chunkSize, uploadDate, md5, optionalMetadata, deprecatedExtraElements); }
static String getErrorMessage(final BsonDocument response, final String errorMessageFieldName) { return response.getString(errorMessageFieldName, new BsonString("")).getValue(); }
public static WriteConcernError createWriteConcernError(final BsonDocument writeConcernErrorDocument) { return new WriteConcernError(writeConcernErrorDocument.getNumber("code").intValue(), writeConcernErrorDocument.getString("codeName", new BsonString("")).getValue(), writeConcernErrorDocument.getString("errmsg").getValue(), writeConcernErrorDocument.getDocument("errInfo", new BsonDocument())); }
private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) { if (unstripped == null) { return null; } List<T> stripped = new ArrayList<T>(unstripped.size()); String prefix = databaseName + "."; for (BsonDocument cur : unstripped) { String name = cur.getString("name").getValue(); String collectionName = name.substring(prefix.length()); cur.put("name", new BsonString(collectionName)); stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build())); } return stripped; } }
public static BsonDocument getAuthCommand(final String userName, final String authHash, final String nonce) { String key = nonce + userName + authHash; BsonDocument cmd = new BsonDocument(); cmd.put("authenticate", new BsonInt32(1)); cmd.put("user", new BsonString(userName)); cmd.put("nonce", new BsonString(nonce)); cmd.put("key", new BsonString(hexMD5(key.getBytes(UTF_8_CHARSET)))); return cmd; }
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; } }
switch (value.getBsonType()) { case INT32: return value.asInt32().getValue(); case INT64: return value.asInt64().getValue(); case STRING: return value.asString().getValue(); case DECIMAL128: return value.asDecimal128().doubleValue(); return value.asDouble().getValue(); case BOOLEAN: return value.asBoolean().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); return value.asSymbol().getSymbol(); case ARRAY: return value.asArray().toArray(); case DOCUMENT: return Document.parse(value.asDocument().toJson()); default: return value;
colValue = keyvalueforStruct.getValue().asString().getValue().toString(); break; colValue = keyvalueforStruct.getValue().asObjectId().getValue().toString(); break; colValue = keyvalueforStruct.getValue().asInt32().getValue(); break; colValue = keyvalueforStruct.getValue().asInt64().getValue(); break; colValue = keyvalueforStruct.getValue().asBoolean().getValue(); break; BsonDocument jwsDoc = keyvalueforStruct.getValue().asJavaScriptWithScope().getScope().asDocument(); String temp = arrValue.asString().getValue(); int temp = arrValue.asInt32().getValue(); long temp = arrValue.asInt64().getValue(); long temp = arrValue.asInt64().getValue(); int temp = arrValue.asInt32().getValue(); boolean temp = arrValue.asBoolean().getValue();
private BsonDocument asCommandDocument() { BsonArray array = new BsonArray(); for (long cursor : cursors) { array.add(new BsonInt64(cursor)); } return new BsonDocument(COMMAND_NAME, namespace == null ? new BsonInt32(1) : new BsonString(namespace.getCollectionName())) .append("cursors", array); }
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; } }
break; case STRING: writeString(value.asString().getValue()); break; case BINARY: break; case BOOLEAN: writeBoolean(value.asBoolean().getValue()); break; case DATE_TIME: break; case INT32: writeInt32(value.asInt32().getValue()); break; case TIMESTAMP: break; case INT64: writeInt64(value.asInt64().getValue()); break; case DECIMAL128:
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; }
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) { BsonDocument document = new BsonDocument(); BsonDocument transformedFilter = null; if (filter != null) { if (filter.containsKey("name")) { if (!filter.isString("name")) { throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string"); } transformedFilter = new BsonDocument(); transformedFilter.putAll(filter); transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue()))); } else { transformedFilter = filter; } } BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$")); BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter))); document.put("$query", query); if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) { document.put("$readPreference", readPreference.toDocument()); } if (maxTimeMS > 0) { document.put("$maxTimeMS", new BsonInt64(maxTimeMS)); } return document; }
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)); }
TypeAdapters.JSON_ELEMENT.write(new BsonWriter(new BsonDocumentWriter(doc)), obj); check(doc.keySet()).notEmpty(); check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN); check(doc.get("boolean").asBoolean()); check(doc.get("int32").getBsonType()).is(BsonType.INT32); check(doc.get("int32").asInt32().getValue()).is(32); check(doc.get("int64").getBsonType()).is(BsonType.INT64); check(doc.get("int64").asInt64().getValue()).is(64L); check(doc.get("double").getBsonType()).is(BsonType.DOUBLE); check(doc.get("double").asDouble().getValue()).is(42.42D); check(doc.get("string").getBsonType()).is(BsonType.STRING); check(doc.get("string").asString().getValue()).is("foo"); check(doc.get("null").getBsonType()).is(BsonType.NULL); check(doc.get("null").isNull());
private BsonDocument asKillCursorsCommandDocument() { return new BsonDocument("killCursors", new BsonString(namespace.getCollectionName())) .append("cursors", new BsonArray(singletonList(new BsonInt64(serverCursor.getId())))); } }
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("comment", new BsonString(comment)); commandDocument.put("maxScan", new BsonInt64(maxScan));
@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", getUpdate()); if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connectionDescription)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } addWriteConcernToCommand(connectionDescription, commandDocument, sessionContext); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } if (arrayFilters != null) { commandDocument.put("arrayFilters", new BsonArray(arrayFilters)); } addTxnNumberToCommand(serverDescription, connectionDescription, commandDocument, sessionContext); return commandDocument; } };
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; }