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

io.tarantool.driver.metadata.TarantoolIndexMetadata 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.protocol.TarantoolIndexQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Represents Tarantool index metadata (index ID, name, etc.)
 *
 * @author Alexey Kuzin
 */
public class TarantoolIndexMetadata {

    private int spaceId;
    private int indexId;
    private String indexName;
    private TarantoolIndexType indexType;
    private TarantoolIndexOptions indexOptions;
    private List indexParts;
    private Map indexPartsByPosition;
    private Map fieldPositionToKeyPosition;

    /**
     * Get ID of a space that this index is defined on
     * @return a number
     */
    public int getSpaceId() {
        return spaceId;
    }

    /**
     * Set space ID
     * @param spaceId a number
     */
    void setSpaceId(int spaceId) {
        this.spaceId = spaceId;
    }

    /**
     * Get index ID in the corresponding space on the Tarantool server
     * @return a natural number
     */
    public int getIndexId() {
        return indexId;
    }

    /**
     * Set index ID
     * @param indexId a positive number
     */
    void setIndexId(int indexId) {
        this.indexId = indexId;
    }

    /**
     * Get index name
     * @return a non-empty {@code String}
     */
    public String getIndexName() {
        return indexName;
    }

    /**
     * Set index name
     * @param indexName a non-empty {@code String}
     */
    void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    /**
     * Get index type
     * @return the index type
     */
    public TarantoolIndexType getIndexType() {
        return indexType;
    }

    /**
     * Set index type
     * @param indexType a non-empty {@link TarantoolIndexType}
     */
    public void setIndexType(TarantoolIndexType indexType) {
        this.indexType = indexType;
    }

    /**
     * Get index options
     * @return index options
     */
    public TarantoolIndexOptions getIndexOptions() {
        return indexOptions;
    }

    /**
     * Set index options
     * @param indexOptions a not-empty {@link TarantoolIndexOptions}
     */
    public void setIndexOptions(TarantoolIndexOptions indexOptions) {
        this.indexOptions = indexOptions;
    }

    /**
     * Set index parts
     * @param indexParts a not-empty list of {@link TarantoolIndexPartMetadata}
     */
    public void setIndexParts(List indexParts) {
        this.indexParts = indexParts;
        this.indexPartsByPosition = indexParts.stream()
                .collect(Collectors.toMap(TarantoolIndexPartMetadata::getFieldIndex, Function.identity()));
        this.fieldPositionToKeyPosition = new HashMap<>();
        int index = 0;
        for (TarantoolIndexPartMetadata meta : indexParts) {
            fieldPositionToKeyPosition.put(meta.getFieldIndex(), index++);
        }
    }

    /**
     * Get index parts
     * @return a not-empty list of {@link TarantoolIndexPartMetadata}
     */
    public List getIndexParts() {
        return indexParts;
    }

    /**
     * Get index parts by field indexes
     * @return a not-empty map of index positions to {@link TarantoolIndexPartMetadata}
     */
    public Map getIndexPartsByPosition() {
        return indexPartsByPosition;
    }

    /**
     * Get map of field positions to index parts positions
     * @param fieldPosition field position in tuple, starting from 0
     * @return field position
     */
    public Optional getIndexPartPositionByFieldPosition(int fieldPosition) {
        return Optional.ofNullable(fieldPositionToKeyPosition.get(fieldPosition));
    }

    /**
     * Returns true if this is a primary index, false otherwise.
     * @return true if this is a primary index, false otherwise.
     */
    public boolean isPrimary() {
        return indexId == TarantoolIndexQuery.PRIMARY;
    }

    /**
     * Returns true if this is a unique index, false otherwise.
     * @return true if this is a unique index, false otherwise.
     */
    public boolean isUnique() {
        return isPrimary() || indexOptions.isUnique();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy