Search in sources :

Example 1 with MongoCmdOptionsBuilder

use of de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder in project beam by apache.

the class MongoDBGridFSIOTest method setup.

@BeforeClass
public static void setup() throws Exception {
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        port = serverSocket.getLocalPort();
    }
    LOG.info("Starting MongoDB embedded instance on {}", port);
    try {
        Files.forceDelete(new File(MONGODB_LOCATION));
    } catch (Exception e) {
    }
    new File(MONGODB_LOCATION).mkdirs();
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION, null, 0)).net(new Net("localhost", port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    LOG.info("Insert test data");
    Mongo client = new Mongo("localhost", port);
    DB database = client.getDB(DATABASE);
    GridFS gridfs = new GridFS(database);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (int x = 0; x < 100; x++) {
        out.write(("Einstein\nDarwin\nCopernicus\nPasteur\n" + "Curie\nFaraday\nNewton\nBohr\nGalilei\nMaxwell\n").getBytes());
    }
    for (int x = 0; x < 5; x++) {
        gridfs.createFile(new ByteArrayInputStream(out.toByteArray()), "file" + x).save();
    }
    gridfs = new GridFS(database, "mapBucket");
    long now = System.currentTimeMillis();
    Random random = new Random();
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" };
    for (int x = 0; x < 10; x++) {
        GridFSInputFile file = gridfs.createFile("file_" + x);
        OutputStream outf = file.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(outf);
        for (int y = 0; y < 5000; y++) {
            long time = now - random.nextInt(3600000);
            String name = scientists[y % scientists.length];
            writer.write(Long.toString(time) + "\t");
            writer.write(name + "\t");
            writer.write(Integer.toString(random.nextInt(100)));
            writer.write("\n");
        }
        for (int y = 0; y < scientists.length; y++) {
            String name = scientists[y % scientists.length];
            writer.write(Long.toString(now) + "\t");
            writer.write(name + "\t");
            writer.write("101");
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    }
    client.close();
}
Also used : Mongo(com.mongodb.Mongo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFS(com.mongodb.gridfs.GridFS) IOException(java.io.IOException) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) Storage(de.flapdoodle.embed.mongo.config.Storage) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) OutputStreamWriter(java.io.OutputStreamWriter) Net(de.flapdoodle.embed.mongo.config.Net) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) File(java.io.File) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) DB(com.mongodb.DB) BeforeClass(org.junit.BeforeClass)

Example 2 with MongoCmdOptionsBuilder

use of de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder in project beam by apache.

the class MongoDbIOTest method setup.

@Before
public void setup() throws Exception {
    LOG.info("Starting MongoDB embedded instance on {}", port);
    try {
        Files.forceDelete(new File(MONGODB_LOCATION));
    } catch (Exception e) {
    }
    new File(MONGODB_LOCATION).mkdirs();
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION, null, 0)).net(new Net("localhost", port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    LOG.info("Insert test data");
    MongoClient client = new MongoClient("localhost", port);
    MongoDatabase database = client.getDatabase(DATABASE);
    MongoCollection collection = database.getCollection(COLLECTION);
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" };
    for (int i = 1; i <= 1000; i++) {
        int index = i % scientists.length;
        Document document = new Document();
        document.append("_id", i);
        document.append("scientist", scientists[index]);
        collection.insertOne(document);
    }
}
Also used : Document(org.bson.Document) MongoClient(com.mongodb.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Storage(de.flapdoodle.embed.mongo.config.Storage) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) File(java.io.File) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) MongoDatabase(com.mongodb.client.MongoDatabase) Before(org.junit.Before)

Example 3 with MongoCmdOptionsBuilder

use of de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder in project embedmongo-maven-plugin by joelittlejohn.

the class StartMojo method executeStart.

