Example usage for com.mongodb.async SingleResultCallback SingleResultCallback

List of usage examples for com.mongodb.async SingleResultCallback SingleResultCallback

Introduction

In this page you can find the example usage for com.mongodb.async SingleResultCallback SingleResultCallback.

Prototype

SingleResultCallback

Source Link

Usage

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void getDocumentViaFilter(final String collectionName, Criteria criteria,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);
    collection.find(criteria.getRestrictions()).first(new SingleResultCallback<Document>() {
        @Override/*  w  w w. j  a v a2 s.c o m*/
        public void onResult(final Document document, final Throwable t) {
            onDocumentResult(document, t, "success", callback);
        }
    });
}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

void Recurse(final int index, final String entityName, final Criteria criteria, final String field,
        final String subField, final List<String> subFieldValues, final ResultCallback callback) {

    if (index == -1) {
        callback.onSuccess("");
        return;//from  www  .j  a  v a  2  s .c om
    }

    MongoCollection<Document> collection = database.getCollection(entityName);

    collection.updateOne(criteria.getRestrictions(),
            pull(field, new Document(subField, subFieldValues.get(index))),
            new SingleResultCallback<UpdateResult>() {

                @Override
                public void onResult(final UpdateResult result, final Throwable t) {

                    if (t == null) {
                        if (result.getModifiedCount() > 0) {

                            Recurse(index - 1, entityName, criteria, field, subField, subFieldValues, callback);

                        } else {
                            callback.onError(errorMessage);
                        }
                    } else {
                        callback.onError(t.toString());
                        t.printStackTrace();
                    }

                }

            });

}

From source file:net.modelbased.proasense.storage.writer.EventWriterMongoAsync.java

License:Apache License

public void run() {
    // Connect to MongoDB database
    MongoClient mongoClient = MongoClients.create(mongoURL);
    MongoDatabase database = mongoClient.getDatabase(EventProperties.STORAGE_DATABASE_NAME);

    // Create hash map of collections
    Map<String, MongoCollection<Document>> collectionMap = new HashMap<String, MongoCollection<Document>>();

    int cnt = 0;//from  w  ww .  j  a  v a2 s  .c om
    long timer0 = System.currentTimeMillis();
    long timer1 = timer0;
    long timer2 = timer0;
    boolean skipLog = false;

    Map<String, List<Document>> documentMap = new HashMap<String, List<Document>>();
    try {
        if (isLogfile)
            logfileWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("EventWriterMongoAsync_benchmark_" + this.threadNumber + ".txt"),
                    "ISO-8859-1"));

        while (true) {
            long timeoutExpired = System.currentTimeMillis() + this.maxWait;
            EventDocument eventDocument = queue.take();

            String collectionId = eventDocument.getCollectionId();

            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                cnt++;
                skipLog = false;
            } else
                skipLog = true;

            // Add data for bulk write
            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                Document document = eventDocument.getDocument();

                if (!collectionMap.containsKey(collectionId)) {
                    collectionMap.put(collectionId, database.getCollection(collectionId));
                    List<Document> documentList = new ArrayList<Document>();
                    documentMap.put(collectionId, documentList);
                }

                documentMap.get(collectionId).add(document);
            }
            // Write data if heartbeat timeout is received
            else {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key),
                                new SingleResultCallback<Void>() {
                                    //                                @Override
                                    public void onResult(final Void result, final Throwable t) {
                                    }
                                });
                        documentMap.get(key).clear();
                    }
                }
            }

            // Write data if bulk size or max wait (ms) is reached
            if ((cnt % this.bulkSize == 0) || (System.currentTimeMillis() >= timeoutExpired)) {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key),
                                new SingleResultCallback<Void>() {
                                    //                                @Override
                                    public void onResult(final Void result, final Throwable t) {
                                    }
                                });
                        documentMap.get(key).clear();
                    }
                }
            }

            // Benchmark output
            if ((!skipLog) && (cnt % this.logSize == 0)) {
                timer2 = System.currentTimeMillis();
                long difference = timer2 - timer1;

                if (difference != 0) {
                    long average = (this.logSize * 1000) / (timer2 - timer1);

                    System.out.println("Benchmark: ");
                    System.out.println("  Records written  : " + cnt);
                    System.out.println("  Average records/s: " + average);
                    timer1 = timer2;

                    if (isLogfile) {
                        logfileWriter.write(cnt + "," + average + System.getProperty("line.separator"));
                        logfileWriter.flush();
                    }

                    if (cnt == this.loadTestMaxMessages) {
                        long loadTestStart = timer0 / 1000;
                        long loadTestEnd = timer2 / 1000;
                        long loadTestTime = loadTestEnd - loadTestStart;
                        long loadTestAverage = this.loadTestMaxMessages / loadTestTime;

                        System.out.println("*****************************");
                        System.out.println("Load test results: ");
                        System.out.println("  Records written  : " + cnt);
                        System.out.println("  Average records/s: " + loadTestAverage);

                        if (isLogfile) {
                            logfileWriter.write(
                                    "*****************************" + System.getProperty("line.separator"));
                            logfileWriter.write("Load test results: " + System.getProperty("line.separator"));
                            logfileWriter.write(
                                    "  Records written  : " + cnt + System.getProperty("line.separator"));
                            logfileWriter.write("  Average records/s: " + loadTestAverage
                                    + System.getProperty("line.separator"));
                            logfileWriter.flush();
                        }
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } finally {
        if (isLogfile)
            try {
                logfileWriter.close();
            } catch (IOException e) {
                System.out.println(e.getClass().getName() + ": " + e.getMessage());
            }
    }

    mongoClient.close();

}