Java Code Examples for org.apache.hadoop.hbase.HConstants#DAY_IN_SECONDS

The following examples show how to use org.apache.hadoop.hbase.HConstants#DAY_IN_SECONDS . 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: PrettyPrinter.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a human readable time interval to seconds. Examples of the human readable
 * time intervals are: 50 DAYS 1 HOUR 30 MINUTES , 25000 SECONDS etc.
 * The units of time specified can be in uppercase as well as lowercase. Also, if a
 * single number is specified without any time unit, it is assumed to be in seconds.
 * @param humanReadableInterval
 * @return value in seconds
 */
private static long humanReadableIntervalToSec(final String humanReadableInterval)
        throws HBaseException {
  if (humanReadableInterval == null || humanReadableInterval.equalsIgnoreCase("FOREVER")) {
    return HConstants.FOREVER;
  }

  try {
    return Long.parseLong(humanReadableInterval);
  } catch(NumberFormatException ex) {
    LOG.debug("Given interval value is not a number, parsing for human readable format");
  }

  String days = null;
  String hours = null;
  String minutes = null;
  String seconds = null;
  String expectedTtl = null;
  long ttl;

  Matcher matcher = PrettyPrinter.INTERVAL_PATTERN.matcher(humanReadableInterval);
  if (matcher.matches()) {
    expectedTtl = matcher.group(2);
    days = matcher.group(4);
    hours = matcher.group(6);
    minutes = matcher.group(8);
    seconds = matcher.group(10);
  }
  ttl = 0;
  ttl += days != null ? Long.parseLong(days)*HConstants.DAY_IN_SECONDS:0;
  ttl += hours != null ? Long.parseLong(hours)*HConstants.HOUR_IN_SECONDS:0;
  ttl += minutes != null ? Long.parseLong(minutes)*HConstants.MINUTE_IN_SECONDS:0;
  ttl += seconds != null ? Long.parseLong(seconds):0;

  if (expectedTtl != null && Long.parseLong(expectedTtl) != ttl) {
    throw new HBaseException("Malformed TTL string: TTL values in seconds and human readable" +
            "format do not match");
  }
  return ttl;
}
 
Example 2
Source File: PrettyPrinter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG",
    justification="Will not overflow")
private static String humanReadableTTL(final long interval){
  StringBuilder sb = new StringBuilder();
  int days, hours, minutes, seconds;

  // edge cases first
  if (interval == Integer.MAX_VALUE) {
    sb.append("FOREVER");
    return sb.toString();
  }
  if (interval < HConstants.MINUTE_IN_SECONDS) {
    sb.append(interval);
    sb.append(" SECOND").append(interval == 1 ? "" : "S");
    return sb.toString();
  }

  days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
  hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
  minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
      - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
  seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
      - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);

  sb.append(interval);
  sb.append(" SECONDS (");

  if (days > 0) {
    sb.append(days);
    sb.append(" DAY").append(days == 1 ? "" : "S");
  }

  if (hours > 0) {
    sb.append(days > 0 ? " " : "");
    sb.append(hours);
    sb.append(" HOUR").append(hours == 1 ? "" : "S");
  }

  if (minutes > 0) {
    sb.append(days + hours > 0 ? " " : "");
    sb.append(minutes);
    sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
  }

  if (seconds > 0) {
    sb.append(days + hours + minutes > 0 ? " " : "");
    sb.append(seconds);
    sb.append(" SECOND").append(minutes == 1 ? "" : "S");
  }

  sb.append(")");

  return sb.toString();
}