/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *  
 *    Licensed 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 org.bboxdb.storage.wal;

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.bboxdb.storage.StorageManagerException;
import org.bboxdb.storage.entity.Tuple;
import org.bboxdb.storage.sstable.SSTableConst;
import org.bboxdb.storage.util.TupleHelper;

public class WriteAheadLogWriter implements Closeable {
	
	/**
	 * The file writer
	 */
	private BufferedOutputStream os;
	
	/**
	 * The file
	 */
	private final File file;

	public WriteAheadLogWriter(final File basedir, final long memtableNumber) throws IOException {
		
		this.file = WriteAheadLogManager.getFileForWal(basedir, memtableNumber);
		
		if(file.exists()) {
			throw new RuntimeException("File " + file + " does already exist");
		}
		
		this.os = new BufferedOutputStream(new FileOutputStream(file));
		
		os.write(SSTableConst.MAGIC_BYTES_WAL);
	}
	
	/**
	 * Add a tuple to the WAL
	 * @param tuple
	 * @throws IOException
	 */
	public void addTuple(final Tuple tuple) throws StorageManagerException {
		try {
			assert (os != null) : "Writer can not be null";		
			TupleHelper.writeTupleToStream(tuple, os);
			os.flush();
		} catch (IOException e) {
			throw new StorageManagerException(e);
		}
	}

	/**
	 * Close the WAL writer
	 */
	@Override
	public void close() throws IOException {
		if(os != null) {
			os.close();
			os = null;
		}
	}	
	
	/**
	 * Get the written file
	 * @return
	 */
	public File getFile() {
		return file;
	}
	
	/**
	 * Delete the base file
	 * @throws IOException
	 */
	public void deleteFile() throws IOException {
		close();
		
		if(file.exists()) {
			file.delete();
		}
	}
}

@@ -124,27 +124,25 @@
 B project%0A *
-  
 %0A
+
  *    Licens
@@ -318,18 +318,16 @@
 se at%0A *
-  
 %0A *     
@@ -372,18 +372,16 @@
 E-2.0%0A *
-  
 %0A *    U
@@ -712,16 +712,11 @@
 nse.
- 
 %0A *
-    
 %0A **
@@ -1213,18 +1213,17 @@
 eable %7B%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -1278,18 +1278,17 @@
 eam os;%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -1425,19 +1425,17 @@
 ption %7B%0A
-%09%09
 %0A
+
 %09%09this.f
@@ -1501,19 +1501,17 @@
 umber);%0A
-%09%09
 %0A
+
 %09%09if(fil
@@ -1599,19 +1599,17 @@
 %22);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09this.o
@@ -1666,19 +1666,17 @@
 file));%0A
-%09%09
 %0A
+
 %09%09os.wri
@@ -1708,26 +1708,25 @@
 ES_WAL);%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Add
@@ -1923,18 +1923,16 @@
 e null%22;
-%09%09
 %0A%09%09%09Tupl
@@ -2204,19 +2204,17 @@
 ;%0A%09%09%7D%0A%09%7D
-%09%0A%09
+%0A
 %0A%09/**%0A%09 
@@ -2296,18 +2296,17 @@
 ile;%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -2418,11 +2418,9 @@
 ();%0A
-%09%09
 %0A
+
 %09%09if
@@ -2446,20 +2446,135 @@
 %09%09fi
-le.delete();
+nal boolean deleteResult = file.delete();%0A%0A%09%09%09if(! deleteResult) %7B%0A%09%09%09%09throw new IOException(%22Unable to delete: %22 + file);%0A%09%09%09%7D
 %0A%09%09%7D

Check delete result