io.tarantool.driver.metadata.AbstractTarantoolMetadata Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cartridge-driver Show documentation
Show all versions of cartridge-driver Show documentation
Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework
package io.tarantool.driver.metadata;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.utils.Assert;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
/**
* Base class for {@link TarantoolMetadataOperations} implementations
*
* @author Alexey Kuzin
*/
public abstract class AbstractTarantoolMetadata implements TarantoolMetadataOperations {
protected final Map spaceMetadata = new ConcurrentHashMap<>();
protected final Map spaceMetadataById = new ConcurrentHashMap<>();
protected final Map> indexMetadata = new ConcurrentHashMap<>();
protected final Map> indexMetadataBySpaceId =
new ConcurrentHashMap<>();
private final CountDownLatch initLatch = new CountDownLatch(1);
protected Map getSpaceMetadata() {
awaitInitLatch();
return spaceMetadata;
}
protected Map getSpaceMetadataById() {
awaitInitLatch();
return spaceMetadataById;
}
protected Map> getIndexMetadata() {
awaitInitLatch();
return indexMetadata;
}
protected Map> getIndexMetadataBySpaceId() {
awaitInitLatch();
return indexMetadataBySpaceId;
}
@Override
public CompletableFuture refresh() throws TarantoolClientException {
return populateMetadata().whenComplete((v, ex) -> {
if (initLatch.getCount() > 0) {
initLatch.countDown();
}
if (ex != null) {
throw new TarantoolClientException("Failed to refresh spaces and indexes metadata", ex);
}
});
}
protected abstract CompletableFuture populateMetadata();
@Override
public Optional getSpaceByName(String spaceName) {
Assert.hasText(spaceName, "Space name must not be null or empty");
return Optional.ofNullable(getSpaceMetadata().get(spaceName));
}
@Override
public Optional getIndexByName(int spaceId, String indexName) {
Assert.state(spaceId > 0, "Space ID must be greater than 0");
Assert.hasText(indexName, "Index name must not be null or empty");
Map metaMap = getIndexMetadataBySpaceId().get(spaceId);
if (metaMap == null) {
return Optional.empty();
}
return Optional.ofNullable(metaMap.get(indexName));
}
@Override
public Optional getIndexByName(String spaceName, String indexName) {
Assert.hasText(spaceName, "Space name must not be null or empty");
Assert.hasText(indexName, "Index name must not be null or empty");
Map metaMap = getIndexMetadata().get(spaceName);
if (metaMap == null) {
return Optional.empty();
}
return Optional.ofNullable(metaMap.get(indexName));
}
@Override
public Optional getIndexById(String spaceName, int indexId) {
Assert.hasText(spaceName, "Space name must not be null or empty");
Assert.state(indexId >= 0, "Index ID must be greater than or equal 0");
Map metaMap = getIndexMetadata().get(spaceName);
if (metaMap == null) {
return Optional.empty();
}
return metaMap.values().stream().filter(i -> i.getIndexId() == indexId).findFirst();
}
@Override
public Optional getIndexById(int spaceId, int indexId) {
Assert.state(spaceId > 0, "Space ID must be greater than 0");
Assert.state(indexId >= 0, "Index ID must be greater than or equal 0");
Map metaMap = getIndexMetadataBySpaceId().get(spaceId);
if (metaMap == null) {
return Optional.empty();
}
return metaMap.values().stream().filter(i -> i.getIndexId() == indexId).findFirst();
}
@Override
public Optional getSpaceById(int spaceId) {
Assert.state(spaceId > 0, "Space ID must be greater than 0");
return Optional.ofNullable(getSpaceMetadataById().get(spaceId));
}
@Override
public Optional