snuyanzin commented on code in PR #56: URL: https://github.com/apache/flink-connector-opensearch/pull/56#discussion_r2810051818
########## flink-connector-opensearch3/src/main/java/org/apache/flink/connector/opensearch/sink/BulkOperation.java: ########## @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.connector.opensearch.sink; + +import org.apache.flink.annotation.Internal; + +import org.opensearch.client.opensearch.core.bulk.DeleteOperation; +import org.opensearch.client.opensearch.core.bulk.IndexOperation; + +import javax.annotation.Nullable; + +import java.io.Serializable; +import java.util.Map; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A serializable wrapper for OpenSearch 3 bulk operations. Since the opensearch-java client's + * BulkOperation is not serializable, we create our own representation that can be serialized and + * later converted to the actual BulkOperation. + */ +@Internal +public class BulkOperation implements Serializable { + + private static final long serialVersionUID = 1L; + + private final OperationType operationType; + private final String index; + @Nullable private final String id; + @Nullable private final Map<String, Object> document; + @Nullable private final Map<String, Object> upsertDocument; + + /** The type of bulk operation. */ + public enum OperationType { + INDEX, + DELETE, + UPDATE + } + + private BulkOperation( + OperationType operationType, + String index, + @Nullable String id, + @Nullable Map<String, Object> document, + @Nullable Map<String, Object> upsertDocument) { + this.operationType = checkNotNull(operationType); + this.index = checkNotNull(index); + this.id = id; + this.document = document; + this.upsertDocument = upsertDocument; + } + + /** + * Creates an index operation. + * + * @param index the target index + * @param id the document id (can be null for auto-generated ids) + * @param document the document to index + * @return the bulk operation + */ + public static BulkOperation index( + String index, @Nullable String id, Map<String, Object> document) { + checkNotNull(document, "Document cannot be null for index operations"); + return new BulkOperation(OperationType.INDEX, index, id, document, null); + } + + /** + * Creates a delete operation. + * + * @param index the target index + * @param id the document id to delete + * @return the bulk operation + */ + public static BulkOperation delete(String index, String id) { Review Comment: In previous method `id` also can be null and marked with `@Nullable` annotation, here not can we align them having same approach? ########## flink-connector-opensearch3/src/main/java/org/apache/flink/connector/opensearch/sink/BulkOperation.java: ########## @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.connector.opensearch.sink; + +import org.apache.flink.annotation.Internal; + +import org.opensearch.client.opensearch.core.bulk.DeleteOperation; +import org.opensearch.client.opensearch.core.bulk.IndexOperation; + +import javax.annotation.Nullable; + +import java.io.Serializable; +import java.util.Map; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A serializable wrapper for OpenSearch 3 bulk operations. Since the opensearch-java client's + * BulkOperation is not serializable, we create our own representation that can be serialized and + * later converted to the actual BulkOperation. + */ +@Internal +public class BulkOperation implements Serializable { + + private static final long serialVersionUID = 1L; + + private final OperationType operationType; + private final String index; + @Nullable private final String id; + @Nullable private final Map<String, Object> document; + @Nullable private final Map<String, Object> upsertDocument; + + /** The type of bulk operation. */ + public enum OperationType { + INDEX, + DELETE, + UPDATE + } + + private BulkOperation( + OperationType operationType, + String index, + @Nullable String id, + @Nullable Map<String, Object> document, + @Nullable Map<String, Object> upsertDocument) { + this.operationType = checkNotNull(operationType); + this.index = checkNotNull(index); + this.id = id; + this.document = document; + this.upsertDocument = upsertDocument; + } + + /** + * Creates an index operation. + * + * @param index the target index + * @param id the document id (can be null for auto-generated ids) + * @param document the document to index + * @return the bulk operation + */ + public static BulkOperation index( + String index, @Nullable String id, Map<String, Object> document) { + checkNotNull(document, "Document cannot be null for index operations"); + return new BulkOperation(OperationType.INDEX, index, id, document, null); + } + + /** + * Creates a delete operation. + * + * @param index the target index + * @param id the document id to delete + * @return the bulk operation + */ + public static BulkOperation delete(String index, String id) { Review Comment: In previous method `id` also can be null and marked with `@Nullable` annotation, here not can we align them having same approach? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
