Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:info.bunji.mongodb.synces.CollectionExtractor.java

License:Apache License

/**
 **********************************//from www.ja va 2s.  co m
 * ??????.
 * <br>
 * ????????
 * @return ????
 **********************************
 */
private Set<String> getTargetColectionList(MongoDatabase db) {
    Set<String> collectionSet = config.getImportCollections();
    if (collectionSet.isEmpty()) {
        // ??????(???)
        MongoCursor<String> it = db.listCollectionNames().iterator();
        while (it.hasNext()) {
            String name = it.next();
            if (!name.startsWith("system.")) {
                collectionSet.add(name);
            }
        }
    }
    return collectionSet;
}

From source file:io.djigger.collector.accessors.InstrumentationEventAccessor.java

License:Open Source License

private Iterator<InstrumentationEvent> query(Bson query) {
    final MongoCursor<Document> documents = instrumentationEventsCollection.find(query).iterator();
    return new Iterator<InstrumentationEvent>() {

        @Override/* ww w .  ja  v a2 s  .  c  o  m*/
        public boolean hasNext() {
            return documents.hasNext();
        }

        @Override
        public InstrumentationEvent next() {
            Document doc = documents.next();
            InstrumentationEvent event = fromDocument(doc);
            return event;
        }

        private InstrumentationEvent fromDocument(Document doc) {

            InstrumentationEvent event;
            if (doc.containsKey("stacktrace")) {
                event = new InstrumentationEventWithThreadInfo(doc.getString("class"), doc.getString("method"));

                StackTraceElement[] stacktrace = fromDBObject(doc.get("stacktrace"));
                ThreadInfo info = new ThreadInfo(stacktrace);
                ((InstrumentationEventWithThreadInfo) event).setThreadInfo(info);
            } else {
                event = new InstrumentationEvent(doc.getString("class"), doc.getString("method"));
            }

            event.setStart(doc.getDate("start").getTime());
            event.setDuration(doc.getLong("duration"));
            event.setId(doc.getObjectId("_id"));
            event.setThreadID(doc.getLong("threadid"));
            event.setParentID(doc.getObjectId("parentid"));
            //            event.setTransactionID((UUID) doc.get("trid"));
            event.setTransactionID(UUID.fromString(doc.getString("trid")));

            if (doc.containsKey("data")) {
                List<?> array = (List<?>) doc.get("data");
                List<InstrumentationEventData> list = new LinkedList<InstrumentationEventData>();
                for (Object value : array) {
                    list.add(new StringInstrumentationEventData((String) value));
                }
                event.setData(list);
            }

            return event;
        }

        private StackTraceElement[] fromDBObject(Object o) {
            @SuppressWarnings("unchecked")
            List<List<Object>> l = (List<List<Object>>) o;
            StackTraceElement[] s = new StackTraceElement[l.size()];
            for (int i = 0; i < l.size(); i++) {
                List<Object> e = (List<Object>) l.get(i);
                s[i] = new StackTraceElement((String) e.get(0), (String) e.get(1), (String) e.get(2),
                        (int) e.get(3));
            }
            return s;
        }

        @Override
        public void remove() {
            throw new RuntimeException("Not implemented");
        }
    };
}

From source file:io.djigger.collector.accessors.MetricAccessor.java

License:Open Source License

private Iterator<Metric<?>> query(Bson query) {
    final MongoCursor<Document> documents = metricsCollection.find(query).iterator();
    return new Iterator<Metric<?>>() {

        @Override//w w  w.  j ava  2s.  c om
        public boolean hasNext() {
            return documents.hasNext();
        }

        @Override
        public Metric<?> next() {
            Document doc = documents.next();
            Metric<?> event = fromDocument(doc);
            return event;
        }

        @Override
        public void remove() {
            throw new RuntimeException("Not implemented");
        }
    };
}

From source file:io.lumeer.storage.mongodb.dao.collection.MongoDataDao.java

License:Open Source License

@Override
public DataDocument getData(final String collectionId, final String documentId) {
    MongoCursor<Document> mongoCursor = dataCollection(collectionId).find(idFilter(documentId)).iterator();
    if (!mongoCursor.hasNext()) {
        throw new ResourceNotFoundException(ResourceType.DOCUMENT);
    }//w  ww. ja  v  a2  s  . com
    return MongoUtils.convertDocument(mongoCursor.next());
}

