/*
 * Copyright (c) 2012-2013 by Jens V. Fischer, Zuse Institute Berlin
 *
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

package org.xtreemfs.sandbox.benchmark;

import org.xtreemfs.common.libxtreemfs.FileHandle;
import org.xtreemfs.common.libxtreemfs.Volume;
import org.xtreemfs.pbrpc.generatedinterfaces.GlobalTypes;

import java.io.IOException;

/**
 * Class implementing a random read benchmark.
 * 
 * @author jensvfischer
 */
public class RandomWriteBenchmark extends RandomOffsetbasedBenchmark {

    RandomWriteBenchmark(Volume volume, Params params) throws Exception {
        super(volume, params);
    }

    /* Called within the benchmark method. Performs the actual reading of data from the volume. */
    @Override
    long performIO(byte[] data, long numberOfBlocks) throws IOException {

        numberOfBlocks = convertTo4KiBBlocks(numberOfBlocks);
        long byteCounter = 0;

		int flags = GlobalTypes.SYSTEM_V_FCNTL.SYSTEM_V_FCNTL_H_O_CREAT.getNumber()
				| GlobalTypes.SYSTEM_V_FCNTL.SYSTEM_V_FCNTL_H_O_TRUNC.getNumber()
				| GlobalTypes.SYSTEM_V_FCNTL.SYSTEM_V_FCNTL_H_O_RDWR.getNumber();

        for (long j = 0; j < numberOfBlocks; j++) {
            FileHandle fileHandle = volume.openFile(params.userCredentials, BASFILE_FILENAME,
                    flags, 511);
            long nextOffset = generateNextRandomOffset();
            byteCounter += fileHandle.write(params.userCredentials, data, RANDOM_IO_BLOCKSIZE, nextOffset);
            fileHandle.close();
        }
        return byteCounter;
    }


}

@@ -1182,16 +1182,18 @@
 OfBlocks
+-1
 ; j++) %7B
@@ -1527,16 +1527,381 @@
        %7D
+%0A%0A%09%09/* last block written needs to be the last block of the basefile to not violate the basefile filesize */%0A%09%09FileHandle fileHandle = volume.openFile(params.userCredentials, BASFILE_FILENAME,%0A%09%09%09%09flags, 511);%0A%09%09byteCounter += fileHandle.write(params.userCredentials, data, RANDOM_IO_BLOCKSIZE, params.basefileSizeInBytes-RANDOM_IO_BLOCKSIZE);%0A%09%09fileHandle.close();
 %0A       

Add bugfix for rw bench to not violate basefile