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

org.apache.druid.timeline.partition.PartitionHolder Maven / Gradle / Ivy

There is a newer version: 31.0.0
Show 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.druid.timeline.partition;

import com.google.common.collect.Iterables;
import org.apache.druid.timeline.Overshadowable;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * An object that clumps together multiple other objects which each represent a shard of some space.
 */
public class PartitionHolder> implements Iterable>
{
  private final OvershadowableManager overshadowableManager;

  private short maxMinorVersion;

  public static > PartitionHolder copyWithOnlyVisibleChunks(
      PartitionHolder partitionHolder
  )
  {
    return new PartitionHolder<>(
        OvershadowableManager.copyVisible(partitionHolder.overshadowableManager),
        partitionHolder.maxMinorVersion
    );
  }

  public static > PartitionHolder deepCopy(PartitionHolder partitionHolder)
  {
    return new PartitionHolder<>(
        OvershadowableManager.deepCopy(partitionHolder.overshadowableManager),
        partitionHolder.maxMinorVersion
    );
  }

  public PartitionHolder(PartitionChunk initialChunk)
  {
    this.overshadowableManager = new OvershadowableManager<>();
    add(initialChunk);
  }

  public PartitionHolder(List> initialChunks)
  {
    this.overshadowableManager = new OvershadowableManager<>();
    for (PartitionChunk chunk : initialChunks) {
      add(chunk);
    }
  }

  protected PartitionHolder(OvershadowableManager overshadowableManager, short maxMinorVersion)
  {
    this.overshadowableManager = overshadowableManager;
    this.maxMinorVersion = maxMinorVersion;
  }

  public boolean add(PartitionChunk chunk)
  {
    boolean added = overshadowableManager.addChunk(chunk);
    if (added && chunk.getObject().getMinorVersion() > maxMinorVersion) {
      maxMinorVersion = chunk.getObject().getMinorVersion();
    }
    return added;
  }

  /**
   * Returns the maximum minor version across all the added segments.
   * We do not handle updates of this variable when segments are removed for the sake of simplicity.
   */
  public short getMaxMinorVersion()
  {
    return maxMinorVersion;
  }

  @Nullable
  public PartitionChunk remove(PartitionChunk chunk)
  {
    return overshadowableManager.removeChunk(chunk);
  }

  public boolean isEmpty()
  {
    return overshadowableManager.isEmpty();
  }

  public boolean isComplete()
  {
    if (overshadowableManager.isEmpty()) {
      return false;
    }

    Iterator> iter = iterator();

    PartitionChunk curr = iter.next();

    if (!curr.isStart()) {
      return false;
    }

    if (curr.isEnd()) {
      return overshadowableManager.isComplete();
    }

    while (iter.hasNext()) {
      PartitionChunk next = iter.next();
      if (!curr.abuts(next)) {
        return false;
      }

      if (next.isEnd()) {
        return overshadowableManager.isComplete();
      }
      curr = next;
    }

    return false;
  }

  public PartitionChunk getChunk(final int partitionNum)
  {
    return overshadowableManager.getChunk(partitionNum);
  }

  @Override
  public Iterator> iterator()
  {
    return overshadowableManager.visibleChunksIterator();
  }

  public List> getOvershadowed()
  {
    return overshadowableManager.getOvershadowedChunks();
  }

  public Iterable payloads()
  {
    return Iterables.transform(this, PartitionChunk::getObject);
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    PartitionHolder that = (PartitionHolder) o;
    return Objects.equals(overshadowableManager, that.overshadowableManager);
  }

  @Override
  public int hashCode()
  {
    return Objects.hash(overshadowableManager);
  }

  @Override
  public String toString()
  {
    return "PartitionHolder{" +
           "overshadowableManager=" + overshadowableManager +
           '}';
  }

  public boolean hasData()
  {
    // it has data as long as one of the payloads has data, otherwise it does not
    for (T payload : payloads()) {
      if (payload.hasData()) {
        return true;
      }
    }
    return false;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy