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