List of usage examples for com.mongodb.client MongoCursor next
@Override TResult next();
From source file:org.restcom.stats.core.service.GaugeService.java
License:Open Source License
public List<GaugeDTO> retrieveSumMetrics(long fromTime, long toTime, String key) { List<GaugeDTO> gauges = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "null").append("totalCount", new Document("$sum", "$count")))); //exec query/* w ww .jav a 2 s . c o m*/ MongoCursor<Document> result = dbm.getCollection(MetricType.GAUGE.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); gauges.add(new GaugeDTO(toTime, statsDoc.getInteger("totalCount"))); } return gauges; }
From source file:org.restcom.stats.core.service.HistogramService.java
License:Open Source License
public List<HistogramDTO> retrieveMetrics(long fromTime, long toTime, String key) { List<HistogramDTO> histograms = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "$timestamp").append("totalCount", new Document("$sum", "$count")))); //define order criteria params.add(new Document("$sort", new Document("_id", 1))); //exec query/*from w w w. j av a 2s. c o m*/ MongoCursor<Document> result = dbm.getCollection(MetricType.HISTOGRAM.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); histograms.add(new HistogramDTO(statsDoc.getLong("_id"), statsDoc.getInteger("totalCount"))); } return histograms; }
From source file:org.restcom.stats.core.service.HistogramService.java
License:Open Source License
public List<HistogramDTO> retrieveSumMetrics(long fromTime, long toTime, String key) { List<HistogramDTO> histograms = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "null").append("totalCount", new Document("$sum", "$count")))); //exec query//w ww. ja v a 2 s . co m MongoCursor<Document> result = dbm.getCollection(MetricType.HISTOGRAM.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); histograms.add(new HistogramDTO(toTime, statsDoc.getInteger("totalCount"))); } return histograms; }
From source file:org.restcom.stats.core.service.MeterService.java
License:Open Source License
public List<MeterDTO> retrieveMetrics(long fromTime, long toTime, String key) { List<MeterDTO> meters = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "$timestamp").append("totalCount", new Document("$sum", "$count")))); //define order criteria params.add(new Document("$sort", new Document("_id", 1))); //exec query/*from www .j a va 2s . co m*/ MongoCursor<Document> result = dbm.getCollection(MetricType.METER.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); meters.add(new MeterDTO(statsDoc.getLong("_id"), statsDoc.getInteger("totalCount"))); } return meters; }
From source file:org.restcom.stats.core.service.MeterService.java
License:Open Source License
public List<MeterDTO> retrieveSumMetrics(long fromTime, long toTime, String key) { List<MeterDTO> meters = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "null").append("totalCount", new Document("$sum", "$count")))); //exec query// w w w. java2 s. com MongoCursor<Document> result = dbm.getCollection(MetricType.METER.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); meters.add(new MeterDTO(toTime, statsDoc.getInteger("totalCount"))); } return meters; }
From source file:org.restcom.stats.core.service.MetricEventService.java
License:Open Source License
/** * Retrieve metric status from interval timestamps. * @param fromTime timestamp from./*from w w w.jav a2 s . c o m*/ * @param toTime timestemp to. * @param metricType Metric type. * @return metric status. */ public MetricStatusDTO restrieveStatus(long fromTime, long toTime, MetricType metricType) { MetricStatusDTO status = new MetricStatusDTO(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); //define grouping criteria params.add( new Document("$group", new Document("_id", "status").append("totalEvents", new Document("$sum", 1)) .append("lastEvent", new Document("$max", "$timestamp")))); //exec query MongoCursor<Document> result = dbm.getCollection(metricType.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto if (result.hasNext()) { Document statsDoc = result.next(); status.setTotalEvents(statsDoc.getInteger("totalEvents")); status.setTimestamp(new Date(statsDoc.getLong("lastEvent"))); } return status; }
From source file:org.restcom.stats.core.service.MetricEventService.java
License:Open Source License
/** * Retrieve metric keys./*from w w w . j a va 2 s. c om*/ * @param metricType Metric type. * @return metric keys. */ public List<String> retrieveMetricKeys(MetricType metricType) { List<String> keys = new ArrayList<>(); //retrieve disctinct key values MongoCursor<String> result = dbm.getCollection(metricType.getCollectionName()).distinct("key", String.class) .iterator(); while (result.hasNext()) { keys.add(result.next()); } return keys; }
From source file:org.restcom.stats.core.service.TimerService.java
License:Open Source License
public List<TimerDTO> retrieveMetrics(long fromTime, long toTime, String key) { List<TimerDTO> timers = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "$timestamp").append("totalCount", new Document("$sum", "$count")))); //define order criteria params.add(new Document("$sort", new Document("_id", 1))); //exec query/*w w w .ja v a 2 s . c om*/ MongoCursor<Document> result = dbm.getCollection(MetricType.TIMER.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); timers.add(new TimerDTO(statsDoc.getLong("_id"), statsDoc.getInteger("totalCount"))); } return timers; }
From source file:org.restcom.stats.core.service.TimerService.java
License:Open Source License
public List<TimerDTO> retrieveSumMetrics(long fromTime, long toTime, String key) { List<TimerDTO> timers = new ArrayList<>(); //create params list List<Bson> params = new ArrayList<>(); //define match criteria params.add(new Document("$match", new Document("timestamp", new Document("$gte", fromTime)))); params.add(new Document("$match", new Document("timestamp", new Document("$lte", toTime)))); params.add(new Document("$match", new Document("key", key))); //define grouping criteria params.add(new Document("$group", new Document("_id", "null").append("totalCount", new Document("$sum", "$count")))); //exec query/*w w w. jav a2 s .c o m*/ MongoCursor<Document> result = dbm.getCollection(MetricType.TIMER.getCollectionName()).aggregate(params) .iterator(); //convert document result into dto while (result.hasNext()) { Document statsDoc = result.next(); timers.add(new TimerDTO(toTime, statsDoc.getInteger("totalCount"))); } return timers; }
From source file:org.restheart.db.CollectionDAO.java
License:Open Source License
ArrayList<BsonDocument> getCollectionData(final MongoCollection<BsonDocument> coll, final int page, final int pagesize, final BsonDocument sortBy, final BsonDocument filters, final BsonDocument keys, CursorPool.EAGER_CURSOR_ALLOCATION_POLICY eager) throws JSONParseException { ArrayList<BsonDocument> ret = new ArrayList<>(); int toskip = pagesize * (page - 1); SkippedFindIterable _cursor = null;/* w w w . j av a2 s . c o m*/ if (eager != CursorPool.EAGER_CURSOR_ALLOCATION_POLICY.NONE) { _cursor = CursorPool.getInstance().get(new CursorPoolEntryKey(coll, sortBy, filters, keys, toskip, 0), eager); } int _pagesize = pagesize; // in case there is not cursor in the pool to reuse FindIterable<BsonDocument> cursor; if (_cursor == null) { cursor = getFindIterable(coll, sortBy, filters, keys); cursor.skip(toskip); MongoCursor<BsonDocument> mc = cursor.iterator(); while (_pagesize > 0 && mc.hasNext()) { ret.add(mc.next()); _pagesize--; } } else { int alreadySkipped; cursor = _cursor.getFindIterable(); alreadySkipped = _cursor.getAlreadySkipped(); long startSkipping = 0; int cursorSkips = alreadySkipped; if (LOGGER.isDebugEnabled()) { startSkipping = System.currentTimeMillis(); } LOGGER.debug("got cursor from pool with skips {}. " + "need to reach {} skips.", alreadySkipped, toskip); MongoCursor<BsonDocument> mc = cursor.iterator(); while (toskip > alreadySkipped && mc.hasNext()) { mc.next(); alreadySkipped++; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("skipping {} times took {} msecs", toskip - cursorSkips, System.currentTimeMillis() - startSkipping); } while (_pagesize > 0 && mc.hasNext()) { ret.add(mc.next()); _pagesize--; } } // the pool is populated here because, skipping with cursor.next() is heavy operation // and we want to minimize the chances that pool cursors are allocated in parallel CursorPool.getInstance().populateCache(new CursorPoolEntryKey(coll, sortBy, filters, keys, toskip, 0), eager); return ret; }