<commit_before>/*
 * 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.worker.block;

import java.io.IOException;
import java.util.List;

import org.apache.thrift.TException;

import tachyon.Users;
import tachyon.thrift.BlockInfoException;
import tachyon.thrift.FailedToCheckpointException;
import tachyon.thrift.FileAlreadyExistException;
import tachyon.thrift.FileDoesNotExistException;
import tachyon.thrift.OutOfSpaceException;
import tachyon.thrift.SuspectedFileSizeException;
import tachyon.thrift.TachyonException;
import tachyon.thrift.WorkerService;

/**
 * Handles all thrift RPC calls to the worker. This class is a thrift server implementation and is
 * thread safe.
 */
public class BlockServiceHandler implements WorkerService.Iface {

  private final BlockDataManager mWorker;

  public BlockServiceHandler(BlockDataManager worker) {
    mWorker = worker;
  }

  /**
   * Used when a client wishes to abort a temporary block it is managing.
   *
   * @param userId The user id of the client
   * @param blockId The id of the block to be aborted
   * @return true if successful, false otherwise
   * @throws TException if the block does not exist or is committed
   */
  public boolean abortBlock(long userId, long blockId) throws TException {
    try {
      return mWorker.abortBlock(userId, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to create a new block on this worker. This is only used for local clients.
   *
   * @param userId The id of the client
   * @param blockId The id of the block
   * @param location The tier to place the block in, 0 for any tier
   * @param initialBytes The amount of space to request for the block initially
   * @return Path to the local file, or null if it failed
   * @throws OutOfSpaceException if there is not enough space in location to create the block
   * @throws FileAlreadyExistException if the block already exists
   */
  public String createBlock(long userId, long blockId, int location, long initialBytes)
      throws TException {
    try {
      return mWorker.createBlock(userId, blockId, location, initialBytes);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to close a block. Calling this method will move the block from the user temporary folder
   * to the worker's data folder.
   *
   * @param userId The id of the client
   * @param blockId The block id to complete
   * @throws TException if the block fails to be completed
   */
  public void completeBlock(long userId, long blockId) throws TException {
    try {
      mWorker.commitBlock(userId, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to remove a block from the Tachyon storage managed by this worker.
   *
   * @param blockId The id of the block
   * @return true if the block is freed successfully, false otherwise
   * @throws TException if the block does not exist
   */
  public boolean freeBlock(long blockId) throws TException {
    try {
      return mWorker.removeBlock(Users.MIGRATE_DATA_USER_ID, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to get a completed block for reading. This method should only be used if the block is in
   * Tachyon managed space on this worker.
   *
   * @param userId The id of the client
   * @param blockId The id of the block to read
   * @param lockId The lock id of the lock acquired on the block
   * @return the path of the block on local disk
   * @throws TException if the block does not exist
   */
  public String getBlock(long userId, long blockId, int lockId) throws TException {
    return mWorker.readBlock(userId, blockId, lockId);
  }

  // TODO: Rename this method when complete, currently is V2 to avoid checkstyle errors
  /**
   * Obtains a read lock on the block.
   *
   * @param userId The id of the client
   * @param blockId The id of the block to lock
   * @return the lockId of the lock obtained
   */
  public long lockBlockV2(long userId, long blockId) {
    return mWorker.lockBlock(userId, blockId);
  }

  // TODO: Rename this method when complete, currently is V2 to avoid checkstyle errors
  /**
   * Relinquishes the lock on the block.
   *
   * @param lockId The id of the lock to relinquish<commit_msg>Make new interface in block service handler match block data manager's.<commit_after>/*
 * 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.worker.block;

import java.io.IOException;
import java.util.List;

import org.apache.thrift.TException;

import tachyon.Users;
import tachyon.thrift.BlockInfoException;
import tachyon.thrift.FailedToCheckpointException;
import tachyon.thrift.FileAlreadyExistException;
import tachyon.thrift.FileDoesNotExistException;
import tachyon.thrift.OutOfSpaceException;
import tachyon.thrift.SuspectedFileSizeException;
import tachyon.thrift.TachyonException;
import tachyon.thrift.WorkerService;

/**
 * Handles all thrift RPC calls to the worker. This class is a thrift server implementation and is
 * thread safe.
 */
public class BlockServiceHandler implements WorkerService.Iface {

  private final BlockDataManager mWorker;

  public BlockServiceHandler(BlockDataManager worker) {
    mWorker = worker;
  }

  /**
   * Used when a client wishes to abort a temporary block it is managing.
   *
   * @param userId The user id of the client
   * @param blockId The id of the block to be aborted
   * @return true if successful, false otherwise
   * @throws TException if the block does not exist or is committed
   */
  public boolean abortBlock(long userId, long blockId) throws TException {
    try {
      return mWorker.abortBlock(userId, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to close a block. Calling this method will move the block from the user temporary folder
   * to the worker's data folder.
   *
   * @param userId The id of the client
   * @param blockId The block id to complete
   * @throws TException if the block fails to be completed
   */
  public void commitBlock(long userId, long blockId) throws TException {
    try {
      mWorker.commitBlock(userId, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  /**
   * Used to create a new block on this worker. This is only used for local clients.
   *
   * @param userId The id of the client
   * @param blockId The id of the block
   * @param location The tier to place the block in, 0 for any tier
   * @param initialBytes The amount of space to request for the block initially
   * @return Path to the local file, or null if it failed
   * @throws OutOfSpaceException if there is not enough space in location to create the block
   * @throws FileAlreadyExistException if the block already exists
   */
  public String createBlock(long userId, long blockId, int location, long initialBytes)
      throws TException {
    try {
      return mWorker.createBlock(userId, blockId, location, initialBytes);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  // TODO: Rename this method when complete, currently is V2 to avoid checkstyle errors
  /**
   * Obtains a read lock on the block.
   *
   * @param userId The id of the client
   * @param blockId The id of the block to lock
   * @return the lockId of the lock obtained
   */
  public long lockBlockV2(long userId, long blockId) {
    return mWorker.lockBlock(userId, blockId);
  }

  /**
   * Used to get a completed block for reading. This method should only be used if the block is in
   * Tachyon managed space on this worker.
   *
   * @param userId The id of the client
   * @param blockId The id of the block to read
   * @param lockId The lock id of the lock acquired on the block
   * @return the path of the block on local disk
   * @throws TException if the block does not exist
   */
  public String readBlock(long userId, long blockId, int lockId) throws TException {
    return mWorker.readBlock(userId, blockId, lockId);
  }

  /**
   * Used to remove a block from the Tachyon storage managed by this worker.
   *
   * @param blockId The id of the block
   * @return true if the block is freed successfully, false otherwise
   * @throws TException if the block does not exist
   */
  public boolean removeBlock(long blockId) throws TException {
    try {
      return mWorker.removeBlock(Users.MIGRATE_DATA_USER_ID, blockId);
    } catch (IOException ioe) {
      throw new TException(ioe);
    }
  }

  // TODO: Rename this method when complete, currently is V2 to avoid checkstyle errors
  /**
   * Relinquishes the lock on the block.
   *
   * @param lockId The id of the lock to relinquish
{'fraction_non_alphanumeric': 0.06448461162677088, 'fraction_numerical': 0.0015632633121641427, 'mean_word_length': 3.4235090751944686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6720925', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2805, 'n_words': 1267}