Github user lvfangmin commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/590#discussion_r219688164
  
    --- Diff: src/java/main/org/apache/zookeeper/server/util/BitMap.java ---
    @@ -0,0 +1,125 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.zookeeper.server.util;
    +
    +import java.util.Map;
    +import java.util.HashMap;
    +import java.util.BitSet;
    +import java.util.concurrent.locks.ReentrantReadWriteLock;
    +
    +/**
    + * This is a helper class to maintain the bit to specific value and the
    + * reversed value to bit mapping.
    + */
    +public class BitMap<T> {
    +
    +    private final Map<T, Integer> value2Bit = new HashMap<T, Integer>();
    +    private final Map<Integer, T> bit2Value = new HashMap<Integer, T>();
    +
    +    private final BitSet freedBitSet = new BitSet();
    +    private Integer nextBit = Integer.valueOf(0);
    +
    +    private final ReentrantReadWriteLock rwLock = new 
ReentrantReadWriteLock();
    +
    +    public Integer add(T value) {
    +        Integer bit = getBit(value);
    +        if (bit != null) {
    +            return bit;
    +        }
    --- End diff --
    
    This BitMap is used by WatchManagerOptimized.watcherBitIdMap, which is used 
to store watcher to bit mapping.
    
    Add might be called a lot if the same client connection is watching on 
thousands of even millions of nodes, remove only called once when the session 
is closed, that's why we optimized to check read lock first in add, but use 
write lock directly in remove.


---

Reply via email to