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

com.hazelcast.jet.mongodb.impl.WriteMongoParams Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
/*
 * Copyright 2023 Hazelcast Inc.
 *
 * Licensed under the Hazelcast Community License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://hazelcast.com/hazelcast-community-license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.jet.mongodb.impl;

import com.hazelcast.function.ConsumerEx;
import com.hazelcast.function.FunctionEx;
import com.hazelcast.function.SupplierEx;
import com.hazelcast.jet.mongodb.WriteMode;
import com.hazelcast.jet.pipeline.DataConnectionRef;
import com.hazelcast.jet.retry.RetryStrategy;
import com.mongodb.TransactionOptions;
import com.mongodb.client.MongoClient;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.WriteModel;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Optional;

import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.internal.util.Preconditions.checkState;
import static com.hazelcast.jet.impl.util.Util.checkNonNullAndSerializable;
import static com.hazelcast.jet.impl.util.Util.checkSerializable;
import static com.hazelcast.jet.pipeline.DataConnectionRef.dataConnectionRef;

@SuppressWarnings({"UnusedReturnValue", "unused"})
public class WriteMongoParams implements Serializable {

    SupplierEx clientSupplier;
    DataConnectionRef dataConnectionRef;
    String databaseName;
    String collectionName;
    Class documentType;
    @Nonnull
    FunctionEx intermediateMappingFn = FunctionEx.identity();
    String documentIdentityFieldName;
    FunctionEx documentIdentityFn;
    @Nonnull
    ConsumerEx replaceOptionAdjuster = ConsumerEx.noop();
    RetryStrategy commitRetryStrategy;
    SupplierEx transactionOptionsSup;
    FunctionEx databaseNameSelectFn;
    FunctionEx collectionNameSelectFn;
    @Nonnull
    WriteMode writeMode = WriteMode.REPLACE;
    FunctionEx> writeModelFn;
    boolean throwOnNonExisting = true;

    public WriteMongoParams() {
    }

    @Nonnull
    public SupplierEx getClientSupplier() {
        return clientSupplier;
    }

    @Nonnull
    public WriteMongoParams setClientSupplier(@Nullable SupplierEx clientSupplier) {
        this.clientSupplier = clientSupplier;
        return this;
    }

    @Nullable
    public DataConnectionRef getDataConnectionRef() {
        return dataConnectionRef;
    }

    @Nonnull
    public WriteMongoParams setDataConnectionRef(@Nullable DataConnectionRef dataConnectionRef) {
        this.dataConnectionRef = dataConnectionRef;
        return this;
    }

    @Nonnull
    public WriteMongoParams setDataConnectionRef(@Nullable String dataConnectionName) {
        if (dataConnectionName != null) {
            setDataConnectionRef(dataConnectionRef(dataConnectionName));
        }
        return this;
    }

    public void checkConnectivityOptionsValid() {
        boolean hasDataConnection = dataConnectionRef != null;
        boolean hasClientSupplier = clientSupplier != null;
        checkState(hasDataConnection || hasClientSupplier, "Client supplier or data connection ref should be provided");
        checkState(hasDataConnection != hasClientSupplier, "Only one of two should be provided: " +
                "Client supplier or data connection ref");
    }

    @Nonnull
    public String getDatabaseName() {
        return databaseName;
    }

    @Nonnull
    public WriteMongoParams setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
        return this;
    }

    public String getCollectionName() {
        return collectionName;
    }

    @Nonnull
    public WriteMongoParams setCollectionName(String collectionName) {
        this.collectionName = collectionName;
        return this;
    }

    @Nonnull
    public Class getDocumentType() {
        return documentType;
    }

    @Nonnull
    public WriteMongoParams setDocumentType(@Nonnull Class documentType) {
        this.documentType = documentType;
        return this;
    }

    @SuppressWarnings("unchecked")
    @Nonnull
    public  FunctionEx getIntermediateMappingFn() {
        return (FunctionEx) intermediateMappingFn;
    }

    @Nonnull
    public  WriteMongoParams setIntermediateMappingFn(FunctionEx intermediateMappingFn) {
        this.intermediateMappingFn = intermediateMappingFn;
        return this;
    }

    @Nonnull
    public FunctionEx getDocumentIdentityFn() {
        return documentIdentityFn;
    }

    @Nonnull
    public WriteMongoParams setDocumentIdentityFn(@Nonnull FunctionEx documentIdentityFn) {
        this.documentIdentityFn = documentIdentityFn;
        return this;
    }

    @Nonnull
    public ConsumerEx getReplaceOptionAdjuster() {
        return replaceOptionAdjuster;
    }

    @Nonnull
    public WriteMongoParams setReplaceOptionAdjuster(@Nonnull ConsumerEx replaceOptionAdjuster) {
        checkNonNullAndSerializable(replaceOptionAdjuster, "replaceOptionAdjuster");
        this.replaceOptionAdjuster = replaceOptionAdjuster;
        return this;
    }

    public String getDocumentIdentityFieldName() {
        return documentIdentityFieldName;
    }

    @Nonnull
    public WriteMongoParams setDocumentIdentityFieldName(String documentIdentityFieldName) {
        this.documentIdentityFieldName = documentIdentityFieldName;
        return this;
    }

    public RetryStrategy getCommitRetryStrategy() {
        return commitRetryStrategy;
    }

    @Nonnull
    public WriteMongoParams setCommitRetryStrategy(RetryStrategy commitRetryStrategy) {
        this.commitRetryStrategy = commitRetryStrategy;
        return this;
    }

    public SupplierEx getTransactionOptionsSup() {
        return transactionOptionsSup;
    }

    @Nonnull
    public WriteMongoParams setTransactionOptionsSup(SupplierEx transactionOptionsSup) {
        this.transactionOptionsSup = transactionOptionsSup;
        return this;
    }

    public FunctionEx getDatabaseNameSelectFn() {
        return databaseNameSelectFn;
    }

    @Nonnull
    public WriteMongoParams setDatabaseNameSelectFn(FunctionEx databaseNameSelectFn) {
        checkSerializable(databaseNameSelectFn, "databaseNameSelectFn");
        this.databaseNameSelectFn = databaseNameSelectFn;
        return this;
    }

    public FunctionEx getCollectionNameSelectFn() {
        return collectionNameSelectFn;
    }

    @Nonnull
    public WriteMongoParams setCollectionNameSelectFn(FunctionEx collectionNameSelectFn) {
        checkSerializable(collectionNameSelectFn, "collectionNameSelectFn");
        this.collectionNameSelectFn = collectionNameSelectFn;
        return this;
    }

    @Nonnull
    public WriteMode getWriteMode() {
        return writeMode;
    }

    public WriteMongoParams setWriteMode(@Nonnull WriteMode writeMode) {
        checkNotNull(writeMode, "writeMode cannot be null");
        this.writeMode = writeMode;
        return this;
    }

    public FunctionEx> getWriteModelFn() {
        return writeModelFn;
    }

    public Optional>> getOptionalWriteModelFn() {
        return Optional.ofNullable(writeModelFn);
    }

    @Nonnull
    public WriteMongoParams setWriteModelFn(FunctionEx> writeModelFn) {
        this.writeModelFn = writeModelFn;
        return this;
    }

    public boolean isThrowOnNonExisting() {
        return throwOnNonExisting;
    }

    public WriteMongoParams setThrowOnNonExisting(boolean throwOnNonExisting) {
        this.throwOnNonExisting = throwOnNonExisting;
        return this;
    }

    public void checkValid() {
        checkConnectivityOptionsValid();
        checkNotNull(documentIdentityFn, "documentIdentityFn must be set");
        checkNotNull(commitRetryStrategy, "commitRetryStrategy must be set");
        checkNotNull(transactionOptionsSup, "transactionOptions must be set");


        checkState((databaseName == null) == (collectionName == null), "if one of [databaseName, collectionName]" +
                " is provided, so should the other one");
        checkState((databaseNameSelectFn == null) == (collectionNameSelectFn == null),
                "if one of [selectDatabaseNameFn, selectCollectionNameFn] is provided, so should the other one");

        checkState((databaseNameSelectFn == null) != (databaseName == null),
                "Only select*Fn or *Name functions should be called, never mixed");
    }
}