List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:org.restheart.handlers.metadata.AfterWriteCheckHandler.java
License:Open Source License
@Override public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception { if ((doesCheckersApply(context) && !applyCheckers(exchange, context)) || (doesGlobalCheckersApply() && !applyGlobalCheckers(exchange, context))) { // restore old data MongoClient client = MongoDBClientSingleton.getInstance().getClient(); MongoDatabase mdb = client.getDatabase(context.getDBName()); MongoCollection<BsonDocument> coll = mdb.getCollection(context.getCollectionName(), BsonDocument.class); BsonDocument oldData = context.getDbOperationResult().getOldData(); Object newEtag = context.getDbOperationResult().getEtag(); if (oldData != null) { // document was updated, restore old one DAOUtils.restoreDocument(context.getClientSession(), coll, oldData.get("_id"), context.getShardKey(), oldData, newEtag, "_etag"); // add to response old etag if (oldData.get("$set") != null && oldData.get("$set").isDocument() && oldData.get("$set").asDocument().get("_etag") != null) { exchange.getResponseHeaders().put(Headers.ETAG, oldData.get("$set").asDocument().get("_etag").asObjectId().getValue().toString()); } else { exchange.getResponseHeaders().remove(Headers.ETAG); }/* w w w . j av a 2 s . c o m*/ } else { // document was created, delete it Object newId = context.getDbOperationResult().getNewData().get("_id"); coll.deleteOne(and(eq("_id", newId), eq("_etag", newEtag))); } ResponseHelper.endExchangeWithMessage(exchange, context, HttpStatus.SC_BAD_REQUEST, "request check failed"); next(exchange, context); return; } next(exchange, context); }
From source file:org.restheart.handlers.metadata.AfterWriteCheckMetadataHandler.java
License:Open Source License
@Override public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception { if (doesCheckerAppy(context)) { if (!check(exchange, context)) { // restore old data MongoClient client = MongoDBClientSingleton.getInstance().getClient(); MongoDatabase mdb = client.getDatabase(context.getDBName()); MongoCollection<BsonDocument> coll = mdb.getCollection(context.getCollectionName(), BsonDocument.class); BsonDocument oldData = context.getDbOperationResult().getOldData(); Object newEtag = context.getDbOperationResult().getEtag(); if (oldData != null) { // document was updated, restore old one DAOUtils.restoreDocument(coll, oldData.get("_id"), context.getShardKey(), oldData, newEtag); // add to response old etag if (oldData.get("$set") != null && oldData.get("$set").isDocument() && oldData.get("$set").asDocument().get("_etag") != null) { exchange.getResponseHeaders().put(Headers.ETAG, oldData.get("$set").asDocument().get("_etag").asObjectId().getValue().toString()); } else { exchange.getResponseHeaders().remove(Headers.ETAG); }/*from w ww . ja v a2s . c o m*/ } else { // document was created, delete it Object newId = context.getDbOperationResult().getNewData().get("_id"); coll.deleteOne(and(eq("_id", newId), eq("_etag", newEtag))); } ResponseHelper.endExchangeWithMessage(exchange, context, HttpStatus.SC_BAD_REQUEST, "request check failed"); next(exchange, context); return; } } next(exchange, context); }
From source file:org.restheart.security.impl.DbIdentityManager.java
License:Open Source License
/** * * @param arguments/*from w ww . j a va 2s. c om*/ * @throws java.io.FileNotFoundException */ public DbIdentityManager(Map<String, Object> arguments) throws FileNotFoundException { init(arguments, "dbim"); if (this.cacheEnabled) { this.cache = CacheFactory.createLocalLoadingCache(this.cacheSize, this.cacheExpirePolicy, this.cacheTTL, (String key) -> { return this.findAccount(key); }); } MongoClient mongoClient = MongoDBClientSingleton.getInstance().getClient(); ArrayList<String> dbNames = new ArrayList<>(); mongoClient.listDatabaseNames().into(dbNames); if (!dbNames.contains(this.db)) { throw new IllegalArgumentException( "error configuring the DbIdentityManager. The specified db does not exist: " + db); } MongoDatabase mongoDb = mongoClient.getDatabase(this.db); ArrayList<String> collectionNames = new ArrayList<>(); mongoDb.listCollectionNames().into(collectionNames); if (!collectionNames.contains(this.coll)) { throw new IllegalArgumentException( "error configuring the DbIdentityManager. The specified collection does not exist: " + coll); } this.mongoColl = mongoDb.getCollection(coll, BsonDocument.class); }
From source file:org.search.system.dao.ImageDao.java
License:Open Source License
public void insertImage(Image image) { MongoInstance mongoInstance = databaseManager.getInstance(); MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort()); try {/*from w ww . j a v a 2 s . c o m*/ MongoDatabase pages = mongo.getDatabase(DATABASE_NAME); Document document = new Document(); document.put("title", image.getTitle()); document.put("description", image.getDescription()); document.put("link", image.getLink()); document.put("tags", image.getTags()); document.put("imageHash", image.getImageHash()); for (String tag : image.getTags()) { MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase()); collection.insertOne(document); } for (String tag : image.getTitle().split(" ")) { MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase()); collection.insertOne(document); } MongoCollection<Document> collection = pages.getCollection("imagesHash"); collection.insertOne(document); mongo.close(); } catch (Exception ex) { mongo.close(); LogUtil.log(ex.toString()); } }
From source file:org.search.system.dao.PageDao.java
License:Open Source License
public void insertPage(Page page) { MongoInstance mongoInstance = databaseManager.getInstance(); MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort()); try {/*from www. j a v a2 s . c o m*/ MongoDatabase pages = mongo.getDatabase(DATABASE_NAME); Document document = new Document(); document.put("title", page.getTitle()); document.put("description", page.getDescription()); document.put("rang", 0); document.put("link", page.getLink()); document.put("tags", page.getTags()); for (String tag : page.getTags()) { MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase()); collection.insertOne(document); } for (String tag : page.getTitle().split(" ")) { MongoCollection<Document> collection = pages.getCollection(tag.toLowerCase()); collection.insertOne(document); } mongo.close(); } catch (Exception ex) { mongo.close(); LogUtil.log(ex.toString()); } }
From source file:org.search.system.dao.WordDao.java
License:Open Source License
public void insertPage(Word word) { MongoInstance mongoInstance = databaseManager.getInstance(); MongoClient mongo = new MongoClient(mongoInstance.getHost(), mongoInstance.getPort()); try {/*w ww . j a v a 2s . c o m*/ MongoDatabase synonyms = mongo.getDatabase("synonyms"); MongoCollection<Document> collection = synonyms.getCollection("synonyms"); Document document = new Document(); document.put("word", word.getWord()); document.put("synonyms", word.getSynonyms()); collection.insertOne(document); mongo.close(); } catch (Exception ex) { mongo.close(); LogUtil.log(ex.toString()); } }
From source file:org.search.system.managers.DatabaseManager.java
License:Open Source License
private List<Document> fetchFromInstance(String databaseName, String collectionName, Document query, MongoInstance instance) {/*from w ww . j a va 2 s . c om*/ List<Document> result = new ArrayList<>(); MongoClient mongo = new MongoClient(instance.getHost(), instance.getPort()); try { MongoDatabase database = mongo.getDatabase(databaseName); MongoCollection<Document> data = database.getCollection(collectionName); FindIterable<Document> cursor; if (query == null) { cursor = data.find(); } else { cursor = data.find(query); } if (cursor == null) { return result; } for (Document doc : cursor) { result.add(doc); } mongo.close(); } catch (Exception ex) { mongo.close(); LogUtil.log(ex.toString()); } return result; }
From source file:org.search.system.managers.DatabaseManager.java
License:Open Source License
public Document findOne(String databaseName, String collectionName, Document query) { Document result = null;// w ww. ja va 2 s.com for (MongoInstance instance : instances) { MongoClient mongo = new MongoClient(instance.getHost(), instance.getPort()); try { MongoDatabase database = mongo.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(collectionName); result = collection.find(query).first(); mongo.close(); } catch (Exception ex) { LogUtil.log(ex.toString()); mongo.close(); } if (result != null) { break; } } return result; }
From source file:org.siphon.jsmongo.MongoExecutor.java
License:Open Source License
public MongoExecutor(MongoClient mongoClient, String database, ScriptEngine jsEngine) { this.mongoClient = mongoClient; this.database = mongoClient.getDatabase(database); this.jsEngine = jsEngine; jsTypeUtil = new JsTypeUtil(jsEngine); this.JSON = new org.siphon.common.js.JSON(jsEngine); }
From source file:org.springframework.data.mongodb.microbenchmark.MongoResultsWriter.java
License:Apache License
@Override public void write(Collection<RunResult> results) { Date now = new Date(); StandardEnvironment env = new StandardEnvironment(); String projectVersion = env.getProperty("project.version", "unknown"); String gitBranch = env.getProperty("git.branch", "unknown"); String gitDirty = env.getProperty("git.dirty", "no"); String gitCommitId = env.getProperty("git.commit.id", "unknown"); MongoClientURI uri = new MongoClientURI(this.uri); MongoClient client = new MongoClient(uri); String dbName = StringUtils.hasText(uri.getDatabase()) ? uri.getDatabase() : "spring-data-mongodb-benchmarks"; MongoDatabase db = client.getDatabase(dbName); for (BasicDBObject dbo : (List<BasicDBObject>) JSON.parse(ResultsWriter.jsonifyResults(results))) { String collectionName = extractClass(dbo.get("benchmark").toString()); Document sink = new Document(); sink.append("_version", projectVersion); sink.append("_branch", gitBranch); sink.append("_commit", gitCommitId); sink.append("_dirty", gitDirty); sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString())); sink.append("_date", now); sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot")); sink.putAll(dbo);// w ww.j a v a 2 s .co m db.getCollection(collectionName).insertOne(fixDocumentKeys(sink)); } client.close(); }