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());
}
}
Aggregations