View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.codehaus.jackson.map.ObjectMapper;
24  
25  import java.io.IOException;
26  import java.util.Map;
27  
28  /**
29   * Superclass for any type that maps to a potentially application-level query.
30   * (e.g. Put, Get, Delete, Scan, Next, etc.)
31   * Contains methods for exposure to logging and debugging tools.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public abstract class Operation {
36    // TODO make this configurable
37    // TODO Do we need this anymore now we have protobuffed it all?
38    private static final int DEFAULT_MAX_COLS = 5;
39  
40    private static final ObjectMapper MAPPER = new ObjectMapper();
41  
42    /**
43     * Produces a Map containing a fingerprint which identifies the type and 
44     * the static schema components of a query (i.e. column families)
45     * @return a map containing fingerprint information (i.e. column families)
46     */
47    public abstract Map<String, Object> getFingerprint();
48  
49    /**
50     * Produces a Map containing a summary of the details of a query 
51     * beyond the scope of the fingerprint (i.e. columns, rows...)
52     * @param maxCols a limit on the number of columns output prior to truncation
53     * @return a map containing parameters of a query (i.e. rows, columns...)
54     */
55    public abstract Map<String, Object> toMap(int maxCols);
56  
57    /**
58     * Produces a Map containing a full summary of a query.
59     * @return a map containing parameters of a query (i.e. rows, columns...)
60     */
61    public Map<String, Object> toMap() {
62      return toMap(DEFAULT_MAX_COLS);
63    }
64  
65    /**
66     * Produces a JSON object for fingerprint and details exposure in a
67     * parseable format.
68     * @param maxCols a limit on the number of columns to include in the JSON
69     * @return a JSONObject containing this Operation's information, as a string
70     */
71    public String toJSON(int maxCols) throws IOException {
72      return MAPPER.writeValueAsString(toMap(maxCols));
73    }
74  
75    /**
76     * Produces a JSON object sufficient for description of a query
77     * in a debugging or logging context.
78     * @return the produced JSON object, as a string
79     */
80    public String toJSON() throws IOException {
81      return toJSON(DEFAULT_MAX_COLS);
82    }
83  
84    /**
85     * Produces a string representation of this Operation. It defaults to a JSON
86     * representation, but falls back to a string representation of the 
87     * fingerprint and details in the case of a JSON encoding failure.
88     * @param maxCols a limit on the number of columns output in the summary
89     * prior to truncation
90     * @return a JSON-parseable String
91     */
92    public String toString(int maxCols) {
93      /* for now this is merely a wrapper from producing a JSON string, but 
94       * toJSON is kept separate in case this is changed to be a less parsable
95       * pretty printed representation.
96       */
97      try {
98        return toJSON(maxCols);
99      } catch (IOException ioe) {
100       return toMap(maxCols).toString();
101     }
102   }
103 
104   /**
105    * Produces a string representation of this Operation. It defaults to a JSON
106    * representation, but falls back to a string representation of the
107    * fingerprint and details in the case of a JSON encoding failure.
108    * @return String
109    */
110   @Override
111   public String toString() {
112     return toString(DEFAULT_MAX_COLS);
113   }
114 }