@Override
@SuppressWarnings("unchecked")
public void executeStart() throws MojoExecutionException, MojoFailureException {
    MongodExecutable executable;
    try {
        final List<String> mongodArgs = this.createMongodArgsList();
        final ICommandLinePostProcessor commandLinePostProcessor = new ICommandLinePostProcessor() {

            @Override
            public List<String> process(final Distribution distribution, final List<String> args) {
                args.addAll(mongodArgs);
                return args;
            }
        };
        IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(Command.MongoD).processOutput(getOutputConfig()).artifactStore(getArtifactStore()).commandLinePostProcessor(commandLinePostProcessor).build();
        int port = getPort();
        if (isRandomPort()) {
            port = NetworkUtils.allocateRandomPort();
        }
        savePortToProjectProperties(port);
        IMongodConfig config = new MongodConfigBuilder().version(getVersion()).net(new Net(bindIp, port, NetworkUtils.localhostIsIPv6())).replication(new Storage(getDataDirectory(), null, 0)).cmdOptions(new MongoCmdOptionsBuilder().enableAuth(authEnabled).useNoJournal(!journal).useStorageEngine(storageEngine).build()).build();
        executable = MongodStarter.getInstance(runtimeConfig).prepare(config);
    } catch (DistributionException e) {
        throw new MojoExecutionException("Failed to download MongoDB distribution: " + e.withDistribution(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to Config MongoDB: ", e);
    }
    try {
        MongodProcess mongod = executable.start();
        if (isWait()) {
            while (true) {
                try {
                    TimeUnit.MINUTES.sleep(5);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
        getPluginContext().put(MONGOD_CONTEXT_PROPERTY_NAME, mongod);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to start the mongod", e);
    }
}
Also used : ICommandLinePostProcessor(de.flapdoodle.embed.process.runtime.ICommandLinePostProcessor) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) Storage(de.flapdoodle.embed.mongo.config.Storage) Distribution(de.flapdoodle.embed.process.distribution.Distribution) MongodProcess(de.flapdoodle.embed.mongo.MongodProcess) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) ArrayList(java.util.ArrayList) List(java.util.List) DistributionException(de.flapdoodle.embed.process.exceptions.DistributionException) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)

Example 4 with MongoCmdOptionsBuilder

use of de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder in project beam by apache.

the class MongoDbReadWriteIT method setUp.

@BeforeClass
public static void setUp() throws Exception {
    int port = NetworkTestHelper.getAvailableLocalPort();
    LOG.info("Starting MongoDB embedded instance on {}", port);
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0)).net(new Net(hostname, port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).verbose(false).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    client = new MongoClient(hostname, port);
    mongoSqlUrl = String.format("mongodb://%s:%d/%s/%s", hostname, port, database, collection);
}
Also used : MongoClient(com.mongodb.MongoClient) Storage(de.flapdoodle.embed.mongo.config.Storage) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) BeforeClass(org.junit.BeforeClass)

Example 5 with MongoCmdOptionsBuilder

use of de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder in project beam by apache.

the class MongoDBGridFSIOTest method start.

@BeforeClass
public static void start() throws Exception {
    port = NetworkTestHelper.getAvailableLocalPort();
    LOG.info("Starting MongoDB embedded instance on {}", port);
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0)).net(new Net("localhost", port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).verbose(false).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    LOG.info("Insert test data");
    MongoClient client = new MongoClient("localhost", port);
    DB database = client.getDB(DATABASE);
    GridFS gridfs = new GridFS(database);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (int x = 0; x < 100; x++) {
        out.write(("Einstein\nDarwin\nCopernicus\nPasteur\n" + "Curie\nFaraday\nNewton\nBohr\nGalilei\nMaxwell\n").getBytes(StandardCharsets.UTF_8));
    }
    for (int x = 0; x < 5; x++) {
        gridfs.createFile(new ByteArrayInputStream(out.toByteArray()), "file" + x).save();
    }
    gridfs = new GridFS(database, "mapBucket");
    long now = System.currentTimeMillis();
    Random random = new Random();
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" };
    for (int x = 0; x < 10; x++) {
        GridFSInputFile file = gridfs.createFile("file_" + x);
        OutputStream outf = file.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(outf, StandardCharsets.UTF_8);
        for (int y = 0; y < 5000; y++) {
            long time = now - random.nextInt(3600000);
            String name = scientists[y % scientists.length];
            writer.write(time + "\t");
            writer.write(name + "\t");
            writer.write(Integer.toString(random.nextInt(100)));
            writer.write("\n");
        }
        for (int y = 0; y < scientists.length; y++) {
            String name = scientists[y % scientists.length];
            writer.write(now + "\t");
            writer.write(name + "\t");
            writer.write("101");
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    }
    client.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFS(com.mongodb.gridfs.GridFS) MongoClient(com.mongodb.MongoClient) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) Storage(de.flapdoodle.embed.mongo.config.Storage) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) OutputStreamWriter(java.io.OutputStreamWriter) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) DB(com.mongodb.DB) BeforeClass(org.junit.BeforeClass)

Aggregations

IMongodConfig (de.flapdoodle.embed.mongo.config.IMongodConfig)6 MongoCmdOptionsBuilder (de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder)6 MongodConfigBuilder (de.flapdoodle.embed.mongo.config.MongodConfigBuilder)6 Net (de.flapdoodle.embed.mongo.config.Net)6 Storage (de.flapdoodle.embed.mongo.config.Storage)6 MongoClient (com.mongodb.MongoClient)4 BeforeClass (org.junit.BeforeClass)4 DB (com.mongodb.DB)2 GridFS (com.mongodb.gridfs.GridFS)2 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Random (java.util.Random)2 Document (org.bson.Document)2 Mongo (com.mongodb.Mongo)1 MongoCollection (com.mongodb.client.MongoCollection)1