/*
 * 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.master.lineage.checkpoint;

import java.util.ArrayDeque;
import java.util.List;

import com.google.common.collect.Lists;

import tachyon.master.lineage.meta.Lineage;
import tachyon.master.lineage.meta.LineageStoreView;

/**
 * This class tries to checkpoint all the latest lineage, i.e. all the leaf nodes in the lineage
 * DAG.This class serves as an example to implement an Evictor.
 */
public final class CheckpointLatestScheduler implements CheckpointScheduler {

  @Override
  public CheckpointPlan schedule(LineageStoreView store) {
    List<Lineage> toCheckpoint = Lists.newArrayList();

    ArrayDeque<Lineage> deque = new ArrayDeque<Lineage>(store.getRootLineage());
    while (!deque.isEmpty()) {
      Lineage lineage = deque.pollFirst();
      List<Lineage> children = store.getChildren(lineage);
      if (children.isEmpty()) {
        toCheckpoint.add(lineage);
      } else {
        for (Lineage child : children) {
          deque.add(child);
        }
      }
    }
    return new CheckpointPlan(toCheckpoint);
  }

}

@@ -968,16 +968,65 @@
 ineage;%0A
+import tachyon.master.lineage.meta.LineageState;%0A
 import t
@@ -1070,16 +1070,16 @@
 reView;%0A
-
 %0A/**%0A * 
@@ -1670,30 +1670,528 @@
     
-if (children.isEmpty()
+// TODO(yupeng): rewrite this in switch statement%0A      if (lineage.getState() == LineageState.ADDED%0A          %7C%7C lineage.getState() == LineageState.IN_RECORD) %7B%0A        // the lineage and its descendants are not ready for checkpoint%0A        // TODO(yupeng): what about LOST state?%0A        continue;%0A      %7D else if (lineage.getState() == LineageState.IN_CHECKPOINT) %7B%0A        // the lineage is being recorded, do thing%0A        continue;%0A      %7D else if (children.isEmpty() && lineage.getState() == LineageState.RECORDED
 ) %7B%0A
@@ -2382,11 +2382,10 @@
 t);%0A
+
   %7D%0A
-%0A
 %7D%0A

Fix checkpoint scheduler