Search in sources :

Example 1 with AggregationOutput

use of com.mongodb.AggregationOutput in project teiid by teiid.

the class TestMongoDBDirectQueryExecution method testDirect.

@Test
public void testDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
    Argument arg2 = new Argument(Direction.IN, null, String.class, null);
    arg2.setArgumentValue(new Literal("foo", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : AggregationOptions(com.mongodb.AggregationOptions) Argument(org.teiid.language.Argument) AggregationOutput(com.mongodb.AggregationOutput) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) BasicDBObject(com.mongodb.BasicDBObject) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 2 with AggregationOutput

use of com.mongodb.AggregationOutput in project sissi by KimShen.

the class MongoMucRelationContext method affiliation.

/**
 * 获取岗位
 *
 * @param from
 * @param to
 * @param def 默认值
 * @return
 */
private ItemAffiliation affiliation(JID from, JID to, ItemAffiliation def) {
    // {"$match":{"jid":to.bare}},{"$unwind":"$affiliations"},{"$match":{"affiliations.jid":from.bare}},{"$project":{"affiliation":"$affiliations.affiliation"}}
    AggregationOutput output = this.config.collection().aggregate(BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_JID, to.asStringWithBare()).get()).get(), this.unwindAffiliation, BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_AFFILIATIONS + "." + Dictionary.FIELD_JID, from.asStringWithBare()).get()).get(), this.projectAffiliations);
    @SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
    return result.isEmpty() ? def : ItemAffiliation.parse(MongoUtils.asString(DBObject.class.cast(result.get(0)), Dictionary.FIELD_AFFILIATION).toString());
}
Also used : AggregationOutput(com.mongodb.AggregationOutput)

Example 3 with AggregationOutput

use of com.mongodb.AggregationOutput in project incubator-skywalking by apache.

the class MongoDBCollectionMethodInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    AbstractSpan activeSpan = ContextManager.activeSpan();
    CommandResult cresult = null;
    if (ret instanceof WriteResult) {
        WriteResult wresult = (WriteResult) ret;
        cresult = wresult.getCachedLastError();
    } else if (ret instanceof AggregationOutput) {
        AggregationOutput aresult = (AggregationOutput) ret;
        cresult = aresult.getCommandResult();
    }
    if (null != cresult && !cresult.ok()) {
        activeSpan.log(cresult.getException());
    }
    ContextManager.stopSpan();
    return ret;
}
Also used : WriteResult(com.mongodb.WriteResult) AggregationOutput(com.mongodb.AggregationOutput) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan) CommandResult(com.mongodb.CommandResult)

Example 4 with AggregationOutput

use of com.mongodb.AggregationOutput in project sissi by KimShen.

the class MongoMucRelation4AffiliationContext method myRelations.

/*
	 * 岗位相符的订阅关系
	 * 
	 * @see com.sissi.ucenter.relation.muc.MucRelationContext#myRelations(com.sissi.context.JID, java.lang.String)
	 */
public Set<Relation> myRelations(JID from, String affiliation) {
    // {"$match":{"jid":group.bare}}, {"$unwind":"$affiliations"}, {"$match":{"affiliations.affiliation":Xxx}}, {"$project":{"affiliation":"$affiliations"}}
    AggregationOutput output = super.config.collection().aggregate(super.buildMatcher(from), super.unwindAffiliation, BasicDBObjectBuilder.start("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_AFFILIATIONS + "." + Dictionary.FIELD_AFFILIATION, affiliation).get()).get(), this.projectAffiliation);
    @SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
    return result.isEmpty() ? super.relations : new AffiliationRelations(result);
}
Also used : AggregationOutput(com.mongodb.AggregationOutput)

Example 5 with AggregationOutput

use of com.mongodb.AggregationOutput in project sissi by KimShen.

the class MongoMucRelationContext method mapping.

@Override
public JIDs mapping(JID group) {
    // {"$match":{"jid":group.bare}}, {"$unwind":"$roles"}, {"$match":{"roles.nick":Xxx}}, {"$project":{"roles":"$roles"}}, {"$group":{"_id":"$roles.jid","resource":{"$push":"$roles.resource"}}}
    AggregationOutput output = this.config.collection().aggregate(this.buildMatcher(group), this.unwindRoles, BasicDBObjectBuilder.start().add("$match", BasicDBObjectBuilder.start(Dictionary.FIELD_ROLES + "." + Dictionary.FIELD_NICK, group.resource()).get()).get(), this.projectRoles, this.groupMapping);
    @SuppressWarnings("deprecation") List<?> result = MongoUtils.asList(output.getCommandResult(), Dictionary.FIELD_RESULT);
    return result.isEmpty() ? this.jids : this.extract(DBObject.class.cast(result.get(0)));
}
Also used : AggregationOutput(com.mongodb.AggregationOutput)

Aggregations

AggregationOutput (com.mongodb.AggregationOutput)10 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)3 CommandResult (com.mongodb.CommandResult)2 DBCollection (com.mongodb.DBCollection)2 AggregationOptions (com.mongodb.AggregationOptions)1 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DB (com.mongodb.DB)1 MongoException (com.mongodb.MongoException)1 WriteResult (com.mongodb.WriteResult)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 InputSplit (org.apache.hadoop.mapreduce.InputSplit)1 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)1 Test (org.junit.Test)1 Argument (org.teiid.language.Argument)1 Command (org.teiid.language.Command)1 Literal (org.teiid.language.Literal)1 MongoDBConnection (org.teiid.mongodb.MongoDBConnection)1 ExecutionContext (org.teiid.translator.ExecutionContext)1