Search in sources :

Example 11 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class NodeLocatorHelper method replicaNodeForId.

/**
 * Returns the target replica node address for a given document ID and replica number on the bucket.
 *
 * @param id the document id to convert.
 * @param replicaNum the replica number.
 * @return the node for the given document id.
 */
public String replicaNodeForId(final String id, int replicaNum) {
    if (replicaNum < 1 || replicaNum > 3) {
        throw new IllegalArgumentException("Replica number must be between 1 and 3.");
    }
    BucketConfig config = bucketConfig.get();
    if (config instanceof CouchbaseBucketConfig) {
        CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
        int partitionId = (int) hashId(id) & cbc.numberOfPartitions() - 1;
        int nodeId = cbc.nodeIndexForReplica(partitionId, replicaNum - 1, false);
        if (nodeId == -1) {
            throw new IllegalStateException("No partition assigned to node for Document ID: " + id);
        }
        if (nodeId == -2) {
            throw new IllegalStateException("Replica not configured for this bucket.");
        }
        return cbc.nodeAtIndex(nodeId).hostname();
    } else {
        throw new UnsupportedOperationException("Bucket type not supported: " + config.getClass().getName());
    }
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Aggregations

CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)11 ArrayList (java.util.ArrayList)7 BucketConfig (com.couchbase.client.core.config.BucketConfig)5 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)5 NodeInfo (com.couchbase.client.core.config.NodeInfo)5 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)5 Test (org.junit.jupiter.api.Test)5 RequestContext (com.couchbase.client.core.msg.RequestContext)4 Core (com.couchbase.client.core.Core)3 MemcachedBucketConfig (com.couchbase.client.core.config.MemcachedBucketConfig)3 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)3 CoreContext (com.couchbase.client.core.CoreContext)2 Reactor (com.couchbase.client.core.Reactor)2 Stability (com.couchbase.client.core.annotation.Stability)2 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)2 Duration (java.time.Duration)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2