private int AllocateBlockId() { var freeBlock = FreeBlocks.Where(b => !InitialFreeBlocks.Contains(b)).FirstOrDefault(); if (freeBlock == 0) { freeBlock = FreeBlocks.FirstOrDefault(); } if (freeBlock == 0) { freeBlock = TailBlockId++; } FreeBlocks.Remove(freeBlock); AllocatedBlocks.Add(freeBlock); return(freeBlock); }
private void SaveChanges() { if (!IsChanged) { return; } MasterBlockData.FreeListBlockId = WriteFreeBlocks(); foreach (var kv in _blocks.Where(kv => AllocatedBlocks.Contains(kv.Key))) { WriteChainedBlock(kv.Value); } FlushStreamBuffers(); WriteMasterBlockData(); //Console.Out.WriteLine("Written: {0}, Free: {1}, Initial free: {2}, Allocated: {3}, Garbage: {4}", // _blocks.Count, FreeBlocks.Count, InitialFreeBlocks.Count, AllocatedBlocks.Count, // GarbageBlocks.Count); }
public void FreeBlock(int blockId) { VerifyValidDataBlock(blockId); IsChanged = true; var chainedBlock = _blocks[blockId]; if (AllocatedBlocks.Contains(blockId) || InitialFreeBlocks.Contains(blockId)) { foreach (var id in chainedBlock.BlockIds) { FreeBlocks.Add(id); } } else { foreach (var id in chainedBlock.BlockIds) { GarbageBlocks.Add(id); } } _blocks.Remove(blockId); }