Search in sources :

Example 1 with FileException

use of org.apache.jena.tdb.base.file.FileException in project jena by apache.

the class ObjectFileStorage method abortWrite.

@Override
public void abortWrite(Block block) {
    allocBlock = null;
    int oldstart = (int) (allocLocation - filesize);
    if (oldstart != oldBufferPosn)
        throw new FileException("Wrong reset point: calc=" + oldstart + " : expected=" + oldBufferPosn);
    writeBuffer.position(oldstart);
    writeBuffer.limit(oldBufferLimit);
    allocLocation = -1;
    oldBufferPosn = -1;
    oldBufferLimit = -1;
    inAllocWrite = false;
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException)

Example 2 with FileException

use of org.apache.jena.tdb.base.file.FileException in project jena by apache.

the class ObjectFileStorage method completeWrite.

@Override
public void completeWrite(Block block) {
    if (logging)
        log("CW: %s @0x%X", block, allocLocation);
    if (!inAllocWrite)
        throw new FileException("Not in the process of an allocated write operation pair");
    if (allocBlock != null && (allocBlock.getByteBuffer() != block.getByteBuffer()))
        throw new FileException("Wrong byte buffer in an allocated write operation pair");
    inAllocWrite = false;
    ByteBuffer buffer = block.getByteBuffer();
    if (allocLocation == -1) {
        // It was too big to use the buffering.
        rawWrite(buffer);
        return;
    }
    // Write area is 0 -> limit
    if (0 != buffer.position())
        log.warn("ObjectFleStorage: position != 0");
    buffer.position(0);
    int actualLength = buffer.limit() - buffer.position();
    // Insert object length
    int idx = (int) (allocLocation - filesize);
    writeBuffer.putInt(idx, actualLength);
    // And bytes to idx+actualLength+4 are used
    allocBlock = null;
    int newLen = idx + actualLength + 4;
    writeBuffer.position(newLen);
    writeBuffer.limit(writeBuffer.capacity());
    allocLocation = -1;
    oldBufferPosn = -1;
    oldBufferLimit = -1;
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with FileException

use of org.apache.jena.tdb.base.file.FileException in project jena by apache.

the class ObjectFileStorage method rawWrite.

private long rawWrite(ByteBuffer bb) {
    if (logging)
        log("RW %s", bb);
    int len = bb.limit() - bb.position();
    lengthBuffer.rewind();
    lengthBuffer.putInt(len);
    lengthBuffer.flip();
    long location = file.position();
    file.write(lengthBuffer);
    int x = file.write(bb);
    if (x != len)
        throw new FileException();
    filesize = filesize + x + SizeOfInt;
    if (logging) {
        log("Posn: %d", file.position());
        log("RW ->0x%X", location);
    }
    return location;
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException)

Example 4 with FileException

use of org.apache.jena.tdb.base.file.FileException in project jena by apache.

the class ObjectFileStorage method reposition.

@Override
public void reposition(long posn) {
    if (inAllocWrite)
        throw new FileException("In the middle of an alloc-write");
    if (posn < 0 || posn > length())
        throw new IllegalArgumentException("reposition: Bad location: " + posn);
    flushOutputBuffer();
    file.truncate(posn);
    filesize = posn;
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException)

Example 5 with FileException

use of org.apache.jena.tdb.base.file.FileException in project jena by apache.

the class ObjectFileStorage method read.

@Override
public ByteBuffer read(long loc) {
    if (logging)
        log("R(0x%X)", loc);
    if (inAllocWrite)
        throw new FileException("In the middle of an alloc-write");
    if (loc < 0)
        throw new IllegalArgumentException("ObjectFile.read[" + file.getLabel() + "]: Bad read: " + loc);
    // Maybe the write buffer should keep more structure? 
    if (loc >= filesize) {
        if (loc >= filesize + writeBuffer.position())
            throw new IllegalArgumentException("ObjectFileStorage.read[" + file.getLabel() + "]: Bad read: location=" + loc + " >= max=" + (filesize + writeBuffer.position()));
        int x = writeBuffer.position();
        int y = writeBuffer.limit();
        int offset = (int) (loc - filesize);
        int len = writeBuffer.getInt(offset);
        int posn = offset + SizeOfInt;
        // Slice the data bytes,
        writeBuffer.position(posn);
        writeBuffer.limit(posn + len);
        ByteBuffer bb = writeBuffer.slice();
        writeBuffer.limit(y);
        writeBuffer.position(x);
        return bb;
    }
    // No - it's in the underlying file storage.
    lengthBuffer.clear();
    int x = file.read(lengthBuffer, loc);
    if (x != 4)
        throw new FileException("ObjectFileStorage.read[" + file.getLabel() + "](" + loc + ")[filesize=" + filesize + "][file.size()=" + file.size() + "]: Failed to read the length : got " + x + " bytes");
    int len = lengthBuffer.getInt(0);
    // Sanity check. 
    if (len > filesize - (loc + SizeOfInt)) {
        String msg = "ObjectFileStorage.read[" + file.getLabel() + "](" + loc + ")[filesize=" + filesize + "][file.size()=" + file.size() + "]: Impossibly large object : " + len + " bytes > filesize-(loc+SizeOfInt)=" + (filesize - (loc + SizeOfInt));
        SystemTDB.errlog.error(msg);
        throw new FileException(msg);
    }
    ByteBuffer bb = ByteBuffer.allocate(len);
    if (len == 0)
        // Zero bytes.
        return bb;
    x = file.read(bb, loc + SizeOfInt);
    bb.flip();
    if (x != len)
        throw new FileException("ObjectFileStorage.read: Failed to read the object (" + len + " bytes) : got " + x + " bytes");
    return bb;
}
Also used : FileException(org.apache.jena.tdb.base.file.FileException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FileException (org.apache.jena.tdb.base.file.FileException)6 ByteBuffer (java.nio.ByteBuffer)3 Pair (org.apache.jena.atlas.lib.Pair)1