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

org.apache.hudi.index.secondary.HoodieSecondaryIndex Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.hudi.index.secondary;

import org.apache.hudi.exception.HoodieSecondaryIndexException;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Represents a single secondary index.
 */
public class HoodieSecondaryIndex {
  private String indexName;
  private SecondaryIndexType indexType;

  // The index fields need to be in order
  private LinkedHashMap> columns;
  private Map options;

  public HoodieSecondaryIndex() {
  }

  public HoodieSecondaryIndex(
      String indexName,
      SecondaryIndexType indexType,
      LinkedHashMap> columns,
      Map options) {
    this.indexName = indexName;
    this.indexType = indexType;
    this.columns = columns;
    this.options = options;

    validate();
  }

  public String getIndexName() {
    return indexName;
  }

  public SecondaryIndexType getIndexType() {
    return indexType;
  }

  public Map> getColumns() {
    return columns;
  }

  public Map getOptions() {
    return options;
  }

  public static Builder builder() {
    return new Builder();
  }

  private void validate() {
    switch (indexType) {
      case LUCENE:
        if (columns.size() != 1) {
          throw new HoodieSecondaryIndexException("Lucene index only support single column");
        }
        break;
      default:
        return;
    }
  }

  @Override
  public String toString() {
    return "HoodieIndex{"
        + "indexName='" + indexName + '\''
        + ", indexType=" + indexType
        + ", columns=" + columns
        + ", options=" + options
        + '}';
  }

  /**
   * Builder for {@link HoodieSecondaryIndex}.
   */
  public static class Builder {
    private String indexName;
    private SecondaryIndexType indexType;
    private LinkedHashMap> columns;
    private Map options;

    public Builder setIndexName(String indexName) {
      this.indexName = indexName;
      return this;
    }

    public Builder setIndexType(String indexType) {
      this.indexType = SecondaryIndexType.of(indexType);
      return this;
    }

    public Builder setColumns(LinkedHashMap> columns) {
      this.columns = columns;
      return this;
    }

    public Builder setOptions(Map options) {
      this.options = options;
      return this;
    }

    public HoodieSecondaryIndex build() {
      return new HoodieSecondaryIndex(indexName, indexType, columns, options);
    }
  }

  /**
   * Comparator for {@link HoodieSecondaryIndex}.
   */
  public static class HoodieIndexCompactor implements Comparator {
    @Override
    public int compare(HoodieSecondaryIndex o1, HoodieSecondaryIndex o2) {
      return o1.indexName.compareTo(o2.indexName);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy