001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.amqp.protocol;
018
019import java.util.ArrayDeque;
020import java.util.Deque;
021
022/**
023 * Utility class that can generate and if enabled pool the binary tag values
024 * used to identify transfers over an AMQP link.
025 */
026public final class AmqpTransferTagGenerator {
027
028    public static final int DEFAULT_TAG_POOL_SIZE = 1024;
029
030    private final Deque<byte[]> tagPool;
031
032    private long nextTagId;
033    private int maxPoolSize = DEFAULT_TAG_POOL_SIZE;
034
035    public AmqpTransferTagGenerator() {
036        this(true);
037    }
038
039    public AmqpTransferTagGenerator(boolean pool) {
040        if (pool) {
041            this.tagPool = new ArrayDeque<>();
042        } else {
043            this.tagPool = null;
044        }
045    }
046
047    /**
048     * Retrieves the next available tag.
049     *
050     * @return a new or unused tag depending on the pool option.
051     */
052    public byte[] getNextTag() {
053        byte[] tagBytes = null;
054
055        if (tagPool != null) {
056            tagBytes = tagPool.pollFirst();
057        }
058
059        if (tagBytes == null) {
060            long tag = nextTagId++;
061            int size = encodingSize(tag);
062
063            tagBytes = new byte[size];
064
065            for (int i = 0; i < size; ++i) {
066                tagBytes[size - 1 - i] = (byte) (tag >>> (i * 8));
067            }
068        }
069
070        return tagBytes;
071    }
072
073    /**
074     * When used as a pooled cache of tags the unused tags should always be returned once
075     * the transfer has been settled.
076     *
077     * @param data
078     *        a previously borrowed tag that is no longer in use.
079     */
080    public void returnTag(byte[] data) {
081        if (tagPool != null && tagPool.size() < maxPoolSize) {
082            tagPool.offerLast(data);
083        }
084    }
085
086    /**
087     * Gets the current max pool size value.
088     *
089     * @return the current max tag pool size.
090     */
091    public int getMaxPoolSize() {
092        return maxPoolSize;
093    }
094
095    /**
096     * Sets the max tag pool size.  If the size is smaller than the current number
097     * of pooled tags the pool will drain over time until it matches the max.
098     *
099     * @param maxPoolSize
100     *        the maximum number of tags to hold in the pool.
101     */
102    public void setMaxPoolSize(int maxPoolSize) {
103        this.maxPoolSize = maxPoolSize;
104    }
105
106    /**
107     * @return true if the generator is using a pool of tags to reduce allocations.
108     */
109    public boolean isPooling() {
110        return tagPool != null;
111    }
112
113    private int encodingSize(long value) {
114        if (value < 0) {
115            return Long.BYTES;
116        }
117
118        int size = 1;
119        while (size < 8 && (value >= (1L << (size * 8)))) {
120            size++;
121        }
122
123        return size;
124    }
125}