Esempio n. 1
0
  public Object putIfAbsent(Data dataKey, Object value, long ttl) {
    checkIfLoaded();
    Record record = records.get(dataKey);
    Object oldValue = null;
    if (record == null) {
      if (mapContainer.getStore() != null) {
        oldValue = mapContainer.getStore().load(mapService.toObject(dataKey));
        if (oldValue != null) {
          record = mapService.createRecord(name, dataKey, oldValue, DEFAULT_TTL);
          records.put(dataKey, record);
          updateSizeEstimator(calculateRecordSize(record));
        }
      }
    } else {
      accessRecord(record);
      oldValue = record.getValue();
    }
    if (oldValue == null) {
      value = mapService.interceptPut(name, null, value);
      value = writeMapStore(dataKey, value, record);
      record = mapService.createRecord(name, dataKey, value, ttl);
      records.put(dataKey, record);
      updateSizeEstimator(calculateRecordSize(record));
      updateTtl(record, ttl);
    }
    saveIndex(record);

    return oldValue;
  }
Esempio n. 2
0
  public boolean set(Data dataKey, Object value, long ttl) {
    checkIfLoaded();
    Record record = records.get(dataKey);
    boolean newRecord = false;
    if (record == null) {
      value = mapService.interceptPut(name, null, value);
      value = writeMapStore(dataKey, value, null);
      record = mapService.createRecord(name, dataKey, value, ttl);
      records.put(dataKey, record);
      updateSizeEstimator(calculateRecordSize(record));
      newRecord = true;
    } else {
      value = mapService.interceptPut(name, record.getValue(), value);
      value = writeMapStore(dataKey, value, record);
      // if key exists before, first reduce size
      updateSizeEstimator(-calculateRecordSize(record));
      setRecordValue(record, value);
      // then increase size.
      updateSizeEstimator(calculateRecordSize(record));
      updateTtl(record, ttl);
    }
    saveIndex(record);

    return newRecord;
  }
Esempio n. 3
0
 public void put(Map.Entry<Data, Object> entry) {
   checkIfLoaded();
   Data dataKey = entry.getKey();
   Object value = entry.getValue();
   Record record = records.get(dataKey);
   if (record == null) {
     value = mapService.interceptPut(name, null, value);
     value = writeMapStore(dataKey, value, null);
     record = mapService.createRecord(name, dataKey, value, DEFAULT_TTL);
     records.put(dataKey, record);
     // increase size.
     updateSizeEstimator(calculateRecordSize(record));
     saveIndex(record);
   } else {
     final Object oldValue = record.getValue();
     value = mapService.interceptPut(name, oldValue, value);
     value = writeMapStore(dataKey, value, record);
     // if key exists before, first reduce size
     updateSizeEstimator(-calculateRecordSize(record));
     setRecordValue(record, value);
     // then increase size
     updateSizeEstimator(calculateRecordSize(record));
     saveIndex(record);
   }
 }
Esempio n. 4
0
 public Object put(Data dataKey, Object value, long ttl) {
   checkIfLoaded();
   Record record = records.get(dataKey);
   Object oldValue = null;
   if (record == null) {
     if (mapContainer.getStore() != null) {
       oldValue = mapContainer.getStore().load(mapService.toObject(dataKey));
     }
     value = mapService.interceptPut(name, null, value);
     value = writeMapStore(dataKey, value, null);
     record = mapService.createRecord(name, dataKey, value, ttl);
     records.put(dataKey, record);
     updateSizeEstimator(calculateRecordSize(record));
     saveIndex(record);
   } else {
     oldValue = record.getValue();
     value = mapService.interceptPut(name, oldValue, value);
     value = writeMapStore(dataKey, value, record);
     // if key exists before, first reduce size
     updateSizeEstimator(-calculateRecordSize(record));
     setRecordValue(record, value);
     // then increase size.
     updateSizeEstimator(calculateRecordSize(record));
     updateTtl(record, ttl);
     saveIndex(record);
   }
   return oldValue;
 }
Esempio n. 5
0
 public Record putBackup(Data key, Object value, long ttl, boolean shouldSchedule) {
   Record record = records.get(key);
   if (record == null) {
     record = mapService.createRecord(name, key, value, ttl, shouldSchedule);
     records.put(key, record);
     updateSizeEstimator(calculateRecordSize(record));
   } else {
     updateSizeEstimator(-calculateRecordSize(record));
     setRecordValue(record, value);
     updateSizeEstimator(calculateRecordSize(record));
   }
   return record;
 }
Esempio n. 6
0
 private Record getRecordInternal(Data dataKey, boolean enableIndex) {
   Record record = null;
   if (mapContainer.getStore() != null) {
     final Object value = mapContainer.getStore().load(mapService.toObject(dataKey));
     if (value != null) {
       record = mapService.createRecord(name, dataKey, value, DEFAULT_TTL);
       records.put(dataKey, record);
       if (enableIndex) {
         saveIndex(record);
       }
       updateSizeEstimator(calculateRecordSize(record));
     }
   }
   return record;
 }
