001/*
002 * MetricsRecordImpl.java
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.metrics.spi;
022
023import java.util.LinkedHashMap;
024import java.util.Map;
025
026import org.apache.hadoop.classification.InterfaceAudience;
027import org.apache.hadoop.classification.InterfaceStability;
028import org.apache.hadoop.metrics.MetricsException;
029import org.apache.hadoop.metrics.MetricsRecord;
030import org.apache.hadoop.metrics.spi.AbstractMetricsContext.TagMap;
031
032/**
033 * An implementation of MetricsRecord.  Keeps a back-pointer to the context
034 * from which it was created, and delegates back to it on <code>update</code>
035 * and <code>remove()</code>.
036 *
037 * @deprecated Use {@link org.apache.hadoop.metrics2.impl.MetricsRecordImpl}
038 * instead.
039 */
040@Deprecated
041@InterfaceAudience.Public
042@InterfaceStability.Evolving
043public class MetricsRecordImpl implements MetricsRecord {
044    
045  private TagMap tagTable = new TagMap();
046  private Map<String,MetricValue> metricTable = new LinkedHashMap<String,MetricValue>();
047    
048  private String recordName;
049  private AbstractMetricsContext context;
050    
051    
052  /** Creates a new instance of FileRecord */
053  protected MetricsRecordImpl(String recordName, AbstractMetricsContext context)
054  {
055    this.recordName = recordName;
056    this.context = context;
057  }
058    
059  /**
060   * Returns the record name. 
061   *
062   * @return the record name
063   */
064  public String getRecordName() {
065    return recordName;
066  }
067    
068  /**
069   * Sets the named tag to the specified value.
070   *
071   * @param tagName name of the tag
072   * @param tagValue new value of the tag
073   * @throws MetricsException if the tagName conflicts with the configuration
074   */
075  public void setTag(String tagName, String tagValue) {
076    if (tagValue == null) {
077      tagValue = "";
078    }
079    tagTable.put(tagName, tagValue);
080  }
081    
082  /**
083   * Sets the named tag to the specified value.
084   *
085   * @param tagName name of the tag
086   * @param tagValue new value of the tag
087   * @throws MetricsException if the tagName conflicts with the configuration
088   */
089  public void setTag(String tagName, int tagValue) {
090    tagTable.put(tagName, Integer.valueOf(tagValue));
091  }
092    
093  /**
094   * Sets the named tag to the specified value.
095   *
096   * @param tagName name of the tag
097   * @param tagValue new value of the tag
098   * @throws MetricsException if the tagName conflicts with the configuration
099   */
100  public void setTag(String tagName, long tagValue) {
101    tagTable.put(tagName, Long.valueOf(tagValue));
102  }
103    
104  /**
105   * Sets the named tag to the specified value.
106   *
107   * @param tagName name of the tag
108   * @param tagValue new value of the tag
109   * @throws MetricsException if the tagName conflicts with the configuration
110   */
111  public void setTag(String tagName, short tagValue) {
112    tagTable.put(tagName, Short.valueOf(tagValue));
113  }
114    
115  /**
116   * Sets the named tag to the specified value.
117   *
118   * @param tagName name of the tag
119   * @param tagValue new value of the tag
120   * @throws MetricsException if the tagName conflicts with the configuration
121   */
122  public void setTag(String tagName, byte tagValue) {
123    tagTable.put(tagName, Byte.valueOf(tagValue));
124  }
125    
126  /**
127   * Removes any tag of the specified name.
128   */
129  public void removeTag(String tagName) {
130    tagTable.remove(tagName);
131  }
132  
133  /**
134   * Sets the named metric to the specified value.
135   *
136   * @param metricName name of the metric
137   * @param metricValue new value of the metric
138   * @throws MetricsException if the metricName or the type of the metricValue 
139   * conflicts with the configuration
140   */
141  public void setMetric(String metricName, int metricValue) {
142    setAbsolute(metricName, Integer.valueOf(metricValue));
143  }
144    
145  /**
146   * Sets the named metric to the specified value.
147   *
148   * @param metricName name of the metric
149   * @param metricValue new value of the metric
150   * @throws MetricsException if the metricName or the type of the metricValue 
151   * conflicts with the configuration
152   */
153  public void setMetric(String metricName, long metricValue) {
154    setAbsolute(metricName, Long.valueOf(metricValue));
155  }
156    
157  /**
158   * Sets the named metric to the specified value.
159   *
160   * @param metricName name of the metric
161   * @param metricValue new value of the metric
162   * @throws MetricsException if the metricName or the type of the metricValue 
163   * conflicts with the configuration
164   */
165  public void setMetric(String metricName, short metricValue) {
166    setAbsolute(metricName, Short.valueOf(metricValue));
167  }
168    
169  /**
170   * Sets the named metric to the specified value.
171   *
172   * @param metricName name of the metric
173   * @param metricValue new value of the metric
174   * @throws MetricsException if the metricName or the type of the metricValue 
175   * conflicts with the configuration
176   */
177  public void setMetric(String metricName, byte metricValue) {
178    setAbsolute(metricName, Byte.valueOf(metricValue));
179  }
180    
181  /**
182   * Sets the named metric to the specified value.
183   *
184   * @param metricName name of the metric
185   * @param metricValue new value of the metric
186   * @throws MetricsException if the metricName or the type of the metricValue 
187   * conflicts with the configuration
188   */
189  public void setMetric(String metricName, float metricValue) {
190    setAbsolute(metricName, new Float(metricValue));
191  }
192    
193  /**
194   * Increments the named metric by the specified value.
195   *
196   * @param metricName name of the metric
197   * @param metricValue incremental value
198   * @throws MetricsException if the metricName or the type of the metricValue 
199   * conflicts with the configuration
200   */
201  public void incrMetric(String metricName, int metricValue) {
202    setIncrement(metricName, Integer.valueOf(metricValue));
203  }
204    
205  /**
206   * Increments the named metric by the specified value.
207   *
208   * @param metricName name of the metric
209   * @param metricValue incremental value
210   * @throws MetricsException if the metricName or the type of the metricValue 
211   * conflicts with the configuration
212   */
213  public void incrMetric(String metricName, long metricValue) {
214    setIncrement(metricName, Long.valueOf(metricValue));
215  }
216    
217  /**
218   * Increments the named metric by the specified value.
219   *
220   * @param metricName name of the metric
221   * @param metricValue incremental value
222   * @throws MetricsException if the metricName or the type of the metricValue 
223   * conflicts with the configuration
224   */
225  public void incrMetric(String metricName, short metricValue) {
226    setIncrement(metricName, Short.valueOf(metricValue));
227  }
228    
229  /**
230   * Increments the named metric by the specified value.
231   *
232   * @param metricName name of the metric
233   * @param metricValue incremental value
234   * @throws MetricsException if the metricName or the type of the metricValue 
235   * conflicts with the configuration
236   */
237  public void incrMetric(String metricName, byte metricValue) {
238    setIncrement(metricName, Byte.valueOf(metricValue));
239  }
240    
241  /**
242   * Increments the named metric by the specified value.
243   *
244   * @param metricName name of the metric
245   * @param metricValue incremental value
246   * @throws MetricsException if the metricName or the type of the metricValue 
247   * conflicts with the configuration
248   */
249  public void incrMetric(String metricName, float metricValue) {
250    setIncrement(metricName, new Float(metricValue));
251  }
252    
253  private void setAbsolute(String metricName, Number metricValue) {
254    metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE));
255  }
256    
257  private void setIncrement(String metricName, Number metricValue) {
258    metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT));
259  }
260    
261  /**
262   * Updates the table of buffered data which is to be sent periodically.
263   * If the tag values match an existing row, that row is updated; 
264   * otherwise, a new row is added.
265   */
266  public void update() {
267    context.update(this);
268  }
269    
270  /**
271   * Removes the row, if it exists, in the buffered data table having tags 
272   * that equal the tags that have been set on this record. 
273   */
274  public void remove() {
275    context.remove(this);
276  }
277
278  TagMap getTagTable() {
279    return tagTable;
280  }
281
282  Map<String, MetricValue> getMetricTable() {
283    return metricTable;
284  }
285}