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.util.ArrayList; 021import java.util.Collections; 022import java.util.List; 023import java.util.Map; 024import java.util.Optional; 025import java.util.Set; 026import java.util.TreeMap; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.apache.yetus.audience.InterfaceAudience; 030 031/** 032 * Container for Actions (i.e. Get, Delete, or Put), which are grouped by regionName. Intended to be 033 * used with {@link AsyncProcess}. 034 */ 035@InterfaceAudience.Private 036public final class MultiAction { 037 // TODO: This class should not be visible outside of the client package. 038 039 /** 040 * A map of regions to lists of puts/gets/deletes for that region. Package visible. 041 */ 042 Map<byte[], List<Action>> actions = new TreeMap<>(Bytes.BYTES_COMPARATOR); 043 044 private long nonceGroup = HConstants.NO_NONCE; 045 046 public MultiAction() { 047 super(); 048 } 049 050 /** 051 * Get the total number of Actions 052 * @return total number of Actions for all groups in this container. 053 */ 054 public int size() { 055 int size = 0; 056 for (List<?> l : actions.values()) { 057 size += l.size(); 058 } 059 return size; 060 } 061 062 /** 063 * Add an Action to this container based on it's regionName. If the regionName is wrong, the 064 * initial execution will fail, but will be automatically retried after looking up the correct 065 * region. 066 */ 067 public void add(byte[] regionName, Action a) { 068 add(regionName, Collections.singletonList(a)); 069 } 070 071 /** 072 * Add an Action to this container based on it's regionName. If the regionName is wrong, the 073 * initial execution will fail, but will be automatically retried after looking up the correct 074 * region. 075 */ 076 public void add(byte[] regionName, List<Action> actionList) { 077 List<Action> rsActions = actions.get(regionName); 078 if (rsActions == null) { 079 rsActions = new ArrayList<>(actionList.size()); 080 actions.put(regionName, rsActions); 081 } 082 rsActions.addAll(actionList); 083 } 084 085 public void setNonceGroup(long nonceGroup) { 086 this.nonceGroup = nonceGroup; 087 } 088 089 public Set<byte[]> getRegions() { 090 return actions.keySet(); 091 } 092 093 public boolean hasNonceGroup() { 094 return nonceGroup != HConstants.NO_NONCE; 095 } 096 097 public long getNonceGroup() { 098 return this.nonceGroup; 099 } 100 101 // returns the max priority of all the actions 102 public int getPriority() { 103 Optional<Action> result = actions.values().stream().flatMap(List::stream) 104 .max((action1, action2) -> Math.max(action1.getPriority(), action2.getPriority())); 105 return result.isPresent() ? result.get().getPriority() : HConstants.PRIORITY_UNSET; 106 } 107}