1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.hbase.codec.prefixtree.encode; 20 21 import java.io.OutputStream; 22 23 import org.apache.hadoop.hbase.classification.InterfaceAudience; 24 25 26 /** 27 * Pool to enable reusing the Encoder objects which can consist of thousands of smaller objects and 28 * would be more garbage than the data in the block. A new encoder is needed for each block in 29 * a flush, compaction, RPC response, etc. 30 * 31 * It is not a pool in the traditional sense, but implements the semantics of a traditional pool 32 * via ThreadLocals to avoid sharing between threads. Sharing between threads would not be 33 * very expensive given that it's accessed per-block, but this is just as easy. 34 * 35 * This pool implementation assumes there is a one-to-one mapping between a single thread and a 36 * single flush or compaction. 37 */ 38 @InterfaceAudience.Private 39 public class ThreadLocalEncoderPool implements EncoderPool{ 40 41 private static final ThreadLocal<PrefixTreeEncoder> ENCODER 42 = new ThreadLocal<PrefixTreeEncoder>(); 43 44 /** 45 * Get the encoder attached to the current ThreadLocal, or create a new one and attach it to the 46 * current thread. 47 */ 48 @Override 49 public PrefixTreeEncoder checkOut(OutputStream os, boolean includeMvccVersion) { 50 PrefixTreeEncoder builder = ENCODER.get(); 51 builder = EncoderFactory.prepareEncoder(builder, os, includeMvccVersion); 52 ENCODER.set(builder); 53 return builder; 54 } 55 56 @Override 57 public void checkIn(PrefixTreeEncoder encoder) { 58 // attached to thread on checkOut, so shouldn't need to do anything here 59 60 // do we need to worry about detaching encoders from compaction threads or are the same threads 61 // used over and over 62 } 63 64 }