com.mongodb.reactivestreams.client.Success Java Examples

The following examples show how to use com.mongodb.reactivestreams.client.Success. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBPublisherTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 6 votes vote down vote up
private void prepareRandomData(long elements) {
    if (elements <= 0) {
        return;
    }

    Flowable<Success> successFlowable = Flowable.fromPublisher(collection.drop())
            .ignoreElements()
            .andThen(
                    Flowable
                            .rangeLong(0L, elements)
                            .map(l -> NewsHarness.generate())
                            .buffer(500, TimeUnit.MILLISECONDS)
                            .flatMap(collection::insertMany)
            );

    if (elements == Long.MAX_VALUE || elements == Integer.MAX_VALUE) {
        successFlowable.subscribe();
    } else {
        successFlowable.blockingSubscribe();
    }
}
 
Example #2
Source File: NewsServicePublisherTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 6 votes vote down vote up
private void prepareItemsInDatabase(long elements) {
    if (elements <= 0) {
        return;
    }

    MongoCollection<News> collection = mongoClient().getDatabase("news")
                                                    .getCollection("news", News.class);

    Flowable<Success> successFlowable = Flowable.fromPublisher(collection.drop())
                                                .ignoreElements()
                                                .andThen(Flowable.rangeLong(0L,
                                                        elements)
                                                                 .map(l -> NewsHarness.generate())
                                                                 .buffer(500,
                                                                         TimeUnit.MILLISECONDS)
                                                                 .flatMap(collection::insertMany));

    if (elements == Long.MAX_VALUE || elements == Integer.MAX_VALUE) {
        successFlowable.subscribe();
    }
    else {
        successFlowable.blockingSubscribe();
    }
}
 
Example #3
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates the capped collection {@code collectionName} using {@code clientWrapper} if it doesn't exists yet.
 *
 * @param database The database to use.
 * @param collectionName The name of the capped collection that should be created.
 * @param cappedCollectionSizeInBytes The size in bytes of the collection that should be created.
 * @param materializer The actor materializer to pre-materialize the restart source.
 * @return Returns the created or retrieved collection.
 */
private static Source<MongoCollection, NotUsed> createOrGetCappedCollection(
        final MongoDatabase database,
        final String collectionName,
        final long cappedCollectionSizeInBytes,
        final ActorMaterializer materializer) {

    final Source<Success, NotUsed> createCollectionSource =
            repeatableCreateCappedCollectionSource(database, collectionName, cappedCollectionSizeInBytes);

    final Source<MongoCollection, NotUsed> infiniteCollectionSource =
            createCollectionSource.map(success -> database.getCollection(collectionName))
                    .flatMapConcat(Source::repeat);

    final Source<MongoCollection, NotUsed> restartSource =
            RestartSource.withBackoff(BACKOFF_MIN, BACKOFF_MAX, 1.0, () -> infiniteCollectionSource);

    // pre-materialize source with BroadcastHub so that a successfully obtained capped collection is reused
    // until the stream fails, whereupon it gets recreated with backoff.
    return restartSource.runWith(BroadcastHub.of(MongoCollection.class, 1), materializer);
}
 
Example #4
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static Source<Success, NotUsed> repeatableCreateCappedCollectionSource(
        final MongoDatabase database,
        final String collectionName,
        final long cappedCollectionSizeInBytes) {

    final CreateCollectionOptions collectionOptions = new CreateCollectionOptions()
            .capped(true)
            .sizeInBytes(cappedCollectionSizeInBytes)
            .maxDocuments(1);

    return Source.lazily(
            () -> Source.fromPublisher(database.createCollection(collectionName, collectionOptions)))
            .mapMaterializedValue(whatever -> NotUsed.getInstance())
            .withAttributes(Attributes.inputBuffer(1, 1))
            .recoverWithRetries(1, new PFBuilder<Throwable, Source<Success, NotUsed>>()
                    .match(MongoCommandException.class,
                            MongoTimestampPersistence::isCollectionAlreadyExistsError,
                            error -> Source.single(Success.SUCCESS))
                    .build());

}
 
