synchronized void get(long position, byte[] chars) { dumpBuffer.position((int) position); dumpBuffer.get(chars); }
/** * Reads a buffer of the given size from the memory-mapped file, and collects all records contained. This method * assumes that all values in the buffer are of the same type. This method does NOT take care of synchronization. * The user must guarantee that the buffer was completely written before calling this method. * * @param c Collector to collect records * @param bufferSize size of the buffer * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void collectBuffer(Collector<OUT> c, int bufferSize) throws IOException { fileBuffer.position(0); while (fileBuffer.position() < bufferSize) { c.collect(deserializer.deserialize()); } }
private void checkIdentifier() { byte[] array = new byte[IDENTIFIER.length]; this.header.position(0); this.header.get(array); if (Arrays.equals(IDENTIFIER, array)) { // ignore } else if (Arrays.equals(new byte[IDENTIFIER.length], array)) { this.header.position(0); this.header.put(IDENTIFIER); } else { throw new IllegalStateException(); } }
public static MappedByteBuffer expandBuffer(String filename, MappedByteBuffer buffer, long newSize) { buffer.force(); int oldPosition = buffer.position(); try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) { raf.setLength(newSize); buffer = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); buffer.position(oldPosition); return buffer; } catch (IOException e) { throw new RuntimeException(e); } }
private void checkStartIndex() { this.header.position(IDENTIFIER.length + 2 + 1); int start = this.header.getInt(); if (start == IDENTIFIER.length + 2 + 1 + 8) { // ignore } else if (start == 0) { this.header.position(IDENTIFIER.length + 2 + 1); this.header.putInt(IDENTIFIER.length + 2 + 1 + 8); } else { throw new IllegalStateException(); } }
private int checkEndIndex() { this.header.position(IDENTIFIER.length + 2 + 1 + 4); int end = this.header.getInt(); if (end == 0) { this.header.position(IDENTIFIER.length + 2 + 1 + 4); this.header.putInt(IDENTIFIER.length + 2 + 1 + 8); return IDENTIFIER.length + 2 + 1 + 8; } else if (end < CONSTANTS_START_INDEX) { throw new IllegalStateException(); } else { return end; } }
public void markMaster() { this.header.position(IDENTIFIER.length + 2); this.header.put((byte) 0x1); }
private byte[] getBlock(long offset) throws IOException { byte[] block; if (offset < mappedSize) { block = new byte[blockSize]; buf.position((int)offset); buf.get(block); return block; } else { Long offsetObj = new Long(offset); block = (byte[]) blockCache.get(offsetObj); if (block == null) { block = new byte[blockSize]; data.seek(offset); data.readFully(block); blockCache.put(offsetObj,block); } return block; } }
public void markSlaver() { this.header.position(IDENTIFIER.length + 2); this.header.put((byte) 0x0); }
public void markPrepare() { this.header.position(IDENTIFIER.length + 2); this.header.put((byte) 0x2); }
private void checkVersion() { this.header.position(IDENTIFIER.length); int major = this.header.get(); int minor = this.header.get(); if (major == 0 && minor == 2) { // ignore } else if (major == 0 && minor == 0) { this.header.position(IDENTIFIER.length); this.header.put((byte) 0x0); this.header.put((byte) 0x2); } else { throw new IllegalStateException(); } }
/** * Reads a number of bytes from the RRD file on the disk * * @param offset Starting file offset * @param b Buffer which receives bytes read from the file. */ @Override protected synchronized void read(long offset, byte[] b) throws IOException { if (byteBuffer != null) { byteBuffer.position((int) offset); byteBuffer.get(b); } else { throw new IOException("Read failed, file " + getPath() + " not mapped for I/O"); } }
synchronized void get(long position, byte[] chars) { MappedByteBuffer buffer = dumpBuffer[getBufferIndex(position)]; buffer.position(getBufferOffset(position)); buffer.get(chars); }
@Override public synchronized int read(ByteBuffer dst) throws IOException { try { int len = dst.remaining(); if (len == 0) { return 0; } len = (int) Math.min(len, fileLength - pos); if (len <= 0) { return -1; } mapped.position(pos); mapped.get(dst.array(), dst.arrayOffset() + dst.position(), len); dst.position(dst.position() + len); pos += len; return len; } catch (IllegalArgumentException e) { EOFException e2 = new EOFException("EOF"); e2.initCause(e); throw e2; } catch (BufferUnderflowException e) { EOFException e2 = new EOFException("EOF"); e2.initCause(e); throw e2; } }
private byte checkMasterFlag(Boolean master) { this.header.position(IDENTIFIER.length + 2); if (master == null) { return this.header.get(); } else if (master) { this.header.put((byte) 0x1); return (byte) 0x1; } else { this.header.put((byte) 0x0); return (byte) 0x0; } }
private void updateEndIndex(int position) { this.endIndex = position; this.header.position(IDENTIFIER.length + 2 + 1 + 4); this.header.putInt(position); int recordUnit = CONSTANTS_RECORD_SIZE + 1; int textLength = this.endIndex - CONSTANTS_START_INDEX; int startIndex = textLength / recordUnit; for (int index = this.recordList.size() - 1; index >= startIndex; index--) { CleanupRecord record = this.recordList.remove(index); if (record == null) { continue; } // end-if (record == null) this.unRegisterRecord(record); } // end-for (int index = this.recordList.size() - 1; index >= startIndex; index--) }
@Override public synchronized int write(ByteBuffer src) throws IOException { int len = src.remaining(); // check if need to expand file if (mapped.capacity() < pos + len) { setFileLength(pos + len); } mapped.position(pos); mapped.put(src); pos += len; return len; }
/** * Writes bytes to the underlying RRD file on the disk * * @param offset Starting file offset * @param b Bytes to be written. */ @Override protected synchronized void write(long offset, byte[] b) throws IOException { if (byteBuffer != null) { byteBuffer.position((int) offset); byteBuffer.put(b); } else { throw new IOException("Write failed, file " + getPath() + " not mapped for I/O"); } }
@Override public synchronized int write(ByteBuffer src) throws IOException { int len = src.remaining(); // check if need to expand file if (mapped.capacity() < pos + len) { setFileLength(pos + len); } mapped.position(pos); mapped.put(src); pos += len; return len; }
/** * Extracts records from an iterator and writes them to the memory-mapped file. This method assumes that all values * in the iterator are of the same type. This method does NOT take care of synchronization. The caller must * guarantee that the file may be written to before calling this method. * * @param input iterator containing records * @param serializer serializer for the input records * @return size of the written buffer * @throws IOException */ protected <IN> int sendBuffer(SingleElementPushBackIterator<IN> input, Serializer<IN> serializer) throws IOException { fileBuffer.clear(); while (input.hasNext()) { IN value = input.next(); ByteBuffer bb = serializer.serialize(value); if (bb.remaining() > mappedFileSizeBytes) { throw new RuntimeException("Serialized object does not fit into a single buffer."); } if (bb.remaining() <= fileBuffer.remaining()) { fileBuffer.put(bb); } else { input.pushBack(value); break; } } int size = fileBuffer.position(); return size; }