Search in sources :

Example 11 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project mongo-java-driver by mongodb.

the class GridFSDownloadPublisherImpl method getChunkPublisher.

private Flux<ByteBuffer> getChunkPublisher(final GridFSFile gridFSFile) {
    Document filter = new Document("files_id", gridFSFile.getId());
    FindPublisher<Document> chunkPublisher;
    if (clientSession != null) {
        chunkPublisher = chunksCollection.find(clientSession, filter);
    } else {
        chunkPublisher = chunksCollection.find(filter);
    }
    AtomicInteger chunkCounter = new AtomicInteger(0);
    int numberOfChunks = (int) Math.ceil((double) gridFSFile.getLength() / gridFSFile.getChunkSize());
    Flux<ByteBuffer> byteBufferFlux = Flux.from(chunkPublisher.sort(new Document("n", 1))).map(chunk -> {
        int expectedChunkIndex = chunkCounter.getAndAdd(1);
        if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), expectedChunkIndex));
        } else if (!(chunk.get("data") instanceof Binary)) {
            throw new MongoGridFSException("Unexpected data format for the chunk");
        }
        byte[] data = chunk.get("data", Binary.class).getData();
        long expectedDataLength = 0;
        if (numberOfChunks > 0) {
            expectedDataLength = expectedChunkIndex + 1 == numberOfChunks ? gridFSFile.getLength() - (expectedChunkIndex * (long) gridFSFile.getChunkSize()) : gridFSFile.getChunkSize();
        }
        if (data.length != expectedDataLength) {
            throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be " + "%s bytes.", data.length, gridFSFile.getId(), expectedChunkIndex, expectedDataLength));
        }
        return ByteBuffer.wrap(data);
    }).doOnComplete(() -> {
        if (chunkCounter.get() < numberOfChunks) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), chunkCounter.get()));
        }
    });
    return bufferSizeBytes == null ? byteBufferFlux : new ResizingByteBufferFlux(byteBufferFlux, bufferSizeBytes);
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Binary(org.bson.types.Binary) Publisher(org.reactivestreams.Publisher) MongoGridFSException(com.mongodb.MongoGridFSException) Mono(reactor.core.publisher.Mono) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) String.format(java.lang.String.format) GridFSDownloadPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSDownloadPublisher) Flux(reactor.core.publisher.Flux) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Nullable(com.mongodb.lang.Nullable) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project mongo-java-driver by mongodb.

the class GridFSPublisherCreator method createDeletePublisher.

public static Publisher<Void> createDeletePublisher(final MongoCollection<GridFSFile> filesCollection, final MongoCollection<Document> chunksCollection, @Nullable final ClientSession clientSession, final BsonValue id) {
    notNull("filesCollection", filesCollection);
    notNull("chunksCollection", chunksCollection);
    notNull("id", id);
    BsonDocument filter = new BsonDocument("_id", id);
    Publisher<DeleteResult> fileDeletePublisher;
    if (clientSession == null) {
        fileDeletePublisher = filesCollection.deleteOne(filter);
    } else {
        fileDeletePublisher = filesCollection.deleteOne(clientSession, filter);
    }
    return Mono.from(fileDeletePublisher).flatMap(deleteResult -> {
        if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
            throw new MongoGridFSException(format("No file found with the ObjectId: %s", id));
        }
        if (clientSession == null) {
            return Mono.from(chunksCollection.deleteMany(new BsonDocument("files_id", id)));
        } else {
            return Mono.from(chunksCollection.deleteMany(clientSession, new BsonDocument("files_id", id)));
        }
    }).flatMap(i -> Mono.empty());
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Publisher(org.reactivestreams.Publisher) MongoGridFSException(com.mongodb.MongoGridFSException) Mono(reactor.core.publisher.Mono) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ClientSession(com.mongodb.reactivestreams.client.ClientSession) BsonString(org.bson.BsonString) ByteBuffer(java.nio.ByteBuffer) String.format(java.lang.String.format) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Bson(org.bson.conversions.Bson) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) UpdateResult(com.mongodb.client.result.UpdateResult) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) DeleteResult(com.mongodb.client.result.DeleteResult) GridFSFindPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSFindPublisher) Nullable(com.mongodb.lang.Nullable) BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 13 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ReactiveClientSessionTests method reusesClientSessionInSessionScopedCallback.

// DATAMONGO-1880
@Test
public void reusesClientSessionInSessionScopedCallback() {
    ClientSession session = Mono.from(client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build())).block();
    CountingSessionSupplier sessionSupplier = new CountingSessionSupplier(session);
    ReactiveSessionScoped sessionScoped = template.withSession(sessionSupplier);
    sessionScoped.execute(action -> action.findOne(new Query(), Document.class, COLLECTION_NAME)).blockFirst();
    assertThat(sessionSupplier.getInvocationCount()).isEqualTo(1);
    sessionScoped.execute(action -> action.findOne(new Query(), Document.class, COLLECTION_NAME)).blockFirst();
    assertThat(sessionSupplier.getInvocationCount()).isEqualTo(1);
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Supplier(java.util.function.Supplier) Client(org.springframework.data.mongodb.test.util.Client) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ClientSessionOptions(com.mongodb.ClientSessionOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Query(org.springframework.data.mongodb.core.query.Query) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Test(org.junit.jupiter.api.Test)

Example 14 with ClientSession

use of com.mongodb.reactivestreams.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTransactionTests method inTransactionCommitsProvidedTransactionalSession.

// DATAMONGO-1970
@Test
public void inTransactionCommitsProvidedTransactionalSession() {
    ClientSession session = Mono.from(client.startSession()).block();
    session.startTransaction();
    template.inTransaction(Mono.just(session)).execute(action -> {
        return action.remove(ID_QUERY, Document.class, COLLECTION_NAME);
    }).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    assertThat(session.hasActiveTransaction()).isFalse();
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Collectors(java.util.stream.Collectors) Client(org.springframework.data.mongodb.test.util.Client) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ClientSessionOptions(com.mongodb.ClientSessionOptions) Assertions(org.assertj.core.api.Assertions) Sort(org.springframework.data.domain.Sort) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

ClientSession (com.mongodb.reactivestreams.client.ClientSession)14 Document (org.bson.Document)11 Mono (reactor.core.publisher.Mono)10 Test (org.junit.jupiter.api.Test)9 ClientSessionOptions (com.mongodb.ClientSessionOptions)7 MongoClient (com.mongodb.reactivestreams.client.MongoClient)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Assertions (org.assertj.core.api.Assertions)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 Criteria (org.springframework.data.mongodb.core.query.Criteria)6 Query (org.springframework.data.mongodb.core.query.Query)6 Client (org.springframework.data.mongodb.test.util.Client)6 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)6 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)6 MongoClientExtension (org.springframework.data.mongodb.test.util.MongoClientExtension)6 MongoTestUtils (org.springframework.data.mongodb.test.util.MongoTestUtils)6 StepVerifier (reactor.test.StepVerifier)6 Publisher (org.reactivestreams.Publisher)5 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)4