public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { collection.drop(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Dropped collection [{}]", collection.getNamespace() != null ? collection.getNamespace().getCollectionName() : collectionName); } return null; } });
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindIterable<Document> iterable = collection.find(query, Document.class); if (LOGGER.isDebugEnabled()) { LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), serializeToJsonSafely(fields.orElseGet(Document::new)), collection.getNamespace() != null ? collection.getNamespace().getFullName() : "n/a"); } if (fields.isPresent()) { iterable = iterable.projection(fields.get()); } return iterable.first(); } }
/** {@inheritDoc} */ @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { final String methodName = method.getName(); if (methodName.startsWith("with") && method.getReturnType() != null && MongoCollection.class.isAssignableFrom(method.getReturnType())) { // inutile de monitorer withDocumentClass(...), etc // mais il faut monitorer la nouvelle instance de MongoCollection en retour MongoCollection<?> result = (MongoCollection<?>) method.invoke(collection, args); final MongoNamespace namespace = collection.getNamespace(); final String name; if (namespace != null) { name = namespace.getFullName(); } else { name = methodName; } result = createCollectionProxy(result, name); return result; } else if (methodName.startsWith("get")) { // inutile de monitorer getDocumentClass(), getNamespace(), etc return method.invoke(collection, args); } final String requestName = collectionName + '.' + method.getName(); return doInvoke(collection, method, args, requestName); } }
protected Map<String, String> getAttributes(ProcessContext context, FlowFile input, Document query, MongoCollection collection) { final Map<String, String> attributes = new HashMap<>(); attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json"); if (context.getProperty(QUERY_ATTRIBUTE).isSet()) { final String queryAttr = context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(input).getValue(); attributes.put(queryAttr, query.toJson()); } attributes.put(DB_NAME, collection.getNamespace().getDatabaseName()); attributes.put(COL_NAME, collection.getNamespace().getCollectionName()); return attributes; } }
/** {@inheritDoc} */ @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { Object result = method.invoke(database, args); if (result instanceof MongoCollection && args != null && args.length > 0 && args[0] instanceof String) { final MongoCollection<?> collection = (MongoCollection<?>) result; final MongoNamespace namespace = collection.getNamespace(); final String name; if (namespace != null) { name = namespace.getFullName(); } else { name = (String) args[0]; } result = createCollectionProxy(collection, name); } else if (result instanceof MongoDatabase) { // il faut monitorer la nouvelle instance de MongoDatabase en retour result = createDatabaseProxy((MongoDatabase) result); } return result; } }
@Override public Boolean doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { return doCount(collection.getNamespace().getCollectionName(), mappedQuery, new CountOptions().limit(1).collation(collation)) > 0; } }
coll.getNamespace() != null ? coll.getNamespace().getCollectionName() : collectionName);
public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { collection.drop(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Dropped collection [{}]", collection.getNamespace() != null ? collection.getNamespace().getCollectionName() : collectionName); } return null; } });
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindIterable<Document> iterable = collection.find(query, Document.class); if (LOGGER.isDebugEnabled()) { LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), serializeToJsonSafely(fields.orElseGet(Document::new)), collection.getNamespace() != null ? collection.getNamespace().getFullName() : "n/a"); } if (fields.isPresent()) { iterable = iterable.projection(fields.get()); } return iterable.first(); } }
@Override public Boolean doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { return doCount(collection.getNamespace().getCollectionName(), mappedQuery, new CountOptions().limit(1).collation(collation)) > 0; } }
coll.getNamespace() != null ? coll.getNamespace().getCollectionName() : collectionName);
@NotNull @Override public Map<String, String> getStats() { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); List<MongoCollection<?>> all = ImmutableList.of(nodes, clusterNodes, settings, journal); all.forEach(c -> toMapBuilder(builder, db.runCommand( new BasicDBObject("collStats", c.getNamespace().getCollectionName()), BasicDBObject.class), c.getNamespace().getCollectionName())); return builder.build(); }
@NotNull @Override public Map<String, String> getStats() { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); List<MongoCollection<?>> all = ImmutableList.of(nodes, clusterNodes, settings, journal); all.forEach(c -> toMapBuilder(builder, db.runCommand( new BasicDBObject("collStats", c.getNamespace().getCollectionName()), BasicDBObject.class), c.getNamespace().getCollectionName())); return builder.build(); }
@NotNull @Override public MongoNamespace getNamespace() { return collection.getNamespace(); }
@Before @SuppressWarnings("unchecked") public void setUp() { collection = Mockito.mock(MongoCollection.class); Mockito.when(collection.getNamespace()).thenReturn(new MongoNamespace("db", "collection")); }
@Test public void testValidate() throws Exception { assertThatExceptionOfType(MongoCommandException.class) .isThrownBy(() -> db.runCommand(new Document("validate", collection.getNamespace().getCollectionName()))) .withMessageContaining("Command failed with error 26 (NamespaceNotFound): 'ns not found'"); collection.insertOne(json("_id: 1")); collection.insertOne(json("_id: 2")); collection.insertOne(json("_id: 3")); collection.deleteOne(json("_id: 2")); Document result = db.runCommand(new Document("validate", collection.getNamespace().getCollectionName())); assertThat(result.get("nrecords")).isEqualTo(2); }
@Test public void testFindAndModifyCommandUpdate() throws Exception { collection.insertOne(json("_id: 1")); Document cmd = new Document("findAndModify", collection.getNamespace().getCollectionName()); cmd.put("query", json("_id: 1")); cmd.put("update", json("$inc: {a: 1}")); Document result = db.runCommand(cmd); assertThat(result.get("lastErrorObject")).isEqualTo(json("updatedExisting: true, n: 1")); assertThat(result.getDouble("ok")).isEqualTo(1.0); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: 1")); }
private void prepareDB(MongoCollection<BasicDBObject> nodes, MongoCollection<BasicDBObject> blobs) { MongoUtils.dropCollections(nodes.getNamespace().getDatabaseName()); MongoUtils.dropCollections(blobs.getNamespace().getDatabaseName()); createTestNodes(nodes); }
@Test public void testFindAndModifyCommandIllegalOp() throws Exception { collection.insertOne(json("_id: 1")); Document cmd = new Document("findAndModify", collection.getNamespace().getCollectionName()); cmd.put("query", json("_id: 1")); cmd.put("update", new Document("$inc", json("_id: 1"))); assertThat(collection.find().first()).isEqualTo(json("_id: 1")); assertThatExceptionOfType(MongoCommandException.class) .isThrownBy(() -> db.runCommand(cmd)) .withMessageContaining("Command failed with error 66 (ImmutableField): 'Performing an update on the path '_id' would modify the immutable field '_id'"); }
@Test public void testQueryWithIllegalReference() throws Exception { collection.insertOne(json("_id: 1")); String collectionName = collection.getNamespace().getCollectionName(); collection.insertOne(new Document("_id", 2).append("ref", new DBRef(collectionName, 1))); collection.insertOne(new Document("_id", 3).append("ref", new DBRef(collectionName, 2))); assertThatExceptionOfType(MongoQueryException.class) .isThrownBy(() -> collection.find(json("ref: {$ref: 'coll'}")).first()) .withMessageContaining("Query failed with error code 2 and error message 'unknown operator: $ref'"); }