Example usage for com.mongodb WriteConcern FSYNC_SAFE

List of usage examples for com.mongodb WriteConcern FSYNC_SAFE

Introduction

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

Prototype

WriteConcern FSYNC_SAFE

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

Click Source Link

Document

Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk.

This field has been superseded by WriteConcern.FSYNCED , and may be deprecated in a future release.

Usage

From source file:org.s1.mongodb.cluster.MongoDBDDS.java

License:Apache License

@Override
public void runWriteCommand(CommandBean b) {
    if (Objects.isNullOrEmpty(b.getCollection())) {
        return;//from   w  ww. j a va 2  s.  c  o  m
    }
    if (Objects.isNullOrEmpty(b.getEntity())) {
        return;
    }
    DBCollection coll = MongoDBConnectionHelper.getConnection(b.getDatabase()).getCollection(b.getCollection());

    if ("add".equals(b.getCommand()) || "set".equals(b.getCommand())) {
        if (b.getParams() == null) {
            return;
        }
        Map<String, Object> search = Objects.newHashMap("id", b.getEntity());
        b.getParams().put("id", b.getEntity());
        int n = coll.update(MongoDBFormat.fromMap(search), MongoDBFormat.fromMap(b.getParams()),
                b.getCommand().equals("add"), false, WriteConcern.FSYNC_SAFE).getN();
        if (LOG.isDebugEnabled())
            LOG.debug("MongoDB records(" + n + ") " + (b.getCommand().equals("add") ? "added" : "updated")
                    + ", " + b);
    } else if ("remove".equals(b.getCommand())) {
        Map<String, Object> search = Objects.newHashMap("id", b.getEntity());
        int n = coll.remove(MongoDBFormat.fromMap(search), WriteConcern.FSYNC_SAFE).getN();
        if (LOG.isDebugEnabled())
            LOG.debug("MongoDB records(" + n + ") removed, " + b);
    }
}

From source file:org.s1.mongodb.cluster.MongoDBOperationLog.java

License:Apache License

@Override
public void addToLocalLog(MessageBean m) {
    DBCollection coll = getCollection();
    Map<String, Object> m1 = m.toMap();
    m1.put("done", false);
    coll.insert(MongoDBFormat.fromMap(m1), WriteConcern.FSYNC_SAFE);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Node write log new record: " + m.toString(true));
    } else if (LOG.isDebugEnabled()) {
        LOG.debug("Node write log new record: " + m.toString(false));
    }//  w  w  w .j a  v a  2  s . c  o  m
}

From source file:org.s1.mongodb.cluster.MongoDBOperationLog.java

License:Apache License

@Override
public void markDone(long id) {
    DBCollection coll = getCollection();
    coll.update(new BasicDBObject("id", id), new BasicDBObject("$set", new BasicDBObject("done", true)), false,
            false, WriteConcern.FSYNC_SAFE);
    if (LOG.isDebugEnabled())
        LOG.debug("Node write log record #" + id + " marked as done:true");
}

From source file:org.s1.mongodb.MongoDBConnectionHelper.java

License:Apache License

/**
 *
 * @param instance// w w  w .j a va2  s  .  c  o  m
 */
private static synchronized void initialize(String instance) {
    if (!connections.containsKey(instance)) {

        Map<String, Object> mopt = Options.getStorage().getMap(OPTIONS);
        Map<String, Object> m = Objects.get(mopt, "connections." + instance);
        if (Objects.isNullOrEmpty(m)) {
            m = Objects.get(mopt, "connections." + DEFAULT_INSTANCE);
        }

        MongoClientOptions.Builder b = MongoClientOptions.builder();
        MongoClientOptions def_opt = MongoClientOptions.builder().build();
        b.connectionsPerHost(Objects.get(m, "connectionsPerHost", def_opt.getConnectionsPerHost()));
        b.autoConnectRetry(Objects.get(m, "autoConnectRetry", def_opt.isAutoConnectRetry()));
        b.connectTimeout(Objects.get(m, "connectTimeout", def_opt.getConnectTimeout()));
        b.socketKeepAlive(Objects.get(m, "socketKeepAlive", def_opt.isSocketKeepAlive()));
        b.socketTimeout(Objects.get(m, "socketTimeout", def_opt.getSocketTimeout()));
        b.maxAutoConnectRetryTime(
                Objects.get(m, "maxAutoConnectRetryTime", def_opt.getMaxAutoConnectRetryTime()));
        b.maxWaitTime(Objects.get(m, "maxWaitTime", def_opt.getMaxWaitTime()));
        b.threadsAllowedToBlockForConnectionMultiplier(
                Objects.get(m, "threadsAllowedToBlockForConnectionMultiplier",
                        def_opt.getThreadsAllowedToBlockForConnectionMultiplier()));
        b.writeConcern(WriteConcern.FSYNC_SAFE);
        MongoClientOptions opt = b.build();

        MongoClient cl = null;
        try {
            cl = new MongoClient(
                    new ServerAddress(Objects.get(m, "host", "localhost"), Objects.get(m, "port", 27017)), opt);
        } catch (UnknownHostException e) {
            throw S1SystemError.wrap(e);
        }

        String dbName = Objects.get(m, "name");
        if (Objects.isNullOrEmpty(dbName))
            throw new S1SystemError("Cannot initialize MongoDB connection, because name is not set");

        DB db = cl.getDB(dbName);

        String user = Objects.get(m, "user");
        String password = Objects.get(m, "password");
        if (!Objects.isNullOrEmpty(user)) {
            if (!db.authenticate(user, password.toCharArray())) {
                throw new S1SystemError(
                        "Cannot authenticate MongoDB connection " + dbName + " with user " + user);
            }
        }
        LOG.info("MongoDB connected " + cl.getAddress().getHost() + ":" + cl.getAddress().getPort());

        connections.put(instance, db);
    }
}