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.HashMap;
022import java.util.Map;
023import java.util.concurrent.atomic.AtomicInteger;
024
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * This class is used to manage the identifiers for
029 * {@link CacheableDeserializer}
030 */
031@InterfaceAudience.Private
032public class CacheableDeserializerIdManager {
033  private static final Map<Integer, CacheableDeserializer<Cacheable>> registeredDeserializers = new HashMap<>();
034  private static final AtomicInteger identifier = new AtomicInteger(0);
035
036  /**
037   * Register the given {@link Cacheable} -- usually an hfileblock instance, these implement
038   * the Cacheable Interface -- deserializer and generate a unique identifier id for it and return
039   * this as our result.
040   * @return the identifier of given cacheable deserializer
041   */
042  public static int registerDeserializer(CacheableDeserializer<Cacheable> cd) {
043    int idx = identifier.incrementAndGet();
044    synchronized (registeredDeserializers) {
045      registeredDeserializers.put(idx, cd);
046    }
047    return idx;
048  }
049
050  /**
051   * Get the cacheable deserializer as the given identifier Id
052   * @param id
053   * @return CacheableDeserializer
054   */
055  public static CacheableDeserializer<Cacheable> getDeserializer(int id) {
056    return registeredDeserializers.get(id);
057  }
058}