turcsanyip commented on code in PR #10467:
URL: https://github.com/apache/nifi/pull/10467#discussion_r2748109111


##########
nifi-extension-bundles/nifi-couchbase-bundle/nifi-couchbase-services/src/main/java/org/apache/nifi/services/couchbase/CouchbaseMapCacheClient.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.nifi.services.couchbase;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.distributed.cache.client.AtomicCacheEntry;
+import 
org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient;
+import org.apache.nifi.distributed.cache.client.Deserializer;
+import org.apache.nifi.distributed.cache.client.Serializer;
+import org.apache.nifi.services.couchbase.exception.CouchbaseException;
+import org.apache.nifi.services.couchbase.utils.CouchbaseGetResult;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+@Tags({"distributed", "cache", "map", "cluster", "couchbase"})
+@CapabilityDescription("Provides the ability to communicate with a Couchbase 
Server cluster as a DistributedMapCacheServer." +
+        " This can be used in order to share a Map between nodes in a NiFi 
cluster." +
+        " Couchbase Server cluster can provide a high available and persistent 
cache storage.")
+public class CouchbaseMapCacheClient extends AbstractCouchbaseService 
implements AtomicDistributedMapCacheClient<Long> {
+
+    public static final PropertyDescriptor COUCHBASE_CONNECTION_SERVICE = new 
PropertyDescriptor.Builder()
+            .name("Couchbase Connection Service")
+            .description("A Couchbase Connection Service which manages 
connections to a Couchbase cluster.")
+            .required(true)
+            .identifiesControllerService(CouchbaseConnectionService.class)
+            .build();
+
+    private static final List<PropertyDescriptor> PROPERTIES = List.of(
+            COUCHBASE_CONNECTION_SERVICE,
+            BUCKET_NAME,
+            SCOPE_NAME,
+            COLLECTION_NAME
+    );
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return PROPERTIES;
+    }
+
+    @Override
+    public <K, V> AtomicCacheEntry<K, V, Long> fetch(K key, Serializer<K> 
keySerializer, Deserializer<V> valueDeserializer) throws IOException {
+        final String documentId = serializeDocumentKey(key, keySerializer);
+        try {
+            final CouchbaseGetResult result = 
couchbaseClient.getDocument(documentId);
+            return new AtomicCacheEntry<>(key, 
deserializeDocument(valueDeserializer, result.resultContent()), result.cas());
+        } catch (CouchbaseException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public <K, V> boolean replace(AtomicCacheEntry<K, V, Long> entry, 
Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException {
+        final String documentId = serializeDocumentKey(entry.getKey(), 
keySerializer);
+        final byte[] document = serializeDocument(entry.getValue(), 
valueSerializer);
+
+        try {
+            couchbaseClient.replaceDocument(documentId, document);
+            return true;
+        } catch (CouchbaseException e) {
+            return false;
+        }
+    }
+
+    @Override
+    public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> 
keySerializer, Serializer<V> valueSerializer) throws IOException {
+        final String documentId = serializeDocumentKey(key, keySerializer);
+        final byte[] document = serializeDocument(value, valueSerializer);
+
+        try {
+            couchbaseClient.insertDocument(documentId, document);
+            return true;
+        } catch (CouchbaseException e) {
+            return false;
+        }
+    }
+
+    @Override
+    public <K, V> V getAndPutIfAbsent(K key, V value, Serializer<K> 
keySerializer, Serializer<V> valueSerializer, Deserializer<V> 
valueDeserializer) throws IOException {
+        if (containsKey(key, keySerializer)) {
+            return get(key, keySerializer, valueDeserializer);
+        }

Review Comment:
   The cache entry can be deleted or expired after `containsKey()` and `get()` 
may return `null`. So it would be more error-proof to just call `get()` and 
return if something is found.



##########
nifi-extension-bundles/nifi-couchbase-bundle/nifi-couchbase-services/src/main/java/org/apache/nifi/services/couchbase/CouchbaseMapCacheClient.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.nifi.services.couchbase;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.distributed.cache.client.AtomicCacheEntry;
+import 
org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient;
+import org.apache.nifi.distributed.cache.client.Deserializer;
+import org.apache.nifi.distributed.cache.client.Serializer;
+import org.apache.nifi.services.couchbase.exception.CouchbaseException;
+import org.apache.nifi.services.couchbase.utils.CouchbaseGetResult;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+@Tags({"distributed", "cache", "map", "cluster", "couchbase"})
+@CapabilityDescription("Provides the ability to communicate with a Couchbase 
Server cluster as a DistributedMapCacheServer." +
+        " This can be used in order to share a Map between nodes in a NiFi 
cluster." +
+        " Couchbase Server cluster can provide a high available and persistent 
cache storage.")
+public class CouchbaseMapCacheClient extends AbstractCouchbaseService 
implements AtomicDistributedMapCacheClient<Long> {
+
+    public static final PropertyDescriptor COUCHBASE_CONNECTION_SERVICE = new 
PropertyDescriptor.Builder()
+            .name("Couchbase Connection Service")
+            .description("A Couchbase Connection Service which manages 
connections to a Couchbase cluster.")
+            .required(true)
+            .identifiesControllerService(CouchbaseConnectionService.class)
+            .build();
+
+    private static final List<PropertyDescriptor> PROPERTIES = List.of(
+            COUCHBASE_CONNECTION_SERVICE,
+            BUCKET_NAME,
+            SCOPE_NAME,
+            COLLECTION_NAME
+    );
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return PROPERTIES;
+    }
+
+    @Override
+    public <K, V> AtomicCacheEntry<K, V, Long> fetch(K key, Serializer<K> 
keySerializer, Deserializer<V> valueDeserializer) throws IOException {
+        final String documentId = serializeDocumentKey(key, keySerializer);
+        try {
+            final CouchbaseGetResult result = 
couchbaseClient.getDocument(documentId);
+            return new AtomicCacheEntry<>(key, 
deserializeDocument(valueDeserializer, result.resultContent()), result.cas());
+        } catch (CouchbaseException e) {

Review Comment:
   Only `DocumentNotFoundException` should be handled gracefully. Other 
exceptions should be wrapped into `IOException` and thrown.
   
   Similarly: `replace()`, `remove()`, `get()`
   
   `DocumentExistsException`: `putIfAbsent()`



##########
nifi-extension-bundles/nifi-couchbase-bundle/nifi-couchbase-services/src/main/java/org/apache/nifi/services/couchbase/CouchbaseMapCacheClient.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.nifi.services.couchbase;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.distributed.cache.client.AtomicCacheEntry;
+import 
org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient;
+import org.apache.nifi.distributed.cache.client.Deserializer;
+import org.apache.nifi.distributed.cache.client.Serializer;
+import org.apache.nifi.services.couchbase.exception.CouchbaseException;
+import org.apache.nifi.services.couchbase.utils.CouchbaseGetResult;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+@Tags({"distributed", "cache", "map", "cluster", "couchbase"})
+@CapabilityDescription("Provides the ability to communicate with a Couchbase 
Server cluster as a DistributedMapCacheServer." +
+        " This can be used in order to share a Map between nodes in a NiFi 
cluster." +
+        " Couchbase Server cluster can provide a high available and persistent 
cache storage.")
+public class CouchbaseMapCacheClient extends AbstractCouchbaseService 
implements AtomicDistributedMapCacheClient<Long> {
+
+    public static final PropertyDescriptor COUCHBASE_CONNECTION_SERVICE = new 
PropertyDescriptor.Builder()
+            .name("Couchbase Connection Service")
+            .description("A Couchbase Connection Service which manages 
connections to a Couchbase cluster.")
+            .required(true)
+            .identifiesControllerService(CouchbaseConnectionService.class)
+            .build();
+
+    private static final List<PropertyDescriptor> PROPERTIES = List.of(
+            COUCHBASE_CONNECTION_SERVICE,
+            BUCKET_NAME,
+            SCOPE_NAME,
+            COLLECTION_NAME
+    );
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return PROPERTIES;
+    }
+
+    @Override
+    public <K, V> AtomicCacheEntry<K, V, Long> fetch(K key, Serializer<K> 
keySerializer, Deserializer<V> valueDeserializer) throws IOException {
+        final String documentId = serializeDocumentKey(key, keySerializer);
+        try {
+            final CouchbaseGetResult result = 
couchbaseClient.getDocument(documentId);
+            return new AtomicCacheEntry<>(key, 
deserializeDocument(valueDeserializer, result.resultContent()), result.cas());
+        } catch (CouchbaseException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public <K, V> boolean replace(AtomicCacheEntry<K, V, Long> entry, 
Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException {
+        final String documentId = serializeDocumentKey(entry.getKey(), 
keySerializer);
+        final byte[] document = serializeDocument(entry.getValue(), 
valueSerializer);
+
+        try {
+            couchbaseClient.replaceDocument(documentId, document);
+            return true;
+        } catch (CouchbaseException e) {
+            return false;
+        }
+    }
+
+    @Override
+    public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> 
keySerializer, Serializer<V> valueSerializer) throws IOException {
+        final String documentId = serializeDocumentKey(key, keySerializer);
+        final byte[] document = serializeDocument(value, valueSerializer);
+
+        try {
+            couchbaseClient.insertDocument(documentId, document);
+            return true;
+        } catch (CouchbaseException e) {
+            return false;
+        }
+    }
+
+    @Override
+    public <K, V> V getAndPutIfAbsent(K key, V value, Serializer<K> 
keySerializer, Serializer<V> valueSerializer, Deserializer<V> 
valueDeserializer) throws IOException {
+        if (containsKey(key, keySerializer)) {
+            return get(key, keySerializer, valueDeserializer);
+        }
+
+        put(key, value, keySerializer, valueSerializer);

Review Comment:
   Based on the method name shouldn't it be `putIfAbsent()`?



##########
nifi-extension-bundles/nifi-couchbase-bundle/nifi-couchbase-standard-services/src/main/java/org/apache/nifi/services/couchbase/StandardCouchbaseClient.java:
##########
@@ -141,6 +152,76 @@ public CouchbaseUpsertResult upsertDocument(String 
documentId, byte[] content) t
         }
     }
 
+    @Override
+    public boolean documentExists(String documentId) throws CouchbaseException 
{
+        try {
+            final ExistsResult result = collection.exists(documentId);
+            return result.exists();
+        } catch (Exception e) {
+            throw new CouchbaseException("Failed to check document [%s] in 
Couchbase".formatted(documentId), e);
+        }
+    }
+
+    @Override
+    public void insertDocument(String documentId, byte[] content) throws 
CouchbaseException {
+        if (!getInputValidator(documentType).test(content)) {
+            throw new CouchbaseException("The provided input is invalid for 
document [%s]".formatted(documentId));
+        }
+
+        try {
+            collection.insert(documentId, content,
+                    InsertOptions.insertOptions()
+                            .durability(persistTo, replicateTo)
+                            .transcoder(getTranscoder(documentType))
+                            .clientContext(new HashMap<>()));
+        } catch (Exception e) {
+            throw new CouchbaseException("Failed to insert document [%s] in 
Couchbase".formatted(documentId), e);
+        }
+    }
+
+    @Override
+    public void removeDocument(String documentId) throws CouchbaseException {
+        try {
+            collection.remove(documentId);
+        } catch (Exception e) {
+            throw new CouchbaseException("Failed to remove document [%s] in 
Couchbase".formatted(documentId), e);
+        }
+    }
+
+    @Override
+    public void replaceDocument(String documentId, byte[] content) throws 
CouchbaseException {
+        if (!getInputValidator(documentType).test(content)) {
+            throw new CouchbaseException("The provided input is invalid for 
document [%s]".formatted(documentId));
+        }
+
+        try {
+            collection.replace(documentId, content,
+                    ReplaceOptions.replaceOptions()
+                            .durability(persistTo, replicateTo)
+                            .transcoder(getTranscoder(documentType))
+                            .clientContext(new HashMap<>()));
+        } catch (Exception e) {
+            throw new CouchbaseException("Failed to replace document [%s] in 
Couchbase".formatted(documentId), e);
+        }
+    }
+
+    @Override
+    public CouchbaseLookupInResult lookUpIn(String documentId, String 
subDocPath) throws CouchbaseException {

Review Comment:
   Based on the Couchbase Java client type and method names, the correct 
spelling would be: `lookupIn()`



-- 
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]

Reply via email to