package tachyon.examples;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import tachyon.Version;
import tachyon.client.ReadType;
import tachyon.client.TachyonFS;
import tachyon.client.TachyonFile;
import tachyon.client.WriteType;

/**
 * Basic example of using the TachyonFS and TachyonFile for writing to and reading from files.
 * <p />
 * This class is different from {@link tachyon.examples.BasicOperations} in the way writes happen.
 * Over there {@link java.nio.ByteBuffer} is used directly, where as here byte data is done
 * via input/output streams.
 * <p />
 * This example also let users play around with how to work with files a bit more.  The
 * {@link tachyon.client.ReadType} is something that can be set, as well as ability to delete
 * file if exists.
 */
public final class BasicNonByteBufferOperations {
  private static final boolean PASS = true;

  private static void usage() {
    System.out.println("java -cp target/tachyon-" + Version.VERSION
        + "-jar-with-dependencies.jar "
        + BasicNonByteBufferOperations.class.getName()
        + " <master address> <file path> [write type] [read type] [delete file] [num writes]"
    );
    System.exit(-1);
  }

  public static void main(final String[] args) throws IOException {
    if (args.length < 2 || args.length > 6) {
      usage();
    }

    String address = args[0];
    String filePath = args[1];
    WriteType writeType = Utils.option(args, 2, WriteType.MUST_CACHE);
    ReadType readType = Utils.option(args, 3, ReadType.NO_CACHE);
    boolean deleteIfExists = Utils.option(args, 4, false);
    int length = Utils.option(args, 5, 20);

    TachyonFS client = TachyonFS.get(address);

    write(client, filePath, writeType, deleteIfExists, length);
    read(client, filePath, readType);

    Utils.printPassInfo(PASS);
    System.exit(0);
  }

  private static void write(TachyonFS client, String filePath, WriteType writeType,
      boolean deleteIfExists, int length) throws IOException {
    // If the file exists already, we will override it.
    TachyonFile file = getOrCreate(client, filePath, deleteIfExists);
    DataOutputStream os = new DataOutputStream(file.getOutStream(writeType));
    try {
      os.writeInt(length);
      for (int i = 0; i < length; i ++) {
        os.writeInt(i);
      }
    } finally {
      os.close();
    }
  }

  private static TachyonFile getOrCreate(TachyonFS client, String filePath, boolean deleteIfExists)
      throws IOException {
    TachyonFile file = client.getFile(filePath);
    if (file == null) {
      // file doesn't exist yet, so create it
      int fileId = client.createFile(filePath);
      file = client.getFile(fileId);
    } else if (deleteIfExists) {
      // file exists, so delete it and recreate
      client.delete(file.getPath(), false);

      int fileId = client.createFile(filePath);
      file = client.getFile(fileId);
    }
    return file;
  }

  private static void read(TachyonFS client, String filePath, ReadType readType)
      throws IOException {
    TachyonFile file = client.getFile(filePath);
    DataInputStream input = new DataInputStream(file.getInStream(readType));
    try {
      int length = input.readInt();
      for (int i = 0; i < length; i ++) {
        System.out.println(input.readInt());
      }
    } finally {
      input.close();
    }
  }
}

@@ -868,53 +868,8 @@
 s %7B%0A
-  private static final boolean PASS = true;%0A%0A
   pr
@@ -1571,20 +1571,19 @@
 rgs, 4, 
-fals
+tru
 e);%0A    
@@ -1738,16 +1738,33 @@
 th);%0A   
+ boolean passes =
  read(cl
@@ -1819,12 +1819,14 @@
 nfo(
-PASS
+passes
 );%0A 
@@ -2943,20 +2943,23 @@
  static 
-void
+boolean
  read(Ta
@@ -3155,32 +3155,59 @@
 eam(readType));%0A
+    boolean passes = true;%0A
     try %7B%0A      
@@ -3286,34 +3286,26 @@
         
-System.out.println
+passes &= 
 (input.r
@@ -3312,16 +3312,21 @@
 eadInt()
+ == i
 );%0A     
@@ -3367,18 +3367,37 @@
 se();%0A    %7D%0A
+    return passes;%0A
   %7D%0A%7D%0A

validate data in example