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

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tachyon.Constants;
import tachyon.TachyonURI;
import tachyon.Version;
import tachyon.client.file.FileInStream;
import tachyon.client.file.TachyonFile;
import tachyon.client.file.TachyonFileSystem;
import tachyon.client.file.TachyonFileSystem.TachyonFileSystemFactory;
import tachyon.exception.TachyonException;
import tachyon.thrift.FileInfo;

/**
 * An example to show to how use Tachyon's API
 */
public class BasicCheckpoint implements Callable<Boolean> {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

  private final TachyonURI mLocation;
  private final String mFileFolder;
  private final int mNumFiles;

  public BasicCheckpoint(TachyonURI tachyonURI, String fileFolder, int numFiles) {
    mLocation = tachyonURI;
    mFileFolder = fileFolder;
    mNumFiles = numFiles;
  }

  @Override
  public Boolean call() throws Exception {
    TachyonFileSystem tachyonClient = TachyonFileSystemFactory.get();
    writeFile(tachyonClient);
    return readFile(tachyonClient);
  }

  private boolean readFile(TachyonFileSystem tachyonClient) throws IOException, TachyonException {
    boolean pass = true;
    for (int i = 0; i < mNumFiles; i ++) {
      TachyonURI filePath = new TachyonURI(mFileFolder + "/part-" + i);
      LOG.debug("Reading data from {}", filePath);
      TachyonFile file = tachyonClient.open(filePath);
      FileInStream is = tachyonClient.getInStream(file);
      FileInfo info = tachyonClient.getInfo(file);
      ByteBuffer buf = ByteBuffer.allocate((int) info.getBlockSizeBytes());
      is.read(buf.array());
      buf.order(ByteOrder.nativeOrder());
      for (int k = 0; k < mNumFiles; k ++) {
        pass = pass && (buf.getInt() == k);
      }
      is.close();
    }
    return pass;
  }

  private void writeFile(TachyonFileSystem tachyonClient) throws IOException, TachyonException {
    for (int i = 0; i < mNumFiles; i ++) {
      ByteBuffer buf = ByteBuffer.allocate(80);
      buf.order(ByteOrder.nativeOrder());
      for (int k = 0; k < mNumFiles; k ++) {
        buf.putInt(k);
      }
      buf.flip();
      TachyonURI filePath = new TachyonURI(mFileFolder + "/part-" + i);
      LOG.debug("Writing data to {}", filePath);
      OutputStream os = tachyonClient.getOutStream(filePath);
      os.write(buf.array());
      os.close();
    }
  }

  public static void main(String[] args) throws IOException {
    if (args.length != 3) {
      System.out.println("java -cp " + Version.TACHYON_JAR
          + " tachyon.examples.BasicCheckpoint <TachyonMasterAddress> <FileFolder> <Files>");
      System.exit(-1);
    }

    Utils.runExample(new BasicCheckpoint(new TachyonURI(args[0]), args[1], Integer
        .parseInt(args[2])));
  }
}

@@ -1577,46 +1577,8 @@
 );%0A%0A
-  private final TachyonURI mLocation;%0A
   pr
@@ -1670,31 +1670,8 @@
 int(
-TachyonURI tachyonURI, 
 Stri
@@ -1705,36 +1705,8 @@
 ) %7B%0A
-    mLocation = tachyonURI;%0A
     
@@ -3354,9 +3354,9 @@
  != 
-3
+2
 ) %7B%0A
@@ -3464,31 +3464,8 @@
 oint
- %3CTachyonMasterAddress%3E
  %3CFi
@@ -3560,39 +3560,14 @@
 int(
-new TachyonURI(args%5B0%5D), args%5B1
+args%5B0
 %5D, I
@@ -3596,17 +3596,17 @@
 nt(args%5B
-2
+1
 %5D)));%0A  

Remove the unused variable mLocation in BasicCheckpoint.java