use of java.nio.channels.FileChannel in project netty by netty.
the class AbstractDiskHttpData method setContent.
@Override
public void setContent(ByteBuf buffer) throws IOException {
if (buffer == null) {
throw new NullPointerException("buffer");
}
try {
size = buffer.readableBytes();
checkSize(size);
if (definedSize > 0 && definedSize < size) {
throw new IOException("Out of size: " + size + " > " + definedSize);
}
if (file == null) {
file = tempFile();
}
if (buffer.readableBytes() == 0) {
// empty file
if (!file.createNewFile()) {
throw new IOException("file exists already: " + file);
}
return;
}
FileOutputStream outputStream = new FileOutputStream(file);
try {
FileChannel localfileChannel = outputStream.getChannel();
ByteBuffer byteBuffer = buffer.nioBuffer();
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
} finally {
outputStream.close();
}
setCompleted();
} finally {
// Release the buffer as it was retained before and we not need a reference to it at all
// See https://github.com/netty/netty/issues/1516
buffer.release();
}
}
use of java.nio.channels.FileChannel in project netty by netty.
the class AbstractDiskHttpData method readFrom.
/**
* Utility function
* @return the array of bytes
*/
private static byte[] readFrom(File src) throws IOException {
long srcsize = src.length();
if (srcsize > Integer.MAX_VALUE) {
throw new IllegalArgumentException("File too big to be loaded in memory");
}
FileInputStream inputStream = new FileInputStream(src);
byte[] array = new byte[(int) srcsize];
try {
FileChannel fileChannel = inputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
int read = 0;
while (read < srcsize) {
read += fileChannel.read(byteBuffer);
}
} finally {
inputStream.close();
}
return array;
}
use of java.nio.channels.FileChannel in project graphdb by neo4j-attic.
the class RelationshipTypeStore method rebuildIdGenerator.
@Override
protected void rebuildIdGenerator() {
logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
closeIdGenerator();
File file = new File(getStorageFileName() + ".id");
if (file.exists()) {
boolean success = file.delete();
assert success;
}
createIdGenerator(getStorageFileName() + ".id");
openIdGenerator();
FileChannel fileChannel = getFileChannel();
long highId = -1;
int recordSize = getRecordSize();
try {
long fileSize = fileChannel.size();
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[recordSize]);
for (int i = 0; i * recordSize < fileSize; i++) {
fileChannel.read(byteBuffer, i * recordSize);
byteBuffer.flip();
byte inUse = byteBuffer.get();
byteBuffer.flip();
if (inUse != Record.IN_USE.byteValue()) {
// hole found, marking as reserved
byteBuffer.clear();
byteBuffer.put(Record.IN_USE.byteValue()).putInt(Record.RESERVED.intValue());
byteBuffer.flip();
fileChannel.write(byteBuffer, i * recordSize);
byteBuffer.clear();
} else {
highId = i;
}
// nextId();
}
highId++;
fileChannel.truncate(highId * recordSize);
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
}
setHighId(highId);
logger.fine("[" + getStorageFileName() + "] high id=" + getHighId());
closeIdGenerator();
openIdGenerator();
}
use of java.nio.channels.FileChannel in project graphdb by neo4j-attic.
the class AbstractDynamicStore method findHighIdBackwards.
private long findHighIdBackwards() throws IOException {
FileChannel fileChannel = getFileChannel();
int recordSize = getBlockSize();
long fileSize = fileChannel.size();
long highId = fileSize / recordSize;
ByteBuffer byteBuffer = ByteBuffer.allocate(1);
for (long i = highId; i > 0; i--) {
fileChannel.position(i * recordSize);
if (fileChannel.read(byteBuffer) > 0) {
byteBuffer.flip();
byte inUse = byteBuffer.get();
byteBuffer.clear();
if (inUse != 0) {
return i;
}
}
}
return 0;
}
use of java.nio.channels.FileChannel in project graphdb by neo4j-attic.
the class AbstractDynamicStore method rebuildIdGenerator.
/**
* Rebuilds the internal id generator keeping track of what blocks are free
* or taken.
*
* @throws IOException
* If unable to rebuild the id generator
*/
protected void rebuildIdGenerator() {
if (getBlockSize() <= 0) {
throw new InvalidRecordException("Illegal blockSize: " + getBlockSize());
}
logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
closeIdGenerator();
File file = new File(getStorageFileName() + ".id");
if (file.exists()) {
boolean success = file.delete();
assert success;
}
createIdGenerator(getStorageFileName() + ".id");
openIdGenerator();
// nextBlockId(); // reserved first block containing blockSize
setHighId(1);
FileChannel fileChannel = getFileChannel();
long highId = 0;
long defraggedCount = 0;
try {
long fileSize = fileChannel.size();
boolean fullRebuild = true;
if (getConfig() != null) {
String mode = (String) getConfig().get("rebuild_idgenerators_fast");
if (mode != null && mode.toLowerCase().equals("true")) {
fullRebuild = false;
highId = findHighIdBackwards();
}
}
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1]);
LinkedList<Long> freeIdList = new LinkedList<Long>();
if (fullRebuild) {
for (long i = 1; i * getBlockSize() < fileSize; i++) {
fileChannel.position(i * getBlockSize());
fileChannel.read(byteBuffer);
byteBuffer.flip();
byte inUse = byteBuffer.get();
byteBuffer.flip();
nextBlockId();
if (inUse == Record.NOT_IN_USE.byteValue()) {
freeIdList.add(i);
} else {
highId = i;
while (!freeIdList.isEmpty()) {
freeBlockId(freeIdList.removeFirst());
defraggedCount++;
}
}
}
}
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
}
setHighId(highId + 1);
logger.fine("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
if (getConfig() != null) {
String storeDir = (String) getConfig().get("store_dir");
StringLogger msgLog = StringLogger.getLogger(storeDir);
msgLog.logMessage(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount, true);
}
closeIdGenerator();
openIdGenerator();
}
Aggregations