All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tarantool.driver.core.metadata.TarantoolIndexMetadataConverter Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.core.metadata;

import io.tarantool.driver.api.metadata.TarantoolIndexMetadata;
import io.tarantool.driver.api.metadata.TarantoolIndexPartMetadata;
import io.tarantool.driver.api.metadata.TarantoolIndexType;
import io.tarantool.driver.mappers.MessagePackValueMapper;
import io.tarantool.driver.mappers.converters.ValueConverter;
import org.msgpack.value.ArrayValue;
import org.msgpack.value.ImmutableStringValue;
import org.msgpack.value.Value;
import org.msgpack.value.impl.ImmutableStringValueImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Maps MessagePack {@link ArrayValue} into {@link TarantoolIndexMetadataImpl}
 *
 * @author Alexey Kuzin
 */
public class TarantoolIndexMetadataConverter implements ValueConverter {

    private static final long serialVersionUID = 20200708L;

    private static final ImmutableStringValue INDEX_FIELD_KEY = new ImmutableStringValueImpl("field");
    private static final ImmutableStringValue INDEX_TYPE_KEY = new ImmutableStringValueImpl("type");

    private final MessagePackValueMapper mapper;

    public TarantoolIndexMetadataConverter(MessagePackValueMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public TarantoolIndexMetadata fromValue(ArrayValue value) {
        Iterator it = value.iterator();
        TarantoolIndexMetadataImpl metadata = new TarantoolIndexMetadataImpl();
        metadata.setSpaceId(mapper.fromValue(it.next().asIntegerValue()));
        metadata.setIndexId(mapper.fromValue(it.next().asIntegerValue()));
        metadata.setIndexName(mapper.fromValue(it.next().asStringValue()));
        metadata.setIndexType(TarantoolIndexType.fromString(mapper.fromValue(it.next().asStringValue())));

        TarantoolIndexOptionsImpl indexOptions = new TarantoolIndexOptionsImpl();
        Map optionsMap = mapper.fromValue(it.next().asMapValue());
        indexOptions.setUnique((Boolean) optionsMap.get("unique"));

        metadata.setIndexOptions(indexOptions);

        ArrayValue indexPartsValue = it.next().asArrayValue();

        List indexParts = new ArrayList<>();

        //There are two index formats:
        // as array: [[0, 'unsigned'], [3, 'string'],...]
        // as map: [{'field' : 0, 'type' : 'unsigned'}, {'field' : 3, 'type' : 'string'}, ...]
        if (indexPartsValue.size() > 0) {
            if (indexPartsValue.get(0).isArrayValue()) {
                indexParts = indexPartsValue.list().stream()
                    .map(partValue -> new TarantoolIndexPartMetadataImpl<>(
                        partValue.asArrayValue().get(0).asIntegerValue().asInt(),
                        partValue.asArrayValue().get(1).asStringValue().asString()
                    )).collect(Collectors.toList());
            } else {
                indexParts = indexPartsValue.list().stream()
                    .map(partValue -> new TarantoolIndexPartMetadataImpl<>(
                        partValue.asMapValue().map().get(INDEX_FIELD_KEY).asIntegerValue().asInt(),
                        partValue.asMapValue().map().get(INDEX_TYPE_KEY).asStringValue().asString()
                    )).collect(Collectors.toList());
            }
        }

        metadata.setIndexParts(indexParts);

        return metadata;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy