67 lines
1.4 KiB
Java
67 lines
1.4 KiB
Java
package org.warp.jcwdb;
|
|
|
|
import com.esotericsoftware.kryo.io.Output;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.channels.Channels;
|
|
import java.nio.channels.FileChannel;
|
|
import java.nio.channels.SeekableByteChannel;
|
|
import java.util.*;
|
|
|
|
public class FileAllocator {
|
|
private final SeekableByteChannel dataFileChannel;
|
|
private volatile long allocableOffset;
|
|
private volatile boolean closed;
|
|
private final Object closeLock = new Object();
|
|
|
|
public FileAllocator(SeekableByteChannel dataFileChannel) throws IOException {
|
|
this.dataFileChannel = dataFileChannel;
|
|
this.allocableOffset = this.dataFileChannel.size();
|
|
}
|
|
|
|
/**
|
|
* TODO: not implemented
|
|
* @param size
|
|
* @param type
|
|
* @return offset
|
|
*/
|
|
public long allocate(int size) {
|
|
checkClosed();
|
|
long allocatedOffset = allocableOffset;
|
|
allocatedOffset += size;
|
|
return allocatedOffset;
|
|
}
|
|
|
|
|
|
public void close() throws IOException {
|
|
if (closed) {
|
|
return;
|
|
}
|
|
synchronized (closeLock) {
|
|
if (closed) {
|
|
return;
|
|
}
|
|
closed = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Frees the unused bytes
|
|
* @param startPosition
|
|
* @param length
|
|
*/
|
|
public void markFree(long startPosition, int length) {
|
|
checkClosed();
|
|
// TODO: advanced feature, not implemented.
|
|
}
|
|
|
|
|
|
private void checkClosed() {
|
|
if (closed) {
|
|
throw new RuntimeException("Index Manager is closed.");
|
|
}
|
|
}
|
|
}
|