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.hdfs.protocol; 019 020import org.apache.hadoop.classification.InterfaceAudience; 021import org.apache.hadoop.classification.InterfaceStability; 022import org.apache.hadoop.io.erasurecode.ECSchema; 023 024/** 025 * A policy about how to write/read/code an erasure coding file. 026 */ 027@InterfaceAudience.Public 028@InterfaceStability.Evolving 029public final class ErasureCodingPolicy { 030 031 private final String name; 032 private final ECSchema schema; 033 private final int cellSize; 034 private final byte id; 035 036 public ErasureCodingPolicy(String name, ECSchema schema, 037 int cellSize, byte id) { 038 this.name = name; 039 this.schema = schema; 040 this.cellSize = cellSize; 041 this.id = id; 042 } 043 044 public ErasureCodingPolicy(ECSchema schema, int cellSize, byte id) { 045 this(composePolicyName(schema, cellSize), schema, cellSize, id); 046 } 047 048 private static String composePolicyName(ECSchema schema, int cellSize) { 049 assert cellSize % 1024 == 0; 050 return schema.getCodecName().toUpperCase() + "-" + 051 schema.getNumDataUnits() + "-" + schema.getNumParityUnits() + 052 "-" + cellSize / 1024 + "k"; 053 } 054 055 public String getName() { 056 return name; 057 } 058 059 public ECSchema getSchema() { 060 return schema; 061 } 062 063 public int getCellSize() { 064 return cellSize; 065 } 066 067 public int getNumDataUnits() { 068 return schema.getNumDataUnits(); 069 } 070 071 public int getNumParityUnits() { 072 return schema.getNumParityUnits(); 073 } 074 075 public String getCodecName() { 076 return schema.getCodecName(); 077 } 078 079 public byte getId() { 080 return id; 081 } 082 083 @Override 084 public boolean equals(Object o) { 085 if (this == o) { 086 return true; 087 } 088 if (o == null || getClass() != o.getClass()) { 089 return false; 090 } 091 ErasureCodingPolicy that = (ErasureCodingPolicy) o; 092 093 return that.getName().equals(name) && 094 that.getCellSize() == cellSize && 095 that.getSchema().equals(schema); 096 } 097 098 @Override 099 public int hashCode() { 100 int result = name.hashCode(); 101 result = 31 * result + schema.hashCode(); 102 result = 31 * result + cellSize; 103 return result; 104 } 105 106 @Override 107 public String toString() { 108 return "ErasureCodingPolicy=[" + "Name=" + name + ", " 109 + "Schema=[" + schema.toString() + "], " 110 + "CellSize=" + cellSize + " " + "]"; 111 } 112}