public static T ReadBlock <T>(this FileStream fileStream, long position) where T : Block { T block = null; Block b = BlockCache.TryGetSunkBlock(position);// 如果已经从数据库读出来过,就不应再读了。 if (b != null) { block = b as T; if (block == null) { throw new Exception("Two types of Block exists in the same position!"); } } else { fileStream.Seek(position, SeekOrigin.Begin); object obj = Consts.formatter.Deserialize(fileStream); block = obj as T; block.ThisPos = position; BlockCache.AddSunkBlock(block); } return(block); }
public static void WriteBlock(this FileStream fileStream, Block block) { if (fileStream.Length < block.ThisPos) { fileStream.SetLength(block.ThisPos); } fileStream.Seek(block.ThisPos, SeekOrigin.Begin); Consts.formatter.Serialize(fileStream, block); BlockCache.TryRemoveFloatingBlock(block); // 如果是new Block(),需要在此时从floating列表移除 BlockCache.AddSunkBlock(block); // 如果是new Block(),需要在此时加入sunk字典 }