merlimat commented on a change in pull request #826: DbLedgerStorage implementation URL: https://github.com/apache/bookkeeper/pull/826#discussion_r155935504
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/ReadCache.java ########## @@ -0,0 +1,185 @@ +/** + * + * 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.bookkeeper.bookie.storage.ldb; + +import static org.apache.bookkeeper.bookie.storage.ldb.WriteCache.align64; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.Unpooled; + +import java.io.Closeable; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap; +import org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair; + +/** + * Read cache implementation. + * + * <p>Uses the specified amount of memory and pairs it with a hashmap. + */ +public class ReadCache implements Closeable { + + private static final int MaxSegmentSize = 1 * 1024 * 1024 * 1024; + + private final List<ByteBuf> cacheSegments; + private final List<ConcurrentLongLongPairHashMap> cacheIndexes; + + private int currentSegmentIdx; + private AtomicInteger currentSegmentOffset = new AtomicInteger(0); + + private final int segmentSize; + + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + + public ReadCache(long maxCacheSize) { + int segmentsCount = Math.max(2, (int) (maxCacheSize / MaxSegmentSize)); + segmentSize = (int) (maxCacheSize / segmentsCount); + + cacheSegments = new ArrayList<>(); + cacheIndexes = new ArrayList<>(); + + for (int i = 0; i < segmentsCount; i++) { + cacheSegments.add(Unpooled.directBuffer(segmentSize, segmentSize)); + cacheIndexes.add(new ConcurrentLongLongPairHashMap(4096, 2 * Runtime.getRuntime().availableProcessors())); + } + } + + @Override + public void close() { + cacheSegments.forEach(ByteBuf::release); + } + + public void put(long ledgerId, long entryId, ByteBuf entry) { + lock.readLock().lock(); Review comment: Sure, the 2 lines there don't need to be in the read lock section. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
