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

import tachyon.worker.BlockStoreLocation;
import tachyon.worker.block.BlockAccessEventListener;
import tachyon.worker.block.BlockMetadataManager;

/**
 * A simple evictor that evicts random blocks until the required size in the target tier is met.
 */
public class NaiveEvictor implements Evictor, BlockAccessEventListener {
  private final BlockMetadataManager mMetadata;

  public NaiveEvictor(BlockMetadataManager metadata) {
    mMetadata = metadata;
  }

  @Override
  public EvictionPlan freeSpace(long bytes, BlockStoreLocation location) {
    return new EvictionPlan(null, null);
  }

  @Override
  public void onAccessBlock(long userId, long blockId) {}
}

@@ -825,16 +825,135 @@
 ictor;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport com.google.common.base.Preconditions;%0A%0Aimport tachyon.Pair;%0A
 import t
@@ -1089,16 +1089,151 @@
 Manager;
+%0Aimport tachyon.worker.block.meta.BlockMeta;%0Aimport tachyon.worker.block.meta.StorageDir;%0Aimport tachyon.worker.block.meta.StorageTier;
 %0A%0A/**%0A *
@@ -1446,20 +1446,23 @@
 er mMeta
-data
+Manager
 ;%0A%0A  pub
@@ -1524,23 +1524,53 @@
 Meta
-data = 
+Manager = Preconditions.checkNotNull(
 metadata
 ;%0A  
@@ -1565,16 +1565,17 @@
 metadata
+)
 ;%0A  %7D%0A%0A 
@@ -1656,24 +1656,1569 @@
 location) %7B%0A
+    List%3CPair%3CLong, BlockStoreLocation%3E%3E toMove = new ArrayList%3CPair%3CLong, BlockStoreLocation%3E%3E();%0A    List%3CLong%3E toEvict = new ArrayList%3CLong%3E();%0A%0A    long sizeFreed = 0;%0A    if (location == BlockStoreLocation.anyTier()) %7B%0A      for (StorageTier tier : mMetaManager.getTiers()) %7B%0A        for (StorageDir dir : tier.getStorageDirs()) %7B%0A          for (BlockMeta block : dir.getBlocks()) %7B%0A            toEvict.add(block.getBlockId());%0A            sizeFreed += block.getBlockSize();%0A            if (sizeFreed %3E= bytes) %7B%0A              return new EvictionPlan(toMove, toEvict);%0A            %7D%0A          %7D%0A        %7D%0A      %7D%0A      return new EvictionPlan(toMove, toEvict);%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        for (BlockMeta block : dir.getBlocks()) %7B%0A          toEvict.add(block.getBlockId());%0A          sizeFreed += block.getBlockSize();%0A          if (sizeFreed %3E= bytes) %7B%0A            return new EvictionPlan(toMove, toEvict);%0A          %7D%0A        %7D%0A      %7D%0A      return new EvictionPlan(toMove, toEvict);%0A    %7D%0A%0A    int dirIndex = location.dir();%0A    StorageDir dir = tier.getDir(dirIndex);%0A    for (BlockMeta block : dir.getBlocks()) %7B%0A      toEvict.add(block.getBlockId());%0A      sizeFreed += block.getBlockSize();%0A      if (sizeFreed %3E= bytes) %7B%0A        return new EvictionPlan(toMove, toEvict);%0A      %7D%0A    %7D%0A
     return n
@@ -3319,12 +3319,34 @@
 ockId) %7B
-%7D
+%0A    // do nothing%0A  %7D%0A
 %0A%7D%0A

Update Naive Evictor