private void mergeToHeap(
        ActionCombinerValue newValList,
        UserActiveHistory oldVal,
        UserActiveHistory.Builder updatedBuilder) {
      HashSet<String> alreadyIn = new HashSet<String>();

      for (String newItem : newValList.getActRecodeMap().keySet()) {
        if (updatedBuilder.getActRecordsCount() >= topNum) {
          break;
        }

        if (!alreadyIn.contains(newItem)) {
          updatedBuilder.addActRecords(newValList.getActRecodeMap().get(newItem));
          alreadyIn.add(newItem);
        }
      }

      if (oldVal != null) {
        for (Recommend.UserActiveHistory.ActiveRecord eachOldVal : oldVal.getActRecordsList()) {
          if (updatedBuilder.getActRecordsCount() >= topNum) {
            break;
          }
          if (alreadyIn.contains(eachOldVal.getItem())) {
            continue;
          }
          updatedBuilder.addActRecords(eachOldVal);
        }
      }
    }
  @Override
  public void processEvent(String sid, Tuple tuple) {
    try {
      String bid = tuple.getStringByField("bid");
      String qq = tuple.getStringByField("qq");
      String uid = tuple.getStringByField("uid");
      String itemId = tuple.getStringByField("item_id");

      String actionType = tuple.getStringByField("action_type");
      String actionTime = tuple.getStringByField("action_time");
      String lbsInfo = tuple.getStringByField("lbs_info");
      String platform = tuple.getStringByField("platform");

      Long bigType = tuple.getLongByField("big_type");
      Long midType = tuple.getLongByField("mid_type");
      Long smallType = tuple.getLongByField("small_type");
      Long itemTime = tuple.getLongByField("item_time");
      String shopId = tuple.getStringByField("shop_id");

      if (!Utils.isItemIdValid(itemId) || Utils.isRecommendAction(actionType)) {
        return;
      }

      if (qq.equals("389687043") || qq.equals("475182144")) {
        logger.info("--input to combiner---" + tuple.toString());
      }

      Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder =
          Recommend.UserActiveHistory.ActiveRecord.newBuilder();
      actBuilder
          .setItem(itemId)
          .setActTime(Long.valueOf(actionTime))
          .setActType(Integer.valueOf(actionType))
          .setBigType(bigType)
          .setMiddleType(midType)
          .setSmallType(smallType)
          .setLBSInfo(lbsInfo)
          .setPlatForm(platform)
          .setShopId(shopId)
          .setItemTime(itemTime);

      ActionCombinerValue value = new ActionCombinerValue();
      value.init(itemId, actBuilder.build());

      if (Utils.isQNumValid(qq)) {
        String key = bid + "#" + qq + "#" + Constants.topN_alg_name;
        combinerKeys(key, value);
      } else if (!uid.equals("0") && !uid.equals("")) {
        String key = bid + "#" + uid + "#" + Constants.topN_Noqq_alg_name;
        if (debug) {
          logger.info("input ,key=" + key);
        }
        combinerKeys(key, value);
      }

    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
  }
 private void combinerKeys(String key, ActionCombinerValue value) {
   synchronized (liveCombinerMap) {
     if (liveCombinerMap.containsKey(key)) {
       ActionCombinerValue oldvalue = liveCombinerMap.get(key);
       oldvalue.incrument(value);
       liveCombinerMap.put(key, oldvalue);
     } else {
       liveCombinerMap.put(key, value);
     }
   }
 }
    private void mergeToHeap(
        ActionCombinerValue newValueList,
        UserActiveDetail oldValueHeap,
        UserActiveDetail.Builder mergeValueBuilder) {

      HashMap<Long, HashMap<String, HashMap<Integer, ActType>>> detailMap =
          new HashMap<Long, HashMap<String, HashMap<Integer, ActType>>>();
      if (oldValueHeap != null && oldValueHeap.getTsegsCount() > 0) {
        mergeOldToMap(oldValueHeap, detailMap);
        if (debug) {
          logger.info("add old values,now size=" + oldValueHeap.getTsegsCount());
        }
      }

      for (String item : newValueList.getActRecodeMap().keySet()) {
        ActiveRecord action = newValueList.getActRecodeMap().get(item);
        Long winId = Utils.getDateByTime(action.getActTime());
        mergeNewRecordsToMap(winId, item, action, detailMap);
        if (debug) {
          logger.info("add new values,size=" + detailMap.get(winId).size());
        }
      }
      changeMapToPB(detailMap, mergeValueBuilder);
    }
  @Override
  public void processEvent(String sid, Tuple tuple) {
    try {
      String bid = tuple.getStringByField("bid");
      String adpos = Constants.DEFAULT_ADPOS;
      String qq = tuple.getStringByField("qq");
      String itemId = tuple.getStringByField("item_id");

      if (!Utils.isQNumValid(qq)) {
        return;
      }

      if (qq.equals("389687043") || qq.equals("475182144")) {
        logger.info("--input to combiner---" + tuple.toString());
      }

      String actionType = tuple.getStringByField("action_type");
      String actionTime = tuple.getStringByField("action_time");
      String lbsInfo = tuple.getStringByField("lbs_info");
      String platform = tuple.getStringByField("platform");

      if (sid.equals(Constants.recommend_action_stream) && Utils.isRecommendAction(actionType)) {
        String actionResult = tuple.getStringByField("action_result");
        String[] items = actionResult.split(";", -1);
        for (String resultItem : items) {
          if (resultItem.endsWith("#1") || resultItem.endsWith("#2")) {
            resultItem = resultItem.substring(0, resultItem.length() - 2);
          }

          if (Utils.isItemIdValid(resultItem)) {
            Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder =
                Recommend.UserActiveHistory.ActiveRecord.newBuilder();
            actBuilder
                .setItem(resultItem)
                .setActTime(Long.valueOf(actionTime))
                .setActType(Integer.valueOf(actionType))
                .setLBSInfo(lbsInfo)
                .setPlatForm(platform);

            ActionCombinerValue value = new ActionCombinerValue();
            value.init(resultItem, actBuilder.build());
            UpdateKey key = new UpdateKey(bid, Long.valueOf(qq), 0, adpos, resultItem);

            if (qq.equals("389687043") || qq.equals("475182144")) {
              logger.info(
                  "--input impress to combiner---,itemId="
                      + resultItem
                      + ",key="
                      + key.getDetailKey());
            }

            combinerKeys(key.getDetailKey(), value);
          }
        }
      } else if (sid.equals(Constants.actions_stream) && !Utils.isRecommendAction(actionType)) {
        Recommend.UserActiveHistory.ActiveRecord.Builder actBuilder =
            Recommend.UserActiveHistory.ActiveRecord.newBuilder();
        actBuilder
            .setItem(itemId)
            .setActTime(Long.valueOf(actionTime))
            .setActType(Integer.valueOf(actionType))
            .setLBSInfo(lbsInfo)
            .setPlatForm(platform);

        if (Utils.isItemIdValid(itemId)) {
          ActionCombinerValue value = new ActionCombinerValue();
          value.init(itemId, actBuilder.build());
          UpdateKey key = new UpdateKey(bid, Long.valueOf(qq), 0, adpos, itemId);
          combinerKeys(key.getDetailKey(), value);
        }
      }

    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
  }