INodeMap
2016-01-25 15:33
483 查看
INodeMap利用LightWeightGSet来存储INode对象,为什么可以存储INode ID和INode的映射呢?因为INode的equals和hashCode方法都已经重载,并且只根据id判断,那么知道id之后,其它字段为空的Inode对象,就可以得到映射的对象。equals和hashCode 方法如下:
INodeMap的源代码:
@Override public final boolean equals(Object that) { if (this == that) { return true; } if (that == null || !(that instanceof INode)) { return false; } return getId() == ((INode) that).getId(); } @Override public final int hashCode() { long id = getId(); return (int)(id^(id>>>32)); }
INodeMap的源代码:
/** * Storing all the {@link INode}s and maintaining the mapping between INode ID * and INode. */ public class INodeMap { static INodeMap newInstance(INodeDirectory rootDir) { // Compute the map capacity by allocating 1% of total memory int capacity = LightWeightGSet.computeCapacity(1, "INodeMap"); GSet<INode, INodeWithAdditionalFields> map = new LightWeightGSet<INode, INodeWithAdditionalFields>(capacity); map.put(rootDir); return new INodeMap(map); } /** Synchronized by external lock. */ private final GSet<INode, INodeWithAdditionalFields> map; public Iterator<INodeWithAdditionalFields> getMapIterator() { return map.iterator(); } private INodeMap(GSet<INode, INodeWithAdditionalFields> map) { Preconditions.checkArgument(map != null); this.map = map; } /** * Add an {@link INode} into the {@link INode} map. Replace the old value if * necessary. * @param inode The {@link INode} to be added to the map. */ public final void put(INode inode) { if (inode instanceof INodeWithAdditionalFields) { map.put((INodeWithAdditionalFields)inode); } } /** * Remove a {@link INode} from the map. * @param inode The {@link INode} to be removed. */ public final void remove(INode inode) { map.remove(inode); } /** * @return The size of the map. */ public int size() { return map.size(); } /** * Get the {@link INode} with the given id from the map. * @param id ID of the {@link INode}. * @return The {@link INode} in the map with the given id. Return null if no * such {@link INode} in the map. */ public INode get(long id) { INode inode = new INodeWithAdditionalFields(id, null, new PermissionStatus( "", "", new FsPermission((short) 0)), 0, 0) { @Override void recordModification(int latestSnapshotId) throws QuotaExceededException { } @Override public void destroyAndCollectBlocks(BlocksMapUpdateInfo collectedBlocks, List<INode> removedINodes) { // Nothing to do } @Override public Counts computeQuotaUsage(Counts counts, boolean useCache, int lastSnapshotId) { return null; } @Override public ContentSummaryComputationContext computeContentSummary( ContentSummaryComputationContext summary) { return null; } @Override public Counts cleanSubtree(int snapshotId, int priorSnapshotId, BlocksMapUpdateInfo collectedBlocks, List<INode> removedINodes, boolean countDiffChange) throws QuotaExceededException { return null; } @Override public byte getStoragePolicyID(){ return BlockStoragePolicySuite.ID_UNSPECIFIED; } @Override public byte getLocalStoragePolicyID() { return BlockStoragePolicySuite.ID_UNSPECIFIED; } }; return map.get(inode); } /** * Clear the {@link #map} */ public void clear() { map.clear(); } }
相关文章推荐
- nodejs 项目编译
- Express + Node.js 实现拦截器
- mount: /dev/sdb1 already mounted or /mnt/hdb busy 导致NameNode无法启动
- nodejs开发cms系统第二篇之首页
- Node.js权威指南 (14) - 使用Express构建Web应用程序
- Node.js权威指南 (12) - Node.js中的其他模块
- Node.js权威指南 (11) - 加密与压缩
- Node.js权威指南 (10) - Node.js中的错误处理与断言处理
- Node.js权威指南 (9) - 进程与子进程
- Node.js权威指南 (6) - 在Node.js中操作文件系统
- 一张图告诉你,只会Node.JS还不够!
- Node.js权威指南 (5) - 使用Buffer类处理二进制数据
- Node.js权威指南 (4) - 模块与npm包管理工具
- Node.js权威指南 (3) - Node.js基础知识
- Node.js权威指南 (2) - Node.js中的交互式运行环境——REPL
- Node.js权威指南 (1) - Node.js介绍
- Nodejs之NPM安装与使用
- 深入浅出Node.js (附录D) - 搭建局域NPM仓库
- 深入浅出Node.js (附录C) - Node编码规范
- 深入浅出Node.js (附录B) - 调试Node