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

org.projectnessie.versioned.tiered.gc.ValueRetriever Maven / Gradle / Ivy

There is a newer version: 0.9.2
Show newest version
/*
 * Copyright (C) 2020 Dremio
 *
 * Licensed 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.projectnessie.versioned.tiered.gc;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;
import org.projectnessie.versioned.store.Store;
import org.projectnessie.versioned.store.ValueType;
import org.projectnessie.versioned.tiered.BaseValue;

/** Utility class used to extract values from underlying store and convert to a Spark Dataset. */
public class ValueRetriever, OUT> {

  private final Supplier supplier;
  private final ValueType valueType;
  private final Function, OUT> builder;
  private final Class clazz;

  /** Construct a retriever. */
  public ValueRetriever(
      Supplier supplier,
      ValueType valueType,
      Function, OUT> builder,
      Class clazz) {
    super();
    this.supplier = supplier;
    this.valueType = valueType;
    this.builder = builder;
    this.clazz = clazz;
  }

  private Stream stream() {
    return supplier.get().getValues(valueType).map(builder::apply);
  }

  /** Get a dataset from this retriever. */
  public Dataset get(SparkSession spark, Optional> filter) {
    List out = this.stream().filter(filter.orElse(t -> true)).collect(Collectors.toList());
    return spark.createDataset(out, Encoders.bean(clazz));
  }

  public static , OUT> Dataset dataset(
      Supplier supplier,
      ValueType valueType,
      Class clazz,
      Optional> filter,
      SparkSession spark,
      Function, OUT> converter) {
    return new ValueRetriever(supplier, valueType, converter, clazz).get(spark, filter);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy