private void writeBuffer(final ByteBuffer buffer, final long offset) throws IOException { buffer.rewind(); writeByteBuffer(buffer, channel, offset); }
public void clearDirty() throws IOException { if (!dirtyFlag) return; lock.lock(); try { if (!dirtyFlag) return; final ByteBuffer buffer = ByteBuffer.allocate(1); OIOUtils.writeByteBuffer(buffer, channel, 0); dirtyFlag = false; } finally { lock.unlock(); } }
private void writeConfigFile(byte[] buffer, ByteBuffer byteBuffer, FileChannel channel) throws IOException { final ByteBuffer versionBuffer = ByteBuffer.allocate(1); versionBuffer.put(FORMAT_VERSION); versionBuffer.position(0); OIOUtils.writeByteBuffer(versionBuffer, channel, OFileClassic.VERSION_OFFSET); final ByteBuffer crc32buffer = ByteBuffer.allocate(OIntegerSerializer.INT_SIZE); final CRC32 crc32 = new CRC32(); crc32.update(buffer); crc32buffer.putInt((int) crc32.getValue()); crc32buffer.position(0); OIOUtils.writeByteBuffer(crc32buffer, channel, CRC_32_OFFSET); channel.force(true); byteBuffer.position(0); OIOUtils.writeByteBuffer(byteBuffer, channel, OFileClassic.HEADER_SIZE); channel.force(true); }
public void makeDirty() throws IOException { if (dirtyFlag) return; lock.lock(); try { if (dirtyFlag) return; final ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put((byte) 1); buffer.position(0); OIOUtils.writeByteBuffer(buffer, channel, 0); dirtyFlag = true; } finally { lock.unlock(); } }
private void writeInternal(long offset, final byte[] data, final int size, final int arrayOffset) throws IOException { if (data != null) { offset += HEADER_SIZE; final ByteBuffer byteBuffer = ByteBuffer.wrap(data, arrayOffset, size); writeByteBuffer(byteBuffer, channel, offset); setDirty(); } }
public void allocateSpace(final ByteBuffer buffer) throws IOException { acquireWriteLock(); try { final long currentSize = this.size; assert channel.size() == currentSize + HEADER_SIZE; buffer.rewind(); //noinspection NonAtomicOperationOnVolatileField this.size += buffer.limit(); assert this.size >= size; OIOUtils.writeByteBuffer(buffer, channel, currentSize + HEADER_SIZE); assert channel.size() == this.size + HEADER_SIZE; } finally { releaseWriteLock(); } }
public void create() throws IOException { lock.lock(); try { if (Files.exists(dirtyFilePath)) { Files.delete(dirtyFilePath); } channel = FileChannel.open(dirtyFilePath, StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE); if (OGlobalConfiguration.FILE_LOCK.getValueAsBoolean()) { lockFile(); } final ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put((byte) 1); buffer.position(0); OIOUtils.writeByteBuffer(buffer, channel, 0); dirtyFlag = true; } finally { lock.unlock(); } }
public void open() throws IOException { lock.lock(); try { if (!Files.exists(dirtyFilePath)) { channel = FileChannel.open(dirtyFilePath, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); ByteBuffer buffer = ByteBuffer.allocate(1); OIOUtils.writeByteBuffer(buffer, channel, 0); } else { channel = FileChannel.open(dirtyFilePath, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); } if (OGlobalConfiguration.FILE_LOCK.getValueAsBoolean()) { lockFile(); } ByteBuffer buffer = ByteBuffer.allocate(1); OIOUtils.readByteBuffer(buffer, channel, 0, true); buffer.position(0); dirtyFlag = buffer.get() > 0; } finally { lock.unlock(); } }
private void openChannel() throws IOException { acquireWriteLock(); try { for (int i = 0; i < OPEN_RETRY_MAX; ++i) { try { frnd = new RandomAccessFile(osFile.toFile(), "rw"); channel = frnd.getChannel(); break; } catch (final FileNotFoundException e) { if (i == OPEN_RETRY_MAX - 1) { throw e; } // TRY TO RE-CREATE THE DIRECTORY (THIS HAPPENS ON WINDOWS AFTER A DELETE IS PENDING, USUALLY WHEN REOPEN THE DB VERY // FREQUENTLY) Files.createDirectories(osFile.getParent()); } } if (channel == null) { throw new FileNotFoundException(osFile.toString()); } if (channel.size() == 0) { final ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE); OIOUtils.writeByteBuffer(buffer, channel, 0); } } finally { releaseWriteLock(); } }
private void writeMasterRecord(final int index, final OLogSequenceNumber masterRecord) throws IOException { masterRecordLSNHolder.position(); final CRC32 crc32 = new CRC32(); final byte[] serializedLSN = new byte[2 * OLongSerializer.LONG_SIZE]; OLongSerializer.INSTANCE.serializeLiteral(masterRecord.getSegment(), serializedLSN, 0); OLongSerializer.INSTANCE.serializeLiteral(masterRecord.getPosition(), serializedLSN, OLongSerializer.LONG_SIZE); crc32.update(serializedLSN); final ByteBuffer buffer = ByteBuffer.allocate(MASTER_RECORD_SIZE); buffer.putInt((int) crc32.getValue()); buffer.putLong(masterRecord.getSegment()); buffer.putLong(masterRecord.getPosition()); buffer.rewind(); OIOUtils.writeByteBuffer(buffer, masterRecordLSNHolder, index * (OIntegerSerializer.INT_SIZE + 2L * OLongSerializer.LONG_SIZE)); }
private void writeNameIdEntry(final FileChannel nameIdMapHolder, final NameFileIdEntry nameFileIdEntry, final boolean sync) throws IOException { final int nameSize = stringSerializer.getObjectSize(nameFileIdEntry.name); final int fileNameSize = stringSerializer.getObjectSize(nameFileIdEntry.fileSystemName); //file id size + file name size + file name + file system name size + file system name final ByteBuffer serializedRecord = ByteBuffer.allocate(3 * OIntegerSerializer.INT_SIZE + nameSize + fileNameSize); //serialize file id OIntegerSerializer.INSTANCE.serializeInByteBufferObject(nameFileIdEntry.fileId, serializedRecord, 0); //serialize file name OIntegerSerializer.INSTANCE.serializeInByteBufferObject(nameSize, serializedRecord, OIntegerSerializer.INT_SIZE); stringSerializer.serializeInByteBufferObject(nameFileIdEntry.name, serializedRecord, 2 * OIntegerSerializer.INT_SIZE); //serialize file system name OIntegerSerializer.INSTANCE .serializeInByteBufferObject(fileNameSize, serializedRecord, 2 * OIntegerSerializer.INT_SIZE + nameSize); stringSerializer .serializeInByteBufferObject(nameFileIdEntry.fileSystemName, serializedRecord, 3 * OIntegerSerializer.INT_SIZE + nameSize); serializedRecord.position(0); OIOUtils.writeByteBuffer(serializedRecord, nameIdMapHolder, nameIdMapHolder.size()); nameIdMapHolder.write(serializedRecord); if (sync) { nameIdMapHolder.force(true); } }
public void write(long offset, final ByteBuffer buffer) throws IOException { int attempts = 0; while (true) { try { acquireWriteLock(); try { offset += HEADER_SIZE; writeByteBuffer(buffer, channel, offset); setDirty(); break; } finally { releaseWriteLock(); attempts++; } } catch (final IOException e) { OLogManager.instance().error(this, "Error during data write for file '" + getName() + "' " + attempts + "-th attempt", e); reopenFile(attempts, e); } } }