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;
  }
  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;
  }
 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;
 }
 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);
   }
 }
 public boolean remove(Data dataKey, Object testValue) {
   checkIfLoaded();
   Record record = records.get(dataKey);
   Object oldValue = null;
   boolean removed = false;
   if (record == null) {
     if (mapContainer.getStore() != null) {
       oldValue = mapContainer.getStore().load(mapService.toObject(dataKey));
     }
     if (oldValue == null) return false;
   } else {
     oldValue = record.getValue();
   }
   if (mapService.compare(name, testValue, oldValue)) {
     mapService.interceptRemove(name, oldValue);
     removeIndex(dataKey);
     mapStoreDelete(record, dataKey);
     // reduce size
     updateSizeEstimator(-calculateRecordSize(record));
     deleteRecord(dataKey);
     cancelAssociatedSchedulers(dataKey);
     removed = true;
   }
   return removed;
 }
 public Object remove(Data dataKey) {
   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) {
         removeIndex(dataKey);
         mapStoreDelete(null, dataKey);
       }
     }
   } else {
     oldValue = record.getValue();
     oldValue = mapService.interceptRemove(name, oldValue);
     if (oldValue != null) {
       removeIndex(dataKey);
       mapStoreDelete(record, dataKey);
     }
     // reduce size
     updateSizeEstimator(-calculateRecordSize(record));
     deleteRecord(dataKey);
     cancelAssociatedSchedulers(dataKey);
   }
   return oldValue;
 }
Beispiel #7
0
 public V get(Object k) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   Data key = service.toData(k, partitionStrategy);
   return (V) service.toObject(getInternal(key));
 }
Beispiel #8
0
 public Object executeOnKey(K key, EntryProcessor entryProcessor) {
   if (key == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   return service.toObject(
       executeOnKeyInternal(service.toData(key, partitionStrategy), entryProcessor));
 }
 @Override
 public final void beforeRun() throws Exception {
   mapService = getService();
   mapContainer = mapService.getMapServiceContext().getMapContainer(name);
   partitionContainer = mapService.getMapServiceContext().getPartitionContainer(getPartitionId());
   recordStore = partitionContainer.getRecordStore(name);
   innerBeforeRun();
 }
Beispiel #10
0
 @Override
 public void submitToKey(K key, EntryProcessor entryProcessor, ExecutionCallback callback) {
   if (key == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   Data keyData = service.toData(key, partitionStrategy);
   executeOnKeyInternal(keyData, entryProcessor, callback);
 }
Beispiel #11
0
 public V remove(Object k) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data result = removeInternal(key);
   return (V) service.toObject(result);
 }
Beispiel #12
0
 public static void fireEvent(Data key, Object value, String mapName, MapService mapService) {
   final NodeEngine nodeEngine = mapService.getNodeEngine();
   mapService.publishEvent(
       nodeEngine.getThisAddress(),
       mapName,
       EntryEventType.EVICTED,
       key,
       mapService.toData(value),
       null);
 }
Beispiel #13
0
 @Override
 public Future submitToKey(K key, EntryProcessor entryProcessor) {
   if (key == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   Data keyData = service.toData(key, partitionStrategy);
   Future f = executeOnKeyInternal(keyData, entryProcessor, null);
   return new DelegatingFuture(f, service.getSerializationService());
 }
 private Operation createExpirationOperation(int expirationPercentage, int partitionId) {
   final MapService mapService = this.mapService;
   final ClearExpiredOperation clearExpiredOperation =
       new ClearExpiredOperation(expirationPercentage);
   clearExpiredOperation
       .setNodeEngine(mapService.getNodeEngine())
       .setCallerUuid(mapService.getNodeEngine().getLocalMember().getUuid())
       .setPartitionId(partitionId)
       .setValidateTarget(false)
       .setService(mapService);
   return clearExpiredOperation;
 }
Beispiel #15
0
 public Map<K, V> getAll(final Set<K> keys) {
   Set<Data> ks = new HashSet(keys.size());
   MapService service = getService();
   for (K key : keys) {
     if (key == null) {
       throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
     }
     Data k = service.toData(key, partitionStrategy);
     ks.add(k);
   }
   return (Map<K, V>) getAllObjectInternal(ks);
 }
Beispiel #16
0
 public boolean tryPut(final K k, final V v, final long timeout, final TimeUnit timeunit) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data value = service.toData(v);
   return tryPutInternal(key, value, timeout, timeunit);
 }
Beispiel #17
0
 public boolean remove(final Object k, final Object v) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data value = service.toData(v);
   return removeInternal(key, value);
 }