Example #5
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> insertMany(final ClientSession clientSession, final List<? extends TDocument> documents,
                                     final InsertManyOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.insertMany(clientSession.getWrapped(), documents, options, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #6
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> rename(final ObjectId id, final String newFilename) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.rename(id, newFilename, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #7
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> insertMany(final List<? extends TDocument> documents, final InsertManyOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.insertMany(documents, options, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #8
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> insertOne(final TDocument document, final InsertOneOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.insertOne(document, options, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #9
Source File: GridFSUploadStreamImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> abort() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.abort(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #10
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> rename(final ClientSession clientSession, final BsonValue id, final String newFilename) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.rename(clientSession.getWrapped(), id, newFilename, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #11
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> delete(final ObjectId id) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.delete(id, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #12
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> delete(final ClientSession clientSession, final BsonValue id) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.delete(clientSession.getWrapped(), id, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #13
Source File: GridFSUploadPublisherImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
GridFSUploadPublisher<ObjectId> withObjectId() {
    final GridFSUploadPublisherImpl wrapped = this;
    return new GridFSUploadPublisher<ObjectId>() {

        @Override
        public ObjectId getObjectId() {
            return wrapped.getObjectId();
        }

        @Override
        public BsonValue getId() {
            return wrapped.getId();
        }

        @Override
        public void subscribe(final Subscriber<? super ObjectId> objectIdSub) {
            wrapped.subscribe(new Subscriber<Success>() {
                @Override
                public void onSubscribe(final Subscription s) {
                    objectIdSub.onSubscribe(s);
                }

                @Override
                public void onNext(final Success success) {
                    objectIdSub.onNext(getObjectId());
                }

                @Override
                public void onError(final Throwable t) {
                    objectIdSub.onError(t);
                }

                @Override
                public void onComplete() {
                    objectIdSub.onComplete();
                }
            });
        }
    };
}
 
Example #14
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> drop() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.drop(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #15
Source File: IndexOperations.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static PartialFunction<Throwable, Source<Success, NotUsed>> buildDropIndexRecovery(
        final String indexDescription) {
    return new PFBuilder<Throwable, Source<Success, NotUsed>>()
            .match(MongoCommandException.class, IndexOperations::isIndexNotFound, throwable -> {
                LOGGER.debug("Index <{}> could not be dropped because it does not exist (anymore).",
                        indexDescription);
                return Source.single(Success.SUCCESS);
            })
            .build();
}
 
Example #16
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> delete(final BsonValue id) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.delete(id, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #17
Source File: MongoDatabaseImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> createView(final ClientSession clientSession, final String viewName, final String viewOn,
                                     final List<? extends Bson> pipeline, final CreateViewOptions createViewOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.createView(clientSession.getWrapped(), viewName, viewOn, pipeline, createViewOptions,
                            voidToSuccessCallback(callback));
                }
            }));
}
 
Example #18
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> drop() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.drop(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #19
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> drop(final ClientSession clientSession) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.drop(clientSession.getWrapped(), voidToSuccessCallback(callback));
                }
            }));
}
 
Example #20
Source File: AggregatePublisherImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> toCollection() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>(){
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.toCollection(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #21
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> dropIndex(final String indexName, final DropIndexOptions dropIndexOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.dropIndex(indexName, dropIndexOptions, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #22
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> dropIndex(final Bson keys, final DropIndexOptions dropIndexOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.dropIndex(keys, dropIndexOptions, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #23
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> dropIndex(final ClientSession clientSession, final Bson keys, final DropIndexOptions dropIndexOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.dropIndex(clientSession.getWrapped(), keys, dropIndexOptions, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #24
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> renameCollection(final MongoNamespace newCollectionNamespace, final RenameCollectionOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.renameCollection(newCollectionNamespace, options, voidToSuccessCallback(callback));
                }
            }));
}
 
Example #25
Source File: PublisherHelper.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
/**
 * Helper to trigger Boolean SingleResultCallbacks for Void operations
 *
 * @param callback the boolean single result callback.
 * @return the results callback for an operation that returns null to signal success.
 */
public static com.mongodb.async.SingleResultCallback<Void> voidToSuccessCallback(
        final com.mongodb.async.SingleResultCallback<Success> callback) {
    return new com.mongodb.async.SingleResultCallback<Void>() {
        @Override
        public void onResult(final Void result, final Throwable t) {
            callback.onResult(Success.SUCCESS, t);
        }
    };
}
 
Example #26
Source File: MongoDatabaseImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> drop() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.drop(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #27
Source File: GridFSDownloadStreamImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> close() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.close(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #28
Source File: MapReducePublisherImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> toCollection() {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.toCollection(voidToSuccessCallback(callback));
                }
            }));
}
 
Example #29
Source File: GridFSBucketImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> uploadFromStream(final ClientSession clientSession, final BsonValue id, final String filename,
                                           final com.mongodb.reactivestreams.client.gridfs.AsyncInputStream source,
                                           final GridFSUploadOptions options) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.uploadFromStream(clientSession.getWrapped(), id, filename, toCallbackAsyncInputStream(source), options,
                            voidToSuccessCallback(callback));
                }
            }));
}
 
Example #30
Source File: MongoDatabaseImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<Success> createView(final String viewName, final String viewOn, final List<? extends Bson> pipeline,
                                     final CreateViewOptions createViewOptions) {
    return new ObservableToPublisher<Success>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<Success>>() {
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<Success> callback) {
                    wrapped.createView(viewName, viewOn, pipeline, createViewOptions, voidToSuccessCallback(callback));
                }
            }));
}