public String getString(final int docId) { final int address = docIdToAddress.getInt(docId << 2); final int firstByte = stringValues.getByte(address)&0xFF; final int length; final int valOffset; if (firstByte == 0xFF) { length = stringValues.getInt(address+1); valOffset = address+5; } else { length = firstByte; valOffset = address+1; } final byte[] bytes = new byte[length]; stringValues.getBytes(valOffset, bytes); return new String(bytes, Charsets.UTF_8); }
public ByteArray(Memory buffer, long address, long length) { if (address < 0) throw new IndexOutOfBoundsException("address must be >= 0"); if (length < 0) throw new IllegalArgumentException("length must be >= 0"); if (address+length > buffer.length()) throw new IndexOutOfBoundsException("address+length must be <= buffer.length()"); this.buffer = buffer.slice(address, length); this.length = length; }
@Override public long readLong() throws IOException { long ret = memory.getLong(position); position+=8; return ret; }
@Override public void readFully(final byte[] b, final int off, final int len) throws IOException { memory.getBytes(position, b, off, len); position+=len; }
private Option<E> readAndCheck(long address, MutableLong nextElementStart) throws IOException { if (address+4 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int length = memory.getInt(address); if (length < 0) { return Option.none(); } if (address+8 > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } if (address+8+length > memory.length()) { throw new ConsistencyException("not enough bytes in file"); } final int checksum = memory.getInt(address+4); MemoryDataInput in = new MemoryDataInput(memory); in.seek(address+8); CRC32 crc32 = new CRC32(); crc32.update(CRC_SEED); byte[] bytes = new byte[length]; in.readFully(bytes); crc32.update(bytes); if ((int)crc32.getValue() != checksum) { throw new ConsistencyException("checksum for record does not match: expected "+checksum+" actual "+(int)crc32.getValue()); } E ret = serializer.read(ByteStreams.newDataInput(bytes)); if (nextElementStart != null) nextElementStart.setValue(address+8+length); return Option.some(ret); }
public long getHashOffset(final long hash, @Nullable final Memory offsets, @Nullable final Select select) { switch (config.getOffsetStorage()) { case INDEXED: if (offsets == null) { throw new IllegalArgumentException("indexed offsets with null memory"); } final long offset; if (bytesPerOffset == 2) { offset = offsets.getShort(hash * 2L); } else if (bytesPerOffset == 4) { offset = offsets.getInt(hash * 4L); } else { offset = offsets.getLong(hash * 8L); } return offset; case SELECTED: final long rawSelected = select == null ? selectOffsets.select(hash) : select.select(hash); final long selected = rawSelected < 0 ? 0 : rawSelected; return config.decompressOffset(selected, hash); case FIXED: return config.decompressOffset(0L /* unused */, hash); default: throw new IllegalArgumentException("unknown offset storage type: " + config.getOffsetStorage()); } }
public long length() { return memory.length(); } }
public void writeBoolean(final boolean v) throws IOException { try { memory.putByte(currentAddress, (byte)(v ? 1 : 0)); currentAddress++; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeBoolean(v); } }
@Override public int readInt() throws IOException { int ret = memory.getInt(position); position+=4; return ret; }
@Override public boolean readBoolean() throws IOException { return memory.getByte(position++) != 0; }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { if (off < 0 || len < 0 || off+len > b.length) { throw new IndexOutOfBoundsException(); } try { memory.putBytes(currentAddress, b, off, len); currentAddress+=len; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); write(b, off, len); } }
public void writeShort(final int v) throws IOException { try { memory.putShort(currentAddress, (short)v); currentAddress+=2; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeShort(v); } }
public void writeInt(final int v) throws IOException { try { memory.putInt(currentAddress, v); currentAddress+=4; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeInt(v); } }
public void writeChar(final int v) throws IOException { try { memory.putChar(currentAddress, (char)v); currentAddress+=2; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeChar(v); } }
public void writeLong(final long v) throws IOException { try { memory.putLong(currentAddress, v); currentAddress+=8; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeLong(v); } }
public void writeDouble(final double v) throws IOException { try { memory.putDouble(currentAddress, v); currentAddress+=8; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeDouble(v); } }
public void writeFloat(final float v) throws IOException { try { memory.putFloat(currentAddress, v); currentAddress+=4; } catch (IndexOutOfBoundsException e) { toClose.add(buffer); buffer = new MMapBuffer(file, 0, memory.length() * 2, FileChannel.MapMode.READ_WRITE, byteOrder); memory = buffer.memory(); writeFloat(v); } }