Search in sources :

Example 6 with CommandResult

use of com.mongodb.CommandResult in project graylog2-server by Graylog2.

the class MongoProbe method createBuildInfo.

private static BuildInfo createBuildInfo(DB adminDb) {
    final BuildInfo buildInfo;
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        buildInfo = BuildInfo.create(buildInfoResult.getString("version"), buildInfoResult.getString("gitVersion"), buildInfoResult.getString("sysInfo"), buildInfoResult.getString("loaderFlags"), buildInfoResult.getString("compilerFlags"), buildInfoResult.getString("allocator"), (List<Integer>) buildInfoResult.get("versionArray"), buildInfoResult.getString("javascriptEngine"), buildInfoResult.getInt("bits"), buildInfoResult.getBoolean("debug"), buildInfoResult.getLong("maxBsonObjectSize"));
    } else {
        LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage());
        buildInfo = null;
    }
    return buildInfo;
}
Also used : List(java.util.List) CommandResult(com.mongodb.CommandResult)

Example 7 with CommandResult

use of com.mongodb.CommandResult 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 8 with CommandResult

use of com.mongodb.CommandResult in project embedmongo-maven-plugin by joelittlejohn.

the class MongoScriptsMojo method executeStart.

@Override
public void executeStart() throws MojoExecutionException, MojoFailureException {
    DB db = connectToMongoAndGetDatabase();
    if (scriptsDirectory.isDirectory()) {
        Scanner scanner = null;
        StringBuilder instructions = new StringBuilder();
        File[] files = scriptsDirectory.listFiles();
        if (files == null) {
            getLog().info("Can't read scripts directory: " + scriptsDirectory.getAbsolutePath());
        } else {
            getLog().info("Folder " + scriptsDirectory.getAbsolutePath() + " contains " + files.length + " file(s):");
            for (File file : files) {
                if (file.isFile()) {
                    try {
                        if (scriptCharsetEncoding == null) {
                            scanner = new Scanner(file);
                        } else {
                            // no need to check encoding, the constructor throws
                            // an IllegalArgumentException if the charset cannot be determined
                            // from the provided value.
                            scanner = new Scanner(file, scriptCharsetEncoding);
                        }
                        while (scanner.hasNextLine()) {
                            instructions.append(scanner.nextLine()).append("\n");
                        }
                    } catch (FileNotFoundException e) {
                        throw new MojoExecutionException("Unable to find file with name '" + file.getName() + "'", e);
                    } catch (IllegalArgumentException e) {
                        throw new MojoExecutionException("Unable to determine charset encoding for provided charset '" + scriptCharsetEncoding + "'", e);
                    } finally {
                        if (scanner != null) {
                            scanner.close();
                        }
                    }
                    CommandResult result;
                    try {
                        result = db.doEval("(function() {" + instructions.toString() + "})();", new Object[0]);
                    } catch (MongoException e) {
                        throw new MojoExecutionException("Unable to execute file with name '" + file.getName() + "'", e);
                    }
                    if (!result.ok()) {
                        getLog().error("- file " + file.getName() + " parsed with error: " + result.getErrorMessage());
                        throw new MojoExecutionException("Error while executing instructions from file '" + file.getName() + "': " + result.getErrorMessage(), result.getException());
                    }
                    getLog().info("- file " + file.getName() + " parsed successfully");
                }
            }
        }
        getLog().info("Data initialized with success");
    }
}
Also used : Scanner(java.util.Scanner) MongoException(com.mongodb.MongoException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DB(com.mongodb.DB) CommandResult(com.mongodb.CommandResult)

Example 9 with CommandResult

use of com.mongodb.CommandResult in project embedmongo-maven-plugin by joelittlejohn.

the class MongoScriptsMojoTest method should_fail_to_execute_instruction_with_error.

@Test
public void should_fail_to_execute_instruction_with_error() throws IOException, MojoFailureException, MojoExecutionException {
    DB database = mock(DB.class);
    initFolderWithError();
    CommandResult result = new EmbedMongoDB("myDB").notOkErrorResult("Error while executing instructions from file '" + rootFolderWithError.listFiles()[0].getName());
    given(database.doEval(anyString(), Matchers.<Object>anyVararg())).willReturn(result);
    thrown.expect(MojoExecutionException.class);
    thrown.expectMessage("Error while executing instructions from file '" + rootFolderWithError.listFiles()[0].getName());
    new MongoScriptsMojoForTest(rootFolderWithError, PORT, "myDB", database, null).execute();
}
Also used : EmbedMongoDB(com.mongodb.EmbedMongoDB) DB(com.mongodb.DB) EmbedMongoDB(com.mongodb.EmbedMongoDB) CommandResult(com.mongodb.CommandResult) Test(org.junit.Test)

Example 10 with CommandResult

use of com.mongodb.CommandResult in project symmetric-ds by JumpMind.

the class MongoDatabaseWriter method sql.

@Override
protected boolean sql(CsvData data) {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        DB db = clientManager.getDB(objectMapper.mapToDatabase(this.targetTable));
        String command = data.getParsedData(CsvData.ROW_DATA)[0];
        log.info("About to run command: {}", command);
        CommandResult results = db.command(command);
        log.info("The results of the command were: {}", results);
    } finally {
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
    return true;
}
Also used : DB(com.mongodb.DB) CommandResult(com.mongodb.CommandResult)

Aggregations

CommandResult (com.mongodb.CommandResult)16 BasicDBObject (com.mongodb.BasicDBObject)7 DB (com.mongodb.DB)5 DBCollection (com.mongodb.DBCollection)4 DBObject (com.mongodb.DBObject)3 MongoException (com.mongodb.MongoException)3 AggregationOutput (com.mongodb.AggregationOutput)2 BasicDBList (com.mongodb.BasicDBList)2 MongoClientURI (com.mongodb.MongoClientURI)2 File (java.io.File)2 List (java.util.List)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 DateTime (org.joda.time.DateTime)2 HostAndPort (com.google.common.net.HostAndPort)1 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DBCursor (com.mongodb.DBCursor)1 EmbedMongoDB (com.mongodb.EmbedMongoDB)1 Mongo (com.mongodb.Mongo)1 MongoClient (com.mongodb.MongoClient)1 ServerAddress (com.mongodb.ServerAddress)1