Example usage for com.mongodb BasicDBObject putAll

List of usage examples for com.mongodb BasicDBObject putAll

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject putAll.

Prototype

@SuppressWarnings("unchecked")
    @Override
    public void putAll(final Map m) 

Source Link

Usage

From source file:com.ikanow.infinit.e.utility.MongoEntityFeatureTxfer.java

License:Apache License

private void doChunkedTransfer(BasicDBObject query, int nSkip, int nLimit, String chunksDescription)
        throws IOException {
    List<BasicDBObject> chunkList = MongoIndexerUtils.getChunks("feature.entity", chunksDescription);
    System.out.println("CHUNKS: Found " + chunkList.size() + " chunks");
    //DEBUG//  w  w  w  . j ava  2 s.c  om
    //System.out.println("Chunklist= " + chunkList);
    for (BasicDBObject chunk : chunkList) {
        BasicDBObject cleanQuery = new BasicDBObject();
        cleanQuery.putAll((BSONObject) query);
        String id = null;
        try {
            id = (String) chunk.remove("$id");
            System.out.println("CHUNK: " + id);
            doTransfer(cleanQuery, 0, 0, chunk);
        } catch (Exception e) {
            System.out.println("FAILED CHUNK: " + id + " ... " + e.getMessage());
        }
    }
}

From source file:com.linuxbox.enkive.statistics.services.retrieval.mongodb.MongoStatsRetrievalService.java

License:Open Source License

@Override
public List<Map<String, Object>> queryStatistics(Map<String, Map<String, Object>> queryMap,
        Map<String, Map<String, Object>> filterMap) throws StatsRetrievalException {
    Set<DBObject> allStats = new HashSet<DBObject>();
    for (String gathererName : queryMap.keySet()) {
        BasicDBObject query = new BasicDBObject();
        query.put(STAT_GATHERER_NAME, gathererName);
        query.putAll(queryMap.get(gathererName));
        if (filterMap.get(gathererName) != null && !filterMap.get(gathererName).isEmpty()) {
            BasicDBObject filter = new BasicDBObject(filterMap.get(gathererName));
            allStats.addAll(coll.find(query, filter)
                    .sort(new BasicDBObject(STAT_TIMESTAMP + "." + CONSOLIDATION_MAX, 1)).toArray());
        } else {/*from  www  .  j  av a  2  s  . c  o  m*/
            allStats.addAll(coll.find(query)
                    .sort(new BasicDBObject(STAT_TIMESTAMP + "." + CONSOLIDATION_MAX, 1)).toArray());
        }
    }
    List<Map<String, Object>> result = createListOfMaps();
    for (DBObject entry : allStats) {
        addMapToList(entry, result);
    }
    return result;
}

From source file:com.linuxbox.util.queueservice.mongodb.MongoQueueService.java

License:Open Source License

@Override
public QueueEntry dequeue(String identifer) throws QueueServiceException {
    final BasicDBObject query = new BasicDBObject();
    query.putAll(QUERY_ENQUEUED_STATUS);
    query.append(IDENTIFIER_FIELD, identifer);
    return dequeueHelper(query);
}

From source file:com.mlveda.dori.Webhook.java

