/** * Get a random key from the given region that is within this instance's range. If no keys qualify * in the region, return null. * * @param aRegion - The region to get the key from. * @param excludeKey - The region to get the key from. * @returns A key from aRegion, or null. */ public Object getRandomKey(Region aRegion, Object excludeKey) { long start = System.currentTimeMillis(); int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); long randomKeyIndex = TestConfig.tab().getRandGen().nextLong(lower, upper); long startKeyIndex = randomKeyIndex; Object key = NameFactory.getObjectNameForCounter(randomKeyIndex); do { boolean done = false; if ((!(key.equals(excludeKey))) && (aRegion.containsKey(key))) done = true; if (done) break; randomKeyIndex++; // go to the next key if (randomKeyIndex > upper) randomKeyIndex = lower; if (randomKeyIndex == startKeyIndex) { // considered all keys key = null; break; } key = NameFactory.getObjectNameForCounter(randomKeyIndex); } while (true); long end = System.currentTimeMillis(); Log.getLogWriter() .info( "Done in TxUtilKeyRange:getRandomKey, key is " + key + " " + aRegion.getFullPath() + " getRandomKey took " + (end - start) + " millis"); return key; }
/** * Creates a new key/value in the given region by creating a new key within the range and a random * value. * * @param aRegion The region to create the new key in. * @param exists Not used in this overridden method; this test wants to use unique keys even on * creates, so we don't do anything different here based on the value of exists. * @return An instance of Operation describing the create operation. */ @Override public Operation createEntry(Region aRegion, boolean exists) { int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); long keyIndex = TestConfig.tab().getRandGen().nextInt(lower, upper); long startKeyIndex = keyIndex; Object key = NameFactory.getObjectNameForCounter(keyIndex); boolean containsKey = aRegion.containsKey(key); while (containsKey) { // looking for a key that does not exist keyIndex++; // go to the next key if (keyIndex > upper) keyIndex = lower; if (keyIndex == startKeyIndex) { // considered all keys return null; } key = NameFactory.getObjectNameForCounter(keyIndex); containsKey = aRegion.containsKey(key); } BaseValueHolder vh = new ValueHolder(key, randomValues, new Integer(modValInitializer++)); try { Log.getLogWriter() .info( "createEntryKeyRange: putting key " + key + ", object " + vh.toString() + " in region " + aRegion.getFullPath()); aRegion.put(key, vh); Log.getLogWriter() .info( "createEntryKeyRange: done putting key " + key + ", object " + vh.toString() + " in region " + aRegion.getFullPath()); } catch (Exception e) { throw new TestException(TestHelper.getStackTrace(e)); } return new Operation(aRegion.getFullPath(), key, Operation.ENTRY_CREATE, null, vh.modVal); }
/** * Check that containsKey() called on the tests' region has the expected result. Throw an error if * any problems. * * @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(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); }
/** Called for debugging */ public void printKeyRange(Region aRegion) { int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); StringBuffer aStr = new StringBuffer(); for (int i = lower; i <= upper; i++) { Object key = NameFactory.getObjectNameForCounter(i); aStr.append( "key " + key + " containsKey: " + aRegion.containsKey(key) + ", containsValueForKey: " + aRegion.containsValueForKey(key) + ", getValueInVM: " + diskReg.DiskRegUtil.getValueInVM(aRegion, key) + "\n"); } Log.getLogWriter().info(aStr.toString()); }