Beispiel #1
0
 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
   LockRequest request =
       new LockRequest(
           getKeyData(), ThreadUtil.getThreadId(), Long.MAX_VALUE, getTimeInMillis(time, unit));
   Boolean result = invoke(request);
   return result;
 }
 @Override
 public boolean evict(K key) {
   final Data keyData = toData(key);
   MapEvictRequest request = new MapEvictRequest(name, keyData, ThreadUtil.getThreadId());
   Boolean result = invoke(request);
   return result;
 }
 private Object invokeOperation(Data key, MapOperation operation) {
   int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
   operation.setThreadId(ThreadUtil.getThreadId());
   try {
     Object result;
     if (statisticsEnabled) {
       long time = System.currentTimeMillis();
       Future future =
           operationService
               .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
               .setResultDeserialized(false)
               .invoke();
       result = future.get();
       mapServiceContext.incrementOperationStats(time, localMapStats, name, operation);
     } else {
       Future future =
           operationService
               .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
               .setResultDeserialized(false)
               .invoke();
       result = future.get();
     }
     return result;
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
 @Override
 public V replace(K key, V value) {
   final Data keyData = toData(key);
   final Data valueData = toData(value);
   MapReplaceRequest request =
       new MapReplaceRequest(name, keyData, valueData, ThreadUtil.getThreadId());
   return invoke(request, keyData);
 }
  public void unlock(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    ClientMessage request =
        MultiMapUnlockCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    invoke(request, keyData);
  }
 @Override
 public void lock(K key, long leaseTime, TimeUnit timeUnit) {
   final Data keyData = toData(key);
   MapLockRequest request =
       new MapLockRequest(
           name, keyData, ThreadUtil.getThreadId(), getTimeInMillis(leaseTime, timeUnit), -1);
   invoke(request, keyData);
 }
 @Override
 public boolean tryRemove(K key, long timeout, TimeUnit timeunit) {
   final Data keyData = toData(key);
   MapTryRemoveRequest request =
       new MapTryRemoveRequest(
           name, keyData, ThreadUtil.getThreadId(), timeunit.toMillis(timeout));
   Boolean result = invoke(request, keyData);
   return result;
 }
 @Override
 public void set(K key, V value, long ttl, TimeUnit timeunit) {
   final Data keyData = toData(key);
   final Data valueData = toData(value);
   MapSetRequest request =
       new MapSetRequest(
           name, keyData, valueData, ThreadUtil.getThreadId(), getTimeInMillis(ttl, timeunit));
   invoke(request, keyData);
 }
 @Override
 public boolean remove(Object key, Object value) {
   final Data keyData = toData(key);
   final Data valueData = toData(value);
   MapRemoveIfSameRequest request =
       new MapRemoveIfSameRequest(name, keyData, valueData, ThreadUtil.getThreadId());
   Boolean result = invoke(request, keyData);
   return result;
 }
  public void lock(K key, long leaseTime, TimeUnit timeUnit) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    checkPositive(leaseTime, "leaseTime should be positive");

    final Data keyData = toData(key);
    ClientMessage request =
        MultiMapLockCodec.encodeRequest(
            name, keyData, ThreadUtil.getThreadId(), getTimeInMillis(leaseTime, timeUnit));
    invoke(request, keyData);
  }
 @Override
 public boolean replace(K key, V oldValue, V newValue) {
   final Data keyData = toData(key);
   final Data oldValueData = toData(oldValue);
   final Data newValueData = toData(newValue);
   MapReplaceIfSameRequest request =
       new MapReplaceIfSameRequest(
           name, keyData, oldValueData, newValueData, ThreadUtil.getThreadId());
   Boolean result = invoke(request, keyData);
   return result;
 }
  public int valueCount(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    ClientMessage request =
        MultiMapValueCountCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapValueCountCodec.ResponseParameters resultParameters =
        MultiMapValueCountCodec.decodeResponse(response);
    return resultParameters.response;
  }
 protected Object getInternal(Data key) {
   // todo action for read-backup true is not well tested.
   if (getMapConfig().isReadBackupData()) {
     Object fromBackup = readBackupDataOrNull(key);
     if (fromBackup != null) {
       return fromBackup;
     }
   }
   MapOperation operation = operationProvider.createGetOperation(name, key);
   operation.setThreadId(ThreadUtil.getThreadId());
   return invokeOperation(key, operation);
 }
 protected EntryView getEntryViewInternal(Data key) {
   int partitionId = partitionService.getPartitionId(key);
   MapOperation operation = operationProvider.createGetEntryViewOperation(name, key);
   operation.setThreadId(ThreadUtil.getThreadId());
   operation.setServiceName(SERVICE_NAME);
   try {
     Future future = operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
     return (EntryView) toObject(future.get());
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
 protected ICompletableFuture<Data> setAsyncInternal(
     Data key, Data value, long ttl, TimeUnit timeunit) {
   int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
   MapOperation operation =
       operationProvider.createSetOperation(name, key, value, getTimeInMillis(ttl, timeunit));
   operation.setThreadId(ThreadUtil.getThreadId());
   try {
     return operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
 @Override
 public Future<V> removeAsync(final K key) {
   final Data keyData = toData(key);
   MapRemoveRequest request = new MapRemoveRequest(name, keyData, ThreadUtil.getThreadId());
   try {
     final ICompletableFuture future =
         getContext().getInvocationService().invokeOnKeyOwner(request, keyData);
     return new DelegatingFuture<V>(future, getContext().getSerializationService());
   } catch (Exception e) {
     throw ExceptionUtil.rethrow(e);
   }
 }
 protected boolean containsKeyInternal(Data key) {
   int partitionId = partitionService.getPartitionId(key);
   MapOperation containsKeyOperation = operationProvider.createContainsKeyOperation(name, key);
   containsKeyOperation.setThreadId(ThreadUtil.getThreadId());
   containsKeyOperation.setServiceName(SERVICE_NAME);
   try {
     Future future =
         operationService.invokeOnPartition(SERVICE_NAME, containsKeyOperation, partitionId);
     return (Boolean) toObject(future.get());
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
 @Override
 public boolean tryLock(K key, long time, TimeUnit timeunit) throws InterruptedException {
   final Data keyData = toData(key);
   MapLockRequest request =
       new MapLockRequest(
           name,
           keyData,
           ThreadUtil.getThreadId(),
           Long.MAX_VALUE,
           getTimeInMillis(time, timeunit));
   Boolean result = invoke(request, keyData);
   return result;
 }
  public boolean put(K key, V value) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
    checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    Data valueData = toData(value);
    ClientMessage request =
        MultiMapPutCodec.encodeRequest(name, keyData, valueData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapPutCodec.ResponseParameters resultParameters =
        MultiMapPutCodec.decodeResponse(response);
    return resultParameters.response;
  }
 public Data executeOnKeyInternal(Data key, EntryProcessor entryProcessor) {
   int partitionId = partitionService.getPartitionId(key);
   MapOperation operation = operationProvider.createEntryOperation(name, key, entryProcessor);
   operation.setThreadId(ThreadUtil.getThreadId());
   try {
     Future future =
         operationService
             .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
             .setResultDeserialized(false)
             .invoke();
     return (Data) future.get();
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
 @Override
 public Future<V> putAsync(final K key, final V value, final long ttl, final TimeUnit timeunit) {
   final Data keyData = toData(key);
   final Data valueData = toData(value);
   MapPutRequest request =
       new MapPutRequest(
           name, keyData, valueData, ThreadUtil.getThreadId(), getTimeInMillis(ttl, timeunit));
   try {
     final ICompletableFuture future =
         getContext().getInvocationService().invokeOnKeyOwner(request, keyData);
     return new DelegatingFuture<V>(future, getContext().getSerializationService());
   } catch (Exception e) {
     throw ExceptionUtil.rethrow(e);
   }
 }
  public Collection<V> get(K key) {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    Data keyData = toData(key);
    ClientMessage request = MultiMapGetCodec.encodeRequest(name, keyData, ThreadUtil.getThreadId());
    ClientMessage response = invoke(request, keyData);
    MultiMapGetCodec.ResponseParameters resultParameters =
        MultiMapGetCodec.decodeResponse(response);
    Collection<Data> result = resultParameters.list;
    Collection<V> resultCollection = new ArrayList<V>(result.size());
    for (Data data : result) {
      final V value = toObject(data);
      resultCollection.add(value);
    }
    return resultCollection;
  }
  public boolean tryLock(K key, long time, TimeUnit timeunit, long leaseTime, TimeUnit leaseUnit)
      throws InterruptedException {
    checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);

    final Data keyData = toData(key);
    long timeoutInMillis = getTimeInMillis(time, timeunit);
    long leaseTimeInMillis = getTimeInMillis(leaseTime, leaseUnit);

    long threadId = ThreadUtil.getThreadId();
    ClientMessage request =
        MultiMapTryLockCodec.encodeRequest(
            name, keyData, threadId, leaseTimeInMillis, timeoutInMillis);
    ClientMessage response = invoke(request, keyData);
    MultiMapTryLockCodec.ResponseParameters resultParameters =
        MultiMapTryLockCodec.decodeResponse(response);
    return resultParameters.response;
  }
 public ICompletableFuture executeOnKeyInternal(
     Data key, EntryProcessor entryProcessor, ExecutionCallback<Object> callback) {
   int partitionId = partitionService.getPartitionId(key);
   MapOperation operation = operationProvider.createEntryOperation(name, key, entryProcessor);
   operation.setThreadId(ThreadUtil.getThreadId());
   try {
     if (callback == null) {
       return operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
     } else {
       return operationService
           .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
           .setExecutionCallback(new MapExecutionCallbackAdapter(callback))
           .invoke();
     }
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
  protected ICompletableFuture<Data> removeAsyncInternal(Data key) {
    int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
    MapOperation operation = operationProvider.createRemoveOperation(name, key, false);
    operation.setThreadId(ThreadUtil.getThreadId());
    try {
      long startTime = System.currentTimeMillis();
      InternalCompletableFuture<Data> future =
          operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);

      if (statisticsEnabled) {
        future.andThen(new IncrementStatsExecutionCallback<Data>(operation, startTime));
      }

      return future;
    } catch (Throwable t) {
      throw rethrow(t);
    }
  }
 protected int valueCountInternal(Data key) {
   throwExceptionIfNull(key);
   Collection<MultiMapRecord> coll = txMap.get(key);
   if (coll == null) {
     CountOperation operation = new CountOperation(name, key);
     operation.setThreadId(ThreadUtil.getThreadId());
     try {
       int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
       final OperationService operationService = getNodeEngine().getOperationService();
       Future<Integer> f =
           operationService.invokeOnPartition(
               MultiMapService.SERVICE_NAME, operation, partitionId);
       return f.get();
     } catch (Throwable t) {
       throw ExceptionUtil.rethrow(t);
     }
   }
   return coll.size();
 }
 protected Collection<MultiMapRecord> getInternal(Data key) {
   throwExceptionIfNull(key);
   Collection<MultiMapRecord> coll = txMap.get(key);
   if (coll == null) {
     GetAllOperation operation = new GetAllOperation(name, key);
     operation.setThreadId(ThreadUtil.getThreadId());
     try {
       int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
       final OperationService operationService = getNodeEngine().getOperationService();
       Future<MultiMapResponse> f =
           operationService.invokeOnPartition(
               MultiMapService.SERVICE_NAME, operation, partitionId);
       MultiMapResponse response = f.get();
       coll = response.getRecordCollection(getNodeEngine());
     } catch (Throwable t) {
       throw ExceptionUtil.rethrow(t);
     }
   }
   return coll;
 }
Beispiel #28
0
 public void lock(long leaseTime, TimeUnit timeUnit) {
   LockRequest request =
       new LockRequest(
           getKeyData(), ThreadUtil.getThreadId(), getTimeInMillis(leaseTime, timeUnit), -1);
   invoke(request);
 }
 private int getThreadId() {
   return ThreadUtil.getThreadId();
 }
Beispiel #30
0
 public void forceUnlock() {
   UnlockRequest request = new UnlockRequest(getKeyData(), ThreadUtil.getThreadId(), true);
   invoke(request);
 }