- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习如何通过使用 Byteable 键和值类来使用 ChronicleMap 3.12。当我使用基于调用堆栈的 ChronicleMap.put 操作循环运行代码时,似乎每次调用 ChronicleMap.put 时都会创建一个值对象。我假设使用 Byteable 值类将阻止对象创建。有人可以告诉我我是否可能做错了什么吗?
创建 ChronicleMap 的代码(TestDataKeyForChronicleMap 和 TestDataForChronicleMap 都是字节型的):
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 接口(interface)
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 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();
}
最佳答案
显然,您在循环中为相同的键或重复的键放置了一个值。在每个 Map.put()
上,根据 Map
契约返回先前的值(因此应该创建一个对象)。
避免这种情况的最简单方法是添加 putReturnsNull(true)
到您的 ChronicleMap 配置,但这会使 ChronicleMap
违反 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);
}
}
}
关于java - 使用 ChronicleMap.put 时如何避免创建字节数据的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593065/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!