From source file:io.lumeer.storage.mongodb.dao.project.MongoViewDao.java

License:Open Source License

@Override
public View getViewByCode(final String code) {
    MongoCursor<JsonView> mongoCursor = databaseCollection().find(codeFilter(code)).iterator();
    if (!mongoCursor.hasNext()) {
        throw new ResourceNotFoundException(ResourceType.VIEW);
    }//w w w .  j av  a  2 s  .  c  o  m
    return mongoCursor.next();
}

From source file:io.mandrel.blob.impl.MongoBlobStore.java

License:Apache License

@Override
public void byPages(int pageSize, Callback callback) {
    MongoCursor<GridFSFile> cursor = bucket.find().iterator();
    boolean loop = true;
    try {//from   ww  w .j ava2 s .  co  m
        while (loop) {
            List<GridFSFile> files = new ArrayList<>(batchSize);
            int i = 0;
            while (cursor.hasNext() && i < batchSize) {
                files.add(cursor.next());
                i++;
            }
            loop = callback.on(files.stream().map(file -> bucket.openDownloadStream(file.getObjectId()))
                    .map(fromFile).collect(Collectors.toList()));
        }
    } finally {
        cursor.close();
    }
}

From source file:io.mandrel.document.impl.MongoDocumentStore.java

License:Apache License

@Override
public void byPages(int pageSize, Callback callback) {
    MongoCursor<org.bson.Document> cursor = collection.find().iterator();
    boolean loop = true;
    try {/*  ww  w.  j  a va2s .  co  m*/
        while (loop) {
            List<org.bson.Document> docs = new ArrayList<>(batchSize);
            int i = 0;
            while (cursor.hasNext() && i < batchSize) {
                docs.add(cursor.next());
                i++;
            }
            loop = callback.on(docs.stream().map(fromBson).collect(Collectors.toList()));
        }
    } finally {
        cursor.close();
    }
}

From source file:io.mandrel.document.impl.MongoDocumentStore.java

License:Apache License

@Override
public Collection<Document> byPages(int pageSize, int pageNumber) {
    MongoCursor<org.bson.Document> cursor = collection.find().skip(pageSize * pageNumber).limit(pageSize)
            .iterator();/*  w  ww.j  a va 2  s .co  m*/
    List<org.bson.Document> docs = new ArrayList<>(10);
    while (cursor.hasNext()) {
        docs.add(cursor.next());
    }
    return docs.stream().map(fromBson).collect(Collectors.toList());
}

From source file:io.mandrel.metadata.impl.MongoMetadataStore.java

License:Apache License

@Override
public void byPages(int pageSize, Callback callback) {
    MongoCursor<org.bson.Document> cursor = collection.find().iterator();
    boolean loop = true;
    try {/*  w  w  w.ja  va2s  .c  o  m*/
        while (loop) {
            List<org.bson.Document> docs = new ArrayList<>(batchSize);
            int i = 0;
            while (cursor.hasNext() && i < batchSize) {
                docs.add(cursor.next());
                i++;
            }
            loop = callback.on(docs.stream().map(doc -> JsonBsonCodec.fromBson(mapper, doc, BlobMetadata.class))
                    .collect(Collectors.toList()));
        }
    } finally {
        cursor.close();
    }
}

From source file:io.mandrel.timeline.impl.MongoTimelineRepository.java

License:Apache License

@Override
public void pool(Listener listener) {

    LocalDateTime date = LocalDateTime.now();
    Bson query = Filters.gt("time", date);

    try {/*from   w ww . j  a v  a  2s.c o  m*/
        while (true) {
            MongoCursor<Document> cursor = timeline.find(query).cursorType(CursorType.TailableAwait).iterator();

            while (true) {
                if (!cursor.hasNext()) {
                    if (cursor.getServerCursor() == null) {
                        break;
                    }
                    continue;
                }

                Document result = cursor.next();
                try {
                    Event event = mapper.readValue(result.toJson(), Event.class);
                    date = event.getTime();

                    listener.on(event);
                } catch (Exception e) {
                    log.warn("Error while getting the event", e);
                }
            }

            query = Filters.gt("time", date);
        }
    } catch (Exception e) {
        log.warn("Event pool process is down!", e);
    }
}