Indexing 索引字段上带谓词的查询

Indexing 索引字段上带谓词的查询,indexing,hazelcast,query-cache,Indexing,Hazelcast,Query Cache,在我的项目中,我最近遇到了一个与配置QueryCache的映射相关的问题 我有两个字段的实体: class AccountingRule { long key; String code; } 其映射的映射配置,包括QueryCache配置: MapConfig mapConfig = new MapConfig("AccountingRule") .setInMemoryFormat(InMemoryFormat.BINARY) .setReadBackupDa

在我的项目中,我最近遇到了一个与配置QueryCache的映射相关的问题

我有两个字段的实体:

class AccountingRule {
    long key;
    String code;
}
其映射的映射配置,包括QueryCache配置:

MapConfig mapConfig = new MapConfig("AccountingRule")
    .setInMemoryFormat(InMemoryFormat.BINARY)
    .setReadBackupData(true)
    .setBackupCount(1)
    .addQueryCacheConfig(new QueryCacheConfig()
        .setName("AccountingRule")
        .setPredicateConfig(new PredicateConfig(TruePredicate.INSTANCE))
        .setIncludeValue(true)
        .setPopulate(true)
        .setDelaySeconds(0)
    );
大体上,我使用配置运行hazelcast实例,然后:

  • 将索引添加到QueryCache
  • 将实体放入IMap
  • 通过检查QueryCache.get是否返回插入的实体,等待插入被传播到QueryCache
  • 使用索引字段上的谓词从QueryCache获取相同的实体
例如:

HazelcastInstance hazelcast = createHazelcastInstance();
IMap<Long, AccountingRule> map = hazelcast.getMap("AccountingRule");
QueryCache<Long, AccountingRule> queryCache = map.getQueryCache("AccountingRule");

queryCache.addIndex("code", false);

while (true) {
    AccountingRule ar = newAccountingRule();
    map.put(ar.getKey(), ar);

    // wait for query cache
    AccountingRule fromQueryCache = queryCache.get(ar.getKey());
    while (fromQueryCache == null) {
        log.info("Waiting for query cache");
        fromQueryCache = queryCache.get(AccountingRule.class, ar.getKey());
     }
    log.info("query cache updated");

    // get entity from query cache using predicate
    Predicate codePredicate = equal("code", ar.getCode());
    AccountingRule fromQueryCacheWithPredicate = getOnlyElement(queryCache.values(codePredicate), null);
    if (fromQueryCacheWithPredicate == null) {
        log.error("AccountingRule with id {} from query cash is null", ar.getKey());
        System.exit(1);
    }
}

问题是,为什么我在配置中添加索引时,它不能正常工作?

关于这个问题,似乎存在错误。执行
QueryCache.addIndex
时,仅当查询缓存中有条目时才会创建索引。此外,根本不会创建源自
Config
(编程或声明)的索引,并且在查询缓存上运行的查询会进行完全扫描


我看到您已经创建了一个关于此问题的Github问题(),请继续进行修复。

关于源自配置的索引,您是对的。我做了一个小的性能测试,看起来使用
QueryCacheConfig添加索引配置。addIndexConfig
根本不会创建索引。关于
QueryCache.addIndex
仅当QueryCache为空时才添加索引。
MapConfig mapConfig = new MapConfig("AccountingRule")
    .setInMemoryFormat(InMemoryFormat.BINARY)
    .setReadBackupData(true)
    .setBackupCount(1)
    .addQueryCacheConfig(new QueryCacheConfig()
        .setName("AccountingRule")
        .setPredicateConfig(new PredicateConfig(TruePredicate.INSTANCE))
        .setIncludeValue(true)
        .setPopulate(true)
        .setDelaySeconds(0)
        .addIndexConfig(new MapIndexConfig("code", false))
    );