Example usage for com.mongodb BasicDBObjectBuilder get

List of usage examples for com.mongodb BasicDBObjectBuilder get

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Gets the top level document.

Usage

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> unwind() {
    BasicDBObject unwind = new BasicDBObject("$unwind", "$tags");

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.add("_id", "$tags");
    group.push("count");
    group.add("$sum", 1);
    group.pop();//from w w  w .j  a  va  2  s  . c  o m
    group.pop();

    BasicDBObjectBuilder sort = new BasicDBObjectBuilder();
    sort.push("$sort");
    sort.add("count", -1);
    sort.pop();

    BasicDBObject limit = new BasicDBObject("$limit", 10);

    BasicDBObjectBuilder project = new BasicDBObjectBuilder();
    project.push("$project");
    project.add("_id", 0);
    project.add("tag", "$_id");
    project.add("count", 1);

    return col.aggregate(unwind, group.get(), sort.get(), limit, project.get()).results().iterator();
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> doubleUnwind() {

    BasicDBObject unwindSizes = new BasicDBObject("$unwind", "$sizes");
    BasicDBObject unwindColors = new BasicDBObject("$unwind", "$colors");

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.push("_id");
    group.add("size", "$sizes");
    group.add("color", "$colors");
    group.pop();//from  ww w  .  j  a  va2  s  .c o m
    group.push("count");
    group.add("$sum", 1);
    group.pop();
    group.pop();

    return col.aggregate(unwindSizes, unwindColors, group.get()).results().iterator();
}

From source file:com.exorath.service.connector.service.MongoDatabaseProvider.java

License:Apache License

@Override
public ServerInfo getServerInfo(Filter filter, Long minLastUpdate) {
    //Fingers crossed
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    if (filter.getGameId() != null)
        builder.append("gameId", filter.getGameId());
    if (filter.getMapId() != null)
        builder.append("mapId", filter.getMapId());
    if (filter.getFlavorId() != null)
        builder.append("flavorId", filter.getFlavorId());
    builder.append("expiry", new BasicDBObject("$gte", System.currentTimeMillis()));
    MapReduceCommand mapReduceCommand = new MapReduceCommand(datastore.getDB().getCollection(collectionName),
            "function(){" + "var ret = {pc:0, opc:0, sc:0, osc:0};" + "ret.pc = this.pc; ret.sc = 1;"
                    + "if(this.joinable && this.pc < this.mpc){ ret.opc = this.mpc - this.pc; ret.osc = 1;}"
                    + "emit('server', ret);}",
            "function(key, values){" + "var ret = {pc:0, opc:0, sc:0, osc:0};"
                    + "values.forEach( function(value) {" + "       ret.pc+= value.pc; ret.sc++;"
                    + "       ret.osc+= value.osc; ret.opc+= value.opc" + "   });" + "return ret;" + "}",
            null, MapReduceCommand.OutputType.INLINE, builder.get());
    MapReduceOutput results = datastore.getDB().getCollection(collectionName).mapReduce(mapReduceCommand);
    if (results.getOutputCount() == 0)
        return new ServerInfo(0, 0, 0, 0, System.currentTimeMillis());
    if (results.getOutputCount() > 1)
        throw new IllegalStateException("mapReduce returned multiple results.");
    for (DBObject res : results.results()) {
        DBObject val = (DBObject) res.get("value");
        return new ServerInfo(((Double) val.get("pc")).intValue(), ((Double) val.get("sc")).intValue(),
                ((Double) val.get("osc")).intValue(), ((Double) val.get("opc")).intValue(),
                System.currentTimeMillis());
    }// ww w . jav  a2 s .  c om
    ////         MapreduceResults<ServerInfo> results = datastore.mapReduce(MapreduceType.INLINE, getFilterQuery(filter), ServerInfo.class, mapReduceCommand);
    ////        if(results.getCounts().getOutputCount() == 0) {
    ////            System.out.println("output 0 :*");
    //            return null;
    //        }
    //        System.out.println("ms: " + results.getElapsedMillis());
    //        results.forEach(info -> System.out.println(info.getOpenSlotCount()));
    return null;
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

@SuppressWarnings("static-access")
private static void replaceIdProperty(BasicDBObjectBuilder qResult, SpaceTypeDescriptor typeDescriptor) {

    DBObject q = qResult.get();

    if (q.containsField(typeDescriptor.getIdPropertyName())) {

        Object value = q.get(typeDescriptor.getIdPropertyName());

        q.removeField(typeDescriptor.getIdPropertyName());

        q.put(Constants.ID_PROPERTY, value);

        qResult.start(q.toMap());//from  ww w.  j a  v  a 2  s .c  o  m
    }
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

private static BasicDBObjectBuilder replaceParameters(Object[] parameters, SpaceDocumentMapper<DBObject> mapper,
        BasicDBObjectBuilder builder, Integer index) {

    BasicDBObjectBuilder newBuilder = BasicDBObjectBuilder.start();

    DBObject document = builder.get();

    Iterator<String> iterator = document.keySet().iterator();

    while (iterator.hasNext()) {
        String field = iterator.next();
        Object ph = document.get(field);

        if (index >= parameters.length)
            return builder;

        if (ph instanceof String) {
            if (PARAM_PLACEHOLDER.equals(ph)) {
                Object p = mapper.toObject(parameters[index++]);

                if (p instanceof DBObject
                        && Constants.CUSTOM_BINARY.equals(((DBObject) p).get(Constants.TYPE))) {
                    newBuilder.add(field + "." + Constants.HASH, ((DBObject) p).get(Constants.HASH));
                } else {
                    newBuilder.add(field, p);
                }/*from   w  w  w . j  a va2s.  c o m*/
            }
        } else if (ph instanceof Pattern) {
            Pattern p = (Pattern) ph;

            if (LIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, convertLikeExpression((String) parameters[index++]));
            } else if (RLIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, Pattern.compile((String) parameters[index++], Pattern.CASE_INSENSITIVE));
            }
        } else {
            DBObject element = (DBObject) ph;

            Object p = mapper.toObject(parameters[index]);

            if (p instanceof DBObject) {
                String t = (String) ((DBObject) p).get(Constants.TYPE);
                String op = element.keySet().iterator().next();

                if (Constants.CUSTOM_BINARY.equals(t) && !op.equals("$ne"))
                    return newBuilder;
            }

            BasicDBObjectBuilder doc = replaceParameters(parameters, mapper,
                    BasicDBObjectBuilder.start(element.toMap()), index);

            newBuilder.add(field, doc.get());
        }
    }
    return newBuilder;
}

From source file:com.gigaspaces.persistency.datasource.MongoSqlQueryDataIterator.java

License:Open Source License

private void init() {
    DBCollection collection = client.getCollection(query.getTypeDescriptor().getTypeName());

    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();

    logger.debug(query);/*from w  w  w .ja v  a2  s .  co  m*/

    if (query.supportsAsSQLQuery())
        q = MongoQueryFactory.create(query);
    else if (query.supportsTemplateAsDocument()) {
        Map m = new DefaultSpaceDocumentMapper(query.getTypeDescriptor())
                .toDBObject(query.getTemplateAsDocument()).toMap();

        q = BasicDBObjectBuilder.start(m);
    }

    cursor = collection.find(q.get());
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private DBObject toDBObjectDocument(SpaceDocument document) {
    BasicDBObjectBuilder bson = BasicDBObjectBuilder.start();

    Set<String> keys = document.getProperties().keySet();

    bson.add(Constants.TYPE, document.getTypeName());

    for (String property : keys) {

        Object value = document.getProperty(property);

        if (value == null)
            continue;

        if (spaceTypeDescriptor.getIdPropertyName().equals(property))
            property = Constants.ID_PROPERTY;

        bson.add(property, toObject(value));
    }/*from  ww w  .j  av  a  2 s .  c o  m*/

    return bson.get();
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

private DBObject toDBObjectPojo(Object pojo) {

    BasicDBObjectBuilder bson = BasicDBObjectBuilder.start();

    Map<String, Method> getters = repository.getGetters(pojo.getClass());

    Class<?> type = pojo.getClass();

    bson.add(Constants.TYPE, type.getName());

    for (String property : getters.keySet()) {
        Object value = null;/* w  w  w  . ja v  a 2  s  .  c  o m*/
        try {

            value = repository.getGetter(type, property).get(pojo);

            if (value == null)
                continue;

            if (spaceTypeDescriptor.getIdPropertyName().equals(property))
                property = Constants.ID_PROPERTY;

            bson.add(property, toObject(value));

        } catch (IllegalArgumentException e) {
            throw new SpaceMongoException("Argument is: " + value, e);
        } catch (IllegalAccessException e) {
            throw new SpaceMongoException("Can not access method", e);
        } catch (InvocationTargetException e) {
            throw new SpaceMongoException("Can not invoke method", e);
        }
    }

    return bson.get();
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

License:Open Source License

public Object toObject(Object property) {
    if (property == null)
        return null;

    switch (type(property.getClass())) {

    case TYPE_CHAR:
    case TYPE_FLOAT:
    case TYPE_BYTE:
    case TYPE_BIGDECIMAL:
    case TYPE_BIGINT:
        return toSpecialType(property);
    case TYPE_OBJECT:
        if (property instanceof SpaceDocument)
            return toDBObject((SpaceDocument) property);
        else if (property instanceof Class)
            return toSpecialType(property);
        else if (property instanceof Locale)
            return toSpecialType(property);
        else if (property instanceof URI)
            return toSpecialType(property);
        else if (property instanceof Timestamp)
            return toSpecialType(property);

        if (!(property instanceof Serializable))
            return toDBObject(property);

        byte[] result = serializeObject(property);

        BasicDBObjectBuilder blob = BasicDBObjectBuilder.start();

        blob.add(Constants.TYPE, Constants.CUSTOM_BINARY);

        blob.add(Constants.VALUE, result);

        blob.add(Constants.HASH, Arrays.hashCode(result));

        return blob.get();
    case TYPE_ENUM:
        return toEnum(property);
    case TYPE_ARRAY:
        return toArray(property);
    case TYPE_COLLECTION:
        return toCollection(property);
    case TYPE_MAP:
        return toMap(property);
    default:/*  w  w w .j  a v  a  2 s  .c o  m*/
        return property;
    }
}

From source file:com.gigaspaces.persistency.metadata.IndexBuilder.java

License:Open Source License

private void createIndex(String typeSimpleName, String routing, SpaceIndexType type,
        BasicDBObjectBuilder option) {
    DBCollection c = client.getCollection(typeSimpleName);

    DBObject key;//  w  w w.  j  a  v  a 2  s.  co m

    if (type == SpaceIndexType.BASIC) {
        key = BasicDBObjectBuilder.start(routing, "hashed").get();
    } else {
        key = BasicDBObjectBuilder.start(routing, 1).get();
    }

    c.createIndex(key, option.get());
}