public ArrayList<Collection> getCollectionList() {

    MongoDatabase db = MongoProvider.getInstance();

    //        MongoCollection<Product> products = db.getCollection("disneytoys.myshopify.com_products", Product.class);
    BasicDBObject document = new BasicDBObject();
    QueryBuilder qb = new QueryBuilder();
    qb.or(new QueryBuilder().put("handle").notEquals("frontpage").get(),
            new QueryBuilder().put("published_at").notEquals(null).get());
    document.putAll(qb.get());

    FindIterable<Document> coll = db.getCollection("dakshal.myshopify.com_collections")
            .find(and(ne("title", "Frontpage"), ne("published_at", null)));
    //        FindIterable<Document> coll = db.getCollection("disneytoys.myshopify.com_collection").find(document);

    //        FindIterable<Document> foundDocument = coll.find(and(ne("title", "Frontpage"), ne("published_at", null)));
    final Type collectionType = new TypeToken<ArrayList<Collection>>() {
    }.getType();/* w  w w .  j  a  va  2  s . c o m*/

    final ArrayList<Collection> collection = new ArrayList<>();

    System.out.println();
    System.out.println();

    System.out.println("document: " + document);
    coll.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {

            collection.add(gson.fromJson(gson.toJson(document), Collection.class));

            //                System.out.println("document: " + document);
            //                ArrayList<Collection> c = gson.fromJson(gson.toJson(document.get("collections")), collectionType);
            //                System.out.println("collection: " + c);
            //                if (!c.isEmpty()) {
            //                    collection.addAll(c);
            //                }
            //                collection.add(t);
        }
    });
    System.out.println("Collection: " + gson.toJson(collection));
    System.out.println();
    System.out.println();

    return collection;
}

From source file:com.mulesoft.quartz.mongo.MongoDBJobStore.java

License:Open Source License

protected ObjectId storeJobInMongo(JobDetail newJob, boolean replaceExisting)
        throws ObjectAlreadyExistsException {
    JobKey key = newJob.getKey();//from   w  ww.  j  a v  a  2  s  .  com

    BasicDBObject job = keyAsDBObject(key);

    if (replaceExisting) {
        DBObject result = jobCollection.findOne(job);
        if (result != null) {
            result = job;
        }
    }

    job.put(JOB_KEY_NAME, key.getName());
    job.put(JOB_KEY_GROUP, key.getGroup());
    job.put(JOB_DESCRIPTION, newJob.getDescription());
    job.put(JOB_CLASS, newJob.getJobClass().getName());

    job.putAll(newJob.getJobDataMap());

    try {
        jobCollection.insert(job);

        return (ObjectId) job.get("_id");
    } catch (DuplicateKey e) {
        throw new ObjectAlreadyExistsException(e.getMessage());
    }
}

From source file:com.mysema.query.mongodb.MongodbSerializer.java

License:Apache License

@Override
public Object visit(Operation<?> expr, Void context) {
    Operator<?> op = expr.getOperator();
    if (op == Ops.EQ) {
        return asDBObject(asDBKey(expr, 0), asDBValue(expr, 1));

    } else if (op == Ops.STRING_IS_EMPTY) {
        return asDBObject(asDBKey(expr, 0), "");

    } else if (op == Ops.AND) {
        BasicDBObject left = (BasicDBObject) handle(expr.getArg(0));
        left.putAll((BSONObject) handle(expr.getArg(1)));
        return left;

    } else if (op == Ops.NOT) {
        //Handle the not's child
        BasicDBObject arg = (BasicDBObject) handle(expr.getArg(0));

        //Only support the first key, let's see if there
        //is cases where this will get broken
        String key = arg.keySet().iterator().next();

        Operator<?> subOp = ((Operation<?>) expr.getArg(0)).getOperator();
        if (subOp != Ops.EQ && subOp != Ops.STRING_IS_EMPTY) {
            return asDBObject(key, asDBObject("$not", arg.get(key)));
        } else {//from  w  w w .j av a 2  s  . c  om
            return asDBObject(key, asDBObject("$ne", arg.get(key)));
        }

    } else if (op == Ops.OR) {
        BasicDBList list = new BasicDBList();
        list.add(handle(expr.getArg(0)));
        list.add(handle(expr.getArg(1)));
        return asDBObject("$or", list);

    } else if (op == Ops.NE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$ne", asDBValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile("^" + regexValue(expr, 1), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.ENDS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$"));

    } else if (op == Ops.ENDS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.EQ_IGNORE_CASE) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile("^" + regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.STRING_CONTAINS) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*"));

    } else if (op == Ops.STRING_CONTAINS_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(".*" + regexValue(expr, 1) + ".*", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.MATCHES) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString()));

    } else if (op == Ops.MATCHES_IC) {
        return asDBObject(asDBKey(expr, 0),
                Pattern.compile(asDBValue(expr, 1).toString(), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.LIKE) {
        String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regex));

    } else if (op == Ops.BETWEEN) {
        BasicDBObject value = new BasicDBObject("$gte", asDBValue(expr, 1));
        value.append("$lte", asDBValue(expr, 2));
        return asDBObject(asDBKey(expr, 0), value);

    } else if (op == Ops.IN) {
        int constIndex = 0;
        int exprIndex = 1;
        if (expr.getArg(1) instanceof Constant<?>) {
            constIndex = 1;
            exprIndex = 0;
        }
        if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
            Collection<?> values = (Collection<?>) ((Constant<?>) expr.getArg(constIndex)).getConstant();
            return asDBObject(asDBKey(expr, exprIndex), asDBObject("$in", values.toArray()));
        } else {
            return asDBObject(asDBKey(expr, exprIndex), asDBValue(expr, constIndex));
        }

    } else if (op == Ops.LT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lt", asDBValue(expr, 1)));

    } else if (op == Ops.GT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gt", asDBValue(expr, 1)));

    } else if (op == Ops.LOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lte", asDBValue(expr, 1)));

    } else if (op == Ops.GOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gte", asDBValue(expr, 1)));

    } else if (op == Ops.IS_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", false));

    } else if (op == Ops.IS_NOT_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", true));

    } else if (op == MongodbOps.NEAR) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$near", asDBValue(expr, 1)));

    } else if (op == MongodbOps.ELEM_MATCH) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$elemMatch", asDBValue(expr, 1)));
    }

    throw new UnsupportedOperationException("Illegal operation " + expr);
}

