zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hanm <...@git.apache.org>
Subject [GitHub] zookeeper pull request #590: [ZOOKEEPER-1177] Add the memory optimized watch...
Date Sat, 22 Sep 2018 04:38:35 GMT
Github user hanm commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/590#discussion_r219659865
  
    --- 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 --
    
    I am also wondering, if this optimization is indeed useful, why not do the same for the
`remove` methods, that is, check and return early with a read lock before trying to acquire
a write lock.


---

Mime
View raw message