/*
 * 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.allocator;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

import tachyon.worker.BlockStoreLocation;
import tachyon.worker.block.BlockMetadataManager;
import tachyon.worker.block.meta.TempBlockMeta;

/**
 * Naive allocation strategy
 */
public class NaiveAllocator implements Allocator {
  private final BlockMetadataManager mMetadata;

  public NaiveAllocator(BlockMetadataManager metadata) {
    mMetadata = Preconditions.checkNotNull(metadata);
  }

  @Override
  public Optional<TempBlockMeta> allocateBlock(long userId, long blockId, long blockSize,
      BlockStoreLocation location) {
    // TODO: implement me
    return Optional.absent();
  }
}

@@ -1005,16 +1005,107 @@
 anager;%0A
+import tachyon.worker.block.meta.StorageDir;%0Aimport tachyon.worker.block.meta.StorageTier;%0A
 import t
@@ -1152,17 +1152,19 @@
 %0A/**%0A * 
-N
+A n
 aive all
@@ -1171,20 +1171,84 @@
 ocat
-ion strategy
+or that returns the first Storage dir fitting the size of block to allocate.
 %0A */
@@ -1341,20 +1341,23 @@
 er mMeta
-data
+Manager
 ;%0A%0A  pub
@@ -1417,20 +1417,23 @@
    mMeta
-data
+Manager
  = Preco
@@ -1617,29 +1617,1171 @@
     
-// TODO: implement me
+if (location == BlockStoreLocation.anyTier()) %7B%0A      // When any tier is ok, loop over all storage tier and dir, and return the first dir that has%0A      // sufficient available space.%0A      for (StorageTier tier : mMetaManager.getTiers()) %7B%0A        for (StorageDir dir : tier.getStorageDirs()) %7B%0A          if (dir.getAvailableBytes() %3E= blockSize) %7B%0A            return Optional.of(new TempBlockMeta(userId, blockId, blockSize, dir));%0A          %7D%0A        %7D%0A      %7D%0A      return Optional.absent();%0A    %7D%0A%0A    int tierAlias = location.tier();%0A    StorageTier tier = mMetaManager.getTier(tierAlias);%0A    if (location == BlockStoreLocation.anyDirInTier(tierAlias)) %7B%0A      // Loop over all dirs in the given tier%0A      for (StorageDir dir : tier.getStorageDirs()) %7B%0A        if (dir.getAvailableBytes() %3E= blockSize) %7B%0A          return Optional.of(new TempBlockMeta(userId, blockId, blockSize, dir));%0A        %7D%0A      %7D%0A      return Optional.absent();%0A    %7D%0A%0A    int dirIndex = location.dir();%0A    StorageDir dir = tier.getDir(dirIndex);%0A    if (dir.getAvailableBytes() %3E= blockSize) %7B%0A      return Optional.of(new TempBlockMeta(userId, blockId, blockSize, dir));%0A    %7D
 %0A   

Update NaiveAllocator