All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.operation.ChangeStreamBatchCursor Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mongodb.operation;

import com.mongodb.Function;
import com.mongodb.MongoChangeStreamException;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.ServerCursor;
import com.mongodb.binding.ConnectionSource;
import com.mongodb.binding.ReadBinding;
import com.mongodb.operation.OperationHelper.CallableWithSource;
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;
import org.bson.RawBsonDocument;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.operation.ChangeStreamBatchCursorHelper.isRetryableError;
import static com.mongodb.operation.OperationHelper.withReadConnectionSource;

// @deprecated Usage of this API is not supported in AEM as a Cloud Service.
@Deprecated(since = "2021-05-27")
final class ChangeStreamBatchCursor implements AggregateResponseBatchCursor {

    private final ReadBinding binding;

    private final ChangeStreamOperation changeStreamOperation;

    private AggregateResponseBatchCursor wrapped;

    private BsonDocument resumeToken;

    private volatile boolean closed;

    ChangeStreamBatchCursor(final ChangeStreamOperation changeStreamOperation, final AggregateResponseBatchCursor wrapped, final ReadBinding binding, final BsonDocument resumeToken) {
        this.changeStreamOperation = changeStreamOperation;
        this.binding = binding.retain();
        this.wrapped = wrapped;
        this.resumeToken = resumeToken;
    }

    AggregateResponseBatchCursor getWrapped() {
        return wrapped;
    }

    @Override
    public boolean hasNext() {
        return resumeableOperation(new Function, Boolean>() {

            @Override
            public Boolean apply(final AggregateResponseBatchCursor queryBatchCursor) {
                return queryBatchCursor.hasNext();
            }
        });
    }

    @Override
    public List next() {
        return resumeableOperation(new Function, List>() {

            @Override
            public List apply(final AggregateResponseBatchCursor queryBatchCursor) {
                List results = convertResults(queryBatchCursor.next());
                cachePostBatchResumeToken(queryBatchCursor);
                return results;
            }
        });
    }

    @Override
    public List tryNext() {
        return resumeableOperation(new Function, List>() {

            @Override
            public List apply(final AggregateResponseBatchCursor queryBatchCursor) {
                List results = convertResults(queryBatchCursor.tryNext());
                cachePostBatchResumeToken(queryBatchCursor);
                return results;
            }
        });
    }

    @Override
    public void close() {
        if (!closed) {
            closed = true;
            wrapped.close();
            binding.release();
        }
    }

    @Override
    public void setBatchSize(final int batchSize) {
        wrapped.setBatchSize(batchSize);
    }

    @Override
    public int getBatchSize() {
        return wrapped.getBatchSize();
    }

    @Override
    public ServerCursor getServerCursor() {
        return wrapped.getServerCursor();
    }

    @Override
    public ServerAddress getServerAddress() {
        return wrapped.getServerAddress();
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not implemented!");
    }

    @Override
    public BsonDocument getPostBatchResumeToken() {
        return wrapped.getPostBatchResumeToken();
    }

    @Override
    public BsonTimestamp getOperationTime() {
        return changeStreamOperation.getStartAtOperationTime();
    }

    @Override
    public boolean isFirstBatchEmpty() {
        return wrapped.isFirstBatchEmpty();
    }

    private void cachePostBatchResumeToken(final AggregateResponseBatchCursor queryBatchCursor) {
        if (queryBatchCursor.getPostBatchResumeToken() != null) {
            resumeToken = queryBatchCursor.getPostBatchResumeToken();
        }
    }

    private List convertResults(final List rawDocuments) {
        List results = null;
        if (rawDocuments != null) {
            results = new ArrayList();
            for (RawBsonDocument rawDocument : rawDocuments) {
                if (!rawDocument.containsKey("_id")) {
                    throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
                }
                results.add(rawDocument.decode(changeStreamOperation.getDecoder()));
            }
            resumeToken = rawDocuments.get(rawDocuments.size() - 1).getDocument("_id");
        }
        return results;
    }

     R resumeableOperation(final Function, R> function) {
        while (true) {
            try {
                return function.apply(wrapped);
            } catch (Throwable t) {
                if (!isRetryableError(t)) {
                    throw MongoException.fromThrowableNonNull(t);
                }
            }
            wrapped.close();
            withReadConnectionSource(binding, new CallableWithSource() {

                @Override
                public Void call(final ConnectionSource source) {
                    changeStreamOperation.setChangeStreamOptionsForResume(resumeToken, source.getServerDescription().getMaxWireVersion());
                    return null;
                }
            });
            wrapped = ((ChangeStreamBatchCursor) changeStreamOperation.execute(binding)).getWrapped();
            // release the new change stream batch cursor's reference to the binding
            binding.release();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy