001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.hfile; 019 020import java.util.Map; 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.concurrent.atomic.AtomicInteger; 023import java.util.stream.Collectors; 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** 027 * This class is used to manage the identifiers for {@link CacheableDeserializer}. All deserializers 028 * are registered with this Manager via the {@link #registerDeserializer(CacheableDeserializer)}}. 029 * On registration, we return an int *identifier* for this deserializer. The int identifier is 030 * passed to {@link #getDeserializer(int)}} to obtain the registered deserializer instance. 031 */ 032@InterfaceAudience.Private 033public class CacheableDeserializerIdManager { 034 private static final Map<Integer, CacheableDeserializer<Cacheable>> registeredDeserializers = 035 new ConcurrentHashMap<>(); 036 private static final AtomicInteger identifier = new AtomicInteger(0); 037 038 /** 039 * Register the given {@link Cacheable} -- usually an hfileblock instance, these implement the 040 * Cacheable Interface -- deserializer and generate a unique identifier id for it and return this 041 * as our result. 042 * @return the identifier of given cacheable deserializer 043 * @see #getDeserializer(int) 044 */ 045 public static int registerDeserializer(CacheableDeserializer<Cacheable> cd) { 046 int idx = identifier.incrementAndGet(); 047 // No synchronization here because keys will be unique 048 registeredDeserializers.put(idx, cd); 049 return idx; 050 } 051 052 /** 053 * Get the cacheable deserializer registered at the given identifier Id. 054 * @see #registerDeserializer(CacheableDeserializer) 055 */ 056 public static CacheableDeserializer<Cacheable> getDeserializer(int id) { 057 return registeredDeserializers.get(id); 058 } 059 060 /** 061 * Snapshot a map of the current identifiers to class names for reconstruction on reading out of a 062 * file. 063 */ 064 public static Map<Integer, String> save() { 065 // No synchronization here because weakly consistent view should be good enough 066 // The assumed risk is that we might not see a new serializer that comes in while iterating, 067 // but with a synchronized block, we won't see it anyway 068 return registeredDeserializers.entrySet().stream() 069 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getClass().getName())); 070 } 071}