示例#1
0
                public void Reallocate(long size)
                {
                    _freeSectors.Clear();
                    _size = size;

                    _totalSectorCount = Roundup(_size, SectorSize) / SectorSize;
                    _usingSectorCount = 0;

                    _bitmapManager.SetLength(_totalSectorCount);
                }
示例#2
0
            public Task CheckBlocks(Action <CheckBlocksProgressReport> progress, CancellationToken token)
            {
                return(Task.Run(() =>
                {
                    // 重複するセクタを確保したブロックを検出しRemoveする。
                    lock (_lockObject)
                    {
                        using (var bitmapManager = new BitmapManager(_bufferManager))
                        {
                            bitmapManager.SetLength(this.Size / SectorSize);

                            var hashes = new List <Hash>();

                            foreach (var(hash, clusterInfo) in _clusterIndex)
                            {
                                foreach (var sector in clusterInfo.Indexes)
                                {
                                    if (!bitmapManager.Get(sector))
                                    {
                                        bitmapManager.Set(sector, true);
                                    }
                                    else
                                    {
                                        hashes.Add(hash);

                                        break;
                                    }
                                }
                            }

                            foreach (var hash in hashes)
                            {
                                this.Remove(hash);
                            }
                        }
                    }

                    // 読めないブロックを検出しRemoveする。
                    {
                        var list = this.ToArray();

                        int badCount = 0;
                        int checkedCount = 0;
                        int blockCount = list.Length;

                        token.ThrowIfCancellationRequested();

                        progress.Invoke(new CheckBlocksProgressReport(badCount, checkedCount, blockCount));

                        foreach (var hash in list)
                        {
                            token.ThrowIfCancellationRequested();

                            ArraySegment <byte>?block = null;

                            try
                            {
                                lock (_lockObject)
                                {
                                    if (this.Contains(hash))
                                    {
                                        block = this.Get(hash);

                                        if (block == null)
                                        {
                                            badCount++;
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                            finally
                            {
                                if (block != null)
                                {
                                    _bufferManager.ReturnBuffer(block.Value.Array);
                                }
                            }

                            checkedCount++;

                            if (checkedCount % 32 == 0)
                            {
                                progress.Invoke(new CheckBlocksProgressReport(badCount, checkedCount, blockCount));
                            }
                        }

                        progress.Invoke(new CheckBlocksProgressReport(badCount, checkedCount, blockCount));
                    }
                }, token));
            }