<commit_before>package com.github.ambry.commons;

import com.github.ambry.clustermap.ClusterMap;
import com.github.ambry.clustermap.PartitionId;
import com.github.ambry.store.StoreKey;
import com.github.ambry.utils.ByteBufferInputStream;
import com.github.ambry.utils.Utils;

import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;


/**
 * BlobId uniquely identifies a stored blob as well as the Partition in which the blob is stored.
 */
public class BlobId extends StoreKey {
  private Short version = 1;
  private static short Version_Size_In_Bytes = 2;
  private PartitionId partitionId;
  private String uuid;
  private static int UUID_Size_In_Bytes = 4;

  /**
   * Constructs a new unique BlobId for the specified partition.
   *
   * @param partitionId of Partition in which blob is to be stored.
   */
  public BlobId(PartitionId partitionId) {
    this.partitionId = partitionId;
    this.uuid = UUID.randomUUID().toString();
  }

  /**
   * Re-constructs existing blobId by deserializing from BlobId "string"
   *
   * @param id of Blob as output by BlobId.getID()
   * @param clusterMap of the cluster that the blob id belongs to
   * @throws IOException
   */
  public BlobId(String id, ClusterMap clusterMap)
      throws IOException {
    this(new DataInputStream(new ByteBufferInputStream(ByteBuffer.wrap(Base64.decodeBase64(id)))), clusterMap);
  }

  /**
   * Re-constructs existing blobId by deserializing from data input stream
   *
   * @param stream from which to deserialize the blobid
   * @param clusterMap of the cluster that the blob id belongs to
   * @throws IOException
   */
  public BlobId(DataInputStream stream, ClusterMap clusterMap)
      throws IOException {
    this.version = stream.readShort();
    if (version == 1) {
      this.partitionId = clusterMap.getPartitionIdFromStream(stream);
      uuid = Utils.readIntString(stream);
    } else {
      throw new IllegalArgumentException("version " + version + " not supported for blob id");
    }
  }

  public short sizeInBytes() {
    return (short) (Version_Size_In_Bytes + partitionId.getBytes().length + UUID_Size_In_Bytes + uuid.length());
  }

  public PartitionId getPartition() {
    return partitionId;
  }

  @Override
  public byte[] toBytes() {
    ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes());
    idBuf.putShort(version);
    idBuf.put(partitionId.getBytes());
    idBuf.putInt(uuid.getBytes().length);
    idBuf.put(uuid.getBytes());
    return idBuf.array();
  }

  @Override
  public String getID() {
    return Base64.encodeBase64URLSafeString(toBytes());
  }

  @Override
  public String getLongForm() {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    sb.append(getID());
    sb.append(":");
    sb.append(version);
    sb.append(":");
    sb.append(partitionId);
    sb.append(":");
    sb.append(uuid);
    sb.append("]");
    return sb.toString();<commit_msg>Minor cleanup<commit_after>package com.github.ambry.commons;

import com.github.ambry.clustermap.ClusterMap;
import com.github.ambry.clustermap.PartitionId;
import com.github.ambry.store.StoreKey;
import com.github.ambry.utils.ByteBufferInputStream;
import com.github.ambry.utils.Utils;

import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;


/**
 * BlobId uniquely identifies a stored blob as well as the Partition in which the blob is stored.
 */
public class BlobId extends StoreKey {
  private Short version = 1;
  private static short Version_Size_In_Bytes = 2;
  private PartitionId partitionId;
  private String uuid;
  private static int UUID_Size_In_Bytes = 4;

  /**
   * Constructs a new unique BlobId for the specified partition.
   *
   * @param partitionId of Partition in which blob is to be stored.
   */
  public BlobId(PartitionId partitionId) {
    this.partitionId = partitionId;
    this.uuid = UUID.randomUUID().toString();
  }

  /**
   * Re-constructs existing blobId by deserializing from BlobId "string"
   *
   * @param id of Blob as output by BlobId.getID()
   * @param clusterMap of the cluster that the blob id belongs to
   * @throws IOException
   */
  public BlobId(String id, ClusterMap clusterMap)
      throws IOException {
    this(new DataInputStream(new ByteBufferInputStream(ByteBuffer.wrap(Base64.decodeBase64(id)))), clusterMap);
  }

  /**
   * Re-constructs existing blobId by deserializing from data input stream
   *
   * @param stream from which to deserialize the blobid
   * @param clusterMap of the cluster that the blob id belongs to
   * @throws IOException
   */
  public BlobId(DataInputStream stream, ClusterMap clusterMap)
      throws IOException {
    this.version = stream.readShort();
    if (version == 1) {
      this.partitionId = clusterMap.getPartitionIdFromStream(stream);
      uuid = Utils.readIntString(stream);
    } else {
      throw new IllegalArgumentException("version " + version + " not supported for blob id");
    }
  }

  public short sizeInBytes() {
    return (short) (Version_Size_In_Bytes + partitionId.getBytes().length + UUID_Size_In_Bytes + uuid.length());
  }

  public PartitionId getPartition() {
    return partitionId;
  }

  @Override
  public byte[] toBytes() {
    ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes());
    idBuf.putShort(version);
    idBuf.put(partitionId.getBytes());
    idBuf.putInt(uuid.getBytes().length);
    idBuf.put(uuid.getBytes());
    return idBuf.array();
  }

  @Override
  public String getID() {
    return Base64.encodeBase64URLSafeString(toBytes());
  }

  @Override
  public String getLongForm() {
    StringBuilder sb = new StringBuilder();
    sb.append("[").append(getID());
    sb.append(":").append(version);
    sb.append(":").append(partitionId);
    sb.append(":").append(uuid).append("]");
    return sb.toString();
{'fraction_non_alphanumeric': 0.10915492957746478, 'fraction_numerical': 0.004694835680751174, 'mean_word_length': 3.7834803528468326, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4626560', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1794, 'n_words': 473}