/*
 * 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);
  }

}

/*
 * 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.LineageState;
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);
      // TODO(yupeng): rewrite this in switch statement
      if (lineage.getState() == LineageState.ADDED
          || lineage.getState() == LineageState.IN_RECORD) {
        // the lineage and its descendants are not ready for checkpoint
        // TODO(yupeng): what about LOST state?
        continue;
      } else if (lineage.getState() == LineageState.IN_CHECKPOINT) {
        // the lineage is being recorded, do thing
        continue;
      } else if (children.isEmpty() && lineage.getState() == LineageState.RECORDED) {
        toCheckpoint.add(lineage);
      } else {
        for (Lineage child : children) {
          deque.add(child);
        }
      }
    }
    return new CheckpointPlan(toCheckpoint);
  }
}

Fix checkpoint scheduler