Search in sources :

Example 1 with MinKey

use of org.bson.types.MinKey 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 MinKey

use of org.bson.types.MinKey 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 MinKey

use of org.bson.types.MinKey in project bson4jackson by michel-kraemer.

the class BsonParserTest method parseMinKey.

/**
 * Check if a MinKey can be parsed. Refers issue #51
 * @throws Exception if something goes wrong
 */
@Test
public void parseMinKey() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("A", new MinKey());
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(1, data.size());
    assertEquals("MinKey", data.get("A"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) MinKey(org.bson.types.MinKey) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 4 with MinKey

use of org.bson.types.MinKey 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 5 with MinKey

use of org.bson.types.MinKey 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)

Aggregations

MinKey (org.bson.types.MinKey)17 MaxKey (org.bson.types.MaxKey)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)7 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