Java Code Examples for javax.cache.processor.MutableEntry#exists()

The following examples show how to use javax.cache.processor.MutableEntry#exists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JCacheCache.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
	Callable<T> valueLoader = (Callable<T>) arguments[0];
	if (entry.exists()) {
		return (T) fromStoreValue(entry.getValue());
	}
	else {
		T value;
		try {
			value = valueLoader.call();
		}
		catch (Exception ex) {
			throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
					"to compute value for key '" + entry.getKey() + "'", ex);
		}
		entry.setValue(toStoreValue(value));
		return value;
	}
}
 
Example 2
Source File: JCacheCache.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
	Callable<T> valueLoader = (Callable<T>) arguments[0];
	if (entry.exists()) {
		return (T) fromStoreValue(entry.getValue());
	}
	else {
		T value;
		try {
			value = valueLoader.call();
		}
		catch (Exception ex) {
			throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
					"to compute value for key '" + entry.getKey() + "'", ex);
		}
		entry.setValue(toStoreValue(value));
		return value;
	}
}
 
Example 3
Source File: JCacheCache.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
	Callable<T> valueLoader = (Callable<T>) arguments[0];
	if (entry.exists()) {
		return (T) fromStoreValue(entry.getValue());
	}
	else {
		T value;
		try {
			value = valueLoader.call();
		}
		catch (Exception ex) {
			throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
					"to compute  value for key '" + entry.getKey() + "'", ex);
		}
		entry.setValue(toStoreValue(value));
		return value;
	}
}
 
Example 4
Source File: DmlStatementsProcessor.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public Boolean process(MutableEntry<Object, Object> entry, Object... arguments)
    throws EntryProcessorException {
    if (!entry.exists())
        return null; // Someone got ahead of us and removed this entry, let's skip it.

    Object entryVal = entry.getValue();

    if (entryVal == null)
        return null;

    // Something happened to the cache while we were performing map-reduce.
    if (!F.eq(entryVal, val))
        return false;

    entryModifier.apply(entry);

    return null; // To leave out only erroneous keys - nulls are skipped on results' processing.
}
 
Example 5
Source File: IgniteCacheInvokeReadThroughAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public Object process(MutableEntry<Object, Object> entry, Object... args) {
    if (!entry.exists()) {
        failed = true;

        fail();
    }

    Integer val = (Integer)entry.getValue();

    if (!val.equals(entry.getKey())) {
        failed = true;

        assertEquals(val, entry.getKey());
    }

    entry.setValue(val + 1);

    return val;
}
 
Example 6
Source File: IgniteCacheInvokeAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public Integer process(MutableEntry<Integer, Integer> e,
    Object... arguments) throws EntryProcessorException {
    Ignite ignite = e.unwrap(Ignite.class);

    assertNotNull(ignite);

    if (e.exists()) {
        Integer val = e.getValue();

        assertNotNull(val);

        e.setValue(val + 1);

        assertTrue(e.exists());

        assertEquals(val + 1, (int) e.getValue());

        return val;
    }
    else {
        e.setValue(1);

        return -1;
    }
}
 
Example 7
Source File: DmlStatementsProcessor.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public Boolean process(MutableEntry<Object, Object> entry, Object... arguments)
    throws EntryProcessorException {
    if (entry.exists())
        return false;

    entry.setValue(val);
    return null; // To leave out only erroneous keys - nulls are skipped on results' processing.
}
 
Example 8
Source File: CacheContinuousQueryOperationFromCallbackTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments)
    throws EntryProcessorException {
    if (entry.exists())
        entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
    else
        entry.setValue(new QueryTestValue(0));

    return null;
}
 
Example 9
Source File: GridCacheAbstractTransformWriteThroughSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public Void process(MutableEntry<String, Integer> e, Object... args) {
    if (!e.exists())
        e.setValue(1);
    else
        e.setValue(e.getValue() + 1);

    return null;
}
 
