001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.io.IOException;
021import java.util.Map;
022import org.apache.hadoop.hbase.util.JsonMapper;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Superclass for any type that maps to a potentially application-level query. (e.g. Put, Get,
027 * Delete, Scan, Next, etc.) Contains methods for exposure to logging and debugging tools.
028 */
029@InterfaceAudience.Public
030public abstract class Operation {
031  // TODO make this configurable
032  // TODO Do we need this anymore now we have protobuffed it all?
033  private static final int DEFAULT_MAX_COLS = 5;
034
035  /**
036   * Produces a Map containing a fingerprint which identifies the type and the static schema
037   * components of a query (i.e. column families)
038   * @return a map containing fingerprint information (i.e. column families)
039   */
040  public abstract Map<String, Object> getFingerprint();
041
042  /**
043   * Produces a Map containing a summary of the details of a query beyond the scope of the
044   * fingerprint (i.e. columns, rows...)
045   * @param maxCols a limit on the number of columns output prior to truncation
046   * @return a map containing parameters of a query (i.e. rows, columns...)
047   */
048  public abstract Map<String, Object> toMap(int maxCols);
049
050  /**
051   * Produces a Map containing a full summary of a query.
052   * @return a map containing parameters of a query (i.e. rows, columns...)
053   */
054  public Map<String, Object> toMap() {
055    return toMap(DEFAULT_MAX_COLS);
056  }
057
058  /**
059   * Produces a JSON object for fingerprint and details exposure in a parseable format.
060   * @param maxCols a limit on the number of columns to include in the JSON
061   * @return a JSONObject containing this Operation's information, as a string
062   */
063  public String toJSON(int maxCols) throws IOException {
064    return JsonMapper.writeMapAsString(toMap(maxCols));
065  }
066
067  /**
068   * Produces a JSON object sufficient for description of a query in a debugging or logging context.
069   * @return the produced JSON object, as a string
070   */
071  public String toJSON() throws IOException {
072    return toJSON(DEFAULT_MAX_COLS);
073  }
074
075  /**
076   * Produces a string representation of this Operation. It defaults to a JSON representation, but
077   * falls back to a string representation of the fingerprint and details in the case of a JSON
078   * encoding failure.
079   * @param maxCols a limit on the number of columns output in the summary prior to truncation
080   * @return a JSON-parseable String
081   */
082  public String toString(int maxCols) {
083    /*
084     * for now this is merely a wrapper from producing a JSON string, but toJSON is kept separate in
085     * case this is changed to be a less parsable pretty printed representation.
086     */
087    try {
088      return toJSON(maxCols);
089    } catch (IOException ioe) {
090      return toMap(maxCols).toString();
091    }
092  }
093
094  /**
095   * Produces a string representation of this Operation. It defaults to a JSON representation, but
096   * falls back to a string representation of the fingerprint and details in the case of a JSON
097   * encoding failure.
098   */
099  @Override
100  public String toString() {
101    return toString(DEFAULT_MAX_COLS);
102  }
103}