Search in sources :

Example 1 with MetadataAwareValue

use of com.hazelcast.map.EntryLoader.MetadataAwareValue in project hazelcast by hazelcast.

the class EntryRecordStoreLoader method getLoadingSequence.

/**
 * Transforms a map to a list of serialised key-value-expirationTime sequences.
 *
 * @param entries the map to be transformed
 * @return the list of serialised alternating key-value pairs
 */
protected List<Data> getLoadingSequence(Map<?, ?> entries) {
    List<Data> keyValueSequence = new ArrayList<>(entries.size() * 2);
    for (Map.Entry<?, ?> entry : entries.entrySet()) {
        Object key = entry.getKey();
        MetadataAwareValue loaderEntry = (MetadataAwareValue) entry.getValue();
        Object value = loaderEntry.getValue();
        long expirationTime = loaderEntry.getExpirationTime();
        Data dataKey = mapServiceContext.toData(key);
        Data dataValue = mapServiceContext.toData(value);
        Data dataExpirationTime = mapServiceContext.toData(expirationTime);
        keyValueSequence.add(dataKey);
        keyValueSequence.add(dataValue);
        keyValueSequence.add(dataExpirationTime);
    }
    return keyValueSequence;
}
Also used : MetadataAwareValue(com.hazelcast.map.EntryLoader.MetadataAwareValue) ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) Map(java.util.Map)

Example 2 with MetadataAwareValue

use of com.hazelcast.map.EntryLoader.MetadataAwareValue in project hazelcast by hazelcast.

the class DefaultRecordStore method loadValueOf.

/**
 * Loads value of key. If necessary loads it by
 * extracting from {@link MetadataAwareValue}
 *
 * @return loaded value from map-store,
 * when no value found returns null
 */
private Object loadValueOf(Data key) {
    Object value = mapDataStore.load(key);
    if (value == null) {
        return null;
    }
    if (mapDataStore.isWithExpirationTime()) {
        MetadataAwareValue loaderEntry = (MetadataAwareValue) value;
        long proposedTtl = expirationTimeToTtl(loaderEntry.getExpirationTime());
        if (proposedTtl <= 0) {
            return null;
        }
        value = loaderEntry.getValue();
    }
    return value;
}
Also used : MetadataAwareValue(com.hazelcast.map.EntryLoader.MetadataAwareValue)

Example 3 with MetadataAwareValue

use of com.hazelcast.map.EntryLoader.MetadataAwareValue in project hazelcast by hazelcast.

the class DefaultRecordStore method loadRecordOrNull.

@Override
public Record loadRecordOrNull(Data key, boolean backup, Address callerAddress) {
    Object value = mapDataStore.load(key);
    if (value == null) {
        return null;
    }
    long ttl = UNSET;
    if (mapDataStore.isWithExpirationTime()) {
        MetadataAwareValue loaderEntry = (MetadataAwareValue) value;
        long proposedTtl = expirationTimeToTtl(loaderEntry.getExpirationTime());
        if (proposedTtl <= 0) {
            return null;
        }
        value = loaderEntry.getValue();
        ttl = proposedTtl;
    }
    long now = getNow();
    Record record = putNewRecord(key, null, value, ttl, UNSET, UNSET, now, null, LOADED, false, backup);
    if (!backup && mapEventPublisher.hasEventListener(name)) {
        mapEventPublisher.publishEvent(callerAddress, name, EntryEventType.LOADED, key, null, record.getValue(), null);
    }
    evictEntries(key);
    // otherwise query-caches cannot see loaded entries
    if (!backup && hasQueryCache()) {
        addEventToQueryCache(key, record);
    }
    return record;
}
Also used : MetadataAwareValue(com.hazelcast.map.EntryLoader.MetadataAwareValue) Record(com.hazelcast.map.impl.record.Record)

Example 4 with MetadataAwareValue

use of com.hazelcast.map.EntryLoader.MetadataAwareValue in project hazelcast by hazelcast.

the class DefaultRecordStore method loadEntries.

