package io.vertx.redis.client.impl;
import io.vertx.redis.client.Response;
import java.util.*;
class Slots {
private static final SplittableRandom RANDOM = new SplittableRandom();
static class Slot {
private final int start;
private final int end;
private final String[] endpoints;
Slot(int start, int end, int size) {
this.start = start;
this.end = end;
endpoints = new String[size];
}
}
private final long ts = System.currentTimeMillis();
private final int size;
private final Slot[] slots;
private final String[] endpoints;
private final String[] masterEndpoints;
Slots(String connectionString, Response reply) {
size = reply.size();
slots = new Slot[size];
final RedisURI uri = new RedisURI(connectionString);
Set<String> uniqueEndpoints = new HashSet<>();
final List<String> masterEndpoints = new ArrayList<>();
for (int i = 0; i < reply.size(); i++) {
Response s = reply.get(i);
slots[i] = new Slot(
s.get(0).toInteger(),
s.get(1).toInteger(),
s.size() - 2);
for (int index = 2; index < s.size(); index++) {
final Response c = s.get(index);
final String host = c.get(0).toString().contains(":") ? "[" + c.get(0).toString() + "]" : c.get(0).toString();
final String endpoint = uri.protocol() + "://" + uri.userinfo() + host + ":" + c.get(1).toInteger();
slots[i].endpoints[index - 2] = endpoint;
uniqueEndpoints.add(endpoint);
if (index == 2) {
masterEndpoints.add(endpoint);
}
}
}
endpoints = new String[uniqueEndpoints.size()];
int i = 0;
for (String endpoint : uniqueEndpoints) {
endpoints[i++] = endpoint;
}
this.masterEndpoints = masterEndpoints.toArray(new String[0]);
}
boolean contains(String endpoint) {
for (String entry : endpoints) {
if (endpoint.equals(entry)) {
return true;
}
}
return false;
}
int size() {
return size;
}
String[] endpointsForSlot(int index) {
return slots[index].endpoints;
}
String[] endpointsForKey(int key) {
for (Slot s : slots) {
if (key >= s.start && key <= s.end) {
return s.endpoints;
}
}
return null;
}
String randomEndPoint(boolean onlyMasterEndpoints) {
if (onlyMasterEndpoints) {
return masterEndpoints[RANDOM.nextInt(masterEndpoints.length)];
}
return endpoints[RANDOM.nextInt(endpoints.length)];
}
String[] endpoints() {
return endpoints;
}
long timestamp() {
return ts;
}
}