hadoop StateStoreSerializableImpl 源码

  • 2022-10-20
  • 浏览 (187)

haddop StateStoreSerializableImpl 代码

文件路径:/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/store/driver/impl/StateStoreSerializableImpl.java

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hdfs.server.federation.store.driver.impl;

import java.io.IOException;
import java.util.Collection;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.server.federation.metrics.StateStoreMetrics;
import org.apache.hadoop.hdfs.server.federation.store.driver.StateStoreSerializer;
import org.apache.hadoop.hdfs.server.federation.store.records.BaseRecord;

/**
 * State Store driver that stores a serialization of the records. The serializer
 * is pluggable.
 */
public abstract class StateStoreSerializableImpl extends StateStoreBaseImpl {

  /** Mark for slashes in path names. */
  protected static final String SLASH_MARK = "0SLASH0";
  /** Mark for colon in path names. */
  protected static final String COLON_MARK = "_";

  /** Default serializer for this driver. */
  private StateStoreSerializer serializer;


  @Override
  public boolean init(final Configuration config, final String id,
      final Collection<Class<? extends BaseRecord>> records,
      final StateStoreMetrics metrics) {
    boolean ret = super.init(config, id, records, metrics);

    this.serializer = StateStoreSerializer.getSerializer(config);

    return ret;
  }

  /**
   * Serialize a record using the serializer.
   * @param record Record to serialize.
   * @return Byte array with the serialization of the record.
   */
  protected <T extends BaseRecord> byte[] serialize(T record) {
    return serializer.serialize(record);
  }

  /**
   * Serialize a record using the serializer.
   * @param record Record to serialize.
   * @return String with the serialization of the record.
   */
  protected <T extends BaseRecord> String serializeString(T record) {
    return serializer.serializeString(record);
  }

  /**
   * Creates a record from an input data string.
   * @param data Serialized text of the record.
   * @param clazz Record class.
   * @param includeDates If dateModified and dateCreated are serialized.
   * @return The created record.
   * @throws IOException
   */
  protected <T extends BaseRecord> T newRecord(
      String data, Class<T> clazz, boolean includeDates) throws IOException {
    return serializer.deserialize(data, clazz);
  }

  /**
   * Get the primary key for a record. If we don't want to store in folders, we
   * need to remove / from the name.
   *
   * @param record Record to get the primary key for.
   * @return Primary key for the record.
   */
  protected static String getPrimaryKey(BaseRecord record) {
    String primaryKey = record.getPrimaryKey();
    primaryKey = primaryKey.replaceAll("/", SLASH_MARK);
    primaryKey = primaryKey.replaceAll(":", COLON_MARK);
    return primaryKey;
  }
}

相关信息

hadoop 源码目录

相关文章

hadoop StateStoreBaseImpl 源码

hadoop StateStoreFileBaseImpl 源码

hadoop StateStoreFileImpl 源码

hadoop StateStoreFileSystemImpl 源码

hadoop StateStoreSerializerPBImpl 源码

hadoop StateStoreZooKeeperImpl 源码

hadoop package-info 源码

0  赞