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

io.tarantool.driver.metadata.TarantoolSpaceMetadataConverter 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.metadata;

import io.tarantool.driver.mappers.MessagePackValueMapper;
import io.tarantool.driver.mappers.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.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

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

    private static final ImmutableStringValue FORMAT_FIELD_NAME = new ImmutableStringValueImpl("name");
    private static final ImmutableStringValue FORMAT_FIELD_TYPE = new ImmutableStringValueImpl("type");

    private MessagePackValueMapper mapper;

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

    @Override
    public TarantoolSpaceMetadata fromValue(ArrayValue value) {
        Iterator it = value.iterator();
        TarantoolSpaceMetadata metadata = new TarantoolSpaceMetadata();
        metadata.setSpaceId(mapper.fromValue(it.next().asIntegerValue()));
        metadata.setOwnerId(mapper.fromValue(it.next().asIntegerValue()));
        metadata.setSpaceName(mapper.fromValue(it.next().asStringValue()));

        Value spaceMetadataValue =  it.next();
        while (!spaceMetadataValue.isArrayValue()) {
            spaceMetadataValue =  it.next();
        }

        LinkedHashMap spaceFormatMetadata = new LinkedHashMap<>();

        int fieldPosition = 0;
        for (Value fieldValueMetadata : spaceMetadataValue.asArrayValue()) {
            Map fieldMap = fieldValueMetadata.asMapValue().map();
            spaceFormatMetadata.put(
                    fieldMap.get(FORMAT_FIELD_NAME).toString(),
                    new TarantoolFieldMetadata(
                            fieldMap.get(FORMAT_FIELD_NAME).toString(),
                            fieldMap.get(FORMAT_FIELD_TYPE).toString(),
                            fieldPosition
                    )
            );
            fieldPosition++;
        }

        metadata.setSpaceFormatMetadata(spaceFormatMetadata);

        return metadata;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy