package com.neverwinterdp.scribengin.storage.hdfs;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException;

import com.neverwinterdp.util.JSONSerializer;

public class SegmentLock {
  private FileSystem             fs;
  private Path                   lockPath;
  private SegmentOperationConfig operationConfig;
  transient private boolean      owner = false;
  
  public SegmentLock(FileSystem fs, Path lockPath, SegmentOperationConfig operationConfig) {
    this.fs = fs ;
    this.lockPath = lockPath;
    this.operationConfig = operationConfig;
  }

  public Path getLockPath() {  return lockPath; }

  public SegmentOperationConfig getOperationConfig() { return operationConfig; }

  public boolean tryLock(long timeout, long tryPeriod) throws IOException, InterruptedException {
    long stopTime = System.currentTimeMillis() + timeout;
    while(stopTime > System.currentTimeMillis()) {
      boolean locked = lock() ;
      if(locked) return true;
      Thread.sleep(tryPeriod);
    }
    return false; 
  }
  
  synchronized public void update(SegmentOperationConfig config) throws IOException {
    checkOwner();
    FSDataOutputStream out = fs.create(lockPath, true);
    byte[] bytes = JSONSerializer.INSTANCE.toBytes(config);
    out.write(bytes);
    out.hflush();
    out.close();
  }
  
  synchronized public boolean lock() throws IOException {
    if(owner) return true;
    try {
      if(fs.exists(lockPath)) return false ;
      FSDataOutputStream out = fs.create(lockPath, false);
      out.hsync();
      byte[] bytes = JSONSerializer.INSTANCE.toBytes(operationConfig);
      out.write(bytes);
      out.hflush();
      out.close();
      owner = true;
      return owner;
    } catch(AlreadyBeingCreatedException ex) {
      System.err.println("Lock Error: " + ex.getMessage());
      return false;
    } catch(IOException ex) {
      if(ex.getMessage().startsWith("File already exists")) {
        System.err.println("Lock Error: " + ex.getMessage());
        return false;
      }
      throw ex;
    }
  }
  
  synchronized public boolean unlock() throws IOException {
    checkOwner();
    boolean deleted = fs.delete(lockPath, false);
    owner = !deleted;
    //System.out.println("<== unlock, owner = " + owner + "\n");
    return deleted;
  }
  
  synchronized void discardDeathLock() throws IOException {
    if(fs.exists(lockPath)) {
    }
  }
  
  void checkOwner() throws IOException {
    if(!owner) {
      throw new IOException("Not the owner of the lock");
    }
  }
}
@@ -261,25 +261,24 @@
 dException;%0A
-%0A
 import com.n
@@ -2005,24 +2005,84 @@
 ption ex) %7B%0A
+      System.err.println(%22Lock Error: %22 + ex.getMessage());%0A
       if(ex.
@@ -2127,24 +2127,108 @@
 exists%22)) %7B%0A
+        return false;%0A      %7D else if(ex instanceof AlreadyBeingCreatedException) %7B%0A
         Syst
@@ -2245,32 +2245,45 @@
 ntln(%22Lock Error
+(IOException)
 : %22 + ex.getMess

debug lock fail