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 /**
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 ObjectMapper mapper = new ObjectMapper();
71 return mapper.writeValueAsString(toMap(maxCols));
72 }
73
74 /**
75 * Produces a JSON object sufficient for description of a query
76 * in a debugging or logging context.
77 * @return the produced JSON object, as a string
78 */
79 public String toJSON() throws IOException {
80 return toJSON(DEFAULT_MAX_COLS);
81 }
82
83 /**
84 * Produces a string representation of this Operation. It defaults to a JSON
85 * representation, but falls back to a string representation of the
86 * fingerprint and details in the case of a JSON encoding failure.
87 * @param maxCols a limit on the number of columns output in the summary
88 * prior to truncation
89 * @return a JSON-parseable String
90 */
91 public String toString(int maxCols) {
92 /* for now this is merely a wrapper from producing a JSON string, but
93 * toJSON is kept separate in case this is changed to be a less parsable
94 * pretty printed representation.
95 */
96 try {
97 return toJSON(maxCols);
98 } catch (IOException ioe) {
99 return toMap(maxCols).toString();
100 }
101 }
102
103 /**
104 * Produces a string representation of this Operation. It defaults to a JSON
105 * representation, but falls back to a string representation of the
106 * fingerprint and details in the case of a JSON encoding failure.
107 * @return String
108 */
109 @Override
110 public String toString() {
111 return toString(DEFAULT_MAX_COLS);
112 }
113 }