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

io.tarantool.driver.core.metadata.RowsMetadataToTarantoolTupleResultConverter 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 java.util.Map;

import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.metadata.TarantoolSpaceMetadata;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.core.TarantoolResultFactory;
import io.tarantool.driver.mappers.converters.ValueConverter;
import io.tarantool.driver.mappers.converters.value.ArrayValueToTarantoolTupleConverter;
import org.msgpack.value.ArrayValue;
import org.msgpack.value.MapValue;
import org.msgpack.value.StringValue;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;

import static io.tarantool.driver.core.TarantoolResultFactory.getInstance;

/**
 * @author Artyom Dubinin
 */
public class RowsMetadataToTarantoolTupleResultConverter
    implements ValueConverter> {

    private static final long serialVersionUID = -5228606294087295535L;

    protected static final StringValue RESULT_META = ValueFactory.newString("metadata");
    protected static final StringValue RESULT_ROWS = ValueFactory.newString("rows");

    protected static final CRUDResponseToTarantoolSpaceMetadataConverter spaceMetadataConverter =
        CRUDResponseToTarantoolSpaceMetadataConverter.getInstance();

    private final ArrayValueToTarantoolTupleConverter tupleConverter;
    private final TarantoolResultFactory tarantoolResultFactory;

    public RowsMetadataToTarantoolTupleResultConverter(ArrayValueToTarantoolTupleConverter tupleConverter) {
        super();
        this.tupleConverter = tupleConverter;
        this.tarantoolResultFactory = getInstance();
    }

    @Override
    public TarantoolResult fromValue(MapValue value) {
        Map tupleMap = value.asMapValue().map();
        ArrayValue rawTuples = tupleMap.get(RESULT_ROWS).asArrayValue();
        ArrayValue rawMetadata = tupleMap.get(RESULT_META).asArrayValue();
        TarantoolSpaceMetadata parsedMetadata = spaceMetadataConverter.fromValue(rawMetadata);

        return tarantoolResultFactory.createTarantoolTupleResultImpl(rawTuples, parsedMetadata, tupleConverter);
    }

    @Override
    public boolean canConvertValue(MapValue value) {
        // {"metadata" : [...], "rows": [...]}
        Map tupleMap = value.asMapValue().map();
        if (!hasRowsAndMetadata(tupleMap)) {
            return false;
        }
        return tupleMap.get(RESULT_ROWS).isArrayValue() && tupleMap.get(RESULT_META).isArrayValue();
    }

    static boolean hasRowsAndMetadata(Map valueMap) {
        return valueMap.containsKey(RESULT_META) && valueMap.containsKey(RESULT_ROWS);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy