@Override public OffHeapValueHolder<V> decode(ByteBuffer byteBuffer, WriteContext writeContext) { long id = byteBuffer.getLong(); long creationTime = byteBuffer.getLong(); long lastAccessTime = byteBuffer.getLong(); long expireTime = byteBuffer.getLong(); byteBuffer.getLong(); // hits read from disk. It is kept for compatibility reasons with previously saved data return new LazyOffHeapValueHolder<>(id, byteBuffer.slice(), serializer, creationTime, expireTime, lastAccessTime, writeContext); } }
/** * Must be called under offheap lock, may corrupt memory otherwise */ @Override void writeBack() { writeContext.setLong(OffHeapValueHolderPortability.ACCESS_TIME_OFFSET, lastAccessTime()); writeContext.setLong(OffHeapValueHolderPortability.EXPIRE_TIME_OFFSET, expirationTime()); writeContext.flush(); }
@Override public V get() { forceDeserialization(); return value; }
@Override void updateMetadata(final Store.ValueHolder<V> valueFlushed) { if(getId() != valueFlushed.getId()) { throw new IllegalArgumentException("Wrong id passed in [this.id != id] : " + getId() + " != " + valueFlushed.getId()); } this.setLastAccessTime(valueFlushed.lastAccessTime()); this.setExpirationTime(valueFlushed.expirationTime()); }
@Test public void testCanAccessBinaryValue() throws ClassNotFoundException { JavaSerializer<String> serializer = new JavaSerializer<>(getClass().getClassLoader()); String testValue = "Let's get binary!"; ByteBuffer serialized = serializer.serialize(testValue); LazyOffHeapValueHolder<String> valueHolder = new LazyOffHeapValueHolder<>(1L, serialized, serializer, 10L, 20L, 15L, mock(WriteContext.class)); valueHolder.detach(); ByteBuffer binaryValue = valueHolder.getBinaryValue(); assertThat(serializer.read(binaryValue), is(testValue)); }
@Test public void testPreventAccessToBinaryValueIfNotPrepared() { JavaSerializer<String> serializer = new JavaSerializer<>(getClass().getClassLoader()); String testValue = "Let's get binary!"; ByteBuffer serialized = serializer.serialize(testValue); LazyOffHeapValueHolder<String> valueHolder = new LazyOffHeapValueHolder<>(1L, serialized, serializer, 10L, 20L, 15L, mock(WriteContext.class)); try { valueHolder.getBinaryValue(); fail("IllegalStateException expected"); } catch (IllegalStateException e) { assertThat(e.getMessage(), containsString("has not been prepared")); } } }
@Override public ByteBuffer getBinaryValue() throws IllegalStateException { if (isBinaryValueAvailable()) { return binaryValue.duplicate(); } else { throw new IllegalStateException("This OffHeapValueHolder has not been prepared to hand off its binary form"); } }
public LazyOffHeapValueHolder(long id, ByteBuffer binaryValue, Serializer<V> serializer, long creationTime, long expireTime, long lastAccessTime, WriteContext writeContext) { super(id, creationTime, expireTime); setLastAccessTime(lastAccessTime); this.binaryValue = binaryValue; this.valueSerializer = serializer; this.writeContext = writeContext; this.mode = Mode.ATTACHED; }
@Override void updateMetadata(final Store.ValueHolder<V> valueFlushed) { if(getId() != valueFlushed.getId()) { throw new IllegalArgumentException("Wrong id passed in [this.id != id] : " + getId() + " != " + valueFlushed.getId()); } this.setLastAccessTime(valueFlushed.lastAccessTime(LazyOffHeapValueHolder.TIME_UNIT), LazyOffHeapValueHolder.TIME_UNIT); this.setExpirationTime(valueFlushed.expirationTime(LazyOffHeapValueHolder.TIME_UNIT), LazyOffHeapValueHolder.TIME_UNIT); }
@Override public ByteBuffer getBinaryValue() throws IllegalStateException { if (isBinaryValueAvailable()) { return binaryValue.duplicate(); } else { throw new IllegalStateException("This OffHeapValueHolder has not been prepared to hand off its binary form"); } }
public LazyOffHeapValueHolder(long id, ByteBuffer binaryValue, Serializer<V> serializer, long creationTime, long expireTime, long lastAccessTime, WriteContext writeContext) { super(id, creationTime, expireTime); setLastAccessTime(lastAccessTime, TIME_UNIT); this.binaryValue = binaryValue; this.valueSerializer = serializer; this.writeContext = writeContext; this.mode = Mode.ATTACHED; }
/** * Must be called under offheap lock, may corrupt memory otherwise */ @Override void writeBack() { writeContext.setLong(OffHeapValueHolderPortability.ACCESS_TIME_OFFSET, lastAccessTime(TimeUnit.MILLISECONDS)); writeContext.setLong(OffHeapValueHolderPortability.EXPIRE_TIME_OFFSET, expirationTime(TimeUnit.MILLISECONDS)); writeContext.flush(); }
@Test public void testDelayedDeserialization() { JavaSerializer<String> serializer = new JavaSerializer<>(getClass().getClassLoader()); String testValue = "Let's get binary!"; ByteBuffer serialized = serializer.serialize(testValue); OffHeapValueHolder<String> valueHolder = new LazyOffHeapValueHolder<>(1L, serialized, serializer, 10L, 20L, 15L, mock(WriteContext.class)); valueHolder.detach(); serialized.clear(); assertThat(valueHolder.get(), is(testValue)); }
@Override public V get() { forceDeserialization(); return value; }
@Override public OffHeapValueHolder<V> decode(ByteBuffer byteBuffer, WriteContext writeContext) { long id = byteBuffer.getLong(); long creationTime = byteBuffer.getLong(); long lastAccessTime = byteBuffer.getLong(); long expireTime = byteBuffer.getLong(); byteBuffer.getLong(); // hits read from disk. It is kept for compatibility reasons with previously saved data return new LazyOffHeapValueHolder<>(id, byteBuffer.slice(), serializer, creationTime, expireTime, lastAccessTime, writeContext); } }