Maploader延迟初始化索引

时间:2017-08-09 13:44:53

标签: java distributed hazelcast

我正在使用maploader,它使用索引进行查询

O_CREAT
  • Maploader

    公共类SimpleMapLoader实现MapLoader {     public Customer load(整数键){         的System.out.println( “负载”);         return null;     }

    public class Customer implements Serializable {
    
        private String id;
        private String name;
    
        public Customer(String id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    }

主要

public Map<Integer, Customer> loadAll(Collection<Integer> keys) {
    System.out.println("loadAll");
    Map<Integer, Customer> map = new HashMap<Integer, Customer>();

    for (Integer k : keys) {
        map.put(k, new Customer(String.valueOf(k), String.valueOf(k)));
    }

    return map;
}

public Iterable<Integer> loadAllKeys() {
    System.out.println("loadallkeys");
    List<Integer> map = new ArrayList<Integer>();
    for (int i = 0; i < 10; i++) {
        map.add(i);
    }

    return map;
}

这是绝对精美的地图,直到我第一次触摸地图才会加载。

但是我为地图添加了一个索引。无论触摸地图,都会急切地加载地图。 来自Hazelcast文档 MapStoreConfig类中的InitialLoadMode配置参数有两个值:LAZY和EAGER。如果InitialLoadMode设置为LAZY,则在创建地图期间不会加载数据。如果将其设置为EAGER,则在创建地图时会加载所有数据,并且所有内容都可以使用。此外,如果使用MapIndexConfig类或addIndex方法向地图添加索引,则会覆盖InitialLoadMode,并且MapStoreConfig的行为就像启用EAGER模式一样。

但有没有办法覆盖此Eager InitialLoadMode行为。 我按以下方式添加索引

public class Main {

    public static void main(String[] args) {

        Config config = new Config();
        HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

        final MapConfig customerMapConf = config.getMapConfig("customers");
        final MapStoreConfig customerMapStoreConf = customerMapConf.getMapStoreConfig();
        customerMapStoreConf.setImplementation(new SimpleMapLoader());
        customerMapStoreConf.setEnabled(true);
        customerMapConf.setMapStoreConfig(customerMapStoreConf);
        config.addMapConfig(customerMapConf);
        Map<Integer, String> map = instance.getMap("customers"); 
        instance.shutdown();
    }

}

使用hazelcast 3.7.4。请建议是否有办法

2 个答案:

答案 0 :(得分:0)

正如文档所述,初始加载模式被覆盖并在创建索引时设置为EAGER。您可以通过分支自己的Hazelcast代码分支来覆盖生产行为,修改您想要的内容并重建它。但是,这可能会对系统的其他区域产生负面影响(例如,由于分区线程占用时间过长导致加载速度缓慢),因此您需要注意处于不受支持的版本的风险。

答案 1 :(得分:0)

正如医生说的那样 The InitialLoadMode configuration parameter in the class MapStoreConfig has two values: LAZY and EAGER. If InitialLoadMode is set to LAZY, data is not loaded during the map creation. If it is set to EAGER, all the data is loaded while the map is created, and everything becomes ready to use. Also, if you add indices to your map with the MapIndexConfig class or the addIndex method, then InitialLoadMode is overridden and MapStoreConfig behaves as if EAGER mode is on. http://docs.hazelcast.org/docs/3.8/manual/html-single/index.html