private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoClient mongoClient, final MongoDatabase database) { this.morphia = morphia; this.mapper = mapper; this.mongoClient = mongoClient; this.database = database.withCodecRegistry(CodecRegistries.fromRegistries( mongoClient.getMongoClientOptions().getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry())); this.db = mongoClient.getDB(database.getName()); this.defConcern = mongoClient.getWriteConcern(); this.indexHelper = new IndexHelper(mapper, database); }
protected Message<T, R> createMessage(T source, Class<R> targetType, RequestOptions options) { SimpleMessage<T, T> message = new SimpleMessage<>(source, source, MessageProperties.builder() .databaseName(template.getDb().getName()).collectionName(options.getCollectionName()).build()); return new LazyMappingDelegatingMessage<>(message, targetType, template.getConverter()); }
public List<MongoDatabase> loadDatabases(MongoServer mongoServer, ServerConfiguration configuration) { final List<MongoDatabase> mongoDatabases = new LinkedList<>(); TaskWithReturnedObject<List<MongoDatabase>> perform = mongoClient -> { String userDatabase = configuration.getUserDatabase(); if (StringUtils.isNotEmpty(userDatabase)) { com.mongodb.client.MongoDatabase database = mongoClient.getDatabase(userDatabase); MongoDatabase mongoDatabase = new MongoDatabase(database.getName(), mongoServer); mongoDatabases.add(createMongoDatabaseAndItsCollections(mongoDatabase, database)); } else { MongoIterable<String> databaseNames = mongoClient.listDatabaseNames(); for (String databaseName : databaseNames) { com.mongodb.client.MongoDatabase database = mongoClient.getDatabase(databaseName); MongoDatabase mongoDatabase = new MongoDatabase(database.getName(), mongoServer); mongoDatabases.add(createMongoDatabaseAndItsCollections(mongoDatabase, database)); } } return mongoDatabases; }; return executeTask(configuration, perform); }
protected Message<T, R> createMessage(T source, Class<R> targetType, RequestOptions options) { SimpleMessage<T, T> message = new SimpleMessage<>(source, source, MessageProperties.builder() .databaseName(template.getDb().getName()).collectionName(options.getCollectionName()).build()); return new LazyMappingDelegatingMessage<>(message, targetType, template.getConverter()); }
/** * Parameterized constructor with collection. * * @param collection * the collection to set */ public PropertyStoreMongo(MongoDatabase db, String collectionName) { this.propertiesCollection = db.getCollection(collectionName); this.dbName = db.getName(); }
/** * Parameterized constructor with collection. * * @param collection * the collection to set */ public FeatureStoreMongo(MongoDatabase db, String collectionName) { this.dbName = db.getName(); this.featuresCollection = db.getCollection(collectionName); }
@NotNull @Override public String getName() { return db.getName(); }
private void insertShardKeyPattern(MongoDatabase mongoDb, MongoClient mongoClient, String collectionName, Document collection) { String databaseName = mongoDb.getName(); String collectionWithDatabase = databaseName + DATABASE_COLLECTION_SEPARATOR + collectionName; Document shardKeys = shardKeys(collection); MongoDbCommands.shardCollection(mongoClient, collectionWithDatabase, shardKeys); }
public Document findRequiredChangeAndAuthorIndex(MongoDatabase db) { MongoCollection<Document> indexes = db.getCollection("system.indexes"); Document index = indexes.find(new Document() .append("ns", db.getName() + "." + changelogCollectionName) .append("key", new Document().append(ChangeEntry.KEY_CHANGEID, 1).append(ChangeEntry.KEY_AUTHOR, 1)) ).first(); return index; }
@Test public void testListDatabaseNames() throws Exception { assertThat(listDatabaseNames()).isEmpty(); collection.insertOne(json("")); assertThat(listDatabaseNames()).containsExactly(db.getName()); getDatabase().getCollection("some-collection").insertOne(json("")); assertThat(listDatabaseNames()).containsExactly("bar", db.getName()); }
public Document findRequiredChangeAndAuthorIndex(MongoDatabase db) { MongoCollection<Document> indexes = db.getCollection("system.indexes"); Document index = indexes.find(new Document() .append("ns", db.getName() + "." + changelogCollectionName) .append("key", new Document().append(ChangeEntry.KEY_CHANGEID, 1).append(ChangeEntry.KEY_AUTHOR, 1)) ).first(); return index; }
private String extractUrl(MongoDbFactory mongoDbFactory) { Cluster cluster = invokeMethod(this.<MongoClient>getField(mongoDbFactory, "mongoClient"), "getCluster"); return cluster.getSettings().getHosts().stream() .map(serverAddress -> String.format("mongodb://%s:%d/%s", serverAddress.getHost(), serverAddress.getPort(), mongoDbFactory.getDb().getName())) .collect(Collectors.joining(",")); }
@Test public void testListCollectionsEmpty() throws Exception { Document result = db.runCommand(json("listCollections: 1")); assertThat(result.getDouble("ok")).isEqualTo(1.0); Document cursor = (Document) result.get("cursor"); assertThat(cursor.keySet()).containsExactly("id", "ns", "firstBatch"); assertThat(cursor.get("id")).isEqualTo(Long.valueOf(0)); assertThat(cursor.get("ns")).isEqualTo(db.getName() + ".$cmd.listCollections"); List<?> firstBatch = (List<?>) cursor.get("firstBatch"); assertThat(firstBatch).isEmpty(); }
private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoClient mongoClient, final MongoDatabase database) { this.morphia = morphia; this.mapper = mapper; this.mongoClient = mongoClient; this.database = database; this.db = mongoClient.getDB(database.getName()); this.defConcern = mongoClient.getWriteConcern(); this.indexHelper = new IndexHelper(mapper, database); }
@Test public void testRenameCollection_dropTarget() throws Exception { collection.insertOne(json("_id: 1")); collection.insertOne(json("_id: 2")); collection.insertOne(json("_id: 3")); MongoCollection<Document> otherCollection = db.getCollection("other-collection-name"); otherCollection.insertOne(json("_id: 1")); collection.renameCollection(new MongoNamespace(db.getName(), "other-collection-name"), new RenameCollectionOptions().dropTarget(true)); List<String> collectionNames = toArray(db.listCollectionNames()); assertThat(collectionNames).containsExactly("other-collection-name"); assertThat(getCollection("other-collection-name").countDocuments()).isEqualTo(3); }
@Test public void testDropDatabaseDropsAllData() throws Exception { collection.insertOne(json("_id: 1")); MongoCollection<Document> collection2 = getCollection("testcoll2"); collection2.insertOne(json("_id: 1")); syncClient.dropDatabase(db.getName()); assertThat(listDatabaseNames()).doesNotContain(db.getName()); assertThat(collection.countDocuments()).isZero(); assertThat(toArray(db.listCollectionNames())).doesNotContain(collection.getNamespace().getCollectionName(), collection2.getNamespace().getCollectionName()); collection.insertOne(json("_id: 1")); collection2.insertOne(json("_id: 1")); }
@Override public void setUp() throws Exception { super.setUp(); MongoDBEntityStoreService es = serviceFinder.findService( MongoDBEntityStoreService.class ).get(); mongo = es.mongoInstanceUsed(); dbName = es.dbInstanceUsed().getName(); }
@Override public void setUp() throws Exception { super.setUp(); MongoDBEntityStoreService es = serviceFinder.findService( MongoDBEntityStoreService.class ).get(); mongo = es.mongoInstanceUsed(); dbName = es.dbInstanceUsed().getName(); }
@Override public void setUp() throws Exception { super.setUp(); Module storageModule = application.findModule( "Infrastructure Layer", "Storage Module" ); MongoDBEntityStoreService es = storageModule.serviceFinder().findService( MongoDBEntityStoreService.class ).get(); mongo = es.mongoInstanceUsed(); dbName = es.dbInstanceUsed().getName(); }
@Before public void before() { c = MongoUtils.getConnection(); assumeTrue(c != null); dbName = c.getDatabase().getName(); MongoUtils.dropCollections(c.getDatabase()); builder = new DocumentMK.Builder().setMongoDB(c.getMongoClient(), c.getDBName()); store = (MongoDocumentStore) builder.getDocumentStore(); ns = builder.getNodeStore(); }