1 18 package org.apache.activemq.broker.region.group; 19 20 import org.apache.activemq.command.ConsumerId; 21 22 27 public class MessageGroupHashBucket implements MessageGroupMap { 28 29 private final int bucketCount; 30 private final ConsumerId[] consumers; 31 32 public MessageGroupHashBucket(int bucketCount) { 33 this.bucketCount = bucketCount; 34 this.consumers = new ConsumerId[bucketCount]; 35 } 36 37 public void put(String groupId, ConsumerId consumerId) { 38 int bucket = getBucketNumber(groupId); 39 consumers[bucket] = consumerId; 40 } 41 42 public ConsumerId get(String groupId) { 43 int bucket = getBucketNumber(groupId); 44 return consumers[bucket]; 45 } 46 47 public ConsumerId removeGroup(String groupId) { 48 int bucket = getBucketNumber(groupId); 49 ConsumerId answer = consumers[bucket]; 50 consumers[bucket] = null; 51 return answer; 52 } 53 54 public MessageGroupSet removeConsumer(ConsumerId consumerId) { 55 MessageGroupSet answer = null; 56 for (int i = 0; i < consumers.length; i++) { 57 ConsumerId owner = consumers[i]; 58 if (owner != null && owner.equals(consumerId)) { 59 answer = createMessageGroupSet(i, answer); 60 consumers[i] = null; 61 } 62 } 63 if (answer == null) { 64 answer = EmptyMessageGroupSet.INSTANCE; 66 } 67 return answer; 68 } 69 70 public String toString() { 71 int count = 0; 72 for (int i = 0; i < consumers.length; i++) { 73 if (consumers[i] != null) { 74 count++; 75 } 76 } 77 return "active message group buckets: " + count; 78 } 79 80 protected MessageGroupSet createMessageGroupSet(int bucketNumber, final MessageGroupSet parent) { 81 final MessageGroupSet answer = createMessageGroupSet(bucketNumber); 82 if (parent == null) { 83 return answer; 84 } 85 else { 86 return new MessageGroupSet() { 88 public boolean contains(String groupID) { 89 return parent.contains(groupID) || answer.contains(groupID); 90 } 91 }; 92 } 93 } 94 95 protected MessageGroupSet createMessageGroupSet(final int bucketNumber) { 96 return new MessageGroupSet() { 97 public boolean contains(String groupID) { 98 int bucket = getBucketNumber(groupID); 99 return bucket == bucketNumber; 100 } 101 }; 102 } 103 104 protected int getBucketNumber(String groupId) { 105 int bucket = groupId.hashCode() % bucketCount; 106 if( bucket < 0 ) 108 bucket *= -1; 109 return bucket; 110 } 111 } 112 | Popular Tags |