Home / Class/ DnsQueryIdRange Class — netty Architecture

DnsQueryIdRange Class — netty Architecture

Architecture documentation for the DnsQueryIdRange class in DnsQueryIdSpace.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  111240bc_7647_f4bc_5270_6c732c4ba85a["DnsQueryIdRange"]
  21716c15_3bc6_ee9f_25cf_7032b72484c7["DnsQueryIdSpace.java"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|defined in| 21716c15_3bc6_ee9f_25cf_7032b72484c7
  28a1a002_2028_b30d_c464_69be19a4601e["DnsQueryIdRange()"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|method| 28a1a002_2028_b30d_c464_69be19a4601e
  5b6bfad2_4909_65b8_2d5f_3f2351c9ec9a["nextId()"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|method| 5b6bfad2_4909_65b8_2d5f_3f2351c9ec9a
  cf295d23_8f39_8124_5d0d_2b95f67a081b["pushId()"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|method| cf295d23_8f39_8124_5d0d_2b95f67a081b
  dc747dbe_ee06_49e5_5f1c_82af577c2f27["usableIds()"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|method| dc747dbe_ee06_49e5_5f1c_82af577c2f27
  fd1982cb_829b_d754_cf65_599769cf75b4["maxUsableIds()"]
  111240bc_7647_f4bc_5270_6c732c4ba85a -->|method| fd1982cb_829b_d754_cf65_599769cf75b4

Relationship Graph

Source Code

resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryIdSpace.java lines 139–211

    private static final class DnsQueryIdRange {

        // Holds all possible ids which are stored as unsigned shorts
        private final short[] ids;
        private final int startId;
        private int count;

        DnsQueryIdRange(int bucketSize, int startId) {
            this.ids = new short[bucketSize];
            this.startId = startId;
            for (int v = startId; v < bucketSize + startId; v++) {
                pushId(v);
            }
        }

        /**
         * Returns the next ID to use for a query or {@code -1} if there is none left to use.
         *
         * @return next id to use.
         */
        int nextId() {
            assert count >= 0;
            if (count == 0) {
                return -1;
            }
            short id = ids[count - 1];
            count--;

            return id & 0xFFFF;
        }

        /**
         * Push back the id, so it can be used again for the next query.
         *
         * @param id the id.
         */
        void pushId(int id) {
            if (count == ids.length) {
                throw new IllegalStateException("overflow");
            }
            assert id <= startId + ids.length && id >= startId;
            // pick a slot for our index, and whatever was in that slot before will get moved to the tail.
            Random random = ThreadLocalRandom.current();
            int insertionPosition = random.nextInt(count + 1);
            short moveId = ids[insertionPosition];
            short insertId = (short) id;

            // Assert that the ids are different or its the same index.
            assert moveId != insertId || insertionPosition == count;

            ids[count] = moveId;
            ids[insertionPosition] = insertId;
            count++;
        }

        /**
         * Return how much more usable ids are left.
         *
         * @return the number of ids that are left for usage.
         */
        int usableIds() {
            return count;
        }

        /**
         * Return the maximum number of ids that are supported.
         *
         * @return the maximum number of ids.
         */
        int maxUsableIds() {
            return ids.length;
        }
    }

Frequently Asked Questions

What is the DnsQueryIdRange class?
DnsQueryIdRange is a class in the netty codebase, defined in resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryIdSpace.java.
Where is DnsQueryIdRange defined?
DnsQueryIdRange is defined in resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryIdSpace.java at line 139.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free