co.capped((Boolean) collectionOptions.get("capped"));
co.capped((Boolean) collectionOptions.get("capped"));
private MongoCollection<Document> getCollection(String collectionName) { // first check if collection exists, if not then create a new collection with cappedSize if (!isCollectionExists(collectionName)) { CreateCollectionOptions option = new CreateCollectionOptions(); option.capped(true); option.maxDocuments(cappedMaxDocuments); option.sizeInBytes(cappedMaxSize); db.createCollection(collectionName, option); } return db.getCollection(collectionName); }
private static void createCappedCollectionIfItDoesNotExist( final MongoClientWrapper clientWrapper, final String collectionName, final long cappedCollectionSizeInBytes, final long createTimeoutSeconds, final Materializer materializer) { try { final CreateCollectionOptions collectionOptions = new CreateCollectionOptions() .capped(true) .sizeInBytes(cappedCollectionSizeInBytes) .maxDocuments(1); final Publisher<Success> publisher = clientWrapper.getDatabase() .createCollection(collectionName, collectionOptions); final Source<Success, NotUsed> source = Source.fromPublisher(publisher); final CompletionStage<Success> done = source.runWith(Sink.head(), materializer); done.toCompletableFuture().get(createTimeoutSeconds, TimeUnit.SECONDS); LOGGER.debug("Successfully created collection: <{}>.", collectionName); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } catch (final TimeoutException e) { throw new IllegalStateException(e); } catch (final ExecutionException e) { if (isCollectionAlreadyExistsError(e.getCause())) { LOGGER.debug("Collection already exists: <{}>.", collectionName); } else { throw new IllegalStateException(e); } } }
final CreateCollectionOptions collectionOptions = new CreateCollectionOptions() .autoIndex(false) .capped(true) .sizeInBytes(cappedCollectionSizeInBytes) .maxDocuments(1);
if (collectionsSize != 0 && maxDocuments != 0) { CreateCollectionOptions options = new CreateCollectionOptions() .capped(true) .sizeInBytes(collectionsSize) .maxDocuments(maxDocuments);