/**
  * Update an existing key in region REGION_NAME. The keys to update are specified in keyIntervals.
  *
  * @return true if all keys to be updated have been completed.
  */
 protected boolean updateExistingKey() {
   long nextKey =
       CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.LASTKEY_UPDATE_EXISTING_KEY);
   if (!keyIntervals.keyInRange(KeyIntervals.UPDATE_EXISTING_KEY, nextKey)) {
     Log.getLogWriter().info("All existing keys updated; returning from updateExistingKey");
     return true;
   }
   Object key = NameFactory.getObjectNameForCounter(nextKey);
   QueryObject existingValue = (QueryObject) aRegion.get(key);
   if (existingValue == null)
     throw new TestException("Get of key " + key + " returned unexpected " + existingValue);
   QueryObject newValue = existingValue.modifyWithNewInstance(QueryObject.NEGATE, 0, true);
   newValue.extra = key; // encode the key in the object for later validation
   if (existingValue.aPrimitiveLong < 0)
     throw new TestException(
         "Trying to update a key which was already updated: " + existingValue.toStringFull());
   Log.getLogWriter()
       .info("Updating existing key " + key + " with value " + TestHelper.toString(newValue));
   aRegion.put(key, newValue);
   Log.getLogWriter()
       .info(
           "Done updating existing key "
               + key
               + " with value "
               + TestHelper.toString(newValue)
               + ", num remaining: "
               + (keyIntervals.getLastKey(KeyIntervals.UPDATE_EXISTING_KEY) - nextKey));
   return (nextKey >= keyIntervals.getLastKey(KeyIntervals.UPDATE_EXISTING_KEY));
 }
 /**
  * Check that the value of the given key is expected as an updated value. Throw an error if any
  * problems.
  *
  * @param key The key to check.
  * @param value The value for the key.
  * @param logStr Used if throwing an error due to an unexpected value.
  */
 protected void checkUpdatedValue(Object key, Object value) {
   if (value instanceof QueryObject) {
     QueryObject qo = (QueryObject) value;
     long keyCounter = NameFactory.getCounterForName(key);
     if (qo.aPrimitiveLong > 0) { // this value has not been updated; updates are negative
       throw new TestException(
           "Expected QueryObject for key "
               + key
               + " to contain negative values (indicating it was updated), but the value for this key is "
               + qo.toStringFull());
     }
   } else {
     throw new TestException(
         "Expected value " + TestHelper.toString(value) + " to be a QueryObject");
   }
 }
 /**
  * Check that the value of the given key is expected for this test. Throw an error if any
  * problems.
  *
  * @param key The key to check.
  * @param value The value for the key.
  * @param logStr Used if throwing an error due to an unexpected value.
  */
 protected void checkValue(Object key, Object value) {
   if (value instanceof QueryObject) {
     QueryObject qo = (QueryObject) value;
     long keyCounter = NameFactory.getCounterForName(key);
     if (keyCounter != qo.aPrimitiveLong) {
       // just pick one field from the QueryObject to test; use aPrimitiveLong
       throw new TestException(
           "Inconsistent QueryObject for key " + key + ":" + qo.toStringFull());
     }
   } else {
     throw new TestException(
         "For key "
             + key
             + ", expected value "
             + TestHelper.toString(value)
             + " to be a QueryObject");
   }
 }
 /**
  * Load a region with keys and values. The number of keys and values is specified by the total
  * number of keys in keyIntervals. This can be invoked by several threads to accomplish the work.
  */
 public void loadRegion() {
   final long LOG_INTERVAL_MILLIS = 10000;
   int numKeysToCreate = keyIntervals.getNumKeys();
   long lastLogTime = System.currentTimeMillis();
   long startTime = System.currentTimeMillis();
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   do {
     long shouldAddCount =
         CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.SHOULD_ADD_COUNT);
     if (shouldAddCount > numKeysToCreate) {
       String aStr =
           "In loadRegion, shouldAddCount is "
               + shouldAddCount
               + ", numOriginalKeysCreated is "
               + sc.read(CQUtilBB.NUM_ORIGINAL_KEYS_CREATED)
               + ", numKeysToCreate is "
               + numKeysToCreate
               + ", region size is "
               + aRegion.size();
       Log.getLogWriter().info(aStr);
       NameBB.getBB().printSharedCounters();
       throw new StopSchedulingTaskOnClientOrder(aStr);
     }
     Object key = NameFactory.getNextPositiveObjectName();
     QueryObject value = getValueToAdd(key);
     value.extra = key;
     Log.getLogWriter().info("Creating with put, key " + key + ", value " + value.toStringFull());
     aRegion.put(key, value);
     sc.increment(CQUtilBB.NUM_ORIGINAL_KEYS_CREATED);
     if (System.currentTimeMillis() - lastLogTime > LOG_INTERVAL_MILLIS) {
       Log.getLogWriter()
           .info(
               "Added "
                   + NameFactory.getPositiveNameCounter()
                   + " out of "
                   + numKeysToCreate
                   + " entries into "
                   + TestHelper.regionToString(aRegion, false));
       lastLogTime = System.currentTimeMillis();
     }
   } while ((minTaskGranularitySec == -1)
       || (System.currentTimeMillis() - startTime < minTaskGranularityMS));
 }
  /**
   * Verify the result of the CQs registered for this VM. Query results from
   * executeWithInitialResults are combined with subsequent cq events to form the final result.
   */
  protected void verifyQueryResultsCombine() {
    Log.getLogWriter().info("In verifyQueryResultsCombine");
    Iterator it = queryMap.keySet().iterator();
    int count = 0;
    while (it.hasNext()) {
      count++;
      String cqName = (String) (it.next());
      Log.getLogWriter()
          .info("Verifying query " + count + " out of " + queryMap.size() + " with name " + cqName);
      CqQuery cq = qService.getCq(cqName);
      String queryStr = cq.getQueryString();
      String readableQueryStr = CQTest.getReadableQueryString(queryStr);

      // combine the initial selectResults with history of events
      CQHistory history = CQHistoryListener.getCQHistory(cqName);
      Map combinedMap = history.getCombinedResults();
      List combinedList = new ArrayList(combinedMap.values());
      List expectedResults = CQTestInstance.getExpectedResults(queryStr);
      List missingInCombined = new ArrayList(expectedResults);
      List unexpectedInCombined = new ArrayList(combinedList);
      unexpectedInCombined.removeAll(expectedResults);
      missingInCombined.removeAll(combinedList);

      // prepare error Strings
      StringBuffer aStr = new StringBuffer();
      if (unexpectedInCombined.size() > 0) {
        String tmpStr =
            getLocationString(unexpectedInCombined, expectedResults, history)
                + "\n"
                + "Found the following "
                + unexpectedInCombined.size()
                + " unexpected elements in combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(unexpectedInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }
      if (missingInCombined.size() > 0) {
        String tmpStr =
            getLocationString(missingInCombined, expectedResults, history)
                + "\n"
                + "The following "
                + missingInCombined.size()
                + " elements were missing from combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(missingInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }

      if (aStr.length() > 0) {
        throw new TestException(
            "Probably bug 38065: For cq "
                + cqName
                + ", "
                + readableQueryStr
                + "\n"
                + aStr.toString());
      }
    }
    Log.getLogWriter().info("Done verifying " + count + " queries");
  }