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.HashMap;
021import java.util.Map;
022import java.util.concurrent.atomic.AtomicInteger;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * This class is used to manage the identifiers for {@link CacheableDeserializer}. All deserializers
027 * are registered with this Manager via the {@link #registerDeserializer(CacheableDeserializer)}}.
028 * On registration, we return an int *identifier* for this deserializer. The int identifier is
029 * passed to {@link #getDeserializer(int)}} to obtain the registered deserializer instance.
030 */
031@InterfaceAudience.Private
032public class CacheableDeserializerIdManager {
033  private static final Map<Integer, CacheableDeserializer<Cacheable>> registeredDeserializers =
034    new HashMap<>();
035  private static final AtomicInteger identifier = new AtomicInteger(0);
036
037  /**
038   * Register the given {@link Cacheable} -- usually an hfileblock instance, these implement the
039   * Cacheable Interface -- deserializer and generate a unique identifier id for it and return this
040   * as our result.
041   * @return the identifier of given cacheable deserializer
042   * @see #getDeserializer(int)
043   */
044  public static int registerDeserializer(CacheableDeserializer<Cacheable> cd) {
045    int idx = identifier.incrementAndGet();
046    synchronized (registeredDeserializers) {
047      registeredDeserializers.put(idx, cd);
048    }
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    Map<Integer, String> snapshot = new HashMap<>();
066    synchronized (registeredDeserializers) {
067      for (Map.Entry<Integer, CacheableDeserializer<Cacheable>> entry : registeredDeserializers
068        .entrySet()) {
069        snapshot.put(entry.getKey(), entry.getValue().getClass().getName());
070      }
071    }
072    return snapshot;
073  }
074}