Esempio n. 7
0
 public void putFromLoad(Data dataKey, Object value, long ttl) {
   Record record = records.get(dataKey);
   if (record == null) {
     value = mapService.interceptPut(name, null, value);
     record = mapService.createRecord(name, dataKey, value, ttl);
     records.put(dataKey, record);
     updateSizeEstimator(calculateRecordSize(record));
   } else {
     value = mapService.interceptPut(name, record.getValue(), value);
     updateSizeEstimator(-calculateRecordSize(record));
     setRecordValue(record, value);
     updateSizeEstimator(calculateRecordSize(record));
     updateTtl(record, ttl);
   }
   saveIndex(record);
 }
Esempio n. 8
0
 public MapEntrySet getAll(Set<Data> keySet) {
   checkIfLoaded();
   final MapEntrySet mapEntrySet = new MapEntrySet();
   Map<Object, Data> keyMapForLoader = null;
   if (mapContainer.getStore() != null) {
     keyMapForLoader = new HashMap<Object, Data>();
   }
   for (Data dataKey : keySet) {
     Record record = records.get(dataKey);
     if (record == null) {
       if (mapContainer.getStore() != null) {
         keyMapForLoader.put(mapService.toObject(dataKey), dataKey);
       }
     } else {
       accessRecord(record);
       Object value = record.getValue();
       value = mapService.interceptGet(name, value);
       if (value != null) {
         mapEntrySet.add(new AbstractMap.SimpleImmutableEntry(dataKey, mapService.toData(value)));
       }
     }
   }
   if (mapContainer.getStore() == null || keyMapForLoader.size() == 0) {
     return mapEntrySet;
   }
   final Map<Object, Object> loadedKeys =
       mapContainer.getStore().loadAll(keyMapForLoader.keySet());
   for (Map.Entry entry : loadedKeys.entrySet()) {
     final Object objectKey = entry.getKey();
     Object value = entry.getValue();
     Data dataKey = keyMapForLoader.get(objectKey);
     if (value != null) {
       Record record = mapService.createRecord(name, dataKey, value, DEFAULT_TTL);
       records.put(dataKey, record);
       saveIndex(record);
       updateSizeEstimator(calculateRecordSize(record));
     }
     value = mapService.interceptGet(name, value);
     if (value != null) {
       mapEntrySet.add(new AbstractMap.SimpleImmutableEntry(dataKey, mapService.toData(value)));
     }
   }
   return mapEntrySet;
 }
Esempio n. 9
0
  public boolean containsKey(Data dataKey) {
    checkIfLoaded();
    Record record = records.get(dataKey);
    if (record == null) {
      if (mapContainer.getStore() != null) {
        Object value = mapContainer.getStore().load(mapService.toObject(dataKey));
        if (value != null) {
          record = mapService.createRecord(name, dataKey, value, DEFAULT_TTL);
          records.put(dataKey, record);
          updateSizeEstimator(calculateRecordSize(record));
        }
      }
    }

    boolean contains = record != null;
    if (contains) {
      accessRecord(record);
    }
    return contains;
  }
Esempio n. 10
0
  public boolean tryPut(Data dataKey, Object value, long ttl) {
    checkIfLoaded();
    Record record = records.get(dataKey);
    if (record == null) {
      value = mapService.interceptPut(name, null, value);
      value = writeMapStore(dataKey, value, null);
      record = mapService.createRecord(name, dataKey, value, ttl);
      records.put(dataKey, record);
      updateSizeEstimator(calculateRecordSize(record));
    } else {
      value = mapService.interceptPut(name, record.getValue(), value);
      value = writeMapStore(dataKey, value, record);
      updateSizeEstimator(-calculateRecordSize(record));
      setRecordValue(record, value);
      updateSizeEstimator(calculateRecordSize(record));
      updateTtl(record, ttl);
    }
    saveIndex(record);

    return true;
  }
Esempio n. 11
0
  public Object get(Data dataKey) {
    checkIfLoaded();
    Record record = records.get(dataKey);
    Object value = null;
    if (record == null) {
      if (mapContainer.getStore() != null) {
        value = mapContainer.getStore().load(mapService.toObject(dataKey));
        if (value != null) {
          record = mapService.createRecord(name, dataKey, value, DEFAULT_TTL);
          records.put(dataKey, record);
          saveIndex(record);
          updateSizeEstimator(calculateRecordSize(record));
        }
      }

    } else {
      accessRecord(record);
      value = record.getValue();
    }
    value = mapService.interceptGet(name, value);

    return value;
  }
Esempio n. 12
0
 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;
 }