use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class BasicBSONEncoder method _putObjectField.
/**
* Encodes any Object type
*
* @param name the field name
* @param initialValue the value to write
*/
protected void _putObjectField(final String name, final Object initialValue) {
if ("_transientFields".equals(name)) {
return;
}
if (name.contains("\0")) {
throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
}
if ("$where".equals(name) && initialValue instanceof String) {
putCode(name, new Code((String) initialValue));
}
Object value = BSON.applyEncodingHooks(initialValue);
if (value == null) {
putNull(name);
} else if (value instanceof Date) {
putDate(name, (Date) value);
} else if (value instanceof Number) {
putNumber(name, (Number) value);
} else if (value instanceof Decimal128) {
putDecimal128(name, (Decimal128) value);
} else if (value instanceof Character) {
putString(name, value.toString());
} else if (value instanceof String) {
putString(name, value.toString());
} else if (value instanceof ObjectId) {
putObjectId(name, (ObjectId) value);
} else if (value instanceof Boolean) {
putBoolean(name, (Boolean) value);
} else if (value instanceof Pattern) {
putPattern(name, (Pattern) value);
} else if (value instanceof Iterable) {
putIterable(name, (Iterable) value);
} else if (value instanceof BSONObject) {
putObject(name, (BSONObject) value);
} else if (value instanceof Map) {
putMap(name, (Map) value);
} else if (value instanceof byte[]) {
putBinary(name, (byte[]) value);
} else if (value instanceof Binary) {
putBinary(name, (Binary) value);
} else if (value instanceof UUID) {
putUUID(name, (UUID) value);
} else if (value.getClass().isArray()) {
putArray(name, value);
} else if (value instanceof Symbol) {
putSymbol(name, (Symbol) value);
} else if (value instanceof BSONTimestamp) {
putTimestamp(name, (BSONTimestamp) value);
} else if (value instanceof CodeWScope) {
putCodeWScope(name, (CodeWScope) value);
} else if (value instanceof Code) {
putCode(name, (Code) value);
} else if (value instanceof DBRef) {
BSONObject temp = new BasicBSONObject();
DBRef dbRef = (DBRef) value;
temp.put("$ref", dbRef.getCollectionName());
temp.put("$id", dbRef.getId());
if (dbRef.getDatabaseName() != null) {
temp.put("$db", dbRef.getDatabaseName());
}
putObject(name, temp);
} else if (value instanceof MinKey) {
putMinKey(name);
} else if (value instanceof MaxKey) {
putMaxKey(name);
} else if (putSpecial(name, value)) {
// no-op
} else {
throw new IllegalArgumentException("Can't serialize " + value.getClass());
}
}
use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.class, found.get("pattern").getClass());
assertEquals(MinKey.class, found.get("minKey").getClass());
assertEquals(MaxKey.class, found.get("maxKey").getClass());
assertEquals(Code.class, found.get("js").getClass());
assertEquals(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.types.MaxKey in project mongo-hadoop by mongodb.
the class UDFTest method setUpClass.
@BeforeClass
public static void setUpClass() {
INPUT_COLLECTION.drop();
insertedDocuments = new ArrayList<Document>(100);
for (int i = 0; i < 100; ++i) {
ObjectId id = new ObjectId();
insertedDocuments.add(new Document("_id", id).append("minkey", new MinKey()).append("maxkey", new MaxKey()).append("dbref", new DBRef("othercollection", new ObjectId())).append("binary", new Binary(new byte[] { 1, 2, 3, 4, 5 })).append("oidBytes", new Binary(id.toByteArray())));
}
INPUT_COLLECTION.insertMany(insertedDocuments);
}
use of org.bson.types.MaxKey in project drill by apache.
the class MongoGroupScan method init.
@SuppressWarnings({ "rawtypes" })
private void init() {
List<String> h = storagePluginConfig.getHosts();
List<ServerAddress> addresses = Lists.newArrayList();
for (String host : h) {
addresses.add(new ServerAddress(host));
}
MongoClient client = storagePlugin.getClient();
chunksMapping = Maps.newHashMap();
chunksInverseMapping = Maps.newLinkedHashMap();
if (useAggregate && isShardedCluster(client)) {
handleUnshardedCollection(getPrimaryShardInfo());
} else if (isShardedCluster(client)) {
MongoDatabase db = client.getDatabase(CONFIG);
MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS);
Document filter = new Document();
filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName());
Document projection = new Document();
projection.put(SHARD, SELECT);
projection.put(MIN, SELECT);
projection.put(MAX, SELECT);
FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection);
MongoCursor<Document> iterator = chunkCursor.iterator();
MongoCollection<Document> shardsCollection = db.getCollection(SHARDS);
projection = new Document();
projection.put(HOST, SELECT);
boolean hasChunks = false;
while (iterator.hasNext()) {
Document chunkObj = iterator.next();
String shardName = (String) chunkObj.get(SHARD);
// creates hexadecimal string representation of ObjectId
String chunkId = chunkObj.get(ID).toString();
filter = new Document(ID, shardName);
FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection);
for (Document hostObj : hostCursor) {
String hostEntry = (String) hostObj.get(HOST);
String[] tagAndHost = StringUtils.split(hostEntry, '/');
String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ',');
Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses));
if (addressList == null) {
addressList = Sets.newHashSet();
for (String host : hosts) {
addressList.add(new ServerAddress(host));
}
}
chunksMapping.put(chunkId, addressList);
ServerAddress address = addressList.iterator().next();
List<ChunkInfo> chunkList = chunksInverseMapping.computeIfAbsent(address.getHost(), k -> new ArrayList<>());
List<String> chunkHostsList = new ArrayList<>();
for (ServerAddress serverAddr : addressList) {
chunkHostsList.add(serverAddr.toString());
}
ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId);
Document minMap = (Document) chunkObj.get(MIN);
Map<String, Object> minFilters = Maps.newHashMap();
Set keySet = minMap.keySet();
for (Object keyObj : keySet) {
Object object = minMap.get(keyObj);
if (!(object instanceof MinKey)) {
minFilters.put(keyObj.toString(), object);
}
}
chunkInfo.setMinFilters(minFilters);
Map<String, Object> maxFilters = Maps.newHashMap();
Map maxMap = (Document) chunkObj.get(MAX);
keySet = maxMap.keySet();
for (Object keyObj : keySet) {
Object object = maxMap.get(keyObj);
if (!(object instanceof MaxKey)) {
maxFilters.put(keyObj.toString(), object);
}
}
chunkInfo.setMaxFilters(maxFilters);
chunkList.add(chunkInfo);
}
hasChunks = true;
}
// unsharded collection and it will be stored in the primary shard of that database.
if (!hasChunks) {
handleUnshardedCollection(getPrimaryShardInfo());
}
} else {
handleUnshardedCollection(storagePluginConfig.getHosts());
}
}
use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class JsonReader method readBsonType.
// CHECKSTYLE:OFF
@Override
public BsonType readBsonType() {
if (isClosed()) {
throw new IllegalStateException("This instance has been closed");
}
if (getState() == State.INITIAL || getState() == State.DONE || getState() == State.SCOPE_DOCUMENT) {
// in JSON the top level value can be of any type so fall through
setState(State.TYPE);
}
if (getState() != State.TYPE) {
throwInvalidState("readBSONType", State.TYPE);
}
if (getContext().getContextType() == BsonContextType.DOCUMENT) {
JsonToken nameToken = popToken();
switch(nameToken.getType()) {
case STRING:
case UNQUOTED_STRING:
setCurrentName(nameToken.getValue(String.class));
break;
case END_OBJECT:
setState(State.END_OF_DOCUMENT);
return BsonType.END_OF_DOCUMENT;
default:
throw new JsonParseException("JSON reader was expecting a name but found '%s'.", nameToken.getValue());
}
JsonToken colonToken = popToken();
if (colonToken.getType() != JsonTokenType.COLON) {
throw new JsonParseException("JSON reader was expecting ':' but found '%s'.", colonToken.getValue());
}
}
JsonToken token = popToken();
if (getContext().getContextType() == BsonContextType.ARRAY && token.getType() == JsonTokenType.END_ARRAY) {
setState(State.END_OF_ARRAY);
return BsonType.END_OF_DOCUMENT;
}
boolean noValueFound = false;
switch(token.getType()) {
case BEGIN_ARRAY:
setCurrentBsonType(BsonType.ARRAY);
break;
case BEGIN_OBJECT:
visitExtendedJSON();
break;
case DOUBLE:
setCurrentBsonType(BsonType.DOUBLE);
currentValue = token.getValue();
break;
case END_OF_FILE:
setCurrentBsonType(BsonType.END_OF_DOCUMENT);
break;
case INT32:
setCurrentBsonType(BsonType.INT32);
currentValue = token.getValue();
break;
case INT64:
setCurrentBsonType(BsonType.INT64);
currentValue = token.getValue();
break;
case REGULAR_EXPRESSION:
setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
currentValue = token.getValue();
break;
case STRING:
setCurrentBsonType(BsonType.STRING);
currentValue = token.getValue();
break;
case UNQUOTED_STRING:
String value = token.getValue(String.class);
if ("false".equals(value) || "true".equals(value)) {
setCurrentBsonType(BsonType.BOOLEAN);
currentValue = Boolean.parseBoolean(value);
} else if ("Infinity".equals(value)) {
setCurrentBsonType(BsonType.DOUBLE);
currentValue = Double.POSITIVE_INFINITY;
} else if ("NaN".equals(value)) {
setCurrentBsonType(BsonType.DOUBLE);
currentValue = Double.NaN;
} else if ("null".equals(value)) {
setCurrentBsonType(BsonType.NULL);
} else if ("undefined".equals(value)) {
setCurrentBsonType(BsonType.UNDEFINED);
} else if ("MinKey".equals(value)) {
visitEmptyConstructor();
setCurrentBsonType(BsonType.MIN_KEY);
currentValue = new MinKey();
} else if ("MaxKey".equals(value)) {
visitEmptyConstructor();
setCurrentBsonType(BsonType.MAX_KEY);
currentValue = new MaxKey();
} else if ("BinData".equals(value)) {
setCurrentBsonType(BsonType.BINARY);
currentValue = visitBinDataConstructor();
} else if ("Date".equals(value)) {
currentValue = visitDateTimeConstructorWithOutNew();
setCurrentBsonType(BsonType.STRING);
} else if ("HexData".equals(value)) {
setCurrentBsonType(BsonType.BINARY);
currentValue = visitHexDataConstructor();
} else if ("ISODate".equals(value)) {
setCurrentBsonType(BsonType.DATE_TIME);
currentValue = visitISODateTimeConstructor();
} else if ("NumberInt".equals(value)) {
setCurrentBsonType(BsonType.INT32);
currentValue = visitNumberIntConstructor();
} else if ("NumberLong".equals(value)) {
setCurrentBsonType(BsonType.INT64);
currentValue = visitNumberLongConstructor();
} else if ("NumberDecimal".equals(value)) {
setCurrentBsonType(BsonType.DECIMAL128);
currentValue = visitNumberDecimalConstructor();
} else if ("ObjectId".equals(value)) {
setCurrentBsonType(BsonType.OBJECT_ID);
currentValue = visitObjectIdConstructor();
} else if ("Timestamp".equals(value)) {
setCurrentBsonType(BsonType.TIMESTAMP);
currentValue = visitTimestampConstructor();
} else if ("RegExp".equals(value)) {
setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
currentValue = visitRegularExpressionConstructor();
} else if ("DBPointer".equals(value)) {
setCurrentBsonType(BsonType.DB_POINTER);
currentValue = visitDBPointerConstructor();
} else if ("UUID".equals(value)) {
setCurrentBsonType(BsonType.BINARY);
currentValue = visitUUIDConstructor();
} else if ("new".equals(value)) {
visitNew();
} else {
noValueFound = true;
}
break;
default:
noValueFound = true;
break;
}
if (noValueFound) {
throw new JsonParseException("JSON reader was expecting a value but found '%s'.", token.getValue());
}
if (getContext().getContextType() == BsonContextType.ARRAY || getContext().getContextType() == BsonContextType.DOCUMENT) {
JsonToken commaToken = popToken();
if (commaToken.getType() != JsonTokenType.COMMA) {
pushToken(commaToken);
}
}
switch(getContext().getContextType()) {
case DOCUMENT:
case SCOPE_DOCUMENT:
default:
setState(State.NAME);
break;
case ARRAY:
case JAVASCRIPT_WITH_SCOPE:
case TOP_LEVEL:
setState(State.VALUE);
break;
}
return getCurrentBsonType();
}
Aggregations