public static boolean evictIfNotLocked(Data key, RecordStore recordStore) { if (recordStore.isLocked(key)) { return false; } recordStore.evict(key); return true; }
private boolean isContainerEmpty(PartitionContainer container) { long size = 0L; final ConcurrentMap<String, RecordStore> maps = container.getMaps(); for (RecordStore store : maps.values()) { size += store.size(); if (size > 0L) { return false; } } return true; }
public void run() { MapService mapService = getService(); int partitionId = getPartitionId(); RecordStore recordStore = mapService.getRecordStore(partitionId, name); Record record = recordStore.getRecord(dataKey); if (record != null) { updateSizeEstimator(-calculateRecordSize(record)); recordStore.deleteRecord(dataKey); } if (unlockKey) { recordStore.forceUnlock(dataKey); } }
@Override public void run() throws Exception { final List<Data> keyValueSequence = this.keyValueSequence; if (keyValueSequence == null || keyValueSequence.isEmpty()) { return; } final int partitionId = getPartitionId(); final MapService mapService = this.mapService; final RecordStore recordStore = mapService.getRecordStore(partitionId, name); for (int i = 0; i < keyValueSequence.size(); i += 2) { final Data key = keyValueSequence.get(i); final Data value = keyValueSequence.get(i + 1); final Object object = mapService.toObject(value); recordStore.putFromLoad(key, object); } }
public void run() { backupRecordInfos = new ArrayList<RecordInfo>(); backupEntrySet = new ArrayList<Map.Entry<Data, Data>>(); int partitionId = getPartitionId(); final MapServiceContext mapServiceContext = mapService.getMapServiceContext(); RecordStore recordStore = mapServiceContext.getRecordStore(partitionId, name); Set<Map.Entry<Data, Data>> entries = entrySet.getEntrySet(); InternalPartitionService partitionService = getNodeEngine().getPartitionService(); Set<Data> keysToInvalidate = new HashSet<Data>(); for (Map.Entry<Data, Data> entry : entries) { Data dataKey = entry.getKey(); Data dataValue = entry.getValue(); if (partitionId == partitionService.getPartitionId(dataKey)) { Data dataOldValue = null; if (initialLoad) { recordStore.putFromLoad(dataKey, dataValue, -1); } else { dataOldValue = mapServiceContext.toData(recordStore.put(dataKey, dataValue, -1)); } mapServiceContext.interceptAfterPut(name, dataValue); EntryEventType eventType = dataOldValue == null ? EntryEventType.ADDED : EntryEventType.UPDATED; final MapEventPublisher mapEventPublisher = mapServiceContext.getMapEventPublisher(); mapEventPublisher.publishEvent( getCallerAddress(), name, eventType, dataKey, dataOldValue, dataValue); keysToInvalidate.add(dataKey); if (mapContainer.getWanReplicationPublisher() != null && mapContainer.getWanMergePolicy() != null) { Record record = recordStore.getRecord(dataKey); final Data dataValueAsData = mapServiceContext.toData(dataValue); final EntryView entryView = EntryViews.createSimpleEntryView(dataKey, dataValueAsData, record); mapEventPublisher.publishWanReplicationUpdate(name, entryView); } backupEntrySet.add(entry); RecordInfo replicationInfo = Records.buildRecordInfo(recordStore.getRecord(dataKey)); backupRecordInfos.add(replicationInfo); } } invalidateNearCaches(keysToInvalidate); }
public static void removeEvictableRecords( final RecordStore recordStore, final MapConfig mapConfig, final MapService mapService) { final int partitionSize = recordStore.size(); if (partitionSize < 1) { return; } final int evictableSize = getEvictableSize(partitionSize, mapConfig, mapService); if (evictableSize < 1) { return; } final MapConfig.EvictionPolicy evictionPolicy = mapConfig.getEvictionPolicy(); final Map<Data, Record> entries = recordStore.getReadonlyRecordMap(); final int size = entries.size(); // size have a tendency to change to here so check again. if (entries.isEmpty()) { return; } // criteria is a long value, like last access times or hits, // used for calculating LFU or LRU. final long[] criterias = new long[size]; int index = 0; for (final Record record : entries.values()) { criterias[index] = getEvictionCriteriaValue(record, evictionPolicy); index++; // in case size may change (increase or decrease) when iterating. if (index == size) { break; } } if (criterias.length == 0) { return; } // just in case there may be unassigned indexes in criterias array due to size variances // assign them to Long.MAX_VALUE so when sorting asc they will locate // in the upper array indexes and we wont care about them. if (index < criterias.length) { for (int i = index; i < criterias.length; i++) { criterias[i] = Long.MAX_VALUE; } } Arrays.sort(criterias); // check in case record store size may be smaller than evictable size. final int evictableBaseIndex = index == 0 ? index : Math.min(evictableSize, index - 1); final long criteriaValue = criterias[evictableBaseIndex]; int evictedRecordCounter = 0; for (final Map.Entry<Data, Record> entry : entries.entrySet()) { final Record record = entry.getValue(); final long value = getEvictionCriteriaValue(record, evictionPolicy); if (value <= criteriaValue) { final Data tmpKey = record.getKey(); final Object tmpValue = record.getValue(); if (evictIfNotLocked(tmpKey, recordStore)) { evictedRecordCounter++; final String mapName = mapConfig.getName(); mapService.interceptAfterRemove(mapName, value); if (mapService.isNearCacheAndInvalidationEnabled(mapName)) { mapService.invalidateAllNearCaches(mapName, tmpKey); } fireEvent(tmpKey, tmpValue, mapName, mapService); } } if (evictedRecordCounter >= evictableSize) { break; } } }
private boolean expirable(RecordStore recordStore) { return recordStore.isExpirable(); }