Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.jackrabbit.oak.segment;
import static java.util.Objects.requireNonNull;
import static java.lang.Thread.currentThread;
import java.io.IOException;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
import org.apache.jackrabbit.guava.common.util.concurrent.Monitor;
import org.apache.jackrabbit.oak.commons.conditions.Validate;
import org.apache.jackrabbit.oak.segment.file.tar.GCGeneration;
import org.jetbrains.annotations.NotNull;
/**
* This {@link WriteOperationHandler} uses a pool of {@link SegmentBufferWriter}s,
* which it passes to its {@link #execute(GCGeneration, WriteOperation) execute} method.
*
* Instances of this class are thread safe.
*/
public abstract class SegmentBufferWriterPool implements WriteOperationHandler {
@NotNull
private final SegmentIdProvider idProvider;
@NotNull
private final Supplier gcGeneration;
@NotNull
private final String wid;
private short writerId = -1;
private SegmentBufferWriterPool(
@NotNull SegmentIdProvider idProvider,
@NotNull String wid,
@NotNull Supplier gcGeneration) {
this.idProvider = requireNonNull(idProvider);
this.wid = requireNonNull(wid);
this.gcGeneration = requireNonNull(gcGeneration);
}
public enum PoolType {
GLOBAL,
THREAD_SPECIFIC;
}
public static class SegmentBufferWriterPoolFactory {
@NotNull
private final SegmentIdProvider idProvider;
@NotNull
private final String wid;
@NotNull
private final Supplier gcGeneration;
private SegmentBufferWriterPoolFactory(
@NotNull SegmentIdProvider idProvider,
@NotNull String wid,
@NotNull Supplier gcGeneration) {
this.idProvider = requireNonNull(idProvider);
this.wid = requireNonNull(wid);
this.gcGeneration = requireNonNull(gcGeneration);
}
@NotNull
public SegmentBufferWriterPool newPool(@NotNull SegmentBufferWriterPool.PoolType poolType) {
switch (poolType) {
case GLOBAL:
return new GlobalSegmentBufferWriterPool(idProvider, wid, gcGeneration);
case THREAD_SPECIFIC:
return new ThreadSpecificSegmentBufferWriterPool(idProvider, wid, gcGeneration);
default:
throw new IllegalArgumentException("Unknown writer pool type.");
}
}
}
public static SegmentBufferWriterPoolFactory factory(
@NotNull SegmentIdProvider idProvider,
@NotNull String wid,
@NotNull Supplier gcGeneration) {
return new SegmentBufferWriterPoolFactory(idProvider, wid, gcGeneration);
}
private static class ThreadSpecificSegmentBufferWriterPool extends SegmentBufferWriterPool {
/**
* Read write lock protecting the state of this pool. Multiple threads can access their writers in parallel,
* acquiring the read lock. The writer lock is needed for the flush operation since it requires none
* of the writers to be in use.
*/
private final ReadWriteLock lock = new ReentrantReadWriteLock(true);
/**
* Pool of writers. Every thread is assigned a unique writer per GC generation, therefore only requiring
* a concurrent map to synchronize access to them.
*/
private final ConcurrentMap