List of usage examples for com.mongodb WriteConcern SAFE
WriteConcern SAFE
To view the source code for com.mongodb WriteConcern SAFE.
Click Source Link
Write operations that use this write concern will wait for acknowledgement from the primary server before returning.
From source file:se.inera.axel.shs.broker.messagestore.internal.MongoDBTestContextConfig.java
License:Open Source License
public @Bean MongoDbFactory mongoDbFactorySafe() throws Exception { SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongo(), "axel-test"); simpleMongoDbFactory.setWriteConcern(WriteConcern.SAFE); return simpleMongoDbFactory; }
From source file:simple.crawler.mongo.CrawlingDB.java
License:Open Source License
public void remove(CrawlingDBObject obj, Collection collection) { DB db = client.getDB(dbName); DBCollection con = db.getCollection(collection.toString()); con.remove(obj, WriteConcern.SAFE); }
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToAscii.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); // options.addOption(OptionBuilder // .withLongOpt("tags") // .withArgName( "property=value" ) // .hasArgs(2) // .withValueSeparator() // .withDescription( // "used to tag files when uploading.") // .create( "t" )); if (args.length == 0) { printHelpExit(options);/* w w w . ja v a 2 s . c o m*/ } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); } else { throw new IllegalArgumentException("You must specify an output filename"); } } catch (ParseException e) { e.printStackTrace(); printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); // boolean auth = db.authenticate(myUserName, myPassword); exportAscii(m, db, graphName, graphBranch, new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToGDF.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("c", "color-properties", true, "An (optional) '.properties' file that contains node type -> RGB mappings."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); // options.addOption(OptionBuilder // .withLongOpt("tags") // .withArgName( "property=value" ) // .hasArgs(2) // .withValueSeparator() // .withDescription( // "used to tag files when uploading.") // .create( "t" )); if (args.length == 0) { printHelpExit(options);// w w w . jav a 2 s .c om } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String colorPropsFilename = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); } else { throw new IllegalArgumentException("You must specify an output filename"); } if (line.hasOption("color-properties")) { colorPropsFilename = line.getOptionValue("color-properties", null); } } catch (ParseException e) { e.printStackTrace(); printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); // boolean auth = db.authenticate(myUserName, myPassword); Map<String, Color> nodeColorMappings = new HashMap<>(); if (colorPropsFilename != null) { nodeColorMappings.putAll(loadColorMappings(new File(colorPropsFilename))); } exportGdf(m, db, nodeColorMappings, graphName, graphBranch, new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToGephi.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, RevisionLogException, IOException, GraphModelException { CommandLineParser parser = new PosixParser(); Options options = new Options(); // options.addOption("g", "format-gdf", false, // "Specifies that the output format is GDF (currently the only option)"); options.addOption("h", "mongo-host", true, "The MongoDB server host to connect to."); options.addOption("d", "mongo-database", true, "The name of a MongoDB database to connect to."); options.addOption("g", "graph-name", true, "The name of a graph to use (there may be multiple graphs per MongoDB database)."); options.addOption("b", "graph-branch", true, "The name of a graph branch to use (there may be multiple branches per graph)."); options.addOption("o", "output-file", true, "Specifies a path to a file to use "); if (args.length == 0) { printHelpExit(options);//from w ww. ja v a2s.c o m } String mongoHost = null; String mongoDatabaseName = null; String graphName = null; String graphBranch = null; String outputFilename = null; try { CommandLine line = parser.parse(options, args); if (line.hasOption("mongo-host")) { mongoHost = line.getOptionValue("mongo-host", null); } else { throw new IllegalArgumentException("You must specify a hostname"); } if (line.hasOption("mongo-database")) { mongoDatabaseName = line.getOptionValue("mongo-database", null); } else { throw new IllegalArgumentException("You must specify a database name"); } if (line.hasOption("graph-name")) { graphName = line.getOptionValue("graph-name", null); } else { throw new IllegalArgumentException("You must specify a graph name"); } if (line.hasOption("graph-branch")) { graphBranch = line.getOptionValue("graph-branch", null); } else { throw new IllegalArgumentException("You must specify a graph branch name"); } if (line.hasOption("output-file")) { outputFilename = line.getOptionValue("output-file", null); if (!outputFilename.contains(".gexf") && outputFilename.contains(".pdf") && outputFilename.contains(".svg") && outputFilename.contains(".gdf")) { throw new IllegalArgumentException( "You must specify an output filename with an extension " + "of [.pdf|.svg|.gexf|gdf]"); } } else { throw new IllegalArgumentException( "You must specify an output filename with an extension " + "of [.pdf|.svg|.gexf|gdf]"); } } catch (ParseException e) { printHelpExit(options); System.exit(1); } Mongo m = new Mongo(); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB(mongoDatabaseName); GraphCheckoutNamingScheme collectionNamer = new GraphCheckoutNamingScheme(graphName, graphBranch); DBCollection nodeCol = db.getCollection(collectionNamer.getNodeCollectionName()); DBCollection edgeCol = db.getCollection(collectionNamer.getEdgeCollectionName()); NodeDAO nodeDao = GraphDAOFactory.createDefaultNodeDAO(classLoader, m, db, nodeCol, edgeCol); EdgeDAO edgeDao = GraphDAOFactory.createDefaultEdgeDAO(classLoader, m, db, nodeCol, edgeCol); RevisionLog log = new RevisionLogDirectToMongoDbImpl(classLoader, m, db); MongoGraphToGephi exporter = new MongoGraphToGephi(nodeDao, edgeDao); exporter.exportGexf(new File(outputFilename)); System.out.println("\n\nDone."); }
From source file:uk.ac.ncl.aries.entanglement.WriteTestObjects.java
License:Apache License
public static void main(String[] args) throws UnknownHostException, JsonSerializerException, DbObjectMarshallerException { Mongo m = new Mongo(); // Mongo m = new Mongo( "localhost" ); // or/*from www. j a v a 2 s . c o m*/ // Mongo m = new Mongo( "localhost" , 27017 ); // or, to connect to a replica set, supply a seed list of members // Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), // new ServerAddress("localhost", 27018), // new ServerAddress("localhost", 27019))); m.setWriteConcern(WriteConcern.SAFE); DB db = m.getDB("aries-test"); // boolean auth = db.authenticate(myUserName, myPassword); DBCollection testCol = db.getCollection("testCollection"); // BasicDBObject newDoc = new BasicDBObject(); // newDoc. // testCol.insert(newDoc); Node node = new Node(); // node.setNumericalId(3); node.setUid(UidGenerator.generateUid()); // node.setGraphUniqueId(UidGenerator.generateUid()); // node.getProperties().put("An_integer", 1); // node.getProperties().put("A String", "Foo"); Map<Integer, String> subMap = new HashMap<>(); subMap.put(1, "one"); subMap.put(2, "two"); subMap.put(3, "three"); // node.getProperties().put("A map", subMap); DbObjectMarshaller marshaller = ObjectMarshallerFactory.create(WriteTestObjects.class.getClassLoader()); DBObject dbObject = marshaller.serialize(node); testCol.insert(dbObject); System.out.println("Listing collections:"); listCollections(db); System.out.println("\n\nFindOne:"); findOne(testCol); System.out.println("\n\nCursor iteration:"); cursorIterateAllDocs(testCol); System.out.println("\n\nDone."); }
From source file:v7db.files.mongodb.MongoContentStorage.java
License:Open Source License
public ContentSHA storeContent(InputStream data) throws IOException { try {/*ww w. j a v a2s . co m*/ MessageDigest completeSHA = MessageDigest.getInstance("SHA"); long completeLength = 0; byte[] chunk = new byte[chunkSize]; int read; List<ContentSHA> chunks = new ArrayList<ContentSHA>(); while (0 < (read = readFully(data, chunk))) { completeSHA.update(chunk, 0, read); completeLength += read; chunks.add(storeContentChunk(chunk, 0, read)); } if (chunks.isEmpty()) return storeContentChunk(ArrayUtils.EMPTY_BYTE_ARRAY, 0, 0); if (chunks.size() == 1) return chunks.get(0); List<Map<String, Object>> bases = new ArrayList<Map<String, Object>>(chunks.size()); for (ContentSHA c : chunks) { bases.add(c.serialize()); } ContentSHA result = ContentSHA.forDigestAndLength(completeSHA.digest(), completeLength); long existing = contentCollection.count(new BasicDBObject(_ID, result.getSHA())); if (existing == 0) { contentCollection.insert( new BasicDBObject(_ID, result.getSHA()).append("store", "cat").append("base", bases), WriteConcern.SAFE); } return result; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(data); } }
From source file:v7db.files.mongodb.MongoContentStorage.java
License:Open Source License
private ContentSHA storeContentChunk(byte[] bytes, final int offset, final int length) throws IOException { ContentSHA _sha = ContentSHA.calculate(bytes, offset, length); byte[] sha = _sha.getSHA(); long existing = contentCollection.count(new BasicDBObject(_ID, sha)); if (existing == 0) { byte[] gzipped = Compression.gzip(bytes, offset, length); if (gzipped != null && gzipped.length > chunkSize) gzipped = null;/*www.ja v a 2 s . c om*/ if (gzipped != null) { bytes = null; contentCollection.insert(new BasicDBObject(_ID, sha).append("zin", gzipped).append("store", "gz"), WriteConcern.SAFE); gzipped = null; } else { if (offset > 0 || bytes.length != length) { bytes = ArrayUtils.subarray(bytes, offset, offset + length); } contentCollection.insert(new BasicDBObject(_ID, sha).append("in", bytes), WriteConcern.SAFE); } } return _sha; }
From source file:v7db.files.mongodb.MongoContentStorage.java
License:Open Source License
public ContentPointer storeContent(Map<String, Object> storageScheme) throws IOException { StorageScheme s = storageSchemes.get(storageScheme.get("store")); if (s == null) throw new UnsupportedOperationException(storageScheme.toString()); DBObject x = new BasicDBObject(); for (Map.Entry<String, Object> e : storageScheme.entrySet()) { x.put(e.getKey(), e.getValue()); }// w ww . j a v a 2 s . c o m long length = BSONUtils.getRequiredLong(x, "length"); byte[] sha = DigestUtils.sha(s.getContent(this, storageScheme).getInputStream()); long existing = contentCollection.count(new BasicDBObject(_ID, sha)); if (existing == 0) { x.put(_ID, sha); contentCollection.insert(x, WriteConcern.SAFE); } return new StoredContent(sha, length); }
From source file:v7db.files.mongodb.MongoReferenceTracking.java
License:Open Source License
public void updateReferences(Object ownerId, ContentPointer... contents) throws IOException { List<byte[]> content = new ArrayList<byte[]>(); for (ContentPointer cp : contents) { if (cp instanceof InlineContent) continue; if (cp instanceof StoredContent) content.add(((StoredContent) cp).getBaseSHA()); else if (cp instanceof ContentSHA) content.add(((ContentSHA) cp).getSHA()); else//from w ww . j ava2s . co m throw new IllegalArgumentException(cp.getClass().getName()); } WriteResult r = refCollection.update(new BasicDBObject("_id", ownerId), new BasicDBObject("$set", new BasicDBObject("refs", content)).append("$addToSet", new BasicDBObject("refHistory", new BasicDBObject("$each", content))), false, false, WriteConcern.SAFE); if (r.getN() == 1) return; if (r.getN() != 0) throw new IllegalStateException(); refCollection.insert(WriteConcern.SAFE, new BasicDBObject("_id", ownerId).append("refs", content).append("refHistory", content)); }