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.wal;
019
020import org.apache.hadoop.hbase.HBaseInterfaceAudience;
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.regionserver.SequenceId;
024import org.apache.hadoop.hbase.util.Bytes;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032import java.util.UUID;
033
034
035/**
036 * Key for WAL Entry.
037 */
038@InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.REPLICATION,
039    HBaseInterfaceAudience.COPROC})
040public interface WALKey extends SequenceId, Comparable<WALKey> {
041  /**
042   * Unmodifiable empty list of UUIDs.
043   */
044  List<UUID> EMPTY_UUIDS = Collections.unmodifiableList(new ArrayList<UUID>());
045
046  default long estimatedSerializedSizeOf() {
047    return 0;
048  }
049
050  /**
051   * @return encoded region name
052   */
053  byte[] getEncodedRegionName();
054
055  /**
056   * @return table name
057   */
058  TableName getTableName();
059
060  /**
061   * @return the write time
062   */
063  long getWriteTime();
064
065  /**
066   * @return The nonce group
067   */
068  default long getNonceGroup() {
069    return HConstants.NO_NONCE;
070  }
071
072  /**
073   * @return The nonce
074   */
075  default long getNonce() {
076    return HConstants.NO_NONCE;
077  }
078
079  UUID getOriginatingClusterId();
080
081  /**
082   * Return a positive long if current WALKeyImpl is created from a replay edit; a replay edit is an
083   * edit that came in when replaying WALs of a crashed server.
084   * @return original sequence number of the WALEdit
085   */
086  long getOrigLogSeqNum();
087
088  /**
089   * Add a named String value to this WALKey to be persisted into the WAL
090   * @param attributeKey Name of the attribute
091   * @param attributeValue Value of the attribute
092   */
093  void addExtendedAttribute(String attributeKey, byte[] attributeValue);
094
095    /**
096     * Return a named String value injected into the WALKey during processing, such as by a
097     * coprocessor
098     * @param attributeKey The key of a key / value pair
099     */
100  default byte[] getExtendedAttribute(String attributeKey){
101    return null;
102  }
103
104    /**
105     * Returns a map of all extended attributes injected into this WAL key.
106     */
107  default Map<String, byte[]> getExtendedAttributes() {
108    return new HashMap<>();
109  }
110  /**
111   * Produces a string map for this key. Useful for programmatic use and
112   * manipulation of the data stored in an WALKeyImpl, for example, printing
113   * as JSON.
114   *
115   * @return a Map containing data from this key
116   */
117  default Map<String, Object> toStringMap() {
118    Map<String, Object> stringMap = new HashMap<>();
119    stringMap.put("table", getTableName());
120    stringMap.put("region", Bytes.toStringBinary(getEncodedRegionName()));
121    stringMap.put("sequence", getSequenceId());
122    Map<String, byte[]> extendedAttributes = getExtendedAttributes();
123    if (extendedAttributes != null){
124      for (Map.Entry<String, byte[]> entry : extendedAttributes.entrySet()){
125        stringMap.put(entry.getKey(), Bytes.toStringBinary(entry.getValue()));
126      }
127    }
128    return stringMap;
129  }
130}