/** * Checks to see if a collection with a given name exists on a server. * * @param collectionName a name of the collection to test for existence * @return {@code false} if no collection by that name exists, {@code true} if a match to an existing collection was found * @throws MongoException if the operation failed */ public boolean collectionExists(final String collectionName) { Set<String> collectionNames = getCollectionNames(); for (final String name : collectionNames) { if (name.equalsIgnoreCase(collectionName)) { return true; } } return false; }
protected boolean ensureIsReplicaSet(MongoClient shardClient) { Set<String> collections = shardClient.getDB(MongoDBRiver.MONGODB_LOCAL_DATABASE).getCollectionNames(); if (!collections.contains(MongoDBRiver.OPLOG_COLLECTION)) { throw new IllegalStateException("Cannot find " + MongoDBRiver.OPLOG_COLLECTION + " collection. Please check this link: http://docs.mongodb.org/manual/tutorial/deploy-replica-set/"); } return true; }
for (String name : slurpedDb.getCollectionNames()) { if (name.length() < 7 || !name.substring(0, 7).equals("system.")) { DBCollection collection = slurpedDb.getCollection(name);
private void addToStream(final Operation operation, final Timestamp<?> currentTimestamp, final DBObject data, final String collection) throws InterruptedException { if (logger.isTraceEnabled()) { String dataString = data.toString(); if (dataString.length() > 400) { logger.trace("addToStream - operation [{}], currentTimestamp [{}], data (_id:[{}], serialized length:{}), collection [{}]", operation, currentTimestamp, data.get("_id"), dataString.length(), collection); } else { logger.trace("addToStream - operation [{}], currentTimestamp [{}], data [{}], collection [{}]", operation, currentTimestamp, dataString, collection); } } if (operation == Operation.DROP_DATABASE) { logger.info("addToStream - Operation.DROP_DATABASE, currentTimestamp [{}], data [{}], collection [{}]", currentTimestamp, data, collection); if (definition.isImportAllCollections()) { for (String name : slurpedDb.getCollectionNames()) { logger.info("addToStream - isImportAllCollections - Operation.DROP_DATABASE, currentTimestamp [{}], data [{}], collection [{}]", currentTimestamp, data, name); context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, Operation.DROP_COLLECTION, data, name)); } } else { context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, Operation.DROP_COLLECTION, data, collection)); } } else { context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, operation, data, collection)); } }
private void addToStream(final Operation operation, final Timestamp<?> currentTimestamp, final DBObject data, final String collection) throws InterruptedException { if (logger.isTraceEnabled()) { String dataString = data.toString(); if (dataString.length() > 400) { logger.trace("addToStream - operation [{}], currentTimestamp [{}], data (_id:[{}], serialized length:{}), collection [{}]", operation, currentTimestamp, data.get("_id"), dataString.length(), collection); } else { logger.trace("addToStream - operation [{}], currentTimestamp [{}], data [{}], collection [{}]", operation, currentTimestamp, dataString, collection); } } if (operation == Operation.DROP_DATABASE) { logger.info("addToStream - Operation.DROP_DATABASE, currentTimestamp [{}], data [{}], collection [{}]", currentTimestamp, data, collection); if (definition.isImportAllCollections()) { for (String name : slurpedDb.getCollectionNames()) { logger.info("addToStream - isImportAllCollections - Operation.DROP_DATABASE, currentTimestamp [{}], data [{}], collection [{}]", currentTimestamp, data, name); context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, Operation.DROP_COLLECTION, data, name)); } } else { context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, Operation.DROP_COLLECTION, data, collection)); } } else { context.getStream().put(new MongoDBRiver.QueueEntry(currentTimestamp, operation, data, collection)); } }
private void addQueryToStream(final Operation operation, final Timestamp<?> currentTimestamp, final DBObject update, final String collection) throws InterruptedException { if (logger.isTraceEnabled()) { logger.trace("addQueryToStream - operation [{}], currentTimestamp [{}], update [{}]", operation, currentTimestamp, update); } if (collection == null) { for (String name : slurpedDb.getCollectionNames()) { DBCollection slurpedCollection = slurpedDb.getCollection(name); addQueryToStream(operation, currentTimestamp, update, name, slurpedCollection); } } else { DBCollection slurpedCollection = slurpedDb.getCollection(collection); addQueryToStream(operation, currentTimestamp, update, collection, slurpedCollection); } }
@Override public void ensureCaps() { for (final MappedClass mc : mapper.getMappedClasses()) { if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) { final CappedAt cap = mc.getEntityAnnotation().cap(); final String collName = mapper.getCollectionName(mc.getClazz()); final BasicDBObjectBuilder dbCapOpts = start("capped", true); if (cap.value() > 0) { dbCapOpts.add("size", cap.value()); } if (cap.count() > 0) { dbCapOpts.add("max", cap.count()); } final DB database = getDB(); if (database.getCollectionNames().contains(collName)) { final DBObject dbResult = database.command(start("collstats", collName).get()); if (((CommandResult) dbResult).getBoolean("capped")) { LOG.debug("DBCollection already exists and is capped already; doing nothing. " + dbResult); } else { LOG.warn("DBCollection already exists with same name(" + collName + ") and is not capped; not creating capped version!"); } } else { getDB().createCollection(collName, dbCapOpts.get()); LOG.debug("Created capped DBCollection (" + collName + ") with opts " + dbCapOpts); } } } }
/** * No authentication test. * */ // @Test public void noAuthenticationTest() { try { _PU = "mongoTest"; String dbname = "KunderaAuthTests"; MongoClient m = new MongoClient(new ServerAddress("localhost", 27017)); DB db = m.getDB(dbname); Assert.assertNotNull(db.getCollectionNames()); emf = Persistence.createEntityManagerFactory(_PU); Assert.assertNotNull(emf); EntityManager em = emf.createEntityManager(); Assert.assertNotNull(em); } catch (Exception e) { Assert.fail(e.getMessage()); } }
/** * Authenticate with valid credentials * * */ // @Test public void authenticateWithValidCredentials() { try { _PU = "validAuthenticationMongoPu"; String dbname = "KunderaAuthTests"; String username = "kunderaUser"; String password = "kunderapassword"; MongoCredential credential = MongoCredential.createMongoCRCredential(username, dbname, password.toCharArray()); MongoClient m = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(credential)); DB db = m.getDB(dbname); Assert.assertNotNull(db.getCollectionNames()); m.close(); emf = Persistence.createEntityManagerFactory(_PU); Assert.assertNotNull(emf); EntityManager em = emf.createEntityManager(); Assert.assertNotNull(em); } catch (Exception e) { Assert.fail(e.getMessage()); } }
DB db_admin = m.getDB(adminDb); Assert.assertNotNull(db.getCollectionNames()); Assert.assertNotNull(db_admin.getCollectionNames());
collectionList = db.getCollectionNames();
@Override protected List<Name> createTypeNames() throws IOException { Set<String> collectionNames = new LinkedHashSet<String>(dataStoreDB.getCollectionNames()); Set<String> typeNameSet = new LinkedHashSet<String>();
@Nonnull public Iterable<String> getCollectionNames() { final List<String> collectionNames = new ArrayList<>(); for (final String name : db.getCollectionNames()) { if (name.startsWith(collectionPrefix)) { collectionNames.add(name.substring(collectionPrefix.length())); } } return collectionNames; }
public static Set<String> getSessionDBCollectionNames() { initSessionDB(); return sessiondb.getCollectionNames(); }
private Set<String> getCollectionsList(String dbName) { Set<String> result = Collections.emptySet(); DB db = getDB(dbName); if (db != null) { result = db.getCollectionNames(); } return result; }
private Set<String> getCollectionsList(String dbName) { Set<String> result = Collections.emptySet(); DB db = getDB(dbName); if (db != null) { result = db.getCollectionNames(); } return result; }
private void deleteAllElements(DB mongoDb) { Set<String> collectionaNames = mongoDb.getCollectionNames(); for (String collectionName : collectionaNames) { if (isNotASystemCollection(collectionName)) { LOGGER.debug("Dropping Collection {}.", collectionName); DBCollection dbCollection = mongoDb.getCollection(collectionName); // Delete ALL, No DROP dbCollection.remove(new BasicDBObject(0)); } } }
public static final void strictAssertEquals(DBObject expectedData, DB mongoDb) { Set<String> collectionaNames = expectedData.keySet(); Set<String> mongodbCollectionNames = mongoDb.getCollectionNames(); checkCollectionsName(collectionaNames, mongodbCollectionNames); for (String collectionName : collectionaNames) { checkCollectionObjects(expectedData, mongoDb, collectionaNames, collectionName); } }
public void cleanDatabase(String dbName) { DB db = mongo.getDB(dbName); Set<String> allColNames = db.getCollectionNames(); for (String colName : allColNames) { if (isSystemClollection(colName)) { logger.info("don't drop collection {}", colName); } else { DBCollection dbCollection = db.getCollection(colName); dbCollection.drop(); } } }
@Test public void deleteNonExistentCollection() throws Exception { String methodName = "testDeleteNonExistantCollection"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); try { client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName); Fail.fail("shouldn't get here"); } catch (ResourceNotFoundException rnfe) { // expected } }