public QueryId() { builder = ApplicationAttemptIdProto.newBuilder(); }
private synchronized void maybeInitBuilder() { if (viaProto || builder == null) { builder = ApplicationAttemptIdProto.newBuilder(proto); } viaProto = false; }
/** QueryId represents a unique identifier of a query. */ public class QueryId implements Comparable<QueryId> { public static final String PREFIX = "q"; public static final String SEPARATOR = "_"; ApplicationAttemptIdProto proto = ApplicationAttemptIdProto.getDefaultInstance(); ApplicationAttemptIdProto.Builder builder = null; boolean viaProto = false; private ApplicationId applicationId = null; public QueryId() { builder = ApplicationAttemptIdProto.newBuilder(); } public QueryId(ApplicationAttemptIdProto proto) { this.proto = proto; viaProto = true; } public synchronized ApplicationAttemptIdProto getProto() { mergeLocalToProto(); proto = viaProto ? proto : builder.build(); viaProto = true; return proto; } private synchronized void mergeLocalToBuilder() { if (this.applicationId != null && !((ApplicationIdPBImpl) applicationId).getProto().equals(builder.getApplicationId())) { builder.setApplicationId(convertToProtoFormat(this.applicationId)); } } private synchronized void mergeLocalToProto() { if (viaProto) maybeInitBuilder(); mergeLocalToBuilder(); proto = builder.build(); viaProto = true; } private synchronized void maybeInitBuilder() { if (viaProto || builder == null) { builder = ApplicationAttemptIdProto.newBuilder(proto); } viaProto = false; } public synchronized int getAttemptId() { ApplicationAttemptIdProtoOrBuilder p = viaProto ? proto : builder; return (p.getAttemptId()); } public synchronized void setAttemptId(int attemptId) { maybeInitBuilder(); builder.setAttemptId((attemptId)); } public synchronized ApplicationId getApplicationId() { ApplicationAttemptIdProtoOrBuilder p = viaProto ? proto : builder; if (this.applicationId != null) { return this.applicationId; } if (!p.hasApplicationId()) { return null; } this.applicationId = convertFromProtoFormat(p.getApplicationId()); return this.applicationId; } public synchronized void setApplicationId(ApplicationId appId) { maybeInitBuilder(); if (appId == null) builder.clearApplicationId(); this.applicationId = appId; } private ApplicationIdPBImpl convertFromProtoFormat(ApplicationIdProto p) { return new ApplicationIdPBImpl(p); } private ApplicationIdProto convertToProtoFormat(ApplicationId t) { return ((ApplicationIdPBImpl) t).getProto(); } @Override public int compareTo(QueryId queryId) { int compVal = getApplicationId().compareTo(queryId.getApplicationId()); if (compVal != 0) { return compVal; } else { return getAttemptId() - queryId.getAttemptId(); } } static final ThreadLocal<NumberFormat> appIdFormat = new ThreadLocal<NumberFormat>() { @Override public NumberFormat initialValue() { NumberFormat fmt = NumberFormat.getInstance(); fmt.setGroupingUsed(false); fmt.setMinimumIntegerDigits(4); return fmt; } }; static final ThreadLocal<NumberFormat> attemptIdFormat = new ThreadLocal<NumberFormat>() { @Override public NumberFormat initialValue() { NumberFormat fmt = NumberFormat.getInstance(); fmt.setGroupingUsed(false); fmt.setMinimumIntegerDigits(6); return fmt; } }; @Override public int hashCode() { // Generated by eclipse. final int prime = 31; int result = 1; ApplicationId appId = getApplicationId(); result = prime * result + appId.hashCode(); result = prime * result + getAttemptId(); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; QueryId other = (QueryId) obj; if (!this.getApplicationId().equals(other.getApplicationId())) return false; if (this.getAttemptId() != other.getAttemptId()) return false; return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(PREFIX) .append(SEPARATOR) .append(this.getApplicationId().getClusterTimestamp()) .append(SEPARATOR) .append(appIdFormat.get().format(this.getApplicationId().getId())) .append(SEPARATOR) .append(attemptIdFormat.get().format(getAttemptId())); return sb.toString(); } }