List of usage examples for com.mongodb WriteConcern JOURNALED
WriteConcern JOURNALED
To view the source code for com.mongodb WriteConcern JOURNALED.
Click Source Link
From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java
License:Apache License
@Override public void save(final CatalogueEntry entry) { if (entry == null) { throw new IllegalArgumentException("Entry cannot be null"); }/* w w w.ja va 2 s . co m*/ // Save the host if (entry.host.id != null) { hostService.save(entry.host); } else { // Attempt to find and update a host with the corresponding domain Host host = hostService.findByDomain(entry.host.host); if (host == null) { hostService.save(entry.host); } else { host.lastRequest = entry.host.lastRequest; host.robotsTxt = entry.host.robotsTxt; hostService.save(host); entry.host = host; } } // Save the entry if (entry.id == null) { DBObject obj = toMongoObject(entry); WriteResult result = catalogue.insert(obj, WriteConcern.JOURNALED); entry.id = obj.get("_id").toString(); } else { catalogue.update(new BasicDBObject("_id", new ObjectId(entry.id)), toMongoObject(entry), false, false, WriteConcern.JOURNALED); } }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java
License:Apache License
@Override public synchronized CatalogueEntry getWorkEntry() { if (!workPool.isEmpty()) { return workPool.remove(); }//from ww w .j ava 2s . co m // Find hosts that have been recently accessed // XXX The delay time needs to be configurable int hostDelaySeconds = 45; Collection<Host> recentHosts = hostService.recentlyRequested(hostDelaySeconds); Set<ObjectId> excludedHostIds = new HashSet<>(); for (Host host : recentHosts) { excludedHostIds.add(new ObjectId(host.id)); } // Find hosts that are currently queued List<ObjectId> queuedHosts = catalogue.distinct("host", new BasicDBObject("status", "QUEUED")); for (ObjectId id : queuedHosts) { excludedHostIds.add(id); } // Exclude recently visited hosts BasicDBObject excludedHosts = new BasicDBObject("host", new BasicDBObject("$nin", excludedHostIds.toArray(new ObjectId[0]))); // Retreive pending (un-indexed) resources BasicDBObject unindexed = new BasicDBObject("status", "PENDING"); // Reindex resources older than 7 days (XXX Make configurable/calculated BasicDBObject staleResources = new BasicDBObject("$and", Arrays.asList(new BasicDBObject("last_visit", new BasicDBObject("$exists", true)), new BasicDBObject("last_visit", new BasicDBObject("$lte", new DateTime().minusDays(7).toDate())), new BasicDBObject("status", "INDEXED"))); // Now put it all together BasicDBObject and = new BasicDBObject("$and", Arrays.asList(excludedHosts, new BasicDBObject("$or", Arrays.asList(unindexed, staleResources)))); DBObject match = new BasicDBObject("$match", and); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("last_updated", 1)); DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$host").append("first", new BasicDBObject("$first", "$$ROOT"))); List<DBObject> pipeline = Arrays.asList(match, group, sort); LOGGER.debug("PIPELINE: {}", pipeline); Iterable<DBObject> refs = catalogue.aggregate(pipeline).results(); for (DBObject obj : refs) { workPool.add(fromMongoObject((DBObject) obj.get("first"))); } LOGGER.debug("WORK POOL: {}", workPool); // Update the objects in mongo to queued state BasicDBList ids = new BasicDBList(); ids.addAll(listIds(workPool)); catalogue.update(new BasicDBObject("_id", new BasicDBObject("$in", ids)), new BasicDBObject("$set", new BasicDBObject("status", "QUEUED")), false, true, WriteConcern.JOURNALED); if (workPool.isEmpty()) { return null; } return workPool.remove(); }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoHostService.java
License:Apache License
@Override public void save(Host host) { if (host == null) { throw new IllegalArgumentException("Host cannot be null"); }/*from w ww . j a v a2s. c o m*/ if (StringUtils.isBlank(host.host)) { throw new IllegalArgumentException("Cannot save host with empty domain"); } // If the host is cached, let's check to see if it changed for (CacheEntry entry : hostCache) { if (entry.getHost().equals(host) && !entry.isDirty()) { return; } } // Save the entry if (host.id == null) { DBObject obj = toMongoObject(host); hosts.insert(obj, WriteConcern.JOURNALED); host.id = obj.get("_id").toString(); } else { hosts.update(new BasicDBObject("_id", new ObjectId(host.id)), toMongoObject(host), false, false, WriteConcern.JOURNALED); } // Add to cache if needed for (CacheEntry entry : hostCache) { if (entry.getHost().equals(host)) { return; } } hostCache.add(new CacheEntry(host)); }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoReferenceService.java
License:Apache License
@Override public void save(final Reference reference) { Reference existing = find(reference.linker, reference.linkee); if (existing != null) { return;//from w w w . j a v a 2 s .c om } DBObject obj = toMongoObject(reference); references.insert(obj, WriteConcern.JOURNALED); reference.id = obj.get("_id").toString(); referenceCache.add(new CacheEntry(reference)); }
From source file:com.glaf.core.container.MongodbContainer.java
License:Apache License
private synchronized void init() { if (mongoClient == null) { String servers = getString("servers"); if (servers == null) { servers = "127.0.0.1:27017"; }/*from w w w . ja va2 s . c om*/ List<String> list = StringTools.split(servers, ","); List<ServerAddress> addrList = new ArrayList<ServerAddress>(); for (String server : list) { String host = server.substring(0, server.indexOf(":")); int port = Integer.parseInt(server.substring(server.indexOf(":") + 1, server.length())); try { ServerAddress addr = new ServerAddress(host, port); addrList.add(addr); } catch (Exception ex) { ex.printStackTrace(); } } mongoClient = new MongoClient(addrList); mongoClient.setWriteConcern(WriteConcern.JOURNALED); Runnable shutdownHook = new MongodbShutdownHook(mongoClient); ShutdownHookManager.get().addShutdownHook(shutdownHook, Thread.NORM_PRIORITY); } }
From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java
License:Open Source License
private DB selectDatabase(Mongo mongo) { DB db = mongo.getDB(dbName);/* w w w . j a v a2 s .c o m*/ // MongoDB defaults are insane, set a reasonable write concern explicitly. MK. // But we would be insane not to override this when writing lock records. LB. db.setWriteConcern(WriteConcern.JOURNALED); if (username != null) { if (authDbName != null) { // authentificating to db which gives access to all other dbs (role - readWriteAnyDatabase) // by default in mongo it should be "admin" DB authDb = mongo.getDB(authDbName); authDb.authenticate(username, password.toCharArray()); } else { db.authenticate(username, password.toCharArray()); } } return db; }
From source file:com.ricardolorenzo.identity.user.impl.UserIdentityManagerMongoDB.java
License:Open Source License
public UserIdentityManagerMongoDB(final Properties conf) throws UnknownHostException { this.properties = conf; this.databaseUsers = new Boolean(this.properties.getProperty("mongodb.databaseUsers", "true")); WriteConcern writeConcern = WriteConcern.MAJORITY; String writeConcernType = conf.getProperty("mongodb.writeConcern", "majority").toLowerCase(); if ("majority".equals(writeConcernType)) { writeConcern = WriteConcern.UNACKNOWLEDGED; } else if ("unacknowledged".equals(writeConcernType)) { writeConcern = WriteConcern.UNACKNOWLEDGED; } else if ("acknowledged".equals(writeConcernType)) { writeConcern = WriteConcern.ACKNOWLEDGED; } else if ("journaled".equals(writeConcernType)) { writeConcern = WriteConcern.JOURNALED; } else if ("replica_acknowledged".equals(writeConcernType)) { writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED; }/*from www . jav a2s . c o m*/ ReadPreference readPreference = null; String readPreferenceType = conf.getProperty("mongodb.readPreference", "primary").toLowerCase(); if ("primary".equals(readPreferenceType)) { readPreference = ReadPreference.primary(); } else if ("primary_preferred".equals(readPreferenceType)) { readPreference = ReadPreference.primaryPreferred(); } else if ("secondary".equals(readPreferenceType)) { readPreference = ReadPreference.secondary(); } else if ("secondary_preferred".equals(readPreferenceType)) { readPreference = ReadPreference.secondaryPreferred(); } else if ("nearest".equals(readPreferenceType)) { readPreference = ReadPreference.nearest(); } MongoClientOptions.Builder options = MongoClientOptions.builder(); options.writeConcern(writeConcern); options.readPreference(readPreference); try { options.connectionsPerHost(Integer.parseInt(conf.getProperty("mongodb.threads", "100"))); } catch (NumberFormatException e) { options.connectionsPerHost(100); } MongoClientURI mongoClientURI = new MongoClientURI( conf.getProperty("mongodb.url", "mongodb://localhost:27017"), options); if (!this.properties.containsKey("mongodb.database")) { if (mongoClientURI.getDatabase() != null && !mongoClientURI.getDatabase().isEmpty()) { this.properties.setProperty("mongodb.database", mongoClientURI.getDatabase()); } else { this.properties.setProperty("mongodb.database", DEFAULT_DATABASE); } } mongoClient = new MongoClient(mongoClientURI); }
From source file:com.stratio.connector.mongodb.core.configuration.MongoClientConfiguration.java
License:Apache License
/** * Convert the mongo connector string option to the appropriate write concern. * * @param writeSetting//from w ww.j a va 2s . c om * the write concern string setting * @return the write concern * @throws MongoValidationException * if the value cannot be parsed to a WriteConcern */ private WriteConcern settingToWritePreference(String writeSetting) throws MongoValidationException { WriteConcern writeConcern = null; switch (writeSetting.trim().toLowerCase()) { case "acknowledged": writeConcern = WriteConcern.ACKNOWLEDGED; break; case "unacknowledged": writeConcern = WriteConcern.UNACKNOWLEDGED; break; case "replica_acknowledged": writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED; break; case "journaled": writeConcern = WriteConcern.JOURNALED; break; default: throw new MongoValidationException("Write preference " + writeSetting + " is not a legal value"); } return writeConcern; }
From source file:com.terkaly.JavaMongoDB.App.java
License:Open Source License
public static void main(String[] args) { try {/*w w w. j ava 2s. c o m*/ // Create a connection using mongoClient // 23.99.88.154 is obtained from the portal MongoClient mongoClient = new MongoClient("[ put your ip address here ]", 27017); // Get a connection to mydb DB db = mongoClient.getDB("mydb"); // mydb has one or more collections, get "testCollection" DBCollection collection = db.getCollection("testCollection"); // Create an empty object BasicDBObject empty = new BasicDBObject(); // Clear out testCollection collection.remove(empty); // Acknowledges the write operation only // after committing the data to the journal mongoClient.setWriteConcern(WriteConcern.JOURNALED); // Here is the data format in JSON // { // "name": "MongoDB", // "type": "database", // "count": 1, // "info": { // "x": 203, // "y": 102 // } // } BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); collection.insert(doc); DBObject myDoc = collection.findOne(); System.out.println(myDoc); } catch (Exception e) { e.printStackTrace(); } }
From source file:eu.przemyslawzawadzki.util.MongoConector.java
public synchronized static MongoClient getClient() { if (instance == null) { instance = new MongoClient(); instance.setWriteConcern(WriteConcern.JOURNALED); }/*from w ww. ja v a 2s .c om*/ return instance; }