private Map<Data, Object> loadEntries(Set<Data> keys, Address callerAddress) {
    Map loadedEntries = mapDataStore.loadAll(keys);
    if (isNullOrEmpty(loadedEntries)) {
        return Collections.emptyMap();
    }
    // holds serialized keys and if values are
    // serialized, also holds them in serialized format.
    Map<Data, Object> resultMap = createHashMap(loadedEntries.size());
    // add loaded key-value pairs to this record-store.
    Set entrySet = loadedEntries.entrySet();
    for (Object object : entrySet) {
        Map.Entry entry = (Map.Entry) object;
        Data key = toData(entry.getKey());
        Object value = entry.getValue();
        if (mapDataStore.isWithExpirationTime()) {
            MetadataAwareValue loaderEntry = (MetadataAwareValue) value;
            if (expirationTimeToTtl(loaderEntry.getExpirationTime()) > 0) {
                resultMap.put(key, loaderEntry.getValue());
            }
            putFromLoad(key, loaderEntry.getValue(), loaderEntry.getExpirationTime(), callerAddress);
        } else {
            resultMap.put(key, value);
            putFromLoad(key, value, callerAddress);
        }
    }
    if (hasQueryCache()) {
        for (Data key : resultMap.keySet()) {
            Record record = storage.get(key);
            // here we are only publishing events for loaded
            // entries. This is required for notifying query-caches
            // otherwise query-caches cannot see loaded entries
            addEventToQueryCache(key, record);
        }
    }
    return resultMap;
}
Also used : DelayedEntry(com.hazelcast.map.impl.mapstore.writebehind.entry.DelayedEntry) MergingValueFactory.createMergingEntry(com.hazelcast.spi.impl.merge.MergingValueFactory.createMergingEntry) Set(java.util.Set) MetadataAwareValue(com.hazelcast.map.EntryLoader.MetadataAwareValue) EntryEventData(com.hazelcast.map.impl.event.EntryEventData) Data(com.hazelcast.internal.serialization.Data) Record(com.hazelcast.map.impl.record.Record) Map(java.util.Map) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap)

Example 5 with MetadataAwareValue

use of com.hazelcast.map.EntryLoader.MetadataAwareValue in project hazelcast by hazelcast.

the class WriteBehindStore method loadAll.

/**
 * {@inheritDoc}
 * The method first checks if some of the keys to be loaded
 * have entries that are staged to be persisted to the
 * underlying store and returns those values instead of loading
 * the values from the store.
 * The keys which don't have staged entries to be persisted will
 * be loaded from the underlying store.
 *
 * @see MapLoader#loadAll(Collection)
 */
@Override
public Map loadAll(Collection keys) {
    if (keys == null || keys.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<Object, Object> map = createHashMap(keys.size());
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Object key = iterator.next();
        Data dataKey = toHeapData(key);
        DelayedEntry delayedEntry = getFromStagingArea(dataKey);
        if (delayedEntry != null) {
            Object value = delayedEntry.getValue();
            if (value != null) {
                if (isWithExpirationTime()) {
                    map.put(dataKey, new MetadataAwareValue(toObject(value), delayedEntry.getExpirationTime()));
                } else {
                    map.put(dataKey, toObject(value));
                }
            }
            iterator.remove();
        }
    }
    map.putAll(super.loadAll(keys));
    return map;
}
Also used : MetadataAwareValue(com.hazelcast.map.EntryLoader.MetadataAwareValue) Iterator(java.util.Iterator) Data(com.hazelcast.internal.serialization.Data) DelayedEntry(com.hazelcast.map.impl.mapstore.writebehind.entry.DelayedEntry) DelayedEntries.newAddedDelayedEntry(com.hazelcast.map.impl.mapstore.writebehind.entry.DelayedEntries.newAddedDelayedEntry)

Aggregations

MetadataAwareValue (com.hazelcast.map.EntryLoader.MetadataAwareValue)7 Data (com.hazelcast.internal.serialization.Data)3 DelayedEntry (com.hazelcast.map.impl.mapstore.writebehind.entry.DelayedEntry)2 Record (com.hazelcast.map.impl.record.Record)2 Map (java.util.Map)2 MapUtil.createHashMap (com.hazelcast.internal.util.MapUtil.createHashMap)1 EntryEventData (com.hazelcast.map.impl.event.EntryEventData)1 DelayedEntries.newAddedDelayedEntry (com.hazelcast.map.impl.mapstore.writebehind.entry.DelayedEntries.newAddedDelayedEntry)1 MergingValueFactory.createMergingEntry (com.hazelcast.spi.impl.merge.MergingValueFactory.createMergingEntry)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Test (org.junit.Test)1