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

org.apache.iceberg.Metrics Maven / Gradle / Ivy

The newest version!
/*
 * 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.iceberg;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.util.ByteBuffers;

/**
 * Iceberg file format metrics.
 */
public class Metrics implements Serializable {

  private Long rowCount = null;
  private Map columnSizes = null;
  private Map valueCounts = null;
  private Map nullValueCounts = null;
  private Map nanValueCounts = null;
  private Map lowerBounds = null;
  private Map upperBounds = null;

  public Metrics() {
  }

  public Metrics(Long rowCount,
                 Map columnSizes,
                 Map valueCounts,
                 Map nullValueCounts,
                 Map nanValueCounts) {
    this.rowCount = rowCount;
    this.columnSizes = columnSizes;
    this.valueCounts = valueCounts;
    this.nullValueCounts = nullValueCounts;
    this.nanValueCounts = nanValueCounts;
  }

  public Metrics(Long rowCount,
                 Map columnSizes,
                 Map valueCounts,
                 Map nullValueCounts,
                 Map nanValueCounts,
                 Map lowerBounds,
                 Map upperBounds) {
    this.rowCount = rowCount;
    this.columnSizes = columnSizes;
    this.valueCounts = valueCounts;
    this.nullValueCounts = nullValueCounts;
    this.nanValueCounts = nanValueCounts;
    this.lowerBounds = lowerBounds;
    this.upperBounds = upperBounds;
  }

  /**
   * Get the number of records (rows) in file.
   *
   * @return the count of records (rows) in the file as a long
   */
  public Long recordCount() {
    return rowCount;
  }

  /**
   * Get the number of bytes for all fields in a file.
   *
   * @return a Map of fieldId to the size in bytes
   */
  public Map columnSizes() {
    return columnSizes;
  }

  /**
   * Get the number of all values, including nulls, NaN and repeated.
   *
   * @return a Map of fieldId to the number of all values including nulls, NaN and repeated
   */
  public Map valueCounts() {
    return valueCounts;
  }

  /**
   * Get the number of null values for all fields in a file.
   *
   * @return a Map of fieldId to the number of nulls
   */
  public Map nullValueCounts() {
    return nullValueCounts;
  }

  /**
   * Get the number of NaN values for all float and double fields in a file.
   *
   * @return a Map of fieldId to the number of NaN counts
   */
  public Map nanValueCounts() {
    return nanValueCounts;
  }

  /**
   * Get the non-null lower bound values for all fields in a file.
   *
   * To convert the {@link ByteBuffer} back to a value, use
   * {@link org.apache.iceberg.types.Conversions#fromByteBuffer}.
   *
   * @return a Map of fieldId to the lower bound value as a ByteBuffer
   * @see 
   *   Iceberg Spec - Appendix D: Single-value serialization
   */
  public Map lowerBounds() {
    return lowerBounds;
  }

  /**
   * Get the non-null upper bound values for all fields in a file.
   *
   * @return a Map of fieldId to the upper bound value as a ByteBuffer
   */
  public Map upperBounds() {
    return upperBounds;
  }

  /**
   * Implemented the method to enable serialization of ByteBuffers.
   * @param out The stream where to write
   * @throws IOException On serialization error
   */
  private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeObject(rowCount);
    out.writeObject(columnSizes);
    out.writeObject(valueCounts);
    out.writeObject(nullValueCounts);
    out.writeObject(nanValueCounts);

    writeByteBufferMap(out, lowerBounds);
    writeByteBufferMap(out, upperBounds);
  }

  private static void writeByteBufferMap(ObjectOutputStream out, Map byteBufferMap)
      throws IOException {
    if (byteBufferMap == null) {
      out.writeInt(-1);

    } else {
      // Write the size
      out.writeInt(byteBufferMap.size());

      for (Map.Entry entry : byteBufferMap.entrySet()) {
        // Write the key and the value converted to byte[]
        out.writeObject(entry.getKey());
        out.writeObject(ByteBuffers.toByteArray(entry.getValue()));
      }
    }
  }

  /**
   * Implemented the method to enable deserialization of ByteBuffers.
   * @param in The stream to read from
   * @throws IOException On serialization error
   * @throws ClassNotFoundException If the class is not found
   */
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    rowCount = (Long) in.readObject();
    columnSizes = (Map) in.readObject();
    valueCounts = (Map) in.readObject();
    nullValueCounts = (Map) in.readObject();
    nanValueCounts = (Map) in.readObject();

    lowerBounds = readByteBufferMap(in);
    upperBounds = readByteBufferMap(in);
  }

  private static Map readByteBufferMap(ObjectInputStream in)
      throws IOException, ClassNotFoundException {
    int size = in.readInt();

    if (size == -1) {
      return null;

    } else {
      Map result = Maps.newHashMapWithExpectedSize(size);

      for (int i = 0; i < size; ++i) {
        Integer key = (Integer) in.readObject();
        byte[] data = (byte[]) in.readObject();

        if (data != null) {
          result.put(key, ByteBuffer.wrap(data));
        } else {
          result.put(key, null);
        }
      }

      return result;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy