/*
 * Licensed to the University of California, Berkeley 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 tachyon.client.next.block;

import java.io.Closeable;
import java.io.IOException;

import tachyon.client.next.ClientOptions;
import tachyon.master.MasterClient;
import tachyon.thrift.BlockInfo;
import tachyon.thrift.FileBlockInfo;

/**
 * Tachyon Block Store client. This is an internal client for all block level operations in Tachyon.
 * An instance of this class can be obtained via {@link TachyonBS#get}. This class is thread safe.
 */
public class TachyonBS implements Closeable {

  public static TachyonBS sCachedClient = null;

  public static synchronized TachyonBS get() {
    if (sCachedClient != null) {
      return sCachedClient;
    }
    sCachedClient = new TachyonBS();
    return sCachedClient;
  }

  private final BSContext mContext;

  public TachyonBS() {
    mContext = BSContext.INSTANCE;
  }

  public void close() {

  }

  /**
   * Deletes a block from Tachyon space and under storage space.
   *
   * @param blockId the blockId of the block to delete
   */
  // TODO: Evaluate if this is necessary for now, or if file level delete is sufficient
  public void delete(long blockId) {
    MasterClient masterClient = mContext.acquireMasterClient();
    try {
      // TODO: Implement delete RPC
    } finally {
      mContext.releaseMasterClient(masterClient);
    }
  }

  /**
   * Frees a block from Tachyon space by removing it from all the workers which currently own a
   * copy of the block. This does not remove the block from under storage.
   *
   * @param blockId the blockId of the block to free
   */
  // TODO: Evaluate if this is necessary for now, or if file level free is sufficient
  public void free(long blockId) {
    // TODO: Implement free RPC
  }

  /**
   * Gets the block info of a block, if it exists.
   *
   * @param blockId the blockId to obtain information about
   * @return a FileBlockInfo containing the metadata of the block
   * @throws IOException if the block does not exist
   */
  public FileBlockInfo getInfo(long blockId) throws IOException {
    MasterClient masterClient = mContext.acquireMasterClient();
    try {
      return masterClient.user_getClientBlockInfo(blockId);
    } finally {
      mContext.releaseMasterClient(masterClient);
    }
  }

  /**
   * Gets a stream to read the data of a block. The stream can be backed by Tachyon storage or
   * the under storage system.
   *
   * @param blockId the block to read from
   * @param options the custom options when reading the block
   * @return a BlockInStream which can be used to read the data in a streaming fashion
   * @throws IOException if the block does not exist
   */
  public BlockInStream getInStream(long blockId, ClientOptions options) throws IOException {
    MasterClient masterClient = mContext.acquireMasterClient();
    try {
      // TODO: Fix this RPC
      BlockInfo blockInfo = masterClient.user_getClientBlockInfo(blockId);
      return new ClientBlockInStream(blockInfo, options);
    } finally {
      mContext.releaseMasterClient(masterClient);
    }
  }

  /**
   * Gets a stream to write data to a block. The stream can only be backed by Tachyon storage.
   *
   * @param blockId the block to write
   * @param options the custom options when writing the block
   * @return a BlockOutStream which can be used to write data to the block in a streaming fashion
   * @throws IOException if the block cannot be written
   */
  public BlockOutStream getOutStream(long blockId, ClientOptions options) throws IOException {
    // No specified location to read from
    if (null == options.getLocation()) {
      // Local client, attempt to do direct write to local storage
      if (mContext.hasLocalWorker()) {
        return new LocalBlockOutStream(blockId, options);
      }
      // Client is not local or the data is not available on the local worker, use remote stream
      return new RemoteBlockOutStream(blockId, options);
    }
    // TODO: Handle the case when a location is specified
    return null;
  }

  // TODO: Evaluate if this is necessary for now, or if file level promote is sufficient
  public boolean promote(long blockId) {
    // TODO: Implement me
    return false;
  }
}

@@ -1646,856 +1646,8 @@
  %7D%0A%0A
-  /**%0A   * Deletes a block from Tachyon space and under storage space.%0A   *%0A   * @param blockId the blockId of the block to delete%0A   */%0A  // TODO: Evaluate if this is necessary for now, or if file level delete is sufficient%0A  public void delete(long blockId) %7B%0A    MasterClient masterClient = mContext.acquireMasterClient();%0A    try %7B%0A      // TODO: Implement delete RPC%0A    %7D finally %7B%0A      mContext.releaseMasterClient(masterClient);%0A    %7D%0A  %7D%0A%0A  /**%0A   * Frees a block from Tachyon space by removing it from all the workers which currently own a%0A   * copy of the block. This does not remove the block from under storage.%0A   *%0A   * @param blockId the blockId of the block to free%0A   */%0A  // TODO: Evaluate if this is necessary for now, or if file level free is sufficient%0A  public void free(long blockId) %7B%0A    // TODO: Implement free RPC%0A  %7D%0A%0A
   /*
@@ -3918,186 +3918,7 @@
 %0A  %7D
-%0A%0A  // TODO: Evaluate if this is necessary for now, or if file level promote is sufficient%0A  public boolean promote(long blockId) %7B%0A    // TODO: Implement me%0A    return false;%0A  %7D
 %0A%7D%0A

Remove unnecessary methods in TachyonBS.