Beispiel #18
0
 public void putTransient(final K k, final V v, final long ttl, final TimeUnit timeunit) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data value = service.toData(v);
   putTransientInternal(key, value, ttl, timeunit);
 }
Beispiel #19
0
 public V replace(final K k, final V v) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data value = service.toData(v);
   return (V) service.toObject(replaceInternal(key, value));
 }
 @Override
 protected Object reduce(Map<Integer, Object> map) {
   MapEntrySet resultSet = new MapEntrySet();
   MapService mapService = getService();
   for (Map.Entry<Integer, Object> entry : map.entrySet()) {
     MapEntrySet mapEntrySet = (MapEntrySet) mapService.toObject(entry.getValue());
     Set<Map.Entry<Data, Data>> entrySet = mapEntrySet.getEntrySet();
     for (Map.Entry<Data, Data> dataEntry : entrySet) {
       resultSet.add(dataEntry);
     }
   }
   return resultSet;
 }
 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);
   }
 }
Beispiel #22
0
 public Future putAsync(final K key, final V value, final long ttl, final TimeUnit timeunit) {
   if (key == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (value == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   Data k = service.toData(key, partitionStrategy);
   Data v = service.toData(value);
   return new DelegatingFuture<V>(
       putAsyncInternal(k, v, ttl, timeunit), getNodeEngine().getSerializationService());
 }
Beispiel #23
0
 public V putIfAbsent(final K k, final V v, final long ttl, final TimeUnit timeunit) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data value = service.toData(v);
   final Data result = putIfAbsentInternal(key, value, ttl, timeunit);
   return (V) service.toObject(result);
 }
 @Override
 protected Object reduce(Map<Integer, Object> map) {
   MapEntrySet result = new MapEntrySet();
   MapService mapService = getService();
   for (Object o : map.values()) {
     if (o != null) {
       MapEntrySet entrySet = (MapEntrySet) mapService.toObject(o);
       Set<Map.Entry<Data, Data>> entries = entrySet.getEntrySet();
       for (Map.Entry<Data, Data> entry : entries) {
         result.add(entry);
       }
     }
   }
   return result;
 }
 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;
 }
 @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 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);
 }
 private void mapStoreDelete(Record record, Data key) {
   final MapStoreWrapper store = mapContainer.getStore();
   if (store != null) {
     long writeDelayMillis = mapContainer.getWriteDelayMillis();
     if (writeDelayMillis == 0) {
       store.delete(mapService.toObject(key));
       // todo ea record will be deleted then why calling onStore
       if (record != null) {
         record.onStore();
       }
     } else {
       mapService.scheduleMapStoreDelete(name, key, writeDelayMillis);
       toBeRemovedKeys.add(key);
     }
   }
 }
 private void updateTtl(Record record, long ttl) {
   if (ttl > 0) {
     mapService.scheduleTtlEviction(name, record, ttl);
   } else if (ttl == 0) {
     mapContainer.getTtlEvictionScheduler().cancel(record.getKey());
   }
 }
Beispiel #30
0
 public boolean replace(final K k, final V o, final V v) {
   if (k == null) {
     throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED);
   }
   if (o == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   if (v == null) {
     throw new NullPointerException(NULL_VALUE_IS_NOT_ALLOWED);
   }
   MapService service = getService();
   final Data key = service.toData(k, partitionStrategy);
   final Data oldValue = service.toData(o);
   final Data value = service.toData(v);
   return replaceInternal(key, oldValue, value);
 }