Java Code Examples for com.gemstone.gemfire.cache.Region#putIfAbsent()
The following examples show how to use
com.gemstone.gemfire.cache.Region#putIfAbsent() .
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: AddCommand.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
@Override public ByteBuffer processBinaryStorageCommand(Object key, byte[] value, long cas, int flags, Cache cache, ByteBuffer response) { Region<Object, ValueWrapper> r = getMemcachedRegion(cache); ValueWrapper val = ValueWrapper.getWrappedValue(value, flags); Object oldVal = r.putIfAbsent(key, val); // set status if (oldVal == null) { if (getLogger().fineEnabled()) { getLogger().fine("added key: "+key); } if (isQuiet()) { return null; } response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort()); //set cas response.putLong(POSITION_CAS, val.getVersion()); } else { if (getLogger().fineEnabled()) { getLogger().fine("key: "+key+" not added as is already exists"); } response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_EXISTS.asShort()); } return response; }
Example 2
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public static void updateKeysInRegion(Region aRegion) { long thrdNum = SecurityClientBB.getBB().getSharedCounters() .incrementAndRead(SecurityClientBB.threadCount); for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = getKeyForOperation(); String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { aRegion.put(Key, Value); } else { Object v = aRegion.putIfAbsent(Key, Value); if (v != null) { aRegion.replace(Key, v, Value); } } } }
Example 3
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public synchronized static void putAllKeysInRange() { Region region = RegionHelper.getRegion(regionName); for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = "KEY-" + keyRng; String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put(Key, Value); } else { Object v = region.putIfAbsent(Key, Value); if (v != null) { region.replace(Key, Value); } } } }
Example 4
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public synchronized static void populateNonSecureRegion() { Region nonSecureRegion = RegionHelper.getRegion(NON_SECURE_REGION_NAME); if (nonSecureRegion == null) return; for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = "KEY-" + keyRng; String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { nonSecureRegion.put(Key, Value); } else { Object v = nonSecureRegion.putIfAbsent(Key, Value); if (v != null) { nonSecureRegion.replace(Key, Value); } } } }
Example 5
Source File: WANClient.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** Populates REGION_NAME with numEntries objects */ public static void populateRegionTask() { Region region = RegionHelper.getRegion(REGION_NAME); int numEntries = CacheClientPrms.getNumEntries(); for (int i = 0; i < numEntries; i++) { Object key = NameFactory.getNextPositiveObjectName(); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put( key, new Long( NameFactory.getCounterForName( key ) ) ); } else { region.putIfAbsent( key, new Long( NameFactory.getCounterForName( key ) ) ); } } Log.getLogWriter().info("populated cache with " + NameFactory.getPositiveNameCounter() + " keys"); // Allow distribution of updates MasterController.sleepForMs( 60000 ); }
Example 6
Source File: WANClient.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** Populates REGION_NAME with numEntries objects */ public static void populateRegionTask() { Region region = RegionHelper.getRegion(REGION_NAME); int numEntries = CacheClientPrms.getNumEntries(); for (int i = 0; i < numEntries; i++) { Object key = NameFactory.getNextPositiveObjectName(); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put( key, new Long( NameFactory.getCounterForName( key ) ) ); } else { region.putIfAbsent( key, new Long( NameFactory.getCounterForName( key ) ) ); } } Log.getLogWriter().info("populated cache with " + NameFactory.getPositiveNameCounter() + " keys"); // Allow distribution of updates MasterController.sleepForMs( 60000 ); }
Example 7
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public synchronized static void populateNonSecureRegion() { Region nonSecureRegion = RegionHelper.getRegion(NON_SECURE_REGION_NAME); if (nonSecureRegion == null) return; for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = "KEY-" + keyRng; String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { nonSecureRegion.put(Key, Value); } else { Object v = nonSecureRegion.putIfAbsent(Key, Value); if (v != null) { nonSecureRegion.replace(Key, Value); } } } }
Example 8
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public synchronized static void putAllKeysInRange() { Region region = RegionHelper.getRegion(regionName); for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = "KEY-" + keyRng; String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put(Key, Value); } else { Object v = region.putIfAbsent(Key, Value); if (v != null) { region.replace(Key, Value); } } } }
Example 9
Source File: AddCommand.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
@Override public ByteBuffer processBinaryStorageCommand(Object key, byte[] value, long cas, int flags, Cache cache, ByteBuffer response) { Region<Object, ValueWrapper> r = getMemcachedRegion(cache); ValueWrapper val = ValueWrapper.getWrappedValue(value, flags); Object oldVal = r.putIfAbsent(key, val); // set status if (oldVal == null) { if (getLogger().fineEnabled()) { getLogger().fine("added key: "+key); } if (isQuiet()) { return null; } response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort()); //set cas response.putLong(POSITION_CAS, val.getVersion()); } else { if (getLogger().fineEnabled()) { getLogger().fine("key: "+key+" not added as is already exists"); } response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_EXISTS.asShort()); } return response; }
Example 10
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public static void updateKeysInRegion(Region aRegion) { long thrdNum = SecurityClientBB.getBB().getSharedCounters() .incrementAndRead(SecurityClientBB.threadCount); for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = getKeyForOperation(); String Value = Key + "_VALUE"; Log.getLogWriter().info("key is: " + Key); if (TestConfig.tab().getRandGen().nextBoolean()) { aRegion.put(Key, Value); } else { Object v = aRegion.putIfAbsent(Key, Value); if (v != null) { aRegion.replace(Key, v, Value); } } } }
Example 11
Source File: AddCommand.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
@Override public ByteBuffer processStorageCommand(String key, byte[] value, int flags, Cache cache) { Region<Object, ValueWrapper> r = getMemcachedRegion(cache); Object oldVal = r.putIfAbsent(key, ValueWrapper.getWrappedValue(value, flags)); String reply = null; if (oldVal == null) { reply = Reply.STORED.toString(); } else { reply = Reply.NOT_STORED.toString(); } return asciiCharset.encode(reply); }
Example 12
Source File: MultipleDiskRegions.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
protected void putIfAbsent(String regionName) { try { Object key = null, val = null; String objectType = MapPrms.getObjectType(); Region region = cache.getRegion(regionName); if (region != null) { // ConcurrentMap operations are not supported for peers with EMPTY or NORMAL dataPolicy DataPolicy dataPolicy = region.getAttributes().getDataPolicy(); if (dataPolicy.equals(DataPolicy.NORMAL) || dataPolicy.equals(DataPolicy.EMPTY)) { return; } int putKeyInt = (int) MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_PUT); key = ObjectHelper.createName(putKeyInt); val = ObjectHelper.createObject(objectType, putKeyInt); Object retVal = region.putIfAbsent(key, val); Log.getLogWriter().info( "----performed putIfAbsent operation on " + regionName + " with " + key + ". putIfAbsent returned " + retVal); } } catch (RegionDestroyedException rdex) { Log.getLogWriter().info( "RegionDestroyedException...may occur in concurrent execution mode. Continuing with test."); recoverRegion(regionName); } catch (Exception ex) { throw new TestException(TestHelper.getStackTrace(ex)); } }
Example 13
Source File: WANOperationsClient.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * ConcurrentMap API testing */ protected void putIfAbsent(Region aRegion, boolean logAddition) { Object key = null; // Expect success most of the time (put a new entry into the cache) int randInt = TestConfig.tab().getRandGen().nextInt(1, 100); if (randInt <= 25) { key = getExistingKey(aRegion); } if (key == null) { key = getNewKey(); } Object anObj = getValueForKey(key); if (logAddition) { Log.getLogWriter().info( "putIfAbsent: calling putIfAbsent for key " + key + ", object " + TestHelper.toString(anObj) + ", region is " + aRegion.getFullPath() + "."); } Object prevVal = null; prevVal = aRegion.putIfAbsent(key, anObj); if (prevVal==null && useUniqueKeyPerThread) { updateBlackboardSnapshot(aRegion, key, anObj, false); } if (logAddition) { Log.getLogWriter().info("putIfAbsent: done putIfAbsent for key " + key + " on region "+ aRegion.getName()+ ", success=" + (prevVal==null)); } }
Example 14
Source File: AddCommand.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
@Override public ByteBuffer processStorageCommand(String key, byte[] value, int flags, Cache cache) { Region<Object, ValueWrapper> r = getMemcachedRegion(cache); Object oldVal = r.putIfAbsent(key, ValueWrapper.getWrappedValue(value, flags)); String reply = null; if (oldVal == null) { reply = Reply.STORED.toString(); } else { reply = Reply.NOT_STORED.toString(); } return asciiCharset.encode(reply); }
Example 15
Source File: MultipleDiskRegions.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
protected void putIfAbsent(String regionName) { try { Object key = null, val = null; String objectType = MapPrms.getObjectType(); Region region = cache.getRegion(regionName); if (region != null) { // ConcurrentMap operations are not supported for peers with EMPTY or NORMAL dataPolicy DataPolicy dataPolicy = region.getAttributes().getDataPolicy(); if (dataPolicy.equals(DataPolicy.NORMAL) || dataPolicy.equals(DataPolicy.EMPTY)) { return; } int putKeyInt = (int) MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_PUT); key = ObjectHelper.createName(putKeyInt); val = ObjectHelper.createObject(objectType, putKeyInt); Object retVal = region.putIfAbsent(key, val); Log.getLogWriter().info( "----performed putIfAbsent operation on " + regionName + " with " + key + ". putIfAbsent returned " + retVal); } } catch (RegionDestroyedException rdex) { Log.getLogWriter().info( "RegionDestroyedException...may occur in concurrent execution mode. Continuing with test."); recoverRegion(regionName); } catch (Exception ex) { throw new TestException(TestHelper.getStackTrace(ex)); } }
Example 16
Source File: WANOperationsClient.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * ConcurrentMap API testing */ protected void putIfAbsent(Region aRegion, boolean logAddition) { Object key = null; // Expect success most of the time (put a new entry into the cache) int randInt = TestConfig.tab().getRandGen().nextInt(1, 100); if (randInt <= 25) { key = getExistingKey(aRegion); } if (key == null) { key = getNewKey(); } Object anObj = getValueForKey(key); if (logAddition) { Log.getLogWriter().info( "putIfAbsent: calling putIfAbsent for key " + key + ", object " + TestHelper.toString(anObj) + ", region is " + aRegion.getFullPath() + "."); } Object prevVal = null; prevVal = aRegion.putIfAbsent(key, anObj); if (prevVal==null && useUniqueKeyPerThread) { updateBlackboardSnapshot(aRegion, key, anObj, false); } if (logAddition) { Log.getLogWriter().info("putIfAbsent: done putIfAbsent for key " + key + " on region "+ aRegion.getName()+ ", success=" + (prevVal==null)); } }
Example 17
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
public synchronized static void doInitialFeed() { Boolean putOpFoundInOpList = false; Region region = null; String userName = null; if (PoolHelper.getPool("brloader").getMultiuserAuthentication()) { Iterator iter = PerUserRequestSecurityTest.proxyRegionMap.keySet() .iterator(); while (iter.hasNext()) { putOpFoundInOpList = false; userName = (String)iter.next(); Map<String, ArrayList<String>> userRoleOpMap = PerUserRequestSecurityTest.userToRolesMap .get(userName); Iterator userRoleOpIter = userRoleOpMap.keySet().iterator(); while (userRoleOpIter.hasNext()) { String roleName = (String)userRoleOpIter.next(); ArrayList<String> roleOpNames = userRoleOpMap.get(roleName); for (int i = 0; i < roleOpNames.size(); i++) { if (roleOpNames.get(i).equalsIgnoreCase("PUT")) { putOpFoundInOpList = true; } } if (putOpFoundInOpList) break; } if (putOpFoundInOpList) { break; } } if(putOpFoundInOpList){ region = PerUserRequestSecurityTest.proxyRegionMap.get(userName); Log.getLogWriter().info("Got the proxyRegion for user :: " + userName); } else { Log.getLogWriter().info("No user found with PUT permission."); } } else { region = RegionHelper.getRegion(regionName); } for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = getKeyForOperation(); Long Value = new Long(1); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put(Key, Value); } else { Object v = region.putIfAbsent(Key, Value); if (v != null) { region.replace(Key, Value); } } } }
Example 18
Source File: CQTest.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** Invalidate an entry in the given region. * * @param aRegion The region to use for invalidating an entry. * @param isLocalInvalidate True if the invalidate should be local, false otherwise. */ protected void invalidateEntry(Region aRegion, boolean isLocalInvalidate) { Object key = null; if (!isOldClient && TestConfig.tab().getRandGen().nextInt(1, 100) <= 50) { // do a putIfAbsent with null key = getNewKey(); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: putIfAbsent with null"); Object returnValue = aRegion.putIfAbsent(key, null); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with putIfAbsent with null, return value is " + TestHelper.toString(returnValue)); } else { int beforeSize = aRegion.size(); key = getExistingKey(aRegion, uniqueKeys, numThreadsInClients); if (key == null) { if (isSerialExecution && (beforeSize != 0)) throw new TestException("getExistingKey returned " + key + ", but region size is " + beforeSize); Log.getLogWriter().info("invalidateEntry: No keys in region"); return; } boolean containsKey = aRegion.containsKey(key); boolean containsValueForKey = aRegion.containsValueForKey(key); Log.getLogWriter().info("containsKey for " + key + ": " + containsKey); Log.getLogWriter().info("containsValueForKey for " + key + ": " + containsValueForKey); try { String callback = invalidateCallbackPrefix + ProcessMgr.getProcessId(); if (isLocalInvalidate) { // do a local invalidate if (TestConfig.tab().getRandGen().nextBoolean()) { // local invalidate with callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: local invalidate for " + key + " callback is " + callback); aRegion.localInvalidate(key, callback); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with local invalidate for " + key); } else { // local invalidate without callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: local invalidate for " + key); aRegion.localInvalidate(key); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with local invalidate for " + key); } } else { // do a distributed invalidate if (TestConfig.tab().getRandGen().nextBoolean()) { // invalidate with callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: invalidating key " + key + " callback is " + callback); aRegion.invalidate(key, callback); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done invalidating key " + key); } else { // invalidate without callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: invalidating key " + key); aRegion.invalidate(key); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done invalidating key " + key); } } } catch (com.gemstone.gemfire.cache.EntryNotFoundException e) { if (isSerialExecution) throw new TestException(TestHelper.getStackTrace(e)); else { Log.getLogWriter().info("Caught " + e + " (expected with concurrent execution); continuing with test"); return; } } } if (isSerialExecution) { // record the current state regionSnapshot.put(key, null); } }
Example 19
Source File: EntryOperations.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
public synchronized static void doInitialFeed() { Boolean putOpFoundInOpList = false; Region region = null; String userName = null; if (PoolHelper.getPool("brloader").getMultiuserAuthentication()) { Iterator iter = PerUserRequestSecurityTest.proxyRegionMap.keySet() .iterator(); while (iter.hasNext()) { putOpFoundInOpList = false; userName = (String)iter.next(); Map<String, ArrayList<String>> userRoleOpMap = PerUserRequestSecurityTest.userToRolesMap .get(userName); Iterator userRoleOpIter = userRoleOpMap.keySet().iterator(); while (userRoleOpIter.hasNext()) { String roleName = (String)userRoleOpIter.next(); ArrayList<String> roleOpNames = userRoleOpMap.get(roleName); for (int i = 0; i < roleOpNames.size(); i++) { if (roleOpNames.get(i).equalsIgnoreCase("PUT")) { putOpFoundInOpList = true; } } if (putOpFoundInOpList) break; } if (putOpFoundInOpList) { break; } } if(putOpFoundInOpList){ region = PerUserRequestSecurityTest.proxyRegionMap.get(userName); Log.getLogWriter().info("Got the proxyRegion for user :: " + userName); } else { Log.getLogWriter().info("No user found with PUT permission."); } } else { region = RegionHelper.getRegion(regionName); } for (int keyRng = 0; keyRng < PUT_KEY_RANGE; keyRng++) { String Key = getKeyForOperation(); Long Value = new Long(1); if (TestConfig.tab().getRandGen().nextBoolean()) { region.put(Key, Value); } else { Object v = region.putIfAbsent(Key, Value); if (v != null) { region.replace(Key, Value); } } } }
Example 20
Source File: CQTest.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** Invalidate an entry in the given region. * * @param aRegion The region to use for invalidating an entry. * @param isLocalInvalidate True if the invalidate should be local, false otherwise. */ protected void invalidateEntry(Region aRegion, boolean isLocalInvalidate) { Object key = null; if (!isOldClient && TestConfig.tab().getRandGen().nextInt(1, 100) <= 50) { // do a putIfAbsent with null key = getNewKey(); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: putIfAbsent with null"); Object returnValue = aRegion.putIfAbsent(key, null); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with putIfAbsent with null, return value is " + TestHelper.toString(returnValue)); } else { int beforeSize = aRegion.size(); key = getExistingKey(aRegion, uniqueKeys, numThreadsInClients); if (key == null) { if (isSerialExecution && (beforeSize != 0)) throw new TestException("getExistingKey returned " + key + ", but region size is " + beforeSize); Log.getLogWriter().info("invalidateEntry: No keys in region"); return; } boolean containsKey = aRegion.containsKey(key); boolean containsValueForKey = aRegion.containsValueForKey(key); Log.getLogWriter().info("containsKey for " + key + ": " + containsKey); Log.getLogWriter().info("containsValueForKey for " + key + ": " + containsValueForKey); try { String callback = invalidateCallbackPrefix + ProcessMgr.getProcessId(); if (isLocalInvalidate) { // do a local invalidate if (TestConfig.tab().getRandGen().nextBoolean()) { // local invalidate with callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: local invalidate for " + key + " callback is " + callback); aRegion.localInvalidate(key, callback); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with local invalidate for " + key); } else { // local invalidate without callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: local invalidate for " + key); aRegion.localInvalidate(key); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done with local invalidate for " + key); } } else { // do a distributed invalidate if (TestConfig.tab().getRandGen().nextBoolean()) { // invalidate with callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: invalidating key " + key + " callback is " + callback); aRegion.invalidate(key, callback); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done invalidating key " + key); } else { // invalidate without callback Log.getLogWriter().info("operation for " + key + ", invalidateEntry: invalidating key " + key); aRegion.invalidate(key); Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done invalidating key " + key); } } } catch (com.gemstone.gemfire.cache.EntryNotFoundException e) { if (isSerialExecution) throw new TestException(TestHelper.getStackTrace(e)); else { Log.getLogWriter().info("Caught " + e + " (expected with concurrent execution); continuing with test"); return; } } } if (isSerialExecution) { // record the current state regionSnapshot.put(key, null); } }