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

org.elasticsearch.xpack.esql.planner.ExchangeLayout Maven / Gradle / Ivy

There is a newer version: 8.16.1
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

package org.elasticsearch.xpack.esql.planner;

import org.elasticsearch.xpack.esql.core.expression.NameId;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Decorating layout that creates the NameId -> Value lazily based on the calls made to its content.
 * Essentially it maps the existing (old) NameIds to the new ones.
 */
class ExchangeLayout implements Layout {
    private final Layout delegate;
    private final List inverse;
    private final Map mappingToOldLayout;
    private int counter;

    ExchangeLayout(Layout delegate) {
        this.delegate = delegate;
        this.inverse = delegate.inverse();
        this.mappingToOldLayout = new HashMap<>(inverse.size());
    }

    @Override
    public ChannelAndType get(NameId id) {
        var oldId = mappingToOldLayout.get(id);
        if (oldId == null && counter < inverse.size()) {
            var names = inverse.get(counter++).nameIds();
            for (var name : names) {
                oldId = name;
                mappingToOldLayout.put(id, oldId);
            }
        }
        return delegate.get(oldId);
    }

    @Override
    public int numberOfChannels() {
        return delegate.numberOfChannels();
    }

    @Override
    public String toString() {
        return "ExchangeLayout{" + delegate + '}';
    }

    @Override
    public Builder builder() {
        throw new UnsupportedOperationException();
    }

    @Override
    public List inverse() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy