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 java.io.IOException;
22  import java.util.Map;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.util.JsonMapper;
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    /**
41     * Produces a Map containing a fingerprint which identifies the type and 
42     * the static schema components of a query (i.e. column families)
43     * @return a map containing fingerprint information (i.e. column families)
44     */
45    public abstract Map<String, Object> getFingerprint();
46  
47    /**
48     * Produces a Map containing a summary of the details of a query 
49     * beyond the scope of the fingerprint (i.e. columns, rows...)
50     * @param maxCols a limit on the number of columns output prior to truncation
51     * @return a map containing parameters of a query (i.e. rows, columns...)
52     */
53    public abstract Map<String, Object> toMap(int maxCols);
54  
55    /**
56     * Produces a Map containing a full summary of a query.
57     * @return a map containing parameters of a query (i.e. rows, columns...)
58     */
59    public Map<String, Object> toMap() {
60      return toMap(DEFAULT_MAX_COLS);
61    }
62  
63    /**
64     * Produces a JSON object for fingerprint and details exposure in a
65     * parseable format.
66     * @param maxCols a limit on the number of columns to include in the JSON
67     * @return a JSONObject containing this Operation's information, as a string
68     */
69    public String toJSON(int maxCols) throws IOException {
70      return JsonMapper.writeMapAsString(toMap(maxCols));
71    }
72  
73    /**
74     * Produces a JSON object sufficient for description of a query
75     * in a debugging or logging context.
76     * @return the produced JSON object, as a string
77     */
78    public String toJSON() throws IOException {
79      return toJSON(DEFAULT_MAX_COLS);
80    }
81  
82    /**
83     * Produces a string representation of this Operation. It defaults to a JSON
84     * representation, but falls back to a string representation of the 
85     * fingerprint and details in the case of a JSON encoding failure.
86     * @param maxCols a limit on the number of columns output in the summary
87     * prior to truncation
88     * @return a JSON-parseable String
89     */
90    public String toString(int maxCols) {
91      /* for now this is merely a wrapper from producing a JSON string, but 
92       * toJSON is kept separate in case this is changed to be a less parsable
93       * pretty printed representation.
94       */
95      try {
96        return toJSON(maxCols);
97      } catch (IOException ioe) {
98        return toMap(maxCols).toString();
99      }
100   }
101 
102   /**
103    * Produces a string representation of this Operation. It defaults to a JSON
104    * representation, but falls back to a string representation of the
105    * fingerprint and details in the case of a JSON encoding failure.
106    * @return String
107    */
108   @Override
109   public String toString() {
110     return toString(DEFAULT_MAX_COLS);
111   }
112 }