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.server.namenode; 019 020import java.util.Iterator; 021import java.util.List; 022 023import org.apache.hadoop.fs.permission.FsPermission; 024import org.apache.hadoop.fs.permission.PermissionStatus; 025import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite; 026import org.apache.hadoop.hdfs.server.namenode.Quota.Counts; 027import org.apache.hadoop.util.GSet; 028import org.apache.hadoop.util.LightWeightGSet; 029 030import com.google.common.base.Preconditions; 031 032/** 033 * Storing all the {@link INode}s and maintaining the mapping between INode ID 034 * and INode. 035 */ 036public class INodeMap { 037 038 static INodeMap newInstance(INodeDirectory rootDir) { 039 // Compute the map capacity by allocating 1% of total memory 040 int capacity = LightWeightGSet.computeCapacity(1, "INodeMap"); 041 GSet<INode, INodeWithAdditionalFields> map 042 = new LightWeightGSet<INode, INodeWithAdditionalFields>(capacity); 043 map.put(rootDir); 044 return new INodeMap(map); 045 } 046 047 /** Synchronized by external lock. */ 048 private final GSet<INode, INodeWithAdditionalFields> map; 049 050 public Iterator<INodeWithAdditionalFields> getMapIterator() { 051 return map.iterator(); 052 } 053 054 private INodeMap(GSet<INode, INodeWithAdditionalFields> map) { 055 Preconditions.checkArgument(map != null); 056 this.map = map; 057 } 058 059 /** 060 * Add an {@link INode} into the {@link INode} map. Replace the old value if 061 * necessary. 062 * @param inode The {@link INode} to be added to the map. 063 */ 064 public final void put(INode inode) { 065 if (inode instanceof INodeWithAdditionalFields) { 066 map.put((INodeWithAdditionalFields)inode); 067 } 068 } 069 070 /** 071 * Remove a {@link INode} from the map. 072 * @param inode The {@link INode} to be removed. 073 */ 074 public final void remove(INode inode) { 075 map.remove(inode); 076 } 077 078 /** 079 * @return The size of the map. 080 */ 081 public int size() { 082 return map.size(); 083 } 084 085 /** 086 * Get the {@link INode} with the given id from the map. 087 * @param id ID of the {@link INode}. 088 * @return The {@link INode} in the map with the given id. Return null if no 089 * such {@link INode} in the map. 090 */ 091 public INode get(long id) { 092 INode inode = new INodeWithAdditionalFields(id, null, new PermissionStatus( 093 "", "", new FsPermission((short) 0)), 0, 0) { 094 095 @Override 096 void recordModification(int latestSnapshotId) { 097 } 098 099 @Override 100 public void destroyAndCollectBlocks(BlocksMapUpdateInfo collectedBlocks, 101 List<INode> removedINodes) { 102 // Nothing to do 103 } 104 105 @Override 106 public Counts computeQuotaUsage(Counts counts, boolean useCache, 107 int lastSnapshotId) { 108 return null; 109 } 110 111 @Override 112 public ContentSummaryComputationContext computeContentSummary( 113 ContentSummaryComputationContext summary) { 114 return null; 115 } 116 117 @Override 118 public Counts cleanSubtree(int snapshotId, int priorSnapshotId, 119 BlocksMapUpdateInfo collectedBlocks, List<INode> removedINodes) { 120 return null; 121 } 122 123 @Override 124 public byte getStoragePolicyID(){ 125 return BlockStoragePolicySuite.ID_UNSPECIFIED; 126 } 127 128 @Override 129 public byte getLocalStoragePolicyID() { 130 return BlockStoragePolicySuite.ID_UNSPECIFIED; 131 } 132 }; 133 134 return map.get(inode); 135 } 136 137 /** 138 * Clear the {@link #map} 139 */ 140 public void clear() { 141 map.clear(); 142 } 143}