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  @Override
065  public String getRecordName() {
066    return recordName;
067  }
068    
069  /**
070   * Sets the named tag to the specified value.
071   *
072   * @param tagName name of the tag
073   * @param tagValue new value of the tag
074   * @throws MetricsException if the tagName conflicts with the configuration
075   */
076  @Override
077  public void setTag(String tagName, String tagValue) {
078    if (tagValue == null) {
079      tagValue = "";
080    }
081    tagTable.put(tagName, tagValue);
082  }
083    
084  /**
085   * Sets the named tag to the specified value.
086   *
087   * @param tagName name of the tag
088   * @param tagValue new value of the tag
089   * @throws MetricsException if the tagName conflicts with the configuration
090   */
091  @Override
092  public void setTag(String tagName, int tagValue) {
093    tagTable.put(tagName, Integer.valueOf(tagValue));
094  }
095    
096  /**
097   * Sets the named tag to the specified value.
098   *
099   * @param tagName name of the tag
100   * @param tagValue new value of the tag
101   * @throws MetricsException if the tagName conflicts with the configuration
102   */
103  @Override
104  public void setTag(String tagName, long tagValue) {
105    tagTable.put(tagName, Long.valueOf(tagValue));
106  }
107    
108  /**
109   * Sets the named tag to the specified value.
110   *
111   * @param tagName name of the tag
112   * @param tagValue new value of the tag
113   * @throws MetricsException if the tagName conflicts with the configuration
114   */
115  @Override
116  public void setTag(String tagName, short tagValue) {
117    tagTable.put(tagName, Short.valueOf(tagValue));
118  }
119    
120  /**
121   * Sets the named tag to the specified value.
122   *
123   * @param tagName name of the tag
124   * @param tagValue new value of the tag
125   * @throws MetricsException if the tagName conflicts with the configuration
126   */
127  @Override
128  public void setTag(String tagName, byte tagValue) {
129    tagTable.put(tagName, Byte.valueOf(tagValue));
130  }
131    
132  /**
133   * Removes any tag of the specified name.
134   */
135  @Override
136  public void removeTag(String tagName) {
137    tagTable.remove(tagName);
138  }
139  
140  /**
141   * Sets the named metric to the specified value.
142   *
143   * @param metricName name of the metric
144   * @param metricValue new value of the metric
145   * @throws MetricsException if the metricName or the type of the metricValue 
146   * conflicts with the configuration
147   */
148  @Override
149  public void setMetric(String metricName, int metricValue) {
150    setAbsolute(metricName, Integer.valueOf(metricValue));
151  }
152    
153  /**
154   * Sets the named metric to the specified value.
155   *
156   * @param metricName name of the metric
157   * @param metricValue new value of the metric
158   * @throws MetricsException if the metricName or the type of the metricValue 
159   * conflicts with the configuration
160   */
161  @Override
162  public void setMetric(String metricName, long metricValue) {
163    setAbsolute(metricName, Long.valueOf(metricValue));
164  }
165    
166  /**
167   * Sets the named metric to the specified value.
168   *
169   * @param metricName name of the metric
170   * @param metricValue new value of the metric
171   * @throws MetricsException if the metricName or the type of the metricValue 
172   * conflicts with the configuration
173   */
174  @Override
175  public void setMetric(String metricName, short metricValue) {
176    setAbsolute(metricName, Short.valueOf(metricValue));
177  }
178    
179  /**
180   * Sets the named metric to the specified value.
181   *
182   * @param metricName name of the metric
183   * @param metricValue new value of the metric
184   * @throws MetricsException if the metricName or the type of the metricValue 
185   * conflicts with the configuration
186   */
187  @Override
188  public void setMetric(String metricName, byte metricValue) {
189    setAbsolute(metricName, Byte.valueOf(metricValue));
190  }
191    
192  /**
193   * Sets the named metric to the specified value.
194   *
195   * @param metricName name of the metric
196   * @param metricValue new value of the metric
197   * @throws MetricsException if the metricName or the type of the metricValue 
198   * conflicts with the configuration
199   */
200  @Override
201  public void setMetric(String metricName, float metricValue) {
202    setAbsolute(metricName, new Float(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  @Override
214  public void incrMetric(String metricName, int metricValue) {
215    setIncrement(metricName, Integer.valueOf(metricValue));
216  }
217    
218  /**
219   * Increments the named metric by the specified value.
220   *
221   * @param metricName name of the metric
222   * @param metricValue incremental value
223   * @throws MetricsException if the metricName or the type of the metricValue 
224   * conflicts with the configuration
225   */
226  @Override
227  public void incrMetric(String metricName, long metricValue) {
228    setIncrement(metricName, Long.valueOf(metricValue));
229  }
230    
231  /**
232   * Increments the named metric by the specified value.
233   *
234   * @param metricName name of the metric
235   * @param metricValue incremental value
236   * @throws MetricsException if the metricName or the type of the metricValue 
237   * conflicts with the configuration
238   */
239  @Override
240  public void incrMetric(String metricName, short metricValue) {
241    setIncrement(metricName, Short.valueOf(metricValue));
242  }
243    
244  /**
245   * Increments the named metric by the specified value.
246   *
247   * @param metricName name of the metric
248   * @param metricValue incremental value
249   * @throws MetricsException if the metricName or the type of the metricValue 
250   * conflicts with the configuration
251   */
252  @Override
253  public void incrMetric(String metricName, byte metricValue) {
254    setIncrement(metricName, Byte.valueOf(metricValue));
255  }
256    
257  /**
258   * Increments the named metric by the specified value.
259   *
260   * @param metricName name of the metric
261   * @param metricValue incremental value
262   * @throws MetricsException if the metricName or the type of the metricValue 
263   * conflicts with the configuration
264   */
265  @Override
266  public void incrMetric(String metricName, float metricValue) {
267    setIncrement(metricName, new Float(metricValue));
268  }
269    
270  private void setAbsolute(String metricName, Number metricValue) {
271    metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE));
272  }
273    
274  private void setIncrement(String metricName, Number metricValue) {
275    metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT));
276  }
277    
278  /**
279   * Updates the table of buffered data which is to be sent periodically.
280   * If the tag values match an existing row, that row is updated; 
281   * otherwise, a new row is added.
282   */
283  @Override
284  public void update() {
285    context.update(this);
286  }
287    
288  /**
289   * Removes the row, if it exists, in the buffered data table having tags 
290   * that equal the tags that have been set on this record. 
291   */
292  @Override
293  public void remove() {
294    context.remove(this);
295  }
296
297  TagMap getTagTable() {
298    return tagTable;
299  }
300
301  Map<String, MetricValue> getMetricTable() {
302    return metricTable;
303  }
304}