/**
 * Copyright 2022 LinkedIn Corp. All rights reserved.
 *
 * 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.
 */
package com.github.ambry.compression;

import com.github.ambry.utils.Utils;
import io.netty.buffer.ByteBuf;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Objects;


/**
 * CompressionMap is a compression algorithm registration keyed by the algorithm name to compression instance.
 * <p>
 * In additional to HashMap function, CompressionMap APIs include:
 * - Add a new compression instance.
 * - Get a compression instance by compressed buffer generated by Compression.compress().
 */
public class CompressionMap extends HashMap<String, Compression> {

  /**
   * Create a set of compression instances using the specified instances.
   * @param compressionInstances List of compression instances.
   * @return an instance of compression set.
   */
  public static CompressionMap of(Compression... compressionInstances) {
    CompressionMap newSet = new CompressionMap();
    for (Compression instance : compressionInstances) {
      newSet.add(instance);
    }
    return newSet;
  }

  /**
   * Add an instance of compression.  Every compression instance has a unique name.
   * Decompression will use the compression algorithm name to find the decompression instance.
   *
   * @param compressionInstance The compression instance to add.
   */
  public void add(Compression compressionInstance) {
    Objects.requireNonNull(compressionInstance, "compressionInstance");
    put(compressionInstance.getAlgorithmName(), compressionInstance);
  }

  /**
   * Find the name of the algorithm used to compress the specified compressed buffer.
   * This method does not alter the index in compressedBuffer.
   *
   * @param compressedBuffer The compressed buffer generated by the compress() method.
   * @return Name of the compression algorithm.
   */
  public String getAlgorithmName(ByteBuf compressedBuffer) {
    if (compressedBuffer == null || compressedBuffer.readableBytes() == 0) {
      throw new IllegalArgumentException("compressedBuffer cannot be null or empty.");
    }

    // The first byte is the version. Skip the version and read the algorithm name length.
    compressedBuffer.markReaderIndex();
    try {
      compressedBuffer.readByte();  // Skip the version.
      byte algorithmNameLength = compressedBuffer.readByte();

      // Read the algorithm name binary.
      byte[] algorithmNameBinary = new byte[algorithmNameLength];
      compressedBuffer.readBytes(algorithmNameBinary);

      // Convert algorithm name binary to string.
      return new String(algorithmNameBinary, StandardCharsets.UTF_8);
    } finally {
      compressedBuffer.resetReaderIndex();
    }
  }

  /**
   * Get the registered compression algorithm by name.
   * @param algorithmName Name of the compression algorithm.
   * @return Compression algorithm if found; null if not found.
   */
  public Compression getByName(String algorithmName) {
    Utils.checkNotNullOrEmpty(algorithmName, "algorithmName cannot be null or empty.");
    return get(algorithmName);
  }
}
