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

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

Go to download

The MongoDB Java Driver uber-artifact, containing the legacy driver, the mongodb-driver, mongodb-driver-core, and bson

There is a newer version: 3.12.14
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.ReadBinding;
import org.bson.BsonDocument;
import org.bson.RawBsonDocument;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.operation.ChangeStreamBatchCursorHelper.isRetryableError;

final class ChangeStreamBatchCursor implements BatchCursor {
    private final ReadBinding binding;
    private final ChangeStreamOperation changeStreamOperation;

    private BsonDocument resumeToken;
    private BatchCursor wrapped;

    ChangeStreamBatchCursor(final ChangeStreamOperation changeStreamOperation,
                            final BatchCursor wrapped,
                            final ReadBinding binding) {
        changeStreamOperation.startOperationTimeForResume(binding.getSessionContext().getOperationTime());
        this.changeStreamOperation = changeStreamOperation;
        this.resumeToken = changeStreamOperation.getResumeToken();
        this.wrapped = wrapped;
        this.binding = binding.retain();
    }

    BatchCursor getWrapped() {
        return wrapped;
    }

    @Override
    public boolean hasNext() {
        return resumeableOperation(new Function, Boolean>() {
            @Override
            public Boolean apply(final BatchCursor queryBatchCursor) {
                return queryBatchCursor.hasNext();
            }
        });
    }

    @Override
    public List next() {
        return resumeableOperation(new Function, List>() {
            @Override
            public List apply(final BatchCursor queryBatchCursor) {
                return convertResults(queryBatchCursor.next());
            }
        });
    }

    @Override
    public List tryNext() {
        return resumeableOperation(new Function, List>() {
            @Override
            public List apply(final BatchCursor queryBatchCursor) {
                return convertResults(queryBatchCursor.tryNext());
            }
        });
    }

    @Override
    public void close() {
        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!");
    }

    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.");
                }
                resumeToken = rawDocument.getDocument("_id");
                results.add(rawDocument.decode(changeStreamOperation.getDecoder()));
            }
        }
        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();

            if (resumeToken != null) {
                changeStreamOperation.startOperationTimeForResume(null);
                changeStreamOperation.resumeAfter(resumeToken);
            }
            wrapped = ((ChangeStreamBatchCursor) changeStreamOperation.execute(binding)).getWrapped();
            binding.release(); // release the new change stream batch cursor's reference to the binding
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy