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

com.yahoo.sketches.hive.frequencies.ItemsState Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
/*
 * Copyright 2016, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.sketches.hive.frequencies;

import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator.AbstractAggregationBuffer;

import com.yahoo.memory.Memory;
import com.yahoo.sketches.ArrayOfItemsSerDe;
import com.yahoo.sketches.frequencies.ItemsSketch;

class ItemsState extends AbstractAggregationBuffer {

  private int maxMapSize_;
  private final ArrayOfItemsSerDe serDe_;
  private ItemsSketch sketch;

  ItemsState(final ArrayOfItemsSerDe serDe) {
    serDe_ = serDe;
  }

  // initializing maxMapSize is needed for building sketches using update(value)
  // not needed for merging sketches using update(sketch)
  void init(final int maxMapSize) {
    maxMapSize_ = maxMapSize;
    sketch = new ItemsSketch(maxMapSize_);
  }

  boolean isInitialized() {
    return sketch != null;
  }

  void update(final T value) {
    sketch.update(value);
  }

  void update(final byte[] serializedSketch) {
    final ItemsSketch incomingSketch = ItemsSketch.getInstance(Memory.wrap(serializedSketch), serDe_);
    if (sketch == null) {
      sketch = incomingSketch;
    } else {
      sketch.merge(incomingSketch);
    }
  }

  public ItemsSketch getResult() {
    return sketch;
  }

  void reset() {
    sketch = null;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy