K
- public class GenericMapStore<K> extends Object implements MapStore<K,GenericRecord>, MapLoaderLifecycleSupport
MapStore
built
on top of Hazelcast SQL engine.
It works with any SQL connector supporting SELECT, INSERT, UPDATE and DELETE statements.
Usage:
First define external data store, e.g. for JDBC use JdbcDataStoreFactory
:
Config config = new Config();
config.addExternalDataStoreConfig(
new ExternalDataStoreConfig("mysql-ref")
.setClassName(JdbcDataStoreFactory.class.getName())
.setProperty("jdbcUrl", dbConnectionUrl)
);
Then create a Map with MapStore
using the GenericMapStore implementation:
MapConfig mapConfig = new MapConfig(mapName);
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName(GenericMapStore.class.getName());
mapStoreConfig.setProperty(OPTION_EXTERNAL_DATASTORE_REF, "mysql-ref");
mapConfig.setMapStoreConfig(mapStoreConfig);
instance().getConfig().addMapConfig(mapConfig);
The GenericMapStore creates a SQL mapping with name "__map-store." + mapName. This mapping is removed when the map is destroyed.
Modifier and Type | Field and Description |
---|---|
static HazelcastProperty |
MAPSTORE_INIT_TIMEOUT
Timeout for initialization of GenericMapStore
|
Constructor and Description |
---|
GenericMapStore() |
Modifier and Type | Method and Description |
---|---|
void |
delete(K key)
Deletes the entry with a given key from the store.
|
void |
deleteAll(Collection<K> keys)
Deletes multiple entries from the store.
|
void |
destroy()
Hazelcast will call this method before shutting down.
|
void |
init(HazelcastInstance instance,
Properties properties,
String mapName)
Initializes this MapLoader implementation.
|
GenericRecord |
load(K key)
Loads the value of a given key.
|
Map<K,GenericRecord> |
loadAll(Collection<K> keys)
Size of the
keys collection is limited by ClusterProperty.MAP_LOAD_CHUNK_SIZE |
Iterable<K> |
loadAllKeys()
Loads all of the keys from the store.
|
void |
store(K key,
GenericRecord record)
Stores the key-value pair.
|
void |
storeAll(Map<K,GenericRecord> map)
Stores multiple entries.
|
public static final HazelcastProperty MAPSTORE_INIT_TIMEOUT
public void init(HazelcastInstance instance, Properties properties, String mapName)
MapLoaderLifecycleSupport
MapLoader
is configured, can be obtained from the
hazelcastInstance
in this method's implementation.
On members joining a cluster, this method is executed during finalization
of the join operation, therefore care should be taken to adhere to the
rules for PostJoinAwareService.getPostJoinOperation()
.
If the implementation executes operations which may wait on locks or otherwise
block (e.g. waiting for network operations), this may result in a time-out and
obstruct the new member from joining the cluster. If blocking operations are
required for initialization of the MapLoader
, consider deferring them
with a lazy initialization scheme.
init
in interface MapLoaderLifecycleSupport
instance
- HazelcastInstance of this mapLoader.properties
- Properties set for this mapStore. see MapStoreConfigmapName
- name of the map.public void destroy()
MapLoaderLifecycleSupport
destroy
in interface MapLoaderLifecycleSupport
public GenericRecord load(K key)
MapLoader
load
in interface MapLoader<K,GenericRecord>
null
value signals value missing in the underlying storepublic Map<K,GenericRecord> loadAll(Collection<K> keys)
keys
collection is limited by ClusterProperty.MAP_LOAD_CHUNK_SIZE
loadAll
in interface MapLoader<K,GenericRecord>
keys
- keys of the values entries to loadpublic Iterable<K> loadAllKeys()
MapLoader
Iterable
may return the keys lazily
by loading them in batches. The Iterator
of this Iterable
may implement the
Closeable
interface in which case it will be closed once iteration is over.
This is intended for releasing resources such as closing a JDBC result set.
The returned Iterable should not contain any null
keys.
loadAllKeys
in interface MapLoader<K,GenericRecord>
null
.public void store(K key, GenericRecord record)
MapStore
If an exception is thrown then the put operation will fail.
store
in interface MapStore<K,GenericRecord>
key
- key of the entry to storerecord
- value of the entry to storepublic void storeAll(Map<K,GenericRecord> map)
MapStore
Implementation of this method can optimize the store operation by storing all entries in one database connection. storeAll() is used when writeDelaySeconds is positive (write-behind).If an exception is thrown, the entries will try to be stored one by one using the store() method.
Note: on the retry phase only entries left in the map will be stored one-by-one. In this way a MapStore implementation can handle partial storeAll() cases when some entries were stored successfully before a failure happens. Entries removed from the map will be not passed to subsequent call to store() method any more.
storeAll
in interface MapStore<K,GenericRecord>
map
- map of entries to storepublic void delete(K key)
MapStore
If an exception is thrown the remove operation will fail.
delete
in interface MapStore<K,GenericRecord>
key
- the key to delete from the storepublic void deleteAll(Collection<K> keys)
MapStore
If an exception is thrown the entries will try to be deleted one by one using the delete() method.
Note: on the retry phase only entries left in the keys will be deleted one-by-one. In this way a MapStore implementation can handle partial deleteAll() cases when some entries were deleted successfully before a failure happens. Entries removed from the keys will be not passed to subsequent call to delete() method any more. The intended usage is to delete items from the provided collection as they are deleted from the store.
deleteAll
in interface MapStore<K,GenericRecord>
keys
- the keys of the entries to delete.Copyright © 2022 Hazelcast, Inc.. All rights reserved.