From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java

License:Open Source License

protected ObjectId storeJobInMongo(JobDetail newJob, boolean replaceExisting)
        throws ObjectAlreadyExistsException {
    JobKey key = newJob.getKey();/* w w w .j  a v a 2 s .  c  o m*/

    BasicDBObject keyDbo = keyToDBObject(key);
    BasicDBObject job = keyToDBObject(key);

    job.put(KEY_NAME, key.getName());
    job.put(KEY_GROUP, key.getGroup());
    job.put(JOB_DESCRIPTION, newJob.getDescription());
    job.put(JOB_CLASS, newJob.getJobClass().getName());
    job.put(JOB_DURABILITY, newJob.isDurable());

    job.putAll(newJob.getJobDataMap());

    DBObject object = jobCollection.findOne(keyDbo);

    ObjectId objectId = null;

    if (object != null && replaceExisting) {
        jobCollection.update(keyDbo, job);
    } else if (object == null) {
        try {
            jobCollection.insert(job);
            objectId = (ObjectId) job.get("_id");
        } catch (DuplicateKey e) {
            // Fine, find it and get its id.
            object = jobCollection.findOne(keyDbo);
            objectId = (ObjectId) object.get("_id");
        }
    } else {
        objectId = (ObjectId) object.get("_id");
    }

    return objectId;
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Returns the number of documents in the given collection (taking into
 * account the filters in case)./*from w  ww  .ja  va 2  s  .c  o m*/
 *
 * @param coll the mongodb DBCollection object.
 * @param filters the filters to apply. it is a Deque collection of mongodb
 * query conditions.
 * @return the number of documents in the given collection (taking into
 * account the filters in case)
 */
public static long getCollectionSize(DBCollection coll, Deque<String> filters) {
    final BasicDBObject query = new BasicDBObject(DOCUMENTS_QUERY);

    if (filters != null) {
        try {
            filters.stream().forEach(f -> {
                query.putAll((BSONObject) JSON.parse(f)); // this can throw JSONParseException for invalid filter parameters
            });
        } catch (JSONParseException jpe) {
            logger.warn("****** error parsing filter expression {}", filters, jpe);
        }
    }

    return coll.count(query);
}

From source file:com.softinstigate.restheart.db.CollectionDAO.java

License:Open Source License

/**
 * Returs the documents of the collection applying, sorting, pagination and
 * filtering./*from ww w. j a v  a  2s  .c o  m*/
 *
 * @param coll the mongodb DBCollection object
 * @param page the page number
 * @param pagesize the size of the page
 * @param sortBy the Deque collection of fields to use for sorting (prepend
 * field name with - for descending sorting)
 * @param filters the filters to apply. it is a Deque collection of mongodb
 * query conditions.
 * @return
 * @throws JSONParseException
 */
public static ArrayList<DBObject> getCollectionData(DBCollection coll, int page, int pagesize,
        Deque<String> sortBy, Deque<String> filters) throws JSONParseException {
    // apply sort_by
    DBObject sort = new BasicDBObject();

    if (sortBy == null || sortBy.isEmpty()) {
        sort.put("_id", 1);
    } else {
        sortBy.stream().forEach((sf) -> {
            sf = sf.replaceAll("_lastupdated_on", "_etag"); // _lastupdated is not stored and actually generated from @tag

            if (sf.startsWith("-")) {
                sort.put(sf.substring(1), -1);
            } else if (sf.startsWith("+")) {
                sort.put(sf.substring(1), -1);
            } else {
                sort.put(sf, 1);
            }
        });
    }

    // apply filter
    final BasicDBObject query = new BasicDBObject(DOCUMENTS_QUERY);

    if (filters != null) {
        filters.stream().forEach((String f) -> {
            BSONObject filterQuery = (BSONObject) JSON.parse(f);
            query.putAll(filterQuery); // this can throw JSONParseException for invalid filter parameters
        });
    }

    ArrayList<DBObject> data = getDataFromCursor(
            coll.find(query).sort(sort).limit(pagesize).skip(pagesize * (page - 1)));

    data.forEach(row -> {
        Object etag = row.get("_etag");

        if (etag != null && ObjectId.isValid("" + etag)) {
            ObjectId _etag = new ObjectId("" + etag);

            row.put("_lastupdated_on", Instant.ofEpochSecond(_etag.getTimestamp()).toString());
        }
    });

    return data;
}

From source file:com.softinstigate.restheart.db.DBDAO.java

License:Open Source License

/**
 * @param dbName/* w w  w  .  j  av  a 2s.  c o m*/
 * @param colls the collections list as got from getDbCollections()
 * @param page
 * @param pagesize
 * @return the db data
 * @throws
 * com.softinstigate.restheart.handlers.IllegalQueryParamenterException
 *
 */
public static List<DBObject> getData(String dbName, List<String> colls, int page, int pagesize)
        throws IllegalQueryParamenterException {
    // filter out reserved resources
    List<String> _colls = colls.stream().filter(coll -> !RequestContext.isReservedResourceCollection(coll))
            .collect(Collectors.toList());

    int size = _colls.size();

    // *** arguments check
    long total_pages;

    if (size > 0) {
        float _size = size + 0f;
        float _pagesize = pagesize + 0f;

        total_pages = Math.max(1, Math.round(Math.ceil(_size / _pagesize)));

        if (page > total_pages) {
            throw new IllegalQueryParamenterException(
                    "illegal query paramenter, page is bigger that total pages which is " + total_pages);
        }
    }

    // apply page and pagesize
    _colls = _colls.subList((page - 1) * pagesize,
            (page - 1) * pagesize + pagesize > _colls.size() ? _colls.size()
                    : (page - 1) * pagesize + pagesize);

    List<DBObject> data = new ArrayList<>();

    _colls.stream().map((collName) -> {
        BasicDBObject properties = new BasicDBObject();

        properties.put("_id", collName);

        DBObject collProperties;

        if (LocalCachesSingleton.isEnabled()) {
            collProperties = LocalCachesSingleton.getInstance().getCollectionProps(dbName, collName);
        } else {
            collProperties = CollectionDAO.getCollectionProps(dbName, collName);
        }

        if (collProperties != null) {
            properties.putAll(collProperties);
        }

        return properties;
    }).forEach((item) -> {
        data.add(item);
    });

    return data;
}