Example usage for com.mongodb WriteConcern JOURNAL_SAFE

List of usage examples for com.mongodb WriteConcern JOURNAL_SAFE

Introduction

In this page you can find the example usage for com.mongodb WriteConcern JOURNAL_SAFE.

Prototype

WriteConcern JOURNAL_SAFE

To view the source code for com.mongodb WriteConcern JOURNAL_SAFE.

Click Source Link

Document

Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk.

Usage

From source file:org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec.java

License:Open Source License

/**
 * Connect with the specified properties and return the Connection.
 *///from   w ww.j a v a  2 s  .  c om
public Connection connectToDataSource(EISAccessor accessor, Properties properties)
        throws DatabaseException, ValidationException {
    if ((this.connectionFactory == null) && (this.name == null)) {
        this.connectionFactory = new MongoConnectionFactory();
    }
    if (!properties.isEmpty()) {
        if (this.connectionSpec == null) {
            this.connectionSpec = new MongoJCAConnectionSpec();
        }
        MongoJCAConnectionSpec spec = (MongoJCAConnectionSpec) this.connectionSpec;
        String host = (String) properties.get(HOST);
        String port = (String) properties.get(PORT);
        String db = (String) properties.get(DB);
        if (host != null) {
            if (host.indexOf(',') == -1) {
                spec.getHosts().add(host);
                if (port != null) {
                    spec.getPorts().add(new Integer(port));
                }
            } else {
                int startIndex = 0;
                while (startIndex < (host.length() - 1)) {
                    int endIndex = host.indexOf(',', startIndex);
                    if (endIndex == -1) {
                        endIndex = host.length();
                    }
                    String nextHost = host.substring(startIndex, endIndex);
                    spec.getHosts().add(nextHost);
                    startIndex = endIndex + 1;
                }
                while (startIndex < (port.length() - 1)) {
                    int endIndex = port.indexOf(',', startIndex);
                    if (endIndex == -1) {
                        endIndex = port.length();
                    }
                    String nextPort = port.substring(startIndex, endIndex);
                    spec.getPorts().add(new Integer(nextPort));
                    startIndex = endIndex + 1;
                }
            }
        }
        if (db != null) {
            spec.setDB(db);
        }

        String user = (String) properties.get("user");
        Object password = properties.get("password");
        if (password instanceof String) {
            password = ((String) password).toCharArray();
        }
        if ((user != null) && (user.length() != 0)) {
            spec.setUser(user);
            spec.setPassword((char[]) password);
        }

        // Allows setting of read preference as a property.
        Object preference = properties.get(READ_PREFERENCE);
        if (preference instanceof ReadPreference) {
            spec.setReadPreference((ReadPreference) preference);
        } else if (preference instanceof String) {
            String constant = (String) preference;
            if (constant.equals("PRIMARY")) {
                spec.setReadPreference(ReadPreference.PRIMARY);
            } else if (constant.equals("SECONDARY")) {
                spec.setReadPreference(ReadPreference.SECONDARY);
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }

        // Allows setting of write concern as a property.
        Object concern = properties.get(WRITE_CONCERN);
        if (concern instanceof WriteConcern) {
            spec.setWriteConcern((WriteConcern) concern);
        } else if (concern instanceof String) {
            String constant = (String) concern;
            if (constant.equals("FSYNC_SAFE")) {
                spec.setWriteConcern(WriteConcern.FSYNC_SAFE);
            } else if (constant.equals("ACKNOWLEDGED")) {
                spec.setWriteConcern(WriteConcern.ACKNOWLEDGED);
            } else if (constant.equals("JOURNAL_SAFE")) {
                spec.setWriteConcern(WriteConcern.JOURNAL_SAFE);
            } else if (constant.equals("MAJORITY")) {
                spec.setWriteConcern(WriteConcern.MAJORITY);
            } else if (constant.equals("NONE")) {
                spec.setWriteConcern(WriteConcern.NONE);
            } else if (constant.equals("NORMAL")) {
                spec.setWriteConcern(WriteConcern.NORMAL);
            } else if (constant.equals("REPLICAS_SAFE")) {
                spec.setWriteConcern(WriteConcern.REPLICAS_SAFE);
            } else if (constant.equals("SAFE")) {
                spec.setWriteConcern(WriteConcern.SAFE);
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }

        // Allows setting of options as a property.
        Object options = properties.get(OPTIONS);
        if (options instanceof Number) {
            spec.setOptions(((Number) options).intValue());
        } else if (options instanceof String) {
            spec.setOptions(Integer.valueOf(((String) options)));
        }
    }

    return super.connectToDataSource(accessor, properties);
}

From source file:org.lucee.mongodb.support.ObjectSupport.java

License:Open Source License

public WriteConcern toWriteConcern(Object obj, WriteConcern defaultValue) {
    if (obj instanceof WriteConcern)
        return (WriteConcern) obj;
    if (decision.isSimpleValue(obj)) {
        String str = caster.toString(obj, "");
        str = str.trim().toUpperCase();//from www  . jav  a 2  s .c  om
        if ("ACKNOWLEDGED".equals(str))
            return WriteConcern.ACKNOWLEDGED;
        else if ("ACKNOWLEDGED".equals(str))
            return WriteConcern.FSYNC_SAFE;
        else if ("FSYNC_SAFE".equals(str) || "FSYNCSAFE".equals(str))
            return WriteConcern.FSYNCED;
        else if ("JOURNAL_SAFE".equals(str) || "JOURNALSAFE".equals(str))
            return WriteConcern.JOURNAL_SAFE;
        else if ("JOURNALED".equals(str))
            return WriteConcern.JOURNALED;
        else if ("MAJORITY".equals(str))
            return WriteConcern.MAJORITY;
        else if ("NORMAL".equals(str))
            return WriteConcern.NORMAL;
        else if ("REPLICA_ACKNOWLEDGED".equals(str) || "REPLICAACKNOWLEDGED".equals(str))
            return WriteConcern.REPLICA_ACKNOWLEDGED;
        else if ("REPLICAS_SAFE".equals(str) || "REPLICASSAFE".equals(str))
            return WriteConcern.REPLICAS_SAFE;
        else if ("SAFE".equals(str))
            return WriteConcern.SAFE;
        else if ("UNACKNOWLEDGED".equals(str))
            return WriteConcern.UNACKNOWLEDGED;
    }
    return defaultValue;
}

From source file:org.qi4j.entitystore.mongodb.MongoMapEntityStoreMixin.java

License:Apache License

private void loadConfiguration() throws UnknownHostException {
    configuration.refresh();//from w  w  w .j av  a2 s .c o m
    MongoEntityStoreConfiguration config = configuration.get();

    // Combine hostname, port and nodes configuration properties
    serverAddresses = new ArrayList<ServerAddress>();
    if (config.hostname().get() != null && !config.hostname().get().isEmpty()) {
        serverAddresses.add(new ServerAddress(config.hostname().get(), config.port().get()));
    }
    serverAddresses.addAll(config.nodes().get());

    // If database name not configured, set it to qi4j:entitystore
    databaseName = config.database().get();
    if (databaseName == null) {
        databaseName = DEFAULT_DATABASE_NAME;
    }

    // If collection name not configured, set it to qi4j:entitystore:entities
    collectionName = config.collection().get();
    if (collectionName == null) {
        collectionName = DEFAULT_COLLECTION_NAME;
    }

    // If write concern not configured, set it to normal
    switch (config.writeConcern().get()) {
    case FSYNC_SAFE:
        writeConcern = WriteConcern.FSYNC_SAFE;
        break;
    case JOURNAL_SAFE:
        writeConcern = WriteConcern.JOURNAL_SAFE;
        break;
    case MAJORITY:
        writeConcern = WriteConcern.MAJORITY;
        break;
    case NONE:
        writeConcern = WriteConcern.NONE;
        break;
    case REPLICAS_SAFE:
        writeConcern = WriteConcern.REPLICAS_SAFE;
        break;
    case SAFE:
        writeConcern = WriteConcern.SAFE;
        break;
    case NORMAL:
    default:
        writeConcern = WriteConcern.NORMAL;
    }

    // Username and password are defaulted to empty strings
    username = config.username().get();
    password = config.password().get().toCharArray();
}