Java Code Examples for com.gemstone.gemfire.cache.Region#containsKey()

The following examples show how to use com.gemstone.gemfire.cache.Region#containsKey() . 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: DestroyEntryPropagationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Creates entries on the server
 *
 */
public static void createEntriesK1andK2()
{
  try {
    Region r1 = cache.getRegion(Region.SEPARATOR+REGION_NAME);
    assertNotNull(r1);
    if (!r1.containsKey("key1")) {
      r1.create("key1", "key-1");
    }
    if (!r1.containsKey("key2")) {
      r1.create("key2", "key-2");
    }
    assertEquals(r1.getEntry("key1").getValue(), "key-1");
    assertEquals(r1.getEntry("key2").getValue(), "key-2");
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 2
Source File: RedundancyLevelTestBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntriesK1andK2()
{
  try {
    Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    assertNotNull(r1);
    if (!r1.containsKey(k1)) {
      r1.create(k1, k1);
    }
    if (!r1.containsKey(k2)) {
      r1.create(k2, k2);
    }
    assertEquals(r1.getEntry(k1).getValue(), k1);
    assertEquals(r1.getEntry(k2).getValue(), k2);
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 3
Source File: NewWANConcurrencyCheckForDestroyDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static long getVersionTimestampAfterPutAllOp() {
  Region region = cache.getRegion("repRegion");
  
  while (!(region.containsKey("testKey") /*&& region.get("testKey").equals("testValue2") */)) {
    pause(10);
  }
  assertEquals(1, region.size());

  Region.Entry entry = ((LocalRegion)region).getEntry("testKey", null, true);
  RegionEntry re = null;
  if (entry instanceof EntrySnapshot) {
    re = ((EntrySnapshot)entry).getRegionEntry();
  } else if (entry instanceof NonTXEntry) {
    re = ((NonTXEntry)entry).getRegionEntry();
  }
  if (re != null) {
    getLogWriter().fine("RegionEntry for testKey: " + re.getKey() + " " + re.getValueInVM((LocalRegion) region));
    
    VersionTag tag = re.getVersionStamp().asVersionTag();
    return tag.getVersionTimeStamp();
  } else {
    return -1;
  }
}
 
Example 4
Source File: VerifyEventIDGenerationInP2PDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntry()
{
  try {
    Region r = cache.getRegion("/" + REGION_NAME);
    assertNotNull(r);

    if (!r.containsKey("key-1")) {
      r.create("key-1", "key-1");
    }
    // Verify that no invalidates occurred to this region
    assertEquals(r.getEntry("key-1").getValue(), "key-1");
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 5
Source File: UpdatePropagationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Creates entries on the server
 *
 */
public static void createEntriesK1andK2()
{
  try {
    Region r1 = cache.getRegion(Region.SEPARATOR+REGION_NAME);
    assertNotNull(r1);
    if (!r1.containsKey("key1")) {
      r1.put("key1", "key-1");
    }
    if (!r1.containsKey("key2")) {
      r1.put("key2", "key-2");
    }
    assertEquals(r1.get("key1"), "key-1");
    if (r1.getAttributes().getPartitionAttributes() == null) {
      assertEquals(r1.getEntry("key1").getValue(), "key-1");
      assertEquals(r1.getEntry("key2").getValue(), "key-2");
    }
    else {
      assertEquals(r1.get("key1"), "key-1");
      assertEquals(r1.get("key2"), "key-2");
    }
  }
  catch (Exception ex) {
    fail("failed while createEntriesK1andK2()", ex);
  }
}
 
Example 6
Source File: InterestListEndpointDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntriesK1andK2()
{
  try {
    Region r1 = cache.getRegion("/"+ REGION_NAME);
    assertNotNull(r1);
    if (!r1.containsKey(k1)) {
      r1.create(k1, client_k1);
    }
    if (!r1.containsKey(k2)) {
      r1.create(k2, client_k2);
    }
    if (r1.getAttributes().getPartitionAttributes() == null) {
      assertEquals(r1.getEntry(k1).getValue(), client_k1);
      assertEquals(r1.getEntry(k2).getValue(), client_k2);
    }
  }
  catch (Exception ex) {
    throw new RuntimeException("failed while createEntries()", ex);
  }
}
 
Example 7
Source File: NewWANConcurrencyCheckForDestroyDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static long getVersionTimestampAfterPutAllOp() {
  Region region = cache.getRegion("repRegion");
  
  while (!(region.containsKey("testKey") /*&& region.get("testKey").equals("testValue2") */)) {
    pause(10);
  }
  assertEquals(1, region.size());

  Region.Entry entry = ((LocalRegion)region).getEntry("testKey", null, true);
  RegionEntry re = null;
  if (entry instanceof EntrySnapshot) {
    re = ((EntrySnapshot)entry).getRegionEntry();
  } else if (entry instanceof NonTXEntry) {
    re = ((NonTXEntry)entry).getRegionEntry();
  }
  if (re != null) {
    getLogWriter().fine("RegionEntry for testKey: " + re.getKey() + " " + re.getValueInVM((LocalRegion) region));
    
    VersionTag tag = re.getVersionStamp().asVersionTag();
    return tag.getVersionTimeStamp();
  } else {
    return -1;
  }
}
 
Example 8
Source File: PeerTypeRegistration.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void addRemoteType(int typeId, PdxType type) {
  verifyConfiguration();
  TXStateInterface currentState = suspendTX();
  Region<Object, Object> r = getIdToType();
  try {
    if(!r.containsKey(typeId)) {
      //This type could actually be for this distributed system,
      //so we need to make sure the type is published while holding
      //the distributed lock.
      lock();
      try {
        r.put(typeId, type);
      } finally {
        unlock();
      }
    }
  } finally {
    resumeTX(currentState);
  }
}
 
Example 9
Source File: PeerTypeRegistration.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void addRemoteType(int typeId, PdxType type) {
  verifyConfiguration();
  TXStateInterface currentState = suspendTX();
  Region<Object, Object> r = getIdToType();
  try {
    if(!r.containsKey(typeId)) {
      //This type could actually be for this distributed system,
      //so we need to make sure the type is published while holding
      //the distributed lock.
      lock();
      try {
        r.put(typeId, type);
      } finally {
        unlock();
      }
    }
  } finally {
    resumeTX(currentState);
  }
}
 
Example 10
Source File: HAInterestBaseTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntriesK1andK2()
{
  try {
    Region r1 = cache.getRegion(Region.SEPARATOR+ REGION_NAME);
    assertNotNull(r1);
    if (!r1.containsKey(k1)) {
      r1.create(k1, client_k1);
    }
    if (!r1.containsKey(k2)) {
      r1.create(k2, client_k2);
    }
    assertEquals(r1.getEntry(k1).getValue(), client_k1);
    assertEquals(r1.getEntry(k2).getValue(), client_k2);
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 11
Source File: EventIDVerificationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntry()
{
  try {
    Region r = cache.getRegion("/" + REGION_NAME);
    assertNotNull(r);

    if (!r.containsKey("key-1")) {
      r.create("key-1", "key-1");
    }
    // Verify that no invalidates occurred to this region
    assertEquals(r.getEntry("key-1").getValue(), "key-1");
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 12
Source File: InterestListFailoverDUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static void createEntries()
{
  try {
    Region r = CacheServerTestUtil.getCache().getRegion("/"+ REGION_NAME);
    assertNotNull(r);

    if (!r.containsKey("key-1")) {
      r.create("key-1", "key-1");
    }
    if (!r.containsKey("key-6")) {
      r.create("key-6", "key-6");
    }
    // Verify that no invalidates occurred to this region
    assertEquals(r.getEntry("key-1").getValue(), "key-1");
    assertEquals(r.getEntry("key-6").getValue(), "key-6");
  }
  catch (Exception ex) {
    fail("failed while createEntries()", ex);
  }
}
 
Example 13
Source File: ConcurrentRegionOperationsJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
void validate(Region r1, Region r2)
{
  if (!this.validate) return;

  Collection entrySet = r2.entrySet();
  Iterator iterator = entrySet.iterator();
  Map.Entry mapEntry = null;
  Object key, value = null;
  while (iterator.hasNext()) {
    mapEntry = (Map.Entry)iterator.next();
    key = mapEntry.getKey();
    value = mapEntry.getValue();
    if (!(r1.containsKey(key))) {
      fail(" region1 does not contain Key " + key
          + " but was expected to be there");
    }
    if (!(r1.get(key).equals(value))) {
      fail(" value for key " + key + " is " + r1.get(key)
          + " which is not consistent, it is supposed to be " + value);
    }
  }
  assertEquals(r2.size(), r1.size());

  r1.destroyRegion();
  r2.destroyRegion();
}
 
Example 14
Source File: PeerTypeRegistration.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void addRemoteEnum(int id, EnumInfo enumInfo) {
  verifyConfiguration();
  TXStateInterface currentState = suspendTX();
  EnumId enumId = new EnumId(id);
  Region<Object, Object> r = getIdToType();
  try {
    if(!r.containsKey(enumId)) {
      //This enum could actually be for this distributed system,
      //so we need to make sure the enum is published while holding
      //the distributed lock.
      lock();
      try {
        r.put(enumId, enumInfo);
      } finally {
        unlock();
      }
    }
  } finally {
    resumeTX(currentState);
  }
}
 
Example 15
Source File: ParRegUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** Verify containsKey for the given region and key.
 *
 * @param aRegion The region to verify.
 * @param key The key in aRegion to verify.
 * @param expected The expected value of containsKey()
 *
 * @throws TestException if containsKey() has the wrong value
 */
public static void verifyContainsKey(Region aRegion, Object key, boolean expected) {
   boolean containsKey = aRegion.containsKey(key);
   if (containsKey != expected) {
      RegionAttributes attr = aRegion.getAttributes();
      String extra = ParRegUtilVersionHelper.getVersionTagStr(aRegion, key);
      throw new TestException("Expected containsKey() for " + key + " to be " + expected + 
                " in " + aRegion.getFullPath() + ", but it is " + containsKey + extra);
   }
}
 
Example 16
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static void checkRegionKeyDoesNotExistsValueDoesNotExists(Region r, Object key) {
  if (HydraUtil.isSerialTest()) {
    if (r.containsKey(key)) {
      throw new TestException("Region " + r.getName() + " contains key " + key + " but was destroyed before");
    }

    if (!r.containsValueForKey(key)) {
      return;
    } else {
      throw new TestException("Region " + r.getName() + " contains value for " + key
          + " but was destroyed/invalidated before");
    }
  }
}
 
Example 17
Source File: ParRegColocation.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Task that does the verification of region size
 * 
 * @param aRegion
 *                Region whose size is to be verified
 */
protected void verifyRegionSize(Region aRegion) {
  // we already completed this check once; we can't do it again without
  // reinitializing the
  // verify state variables

  int size = aRegion.size();
  long numOriginalKeysCreated = InitImageBB.getBB().getSharedCounters().read(
      InitImageBB.NUM_ORIGINAL_KEYS_CREATED);
  int numKeysToCreate = keyIntervals.getNumKeys();
  long nameCounter = NameFactory.getPositiveNameCounter();
  Log.getLogWriter().info(
      "In HydraTask_verifyRegionSize, region size is " + size
          + ", numKeysToCreate is " + numKeysToCreate
          + ", numOriginalKeysCreated is " + numOriginalKeysCreated
          + ", nameCounter is " + nameCounter);

  // make sure the test agrees with itself
  if ((numOriginalKeysCreated != numKeysToCreate)
      || (numOriginalKeysCreated != nameCounter)) {
    throw new TestException("Error in test, numOriginalKeysCreated "
        + numOriginalKeysCreated + ", numKeysToCreate " + numKeysToCreate
        + ", nameCounter " + nameCounter);
  }

  int verifyRegionSizeIndex = 0;
  while (verifyRegionSizeIndex < numKeysToCreate) {
    verifyRegionSizeIndex++;
    Object key = NameFactory.getObjectNameForCounter(verifyRegionSizeIndex);
    Log.getLogWriter().info(
        "For region : " + aRegion.getName()
            + " : Key : " + key);
    if (!aRegion.containsKey(key))
      throw new TestException("Key " + key + " not found in the region "
          + aRegion.getName());
    if (!aRegion.containsValueForKey(key))
      throw new TestException("For the key " + key
          + " value is not found in the region " + aRegion.getName());
  }

  if (size != numKeysToCreate) {
    throw new TestException("Unexpected region size " + size + "; expected "
        + numKeysToCreate);
  }
  String aStr = "In HydraTask_verifyRegionSize, verified "
      + verifyRegionSizeIndex + " keys and values";
  Log.getLogWriter().info(aStr);
}
 
Example 18
Source File: DeltaTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/** 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 aReg, boolean isLocalInvalidate) {
   int beforeSize = aReg.size();
   Object key = getExistingKey(aReg, 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 = aReg.containsKey(key);
   boolean containsValueForKey = aReg.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);
            aReg.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);
            aReg.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);
            aReg.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);
            aReg.invalidate(key);
            Log.getLogWriter().info("operation for " + key + ", invalidateEntry: done invalidating key " + key);
         }
      }

      // validation
      if (isSerialExecution || uniqueKeys) {
         // record the current state
         regionSnapshot.put(key, null);
      }
   } catch (com.gemstone.gemfire.cache.EntryNotFoundException e) {
      if (isSerialExecution || uniqueKeys)
         throw new TestException(TestHelper.getStackTrace(e));
      else {
         Log.getLogWriter().info("Caught " + e + " (expected with concurrent execution); continuing with test");
         return;
      }
   }
}
 
Example 19
Source File: ResumableKnownKeysTest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/** Check that containsKey() called on the given region has the expected result.
 *  Throw an error if any problems.
 *  
 *  @param aRegion The region to check.
 *  @param key The key to check.
 *  @param expected The expected result of containsKey
 *  @param logStr Used if throwing an error due to an unexpected value.
 */
protected void checkContainsKey(Region aRegion, Object key, boolean expected, String logStr) {
  boolean containsKey = aRegion.containsKey(key);
  if (containsKey != expected)
    throw new TestException("Expected containsKey(" + key + ") to be " + expected + 
        ", but it was " + containsKey + ": " + logStr);
}
 
Example 20
Source File: ResumableKnownKeysTest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/** Check that containsKey() called on the given region has the expected result.
 *  Throw an error if any problems.
 *  
 *  @param aRegion The region to check.
 *  @param key The key to check.
 *  @param expected The expected result of containsKey
 *  @param logStr Used if throwing an error due to an unexpected value.
 */
protected void checkContainsKey(Region aRegion, Object key, boolean expected, String logStr) {
  boolean containsKey = aRegion.containsKey(key);
  if (containsKey != expected)
    throw new TestException("Expected containsKey(" + key + ") to be " + expected + 
        ", but it was " + containsKey + ": " + logStr);
}