View Javadoc

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