Search in sources :

Example 16 with BsonString

use of org.bson.BsonString in project jackrabbit-oak by apache.

the class MongoUtilsTest method response.

private static BsonDocument response(int code) {
    BsonDocument response = new BsonDocument();
    response.put("code", new BsonInt32(code));
    response.put("errmsg", new BsonString("message"));
    return response;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 17 with BsonString

use of org.bson.BsonString in project incubator-skywalking by apache.

the class MongoDBMethodInterceptorTest method setUp.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setUp() throws Exception {
    interceptor = new MongoDBMethodInterceptor();
    Config.Plugin.MongoDB.TRACE_PARAM = true;
    when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("127.0.0.1:27017");
    BsonDocument document = new BsonDocument();
    document.append("name", new BsonString("by"));
    MongoNamespace mongoNamespace = new MongoNamespace("test.user");
    Decoder decoder = PowerMockito.mock(Decoder.class);
    FindOperation findOperation = new FindOperation(mongoNamespace, decoder);
    findOperation.filter(document);
    arguments = new Object[] { findOperation };
    argumentTypes = new Class[] { findOperation.getClass() };
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Decoder(org.bson.codecs.Decoder) FindOperation(com.mongodb.operation.FindOperation) Before(org.junit.Before)

Example 18 with BsonString

use of org.bson.BsonString in project drill by axbaretto.

the class TestBsonRecordReader method testSpecialCharStringType.

@Test
public void testSpecialCharStringType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    bsonDoc.append("stringKey", new BsonString("§§§§§§§§§1"));
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    assertEquals("§§§§§§§§§1", mapReader.reader("stringKey").readText().toString());
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonString(org.bson.BsonString) BsonDocumentReader(org.bson.BsonDocumentReader) Test(org.junit.Test)

Example 19 with BsonString

use of org.bson.BsonString in project drill by apache.

the class MongoPluginImplementor method implement.

@Override
public void implement(PluginProjectRel project) throws IOException {
    runAggregate = runAggregate || project.getProjects().stream().anyMatch(expression -> !expression.isA(SqlKind.INPUT_REF));
    visitChild(project.getInput());
    if (runAggregate) {
        RexToMongoTranslator translator = new RexToMongoTranslator((JavaTypeFactory) project.getCluster().getTypeFactory(), MongoAggregateUtils.mongoFieldNames(project.getInput().getRowType()));
        List<BsonElement> items = new ArrayList<>();
        for (Pair<RexNode, String> pair : project.getNamedProjects()) {
            String name = pair.right;
            BsonValue expr = pair.left.accept(translator);
            items.add(expr.equals(new BsonString("$" + name)) ? new BsonElement(MongoAggregateUtils.maybeQuote(name), new BsonInt32(1)) : new BsonElement(MongoAggregateUtils.maybeQuote(name), expr));
        }
        BsonDocument projection = Aggregates.project(new BsonDocument(items)).toBsonDocument();
        operations.add(projection);
        List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
        this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
    } else {
        List<String> outNames = MongoAggregateUtils.mongoFieldNames(project.getRowType());
        this.columns = outNames.stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
    }
}
Also used : ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonElement(org.bson.BsonElement) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) SchemaPath(org.apache.drill.common.expression.SchemaPath) BsonString(org.bson.BsonString) RexNode(org.apache.calcite.rex.RexNode) BsonValue(org.bson.BsonValue)

Example 20 with BsonString

use of org.bson.BsonString in project drill by apache.

the class RexToMongoTranslator method visitCall.

@Override
public BsonValue visitCall(RexCall call) {
    String name = isItem(call);
    if (name != null) {
        return new BsonString("'$" + name + "'");
    }
    List<BsonValue> strings = call.operands.stream().map(operand -> operand.accept(this)).collect(Collectors.toList());
    if (call.getKind() == SqlKind.CAST) {
        return strings.get(0);
    }
    String stdOperator = MONGO_OPERATORS.get(call.getOperator());
    if (stdOperator != null) {
        return new BsonDocument(stdOperator, new BsonArray(strings));
    }
    if (call.getOperator() == SqlStdOperatorTable.ITEM) {
        RexNode op1 = call.operands.get(1);
        if (op1 instanceof RexLiteral) {
            if (op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
                return new BsonDocument("$arrayElemAt", new BsonArray(Arrays.asList(strings.get(0), new BsonInt32(((RexLiteral) op1).getValueAs(Integer.class)))));
            } else if (op1.getType().getSqlTypeName() == SqlTypeName.CHAR) {
                return new BsonString(strings.get(0).asString().getValue() + "." + ((RexLiteral) op1).getValueAs(String.class));
            }
        }
    }
    if (call.getOperator() == SqlStdOperatorTable.CASE) {
        // case(a, b, c)  -> $cond:[a, b, c]
        // case(a, b, c, d) -> $cond:[a, b, $cond:[c, d, null]]
        // case(a, b, c, d, e) -> $cond:[a, b, $cond:[c, d, e]]
        BsonDocument result = new BsonDocument();
        BsonArray args = new BsonArray();
        result.put("$cond", args);
        for (int i = 0; i < strings.size(); i += 2) {
            args.add(strings.get(i));
            args.add(strings.get(i + 1));
            if (i == strings.size() - 3) {
                args.add(strings.get(i + 2));
                break;
            }
            if (i == strings.size() - 2) {
                args.add(BsonNull.VALUE);
                break;
            }
            BsonArray innerArgs = new BsonArray();
            BsonDocument innerDocument = new BsonDocument();
            innerDocument.put("$cond", innerArgs);
            args.add(innerDocument);
            args = innerArgs;
        }
        return result;
    }
    throw new IllegalArgumentException("Translation of " + call + " is not supported by MongoProject");
}
Also used : RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Arrays(java.util.Arrays) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) BsonNull(org.bson.BsonNull) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexLiteral(org.apache.calcite.rex.RexLiteral) RexImpTable(org.apache.calcite.adapter.enumerable.RexImpTable) BsonString(org.bson.BsonString) Collectors(java.util.stream.Collectors) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) RexInputRef(org.apache.calcite.rex.RexInputRef) MongoOp(org.apache.drill.exec.store.mongo.common.MongoOp) List(java.util.List) ImmutableMap(org.apache.drill.shaded.guava.com.google.common.collect.ImmutableMap) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RexNode(org.apache.calcite.rex.RexNode) BsonArray(org.bson.BsonArray) Map(java.util.Map) BsonInt32(org.bson.BsonInt32) SqlOperator(org.apache.calcite.sql.SqlOperator) RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

BsonString (org.bson.BsonString)168 BsonDocument (org.bson.BsonDocument)146 BsonInt32 (org.bson.BsonInt32)54 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)33 BsonValue (org.bson.BsonValue)31 Document (org.bson.Document)28 BsonInt64 (org.bson.BsonInt64)27 ArrayList (java.util.ArrayList)23 Map (java.util.Map)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 BsonDouble (org.bson.BsonDouble)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 Test (org.junit.jupiter.api.Test)11 List (java.util.List)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9 BsonBoolean (org.bson.BsonBoolean)9