Example #1
0
  @Override
  public boolean merge(Data key, EntryView mergingEntry, MapMergePolicy mergePolicy) {
    checkIfLoaded();
    final long now = getNow();

    Record record = getRecordOrNull(key, now, false);
    mergingEntry =
        EntryViews.convertToLazyEntryView(mergingEntry, serializationService, mergePolicy);
    Object newValue;
    if (record == null) {
      final Object notExistingKey = mapServiceContext.toObject(key);
      final EntryView nullEntryView = EntryViews.createNullEntryView(notExistingKey);
      newValue = mergePolicy.merge(name, mergingEntry, nullEntryView);
      if (newValue == null) {
        return false;
      }
      newValue = mapDataStore.add(key, newValue, now);
      record = createRecord(key, newValue, now);
      mergeRecordExpiration(record, mergingEntry);
      records.put(key, record);
      updateSizeEstimator(calculateRecordHeapCost(record));
    } else {
      Object oldValue = record.getValue();
      EntryView existingEntry =
          EntryViews.createLazyEntryView(
              record.getKey(), record.getValue(), record, serializationService, mergePolicy);
      newValue = mergePolicy.merge(name, mergingEntry, existingEntry);
      // existing entry will be removed
      if (newValue == null) {
        removeIndex(key);
        mapDataStore.remove(key, now);
        onStore(record);
        // reduce size.
        updateSizeEstimator(-calculateRecordHeapCost(record));
        // remove from map & invalidate.
        deleteRecord(key);
        return true;
      }
      if (newValue == mergingEntry.getValue()) {
        mergeRecordExpiration(record, mergingEntry);
      }
      // same with the existing entry so no need to map-store etc operations.
      if (mapServiceContext.compare(name, newValue, oldValue)) {
        return true;
      }
      newValue = mapDataStore.add(key, newValue, now);
      onStore(record);
      updateSizeEstimator(-calculateRecordHeapCost(record));
      recordFactory.setValue(record, newValue);
      updateSizeEstimator(calculateRecordHeapCost(record));
    }
    saveIndex(record);
    return newValue != null;
  }
 public boolean merge(Data dataKey, EntryView mergingEntry, MapMergePolicy mergePolicy) {
   checkIfLoaded();
   Record record = records.get(dataKey);
   Object newValue = null;
   if (record == null) {
     newValue = mergingEntry.getValue();
     newValue = writeMapStore(dataKey, newValue, null);
     record = mapService.createRecord(name, dataKey, newValue, DEFAULT_TTL);
     records.put(dataKey, record);
     updateSizeEstimator(calculateRecordSize(record));
   } else {
     Object oldValue = record.getValue();
     EntryView existingEntry =
         mapService.createSimpleEntryView(
             mapService.toObject(record.getKey()), mapService.toObject(record.getValue()), record);
     newValue = mergePolicy.merge(name, mergingEntry, existingEntry);
     if (newValue == null) { // existing entry will be removed
       removeIndex(dataKey);
       mapStoreDelete(record, dataKey);
       // reduce size.
       updateSizeEstimator(-calculateRecordSize(record));
       // remove from map & invalidate.
       deleteRecord(dataKey);
       return true;
     }
     // same with the existing entry so no need to mapstore etc operations.
     if (mapService.compare(name, newValue, oldValue)) {
       return true;
     }
     newValue = writeMapStore(dataKey, newValue, record);
     updateSizeEstimator(-calculateRecordSize(record));
     recordFactory.setValue(record, newValue);
     updateSizeEstimator(calculateRecordSize(record));
   }
   saveIndex(record);
   return newValue != null;
 }