Example usage for com.mongodb ReadConcernLevel fromString

List of usage examples for com.mongodb ReadConcernLevel fromString

Introduction

In this page you can find the example usage for com.mongodb ReadConcernLevel fromString.

Prototype

public static ReadConcernLevel fromString(final String readConcernLevel) 

Source Link

Document

Returns the ReadConcern from the string read concern level.

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value find(Value request) throws FaultException {
    Value v = Value.create();// w w w.j av  a2  s.  c  o m
    FindIterable<BsonDocument> iterable = null;
    ;
    try {

        String collectionName = request.getFirstChild("collection").strValue();
        MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
        if (request.hasChildren("readConcern")) {

            ReadConcern readConcern = new ReadConcern(
                    ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
            collection.withReadConcern(readConcern);
        }

        if (request.hasChildren("filter")) {
            BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
            prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
            printlnJson("Query filter", bsonQueryDocument);
            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery);
            }

            if (request.hasChildren("sort") && request.hasChildren("limit") && request.hasChildren("skip")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                int skipPosition = request.getFirstChild("skip").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery)
                        .skip(skipPosition);
            }

            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).limit(limitQuery);

            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find(bsonQueryDocument);
            }

        } else {

            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int findLimit = request.getFirstChild("limit").intValue();

                iterable = collection.find(new Document()).sort(bsonSortDocument); ///.sort(bsonSortDocument).limit(limitQuery);
            }
            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(new Document()).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(new Document()).limit(limitQuery);
            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find();
            }

        }
        iterable.forEach(new Block<BsonDocument>() {
            @Override
            public void apply(BsonDocument t) {
                Value queryValue = processQueryRow(t);
                printlnJson("Query document", t);
                v.getChildren("document").add(queryValue);
            }
        });
        showLog();
    } catch (MongoException ex) {
        showLog();

        throw new FaultException("MongoException", ex);

    } catch (JsonParseException ex) {
        showLog();
        throw new FaultException("JsonParseException", ex);
    }

    return v;
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value aggregate(Value request) throws FaultException {
    Value v = Value.create();/*from   w  w  w  .j a  v a2  s  .co m*/
    ArrayList<BsonDocument> bsonAggreagationDocuments = new ArrayList<>();
    String collectionName = request.getFirstChild("collection").strValue();
    MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
    if (request.hasChildren("readConcern")) {
        ReadConcern readConcern = new ReadConcern(
                ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
        collection.withReadConcern(readConcern);
    }
    for (int counter = 0; counter < request.getChildren("filter").size(); counter++) {
        BsonDocument bsonAggregationDocument = BsonDocument
                .parse(request.getChildren("filter").get(counter).strValue());
        prepareBsonQueryData(bsonAggregationDocument, request.getChildren("filter").get(counter));
        printlnJson("Aggregate filter", bsonAggregationDocument);
        bsonAggreagationDocuments.add(bsonAggregationDocument);
    }
    AggregateIterable<BsonDocument> aggregation = db.getCollection(collectionName)
            .aggregate(bsonAggreagationDocuments, BsonDocument.class).allowDiskUse(Boolean.TRUE);
    aggregation.forEach(new Block<BsonDocument>() {
        @Override
        public void apply(BsonDocument t) {
            printlnJson("Aggregate Document", t);
            Value queryResult = processQueryRow(t);
            v.getChildren("document").add(queryResult);
        }
    });

    return v;
}

From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

License:Open Source License

/**
 * Utility method which can be used to create MongoClientOptionsBuilder from values defined in the
 * deployment yaml file.// ww w.j a v a 2s  . c  om
 *
 * @param storeAnnotation the source annotation which contains the needed parameters.
 * @param configReader    {@link ConfigReader} Configuration Reader
 * @return MongoClientOptions.Builder
 */
public static MongoClientOptions.Builder extractMongoClientOptionsBuilder(Annotation storeAnnotation,
        ConfigReader configReader) {

    MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder();
    try {
        mongoClientOptionsBuilder.connectionsPerHost(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECTIONS_PER_HOST, "100")));
        mongoClientOptionsBuilder.connectTimeout(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.CONNECT_TIMEOUT, "10000")));
        mongoClientOptionsBuilder.heartbeatConnectTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_CONNECT_TIMEOUT, "20000")));
        mongoClientOptionsBuilder.heartbeatSocketTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_SOCKET_TIMEOUT, "20000")));
        mongoClientOptionsBuilder.heartbeatFrequency(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.HEARTBEAT_FREQUENCY, "10000")));
        mongoClientOptionsBuilder.localThreshold(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.LOCAL_THRESHOLD, "15")));
        mongoClientOptionsBuilder.maxWaitTime(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MAX_WAIT_TIME, "120000")));
        mongoClientOptionsBuilder.minConnectionsPerHost(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_CONNECTIONS_PER_HOST, "0")));
        mongoClientOptionsBuilder.minHeartbeatFrequency(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.MIN_HEARTBEAT_FREQUENCY, "500")));
        mongoClientOptionsBuilder.serverSelectionTimeout(Integer
                .parseInt(configReader.readConfig(MongoTableConstants.SERVER_SELECTION_TIMEOUT, "30000")));
        mongoClientOptionsBuilder.socketTimeout(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.SOCKET_TIMEOUT, "0")));
        mongoClientOptionsBuilder.threadsAllowedToBlockForConnectionMultiplier(
                Integer.parseInt(configReader.readConfig(MongoTableConstants.THREADS_ALLOWED_TO_BLOCK, "5")));
        mongoClientOptionsBuilder.socketKeepAlive(
                Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SOCKET_KEEP_ALIVE, "false")));
        mongoClientOptionsBuilder.sslEnabled(
                Boolean.parseBoolean(configReader.readConfig(MongoTableConstants.SSL_ENABLED, "false")));
        mongoClientOptionsBuilder.cursorFinalizerEnabled(Boolean
                .parseBoolean(configReader.readConfig(MongoTableConstants.CURSOR_FINALIZER_ENABLED, "true")));
        mongoClientOptionsBuilder.readPreference(ReadPreference
                .valueOf(configReader.readConfig(MongoTableConstants.READ_PREFERENCE, "primary")));
        mongoClientOptionsBuilder.writeConcern(WriteConcern
                .valueOf(configReader.readConfig(MongoTableConstants.WRITE_CONCERN, "acknowledged")));

        String readConcern = configReader.readConfig(MongoTableConstants.READ_CONCERN, "DEFAULT");
        if (!readConcern.matches("DEFAULT")) {
            mongoClientOptionsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(readConcern)));
        }

        int maxConnectionIdleTime = Integer
                .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_IDLE_TIME, "0"));
        if (maxConnectionIdleTime != 0) {
            mongoClientOptionsBuilder.maxConnectionIdleTime(maxConnectionIdleTime);
        }

        int maxConnectionLifeTime = Integer
                .parseInt(configReader.readConfig(MongoTableConstants.MAX_CONNECTION_LIFE_TIME, "0"));
        if (maxConnectionIdleTime != 0) {
            mongoClientOptionsBuilder.maxConnectionLifeTime(maxConnectionLifeTime);
        }

        String requiredReplicaSetName = configReader.readConfig(MongoTableConstants.REQUIRED_REPLICA_SET_NAME,
                "");
        if (!requiredReplicaSetName.equals("")) {
            mongoClientOptionsBuilder.requiredReplicaSetName(requiredReplicaSetName);
        }

        String applicationName = configReader.readConfig(MongoTableConstants.APPLICATION_NAME, "");
        if (!applicationName.equals("")) {
            mongoClientOptionsBuilder.applicationName(applicationName);
        }

        String secureConnectionEnabled = storeAnnotation
                .getElement(MongoTableConstants.ANNOTATION_ELEMENT_SECURE_CONNECTION);
        secureConnectionEnabled = secureConnectionEnabled == null ? "false" : secureConnectionEnabled;

        if (secureConnectionEnabled.equalsIgnoreCase("true")) {
            mongoClientOptionsBuilder.sslEnabled(true);
            String trustStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTORE);
            trustStore = trustStore == null ? configReader.readConfig("trustStore", DEFAULT_TRUST_STORE_FILE)
                    : trustStore;
            trustStore = resolveCarbonHome(trustStore);

            String trustStorePassword = storeAnnotation
                    .getElement(MongoTableConstants.ANNOTATION_ELEMENT_TRUSTSTOREPASS);
            trustStorePassword = trustStorePassword == null
                    ? configReader.readConfig("trustStorePassword", DEFAULT_TRUST_STORE_PASSWORD)
                    : trustStorePassword;

            String keyStore = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_KEYSTORE);
            keyStore = keyStore == null ? configReader.readConfig("keyStore", DEFAULT_KEY_STORE_FILE)
                    : keyStore;
            keyStore = resolveCarbonHome(keyStore);

            String keyStorePassword = storeAnnotation
                    .getElement(MongoTableConstants.ANNOTATION_ELEMENT_STOREPASS);
            keyStorePassword = keyStorePassword == null
                    ? configReader.readConfig("keyStorePassword", DEFAULT_KEY_STORE_PASSWORD)
                    : keyStorePassword;

            mongoClientOptionsBuilder.socketFactory(MongoTableUtils.extractSocketFactory(trustStore,
                    trustStorePassword, keyStore, keyStorePassword));
        }
        return mongoClientOptionsBuilder;
    } catch (IllegalArgumentException e) {
        throw new MongoTableException("Values Read from config readers have illegal values : ", e);
    }
}