我正在学习如何使用Byteable键和值类来使用ChronicleMap 3.12。当我使用基于调用堆栈的ChronicleMap.put操作循环运行我的代码时,似乎每次调用ChronicleMap.put时都会创建一个值对象。我假设使用Byteable值类将阻止对象创建。有人可以告诉我,我是否有可能做错了什么?
创建ChronicleMap的代码(TestDataKeyForChronicleMap和TestDataForChronicleMap都是Byteable):
map = ChronicleMap.of( TestDataKeyForChronicleMap.class, TestDataForChronicleMap.class)
.name( "TestDataMapForChronicleMap")
.entries(aMaxNoOfRecords).
.create();
运行ChronicleMap.put时的调用堆栈
at sun.reflect.GeneratedConstructorAccessor1.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at net.openhft.chronicle.core.util.ObjectUtils.lambda$null$0(ObjectUtils.java:71)
at net.openhft.chronicle.core.util.ThrowingSupplier.lambda$asSupplier$0(ThrowingSupplier.java:42)
at net.openhft.chronicle.core.util.ObjectUtils.newInstance(ObjectUtils.java:297)
at net.openhft.chronicle.hash.serialization.impl.InstanceCreatingMarshaller.createInstance(InstanceCreatingMarshaller.java:67)
at net.openhft.chronicle.hash.serialization.impl.ByteableSizedReader.read(ByteableSizedReader.java:42)
at net.openhft.chronicle.hash.serialization.impl.ByteableSizedReader.read(ByteableSizedReader.java:31)
at net.openhft.chronicle.map.impl.CompiledMapQueryContext$EntryValueBytesData.innerGetUsing(CompiledMapQueryContext.java:585)
at net.openhft.chronicle.map.impl.CompiledMapQueryContext$EntryValueBytesData.getUsing(CompiledMapQueryContext.java:595)
at net.openhft.chronicle.map.impl.CompiledMapQueryContext$DefaultReturnValue.initDefaultReturnedValue(CompiledMapQueryContext.java:411)
at net.openhft.chronicle.map.impl.CompiledMapQueryContext$DefaultReturnValue.returnValue(CompiledMapQueryContext.java:400)
at net.openhft.chronicle.map.MapMethods.put(MapMethods.java:86)
at net.openhft.chronicle.map.VanillaChronicleMap.put(VanillaChronicleMap.java:716)
感谢。
更新以包含TestDataKeyForChronicleMap:
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.BytesStore;
public class TestDataKeyForChronicleMap implements Byteable
{
private final static long MAX_SIZE = 16;
private BytesStore bytesStore;
private long offset;
@Override
public BytesStore bytesStore()
{
return bytesStore;
}
@Override
public void bytesStore(BytesStore aBytesStore, long anOffset, long aSize)
throws IllegalStateException, IllegalArgumentException,
BufferOverflowException, BufferUnderflowException
{
if ( aSize != MAX_SIZE )
{
throw new IllegalArgumentException();
}
bytesStore = aBytesStore;
offset = anOffset;
}
@Override
public long maxSize()
{
return MAX_SIZE;
}
@Override
public long offset()
{
return offset;
}
/**
* set key
* @param aKey1 key 1
* @param aKey2 key 2
*/
public void setKey( long aKey1, long aKey2 )
{
bytesStore.writeLong( offset, aKey1 );
bytesStore.writeLong( offset + 8, aKey2 );
}
/**
* get key 1
* @return key 1
*/
public long getKey1()
{
return bytesStore.readLong( offset );
}
/**
* get key 2
* @return key 2
*/
public long getKey2()
{
return bytesStore.readLong( offset + 8 );
}
}
TestDataForChronicleMap的代码:
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.BytesStore;
public final class TestDataForChronicleMap implements TestData, Byteable
{
private final static long MAX_SIZE = 48;
private BytesStore bytesStore;
private long offset;
public TestDataForChronicleMap()
{
Thread.currentThread().dumpStack();
}
@Override
public BytesStore bytesStore()
{
return bytesStore;
}
@Override
public void bytesStore(BytesStore aBytesStore, long anOffset, long aSize)
throws IllegalStateException, IllegalArgumentException,
BufferOverflowException, BufferUnderflowException
{
if ( aSize != MAX_SIZE )
{
throw new IllegalArgumentException();
}
bytesStore = aBytesStore;
offset = anOffset;
}
@Override
public long maxSize()
{
return MAX_SIZE;
}
@Override
public long offset()
{
return offset;
}
@Override
public void setKey(long aKey1, long aKey2)
{
bytesStore.writeLong(offset+0, aKey1);
bytesStore.writeLong(offset+8, aKey2);
bytesStore.writeLong(offset+16, -1L);
bytesStore.writeLong(offset+24, -1L);
bytesStore.writeLong(offset+32, -1L);
bytesStore.writeLong(offset+40, -1L);
}
@Override
public void setData(long aKey1, long aKey2)
{
bytesStore.writeLong(offset+0, aKey1);
bytesStore.writeLong(offset+8, aKey2);
bytesStore.writeLong(offset+16, aKey1);
bytesStore.writeLong(offset+24, aKey2);
bytesStore.writeLong(offset+32, aKey2);
bytesStore.writeLong(offset+40, aKey1);
}
@Override
public boolean isCorrect()
{
return ( bytesStore.readLong(offset+0) == bytesStore.readLong(offset+16) && bytesStore.readLong(offset+0) == bytesStore.readLong(offset+40) )
&&
( bytesStore.readLong(offset+8) == bytesStore.readLong(offset+24) && bytesStore.readLong(offset+8) == bytesStore.readLong(offset+32) );
}
@Override
public long getKey1()
{
return bytesStore.readLong(offset+0 );
}
@Override
public long getKey2()
{
return bytesStore.readLong(offset+8 );
}
@Override
public String getPrintableText()
{
StringBuilder builder = new StringBuilder();
builder.append( bytesStore.readLong(offset+0) );
builder.append( ' ' );
builder.append( bytesStore.readLong(offset+8) );
builder.append( ' ' );
builder.append( bytesStore.readLong(offset+16) );
builder.append( ' ' );
builder.append( bytesStore.readLong(offset+24) );
builder.append( ' ' );
builder.append( bytesStore.readLong(offset+32) );
builder.append( ' ' );
builder.append( bytesStore.readLong(offset+40) );
return builder.toString();
}
}
put循环的代码:
/**
* test add
* @param aMap map to be used
* @param aNoOfData no of data to be used for testing
* @return time taken in ms
*/
public final static long TestAdd( final TestDataMap aMap, final long aNoOfData )
{
long time = System.currentTimeMillis();
TestData data = aMap.createTestData();
for( long count=0; count<aNoOfData; count++ )
{
data.setData( count, count+aNoOfData);
aMap.put(data);
}
return System.currentTimeMillis() - time;
}
TestDataMap的接口
public interface TestDataMap
{
/**
* create test data
*/
public TestData createTestData();
/**
* create test data for zero copy
*/
public TestData createTestDataForZeroCopy();
/**
* check if map requires new data in each entry
* @return true if new data is needed for each entry or false if data can be reused
*/
public boolean needNewData();
/**
* put test data into the map
* @param aData
*/
public void put( TestData aData );
/**
* get test data from the map
*/
public TestData get( TestData aData );
/**
* get test data from the map with zero copy
*/
public boolean getZeroCopy( TestData aData );
/**
* remove the test data from the map
*/
public boolean remove( TestData aData );
/**
* get if the map contains the test data
*/
public boolean contains( TestData aData );
/**
* get size
*/
public long getSize();
/**
* clear the map
*/
public void clear();
/**
* dispose
*/
public void dispose();
}
和TestDataMapForChronicleMap
import java.io.IOException;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.map.ChronicleMap;
public final class TestDataMapForChronicleMap implements TestDataMap
{
private ChronicleMap<TestDataKeyForChronicleMap,TestDataForChronicleMap> map;
private TestDataKeyForChronicleMap key = new TestDataKeyForChronicleMap();
public TestDataMapForChronicleMap( long aMaxNoOfRecords ) throws IOException
{
map = ChronicleMap.of( TestDataKeyForChronicleMap.class, TestDataForChronicleMap.class)
.name( "TestDataMapForChronicleMap")
.entries(aMaxNoOfRecords)
.putReturnsNull( true )
.create();
BytesStore bytesStore = BytesStore.wrap( new byte[16] );
key.bytesStore( bytesStore, 0, bytesStore.capacity() );
}
/**
* put test data into the map
* @param aData
*/
public void put( final TestData aData )
{
key.setKey( aData.getKey1(), aData.getKey2() );
map.put( key, (TestDataForChronicleMap)aData );
}
/**
* get test data from the map
*/
public TestData get( final TestData aData )
{
key.setKey( aData.getKey1(), aData.getKey2() );
return map.getUsing( key, (TestDataForChronicleMap)aData );
}
/**
* remove the test data from the map
*/
public boolean remove( final TestData aData )
{
key.setKey( aData.getKey1(), aData.getKey2() );
return map.remove( key ) != null;
}
/**
* get if the map contains the test data
*/
public boolean contains( final TestData aData )
{
key.setKey( aData.getKey1(), aData.getKey2() );
return map.containsKey( key );
}
/**
* dispose the map and releases all the resources
* @param shouldRemoveFiles true will remove all the existing memory mapped files from the system
*/
public void dispose( final boolean shouldRemoveFiles )
{
map.close();
}
/**
* get size
* @return size of the map
*/
public long getSize()
{
return map.size();
}
/**
* clear all the values from the map
*/
public void clear()
{
map.clear();
}
@Override
public boolean getZeroCopy(final TestData aData )
{
key.setKey( aData.getKey1(), aData.getKey2() );
return map.getUsing( key, (TestDataForChronicleMap)aData ) != null;
}
@Override
public TestData createTestData()
{
TestDataForChronicleMap data = new TestDataForChronicleMap();
BytesStore bytesStore = BytesStore.wrap( new byte[48] );
data.bytesStore( bytesStore, 0, bytesStore.capacity() );
return data;
}
@Override
public TestData createTestDataForZeroCopy()
{
//return createTestData();
return null;
}
@Override
public boolean needNewData()
{
return false;
}
@Override
public void dispose()
{
map.close();
}
/**
* get heap memory
*/
public long getOffHeapMemory()
{
return map.offHeapMemoryUsed();
}
}
TestData代码
/**
* TestData has the following layout
* long key1
* long key2
* long value1 (value = key1)
* long value2 (value = key2)
* long value3 (value = key2)
* long value4 (value = key1)
*/
public interface TestData
{
/**
* set key
* @param aKey1 key 1
* @param aKey2 key 2
*/
public void setKey( long aKey1, long aKey2 );
/**
* set data
* @param aKey1 key 1
* @param aKey2 key 2
*/
public void setData( long aKey1, long aKey2 );
/**
* check if the data is correct
*/
public boolean isCorrect();
/**
* get key 1
* @return key 1
*/
public long getKey1();
/**
* get key 2
* @return key 2
*/
public long getKey2();
/**
* to string
*/
public String getPrintableText();
}
答案 0 :(得分:0)
显然,您在循环或重复键中为同一个键设置了值。在每个Map.put()
上,根据Map
合同返回前一个值(因此应创建一个对象)。
避免这种情况的最简单方法是将putReturnsNull(true)
添加到ChronicleMap配置中,但这会导致ChronicleMap
违反Map
合同。
另一种方法是使用上下文:
static <K, V> void justPut(ChronicleMap<K, V> map, K key, V value) {
try (ExternalMapQueryContext<K, V, ?> c = map.queryContext(key)) {
c.updateLock().lock();
MapEntry<K, V> entry = c.entry();
if (entry != null) {
c.replaceValue(entry, value);
} else {
c.insert(c.absentEntry(), value);
}
}
}