Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何避免在使用ChroniceMap.put时创建可字节数据的新实例_Java_Chronicle Map - Fatal编程技术网

Java 如何避免在使用ChroniceMap.put时创建可字节数据的新实例

Java 如何避免在使用ChroniceMap.put时创建可字节数据的新实例,java,chronicle-map,Java,Chronicle Map,我正在学习如何通过使用Byteable键和值类来使用编年史地图3.12。当我使用基于调用堆栈的chronicmap.put操作循环运行代码时,每次调用chronicmap.put时,它似乎都会创建一个值对象。我假设使用Byteable值类将阻止对象创建。有人能告诉我我是否可能做了一些不正确的事情吗 创建编年史地图的代码(TestDataKeyForChronicleMap和TestDataForChronicleMap都是可删除的): 运行Chronicle Map.put时的调用堆栈 at s

我正在学习如何通过使用Byteable键和值类来使用编年史地图3.12。当我使用基于调用堆栈的chronicmap.put操作循环运行代码时,每次调用chronicmap.put时,它似乎都会创建一个值对象。我假设使用Byteable值类将阻止对象创建。有人能告诉我我是否可能做了一些不正确的事情吗

创建编年史地图的代码(TestDataKeyForChronicleMap和TestDataForChronicleMap都是可删除的):

运行Chronicle Map.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)
谢谢

更新以包括TestDataKeyForChronicle映射:

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 );
    }
}
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();
    }
}
TestDataForChroniceMap的代码:

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 );
    }
}
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;
}
以及TestDataMapFor编年史地图

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();
    }
}

显然,您在循环中为同一个键或重复的键设置了一个值。在每个
Map.put()
上,根据
Map
contract返回上一个值(因此应该创建一个对象)

避免这种情况的最简单方法是添加到您的编年史地图配置中,但是这会使
编年史地图
违反
地图
契约

另一种方法是使用上下文:

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);
        }
    }
}
静态void justPut(编年史地图、K键、V值){
try(ExternalMapQueryContext c=map.queryContext(键)){
c、 updateLock().lock();
MapEntry=c.entry();
if(条目!=null){
c、 替换值(条目、值);
}否则{
c、 插入(c.缺席(),值);
}
}
}

因为循环中键的值不同,但键的实例相同,但可能是因为我只实现了Byteable,但没有提供相应的equals方法来使用Byteable内容进行比较。我将使用putReturnsAll(true),因为我不需要put的返回值。谢谢,这很奇怪。你能发布TestDataKeyForChronicleMap的整个源代码,以及使用put()的循环吗?我可以确认,放置equal和hashCode似乎对这种情况没有帮助,但是使用putReturnsAll(true)。我假设为了满足映射契约,每次调用put时,编年史映射都必须创建一个新的数据实例。我将更新原始帖子,以包含TestDataKeyForChronicleMap的源代码,而带有putequals()和hashCode()的循环也帮不上忙,ChronicleMap没有使用它们。我怀疑您的Byteable实现很可能存在问题。我将TestDataKeyForChroniceMap和TestDataForChroniceMap的代码添加到原始发布中。非常感谢你的帮助
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);
        }
    }
}