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 {@link CacheableDeserializer}.
029 * All deserializers are registered with this Manager via the
030 * {@link #registerDeserializer(CacheableDeserializer)}}. On registration, we return an
031 * int *identifier* for this deserializer. The int identifier is passed to
032 * {@link #getDeserializer(int)}} to obtain the registered deserializer instance.
033 */
034@InterfaceAudience.Private
035public class CacheableDeserializerIdManager {
036  private static final Map<Integer, CacheableDeserializer<Cacheable>> registeredDeserializers = new HashMap<>();
037  private static final AtomicInteger identifier = new AtomicInteger(0);
038
039  /**
040   * Register the given {@link Cacheable} -- usually an hfileblock instance, these implement
041   * the Cacheable Interface -- deserializer and generate a unique identifier id for it and return
042   * this as our result.
043   * @return the identifier of given cacheable deserializer
044   * @see #getDeserializer(int)
045   */
046  public static int registerDeserializer(CacheableDeserializer<Cacheable> cd) {
047    int idx = identifier.incrementAndGet();
048    synchronized (registeredDeserializers) {
049      registeredDeserializers.put(idx, cd);
050    }
051    return idx;
052  }
053
054  /**
055   * Get the cacheable deserializer registered at the given identifier Id.
056   * @see #registerDeserializer(CacheableDeserializer)
057   */
058  public static CacheableDeserializer<Cacheable> getDeserializer(int id) {
059    return registeredDeserializers.get(id);
060  }
061
062  /**
063   * Snapshot a map of the current identifiers to class names for reconstruction on reading out
064   * of a file.
065   */
066  public static Map<Integer,String> save() {
067    Map<Integer, String> snapshot = new HashMap<>();
068    synchronized (registeredDeserializers) {
069      for (Map.Entry<Integer, CacheableDeserializer<Cacheable>> entry :
070          registeredDeserializers.entrySet()) {
071        snapshot.put(entry.getKey(), entry.getValue().getClass().getName());
072      }
073    }
074    return snapshot;
075  }
076}