Search in sources :

Example 1 with MaxKey

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());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 2 with MaxKey

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);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) CodeWithScope(org.bson.types.CodeWithScope) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) Code(org.bson.types.Code) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 3 with MaxKey

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);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) DBRef(com.mongodb.DBRef) Binary(org.bson.types.Binary) Document(org.bson.Document) BeforeClass(org.junit.BeforeClass)

Example 4 with MaxKey

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());
    }
}
Also used : Document(org.bson.Document) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DrillbitEndpoint(org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint) PriorityQueue(java.util.PriorityQueue) LoggerFactory(org.slf4j.LoggerFactory) Sets(org.apache.drill.shaded.guava.com.google.common.collect.Sets) MongoDatabase(com.mongodb.client.MongoDatabase) VisibleForTesting(org.apache.drill.shaded.guava.com.google.common.annotations.VisibleForTesting) StringUtils(org.apache.commons.lang3.StringUtils) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) Map(java.util.Map) Joiner(org.apache.drill.shaded.guava.com.google.common.base.Joiner) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) MongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.MongoSubScanSpec) MaxKey(org.bson.types.MaxKey) SchemaPath(org.apache.drill.common.expression.SchemaPath) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Entry(java.util.Map.Entry) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) Preconditions(org.apache.drill.shaded.guava.com.google.common.base.Preconditions) Queue(java.util.Queue) ReadPreference(com.mongodb.ReadPreference) StoragePluginRegistry(org.apache.drill.exec.store.StoragePluginRegistry) MongoClient(com.mongodb.client.MongoClient) PlanStringBuilder(org.apache.drill.common.PlanStringBuilder) MongoCollection(com.mongodb.client.MongoCollection) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) AbstractGroupScan(org.apache.drill.exec.physical.base.AbstractGroupScan) ShardedMongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.ShardedMongoSubScanSpec) ArrayList(java.util.ArrayList) GroupScanProperty(org.apache.drill.exec.physical.base.ScanStats.GroupScanProperty) Bson(org.bson.conversions.Bson) Maps(org.apache.drill.shaded.guava.com.google.common.collect.Maps) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoCursor(com.mongodb.client.MongoCursor) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) MinKey(org.bson.types.MinKey) DocumentCodec(org.bson.codecs.DocumentCodec) LinkedList(java.util.LinkedList) ServerAddress(com.mongodb.ServerAddress) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ScanStats(org.apache.drill.exec.physical.base.ScanStats) TimeUnit(java.util.concurrent.TimeUnit) Lists(org.apache.drill.shaded.guava.com.google.common.collect.Lists) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) EndpointAffinity(org.apache.drill.exec.physical.EndpointAffinity) GroupScan(org.apache.drill.exec.physical.base.GroupScan) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) Set(java.util.Set) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MaxKey(org.bson.types.MaxKey) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) MinKey(org.bson.types.MinKey) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MongoCursor(com.mongodb.client.MongoCursor) Map(java.util.Map) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 5 with MaxKey

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();
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey)

Aggregations

MaxKey (org.bson.types.MaxKey)16 MinKey (org.bson.types.MinKey)16 Binary (org.bson.types.Binary)8 Date (java.util.Date)7 Document (org.bson.Document)7 Code (org.bson.types.Code)7 ObjectId (org.bson.types.ObjectId)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 BSONTimestamp (org.bson.types.BSONTimestamp)5 CodeWScope (org.bson.types.CodeWScope)5 DBRef (com.mongodb.DBRef)4 List (java.util.List)4 Map (java.util.Map)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 ServerAddress (com.mongodb.ServerAddress)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 String (java.lang.String)2 SimpleDateFormat (java.text.SimpleDateFormat)2