private void Save(MemoryMappedFile mmf, Block block) { if (block.Size < 256) { throw new ArgumentOutOfRangeException(nameof(block)); } //// 最小数 //var min = MinSize; //if (block.Size < min) block.Size = min; // 索引区结构:头部 + 哈希桶 + 链表数据 // 前面256字节为头部 var buf = new Byte[256]; var ms = new MemoryStream(buf); // 头部 var bk1 = new Block(8 * 5, 256 - 8 * 5); var bk2 = new Block(bk1.Position + bk1.Size, 31 * 16); var bk3 = new Block(bk2.Position + bk2.Size, 1024 * 1024); var writer = new BinaryWriter(ms); writer.Write((Int32)bk2.Position); writer.Write((Int32)bk2.Size); writer.Write((Int32)bk3.Position); writer.Write((Int32)bk3.Size); // 校验 var crc = buf.ReadBytes(0, (Int32)ms.Position).Crc(); writer.Write(crc); // 写回去 using (var ac = mmf.CreateAccessor(block)) { ac.WriteArray(block.Position, buf, 0, buf.Length); } // 头部 _head = mmf.CreateAccessor(bk1 + block.Position); // 哈希桶 _buckets = mmf.CreateAccessor(bk2 + block.Position); // 链表数据 _data = mmf.CreateAccessor(bk3 + block.Position); }
//private MemoryMappedViewAccessor GetAccessor() //{ // var accessor = _mmf.CreateAccessor(_block); // return accessor; //} private Boolean Load(MemoryMappedFile mmf, Block block) { if (block.Size < 256) { return(false); } // 索引区结构:头部 + 哈希桶 + 链表数据 var ac = mmf.CreateAccessor(block); // 前面256字节为头部 var buf = ac.ReadArray(0, 256); var ms = new MemoryStream(buf); var reader = new BinaryReader(ms); // 头部 var bk2 = new Block(reader.ReadInt32(), reader.ReadInt32()); var bk3 = new Block(reader.ReadInt32(), reader.ReadInt32()); var len = (Int32)ms.Position; var crc = reader.ReadUInt32(); // 校验数据 if (crc != buf.ReadBytes(0, len).Crc()) { return(false); } // 头部 _head = mmf.CreateViewAccessor(block.Position + ms.Position, ms.Length - ms.Position); // 哈希桶 _buckets = mmf.CreateAccessor(bk2 + block.Position); // 链表数据 _data = mmf.CreateAccessor(bk3 + block.Position); return(true); }