/** * @param keyBytes * @param value * @param hash2 * @return */ V acquireEntry(DirectBytes keyBytes, V value, int hash2) { value = createValueIfNull(value); final int pos = nextFree(); final long offset = entriesOffset + pos * entrySize; tmpBytes.storePositionAndSize(bytes, offset, entrySize); final long keyLength = keyBytes.remaining(); tmpBytes.writeStopBit(keyLength); tmpBytes.write(keyBytes); if (value instanceof Byteable) { Byteable byteable = (Byteable) value; int length = byteable.maxSize(); tmpBytes.writeStopBit(length); tmpBytes.position(align(tmpBytes.position())); if (length > tmpBytes.remaining()) throw new IllegalStateException( "Not enough space left in entry for value, needs " + length + " but only " + tmpBytes.remaining() + " left"); tmpBytes.zeroOut(tmpBytes.position(), tmpBytes.position() + length); byteable.bytes(bytes, offset + tmpBytes.position()); } else { appendInstance(keyBytes, value); } // add to index if successful. hashLookup.put(hash2, pos); incrementSize(); return value; }
/** * Reads from {@link this.tmpBytes} an object at {@param offset}, will reuse {@param value} if * possible, to reduce object creation. * * @param offset the offset to read the data from * @param value the object to reuse ( if possible ), if null a new object will be created an * object and no reuse will occur. */ @SuppressWarnings("unchecked") V readObjectUsing(V value, final long offset) { if (value instanceof Byteable) { ((Byteable) value).bytes(bytes, offset); return value; } if (generatedValueType) { if (value == null) value = DataValueClasses.newInstance(vClass); ((BytesMarshallable) value).readMarshallable(tmpBytes); return value; } return tmpBytes.readInstance(vClass, value); }
@Before public void setup() throws IOException { value = DataValueClasses.newDirectReference(IntValue.class); ((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0); final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1); timeProvider = new TimeProvider() { Random rnd = new Random(4); @Override public long currentTime() { if (rnd.nextBoolean()) return t++; else return t; } }; { final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port, endpoint); map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(Builder.SIZE) .timeProvider(timeProvider) .replication((byte) 1, tcpConfig1) .create(); } { final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of(s_port + 1); map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(Builder.SIZE) .timeProvider(timeProvider) .replication((byte) 2, tcpConfig2) .create(); } s_port += 2; }