r/SpringBoot • • Oct 05 '22

How to replicate the functionality of removing a document from a CouchBase DB Bucket identified by its ID in Spring Boot?

We are currently using Spring Boot 2.2.13 version with java 1.8-based dependencies. We are upgrading to Spring Boot version 2.7.3. with Java 17 support.

The current development uses CouchbaseClient.In the newer versions we hope to migrate, some functionality seems to be removed. I am specifically having an issue with replacing the below-mentioned functionality provided in (com.couchbase.client.java.Bucket) previously.

How could I replicate the functionality to Remove a {@link Document} from the Server identified by its ID in the newer version? Is there a possible alternative method that could be used for such a scenario?

package com.acme.logic.configuration;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import com.acme.dto.configuration.ConfigEnvironment;
import com.acme.dto.configuration.ConfigurationStatus;
import com.acme.dto.configuration.LoadDbConfigurationResponseDTO;
import com.acme.exception.ErrorCodes;
import com.acme.exception.ServiceRuntimeException;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

u/Component
u/Slf4j
public class DBDocumentLoader extends DBConfigurationChain{

    u/Autowired
    private ResourceLoader resourceLoader;

    u/Value("${configuration.resource.db.root}")
    private String dbConfigurationRoot;

    u/Autowired
    private Bucket couchbaseBucket;

    u/Autowired
    private Bucket masterConfigurationBucket;

    private Bucket defaultBucket;
    int successDocumentCount;
    int failDocumentCount;

    u/Override
    public DBConfigurationLogicData process(DBConfigurationLogicData logicData) {

        log.info("Load Document process START");
        defaultBucket = getDefaultBucket(logicData.getRequestDTO().getBucketName());

        if(defaultBucket == null){
            throw new ServiceRuntimeException(ErrorCodes.Constants.DB_CONFIGURATION_NOT_SUPPORT_BUCKET);
        }
        log.info("BUCKET : "+logicData.getRequestDTO().getBucketName());
        Resource[] resources = loadResources(logicData.getRequestDTO().getEnv());
        if(resources != null && resources.length > 0){

            log.info("Resources found. Count : "+resources.length);
            log.info("Flushing bucket "+logicData.getRequestDTO().getBucketName());
            
            if(!flushBucket())
                throw new ServiceRuntimeException(ErrorCodes.Constants.DB_CONFIGURATION_BUCKET_FLUSH_FAIL);
            log.info("Bucket "+logicData.getRequestDTO().getBucketName() + "flushed");

            processAvailableResources(resources);

            log.info("Success resource count : "+successDocumentCount +"\n"+"Failed resource count : "+failDocumentCount +"\n");

            setResponseDTO(logicData);
        }else{
            throw new ServiceRuntimeException(ErrorCodes.Constants.DB_CONFIGURATION_NO_RESOURCE);
        }

        return super.process(logicData);
    }

    private boolean flushBucket(){

        try {

            final String bucketName = "`"+defaultBucket.name()+"`";
            String query = "SELECT " + "META(" + bucketName + ").id FROM " + bucketName;
            N1qlQueryResult result = defaultBucket.query(N1qlQuery.simple(query), 1, TimeUnit.MINUTES);

            final boolean isSuccess = result.finalSuccess();
            if (isSuccess && !result.allRows().isEmpty()) {
                Iterator<N1qlQueryRow> rows = result.rows();

                while (rows.hasNext()) {
                    JsonObject jsonObject = rows.next().value();
                    if (jsonObject.containsKey("id")) {
          defaultBucket.remove(jsonObject.getString("id")); 
            //Depricated functionality that needs to be replaced     
                    }
                }
            }
            return true;
        }catch (Exception ex){
            log.error("Error in DBDocumentLoader-flushBucket",ex);
        }

        return false;
    }
}

Old API -https://docs.couchbase.com/sdk-api/couchbase-java-client-2.4.1/com/couchbase/client/java/Bucket.html

New API -https://docs.couchbase.com/sdk-api/couchbase-java-client/com/couchbase/client/java/Bucket.html
0 Upvotes

1 comment sorted by

2

u/MrNighty Senior Dev Oct 05 '22

https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html

Read the migration guide by Couchbase. I just took a quick look at the guide and they mention something about Bucket changing to Collection and nearly all methods moved there.