Example 10
Source File: InitStateProcessor.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResult<Nothing> process(MutableEntry<K, GridBucketState> mutableEntry, Object... arguments) {
    if (mutableEntry.exists()) {
        return CommandResult.success(null);
    }
    long currentTimeNanos = currentTimeNanos();
    BucketState bucketState = BucketState.createInitialState(configuration, currentTimeNanos);
    GridBucketState gridBucketState = new GridBucketState(configuration, bucketState);
    mutableEntry.setValue(gridBucketState);
    return CommandResult.success(null);
}
 
Example 11
Source File: ExecuteProcessor.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResult<T> process(MutableEntry<K, GridBucketState> mutableEntry, Object... arguments) {
    if (!mutableEntry.exists()) {
        return CommandResult.bucketNotFound();
    }
    long currentTimeNanos = currentTimeNanos();
    GridBucketState gridBucketState = mutableEntry.getValue();

    T result = targetCommand.execute(gridBucketState, currentTimeNanos);
    if (targetCommand.isBucketStateModified()) {
        mutableEntry.setValue(gridBucketState);
    }
    return CommandResult.success(result);
}
 
Example 12
Source File: SimpleEntryProcessor.java    From tutorials with MIT License 5 votes vote down vote up
public String process(MutableEntry<String, String> entry, Object... args) throws EntryProcessorException {

        if (entry.exists()) {
            String current = entry.getValue();
            entry.setValue(current + " - modified");
            return current;
        }
        return null;
    }
 
Example 13
Source File: TransformingCacheProxy.java    From cache2k with Apache License 2.0 5 votes vote down vote up
private MutableEntry<K, V> wrapMutableEntry(final MutableEntry<K0, V0> entry) {
  return new MutableEntry<K, V>() {
    @Override
    public boolean exists() {
      return entry.exists();
    }

    @Override
    public void remove() {
      entry.remove();
    }

    @Override
    public void setValue(V value) {
      entry.setValue(valueTransformer.compact(value));
    }

    @Override
    public K getKey() {
      return keyTransformer.expand(entry.getKey());
    }

    @Override
    public V getValue() {
      return valueTransformer.expand(entry.getValue());
    }

    @Override
    public <T> T unwrap(Class<T> clazz) {
      return entry.unwrap(clazz);
    }
  };
}
 
Example 14
Source File: ExistEntryProcessor.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Boolean process(MutableEntry<Integer, String> entry, Object... arguments) {
    return entry.exists();
}
 
Example 15
Source File: TouchyJCacheAdapter.java    From cache2k with Apache License 2.0 4 votes vote down vote up
private <T> javax.cache.processor.EntryProcessor<K,V,T> wrapEntryProcessor(final javax.cache.processor.EntryProcessor<K,V,T> ep) {
  if (ep == null) {
    throw new NullPointerException("processor is null");
  }
  return new javax.cache.processor.EntryProcessor<K,V, T>() {
    boolean freshOrJustLoaded = false;
    @Override
    public T process(final MutableEntry<K, V> e0, Object... _args) throws EntryProcessorException {
      MutableEntry<K, V> me = new MutableEntry<K, V>() {

        @Override
        public boolean exists() {
          return e0.exists();
        }

        @Override
        public void remove() {
          e0.remove();
        }

        @Override
        public void setValue(V value) {
          checkNullValue(value);
          freshOrJustLoaded = true;
          e0.setValue(value);
        }

        @Override
        public K getKey() {
          return e0.getKey();
        }

        @Override
        public V getValue() {
          boolean _doNotCountCacheAccessIfEntryGetsLoaded = !exists();
          boolean _doNotCountCacheAccessIfEntryIsFresh = freshOrJustLoaded;
          if (_doNotCountCacheAccessIfEntryIsFresh || _doNotCountCacheAccessIfEntryGetsLoaded) {
            if (!cache.readThrough && !exists()) {
              return null;
            }
            freshOrJustLoaded = true;
            return e0.getValue();
          }
          return returnValue(e0.getKey(), e0.getValue());
        }

        @Override
        public <X> X unwrap(Class<X> clazz) {
          return null;
        }
      };
      return ep.process(me, _args);
    }
  };
}