コード例 #1
0
 public void Add(ShareInfo info)
 {
     foreach (var hash in info.Hashes)
     {
         _map.GetOrAdd(hash, (_) => new SmallList <ShareInfo>()).Add(info);
     }
 }
コード例 #2
0
                public void Remove(ShareInfo info)
                {
                    foreach (var hash in info.Hashes)
                    {
                        if (_map.TryGetValue(hash, out var infos))
                        {
                            infos.Remove(info);

                            if (infos.Count == 0)
                            {
                                _map.Remove(hash);
                            }
                        }
                    }
                }
コード例 #3
0
        public Task <Metadata> Import(string path, DateTime creationTime, CancellationToken token)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            return(Task.Run(() =>
            {
                // Check
                lock (_lockObject)
                {
                    var info = _contentInfoManager.GetFileContentInfo(path);
                    if (info != null)
                    {
                        return info.Metadata;
                    }
                }

                Metadata metadata = null;
                var lockedHashes = new HashSet <Hash>();
                ShareInfo shareInfo = null;

                {
                    const int blockLength = 1024 * 1024;
                    const HashAlgorithm hashAlgorithm = HashAlgorithm.Sha256;
                    const CorrectionAlgorithm correctionAlgorithm = CorrectionAlgorithm.ReedSolomon8;

                    int depth = 0;

                    var groupList = new List <Group>();

                    // File
                    using (var stream = new UnbufferedFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.None, _bufferManager))
                    {
                        if (stream.Length <= blockLength)
                        {
                            Hash hash;

                            using (var safeBuffer = _bufferManager.CreateSafeBuffer(blockLength))
                            {
                                int length = (int)stream.Length;
                                stream.Read(safeBuffer.Value, 0, length);

                                if (hashAlgorithm == HashAlgorithm.Sha256)
                                {
                                    hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(safeBuffer.Value, 0, length));
                                }
                            }

                            shareInfo = new ShareInfo(path, stream.Length, (int)stream.Length, new Hash[] { hash });
                            metadata = new Metadata(depth, hash);
                        }
                        else
                        {
                            var sharedHashes = new List <Hash>();

                            for (; ;)
                            {
                                var targetHashes = new List <Hash>();
                                var targetBuffers = new List <ArraySegment <byte> >();
                                long sumLength = 0;

                                try
                                {
                                    for (int i = 0; stream.Position < stream.Length; i++)
                                    {
                                        token.ThrowIfCancellationRequested();

                                        var buffer = new ArraySegment <byte>();

                                        try
                                        {
                                            int length = (int)Math.Min(stream.Length - stream.Position, blockLength);
                                            buffer = new ArraySegment <byte>(_bufferManager.TakeBuffer(length), 0, length);
                                            stream.Read(buffer.Array, 0, length);

                                            sumLength += length;
                                        }
                                        catch (Exception)
                                        {
                                            if (buffer.Array != null)
                                            {
                                                _bufferManager.ReturnBuffer(buffer.Array);
                                            }

                                            throw;
                                        }

                                        Hash hash;

                                        if (hashAlgorithm == HashAlgorithm.Sha256)
                                        {
                                            hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(buffer));
                                        }

                                        sharedHashes.Add(hash);

                                        targetHashes.Add(hash);
                                        targetBuffers.Add(buffer);

                                        if (targetBuffers.Count >= 128)
                                        {
                                            break;
                                        }
                                    }

                                    var parityHashes = this.ParityEncoding(targetBuffers, hashAlgorithm, correctionAlgorithm, token);
                                    lockedHashes.UnionWith(parityHashes);

                                    groupList.Add(new Group(correctionAlgorithm, sumLength, CollectionUtils.Unite(targetHashes, parityHashes).ToArray()));
                                }
                                finally
                                {
                                    foreach (var buffer in targetBuffers)
                                    {
                                        if (buffer.Array == null)
                                        {
                                            continue;
                                        }

                                        _bufferManager.ReturnBuffer(buffer.Array);
                                    }
                                }

                                if (stream.Position == stream.Length)
                                {
                                    break;
                                }
                            }

                            shareInfo = new ShareInfo(path, stream.Length, blockLength, sharedHashes);

                            depth++;
                        }
                    }

                    while (groupList.Count > 0)
                    {
                        // Index
                        using (var stream = (new Index(groupList)).Export(_bufferManager))
                        {
                            groupList.Clear();

                            if (stream.Length <= blockLength)
                            {
                                Hash hash;

                                using (var safeBuffer = _bufferManager.CreateSafeBuffer(blockLength))
                                {
                                    int length = (int)stream.Length;
                                    stream.Read(safeBuffer.Value, 0, length);

                                    if (hashAlgorithm == HashAlgorithm.Sha256)
                                    {
                                        hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(safeBuffer.Value, 0, length));
                                    }

                                    _blocksManager.Lock(hash);
                                    _blocksManager.Set(hash, new ArraySegment <byte>(safeBuffer.Value, 0, length));

                                    lockedHashes.Add(hash);
                                }

                                metadata = new Metadata(depth, hash);
                            }
                            else
                            {
                                for (; ;)
                                {
                                    var targetHashes = new List <Hash>();
                                    var targetBuffers = new List <ArraySegment <byte> >();
                                    long sumLength = 0;

                                    try
                                    {
                                        for (int i = 0; stream.Position < stream.Length; i++)
                                        {
                                            token.ThrowIfCancellationRequested();

                                            var buffer = new ArraySegment <byte>();

                                            try
                                            {
                                                int length = (int)Math.Min(stream.Length - stream.Position, blockLength);
                                                buffer = new ArraySegment <byte>(_bufferManager.TakeBuffer(length), 0, length);
                                                stream.Read(buffer.Array, 0, length);

                                                sumLength += length;
                                            }
                                            catch (Exception)
                                            {
                                                if (buffer.Array != null)
                                                {
                                                    _bufferManager.ReturnBuffer(buffer.Array);
                                                }

                                                throw;
                                            }

                                            Hash hash;

                                            if (hashAlgorithm == HashAlgorithm.Sha256)
                                            {
                                                hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(buffer));
                                            }

                                            _blocksManager.Lock(hash);
                                            _blocksManager.Set(hash, buffer);

                                            lockedHashes.Add(hash);

                                            targetHashes.Add(hash);
                                            targetBuffers.Add(buffer);

                                            if (targetBuffers.Count >= 128)
                                            {
                                                break;
                                            }
                                        }

                                        var parityHashes = this.ParityEncoding(targetBuffers, hashAlgorithm, correctionAlgorithm, token);
                                        lockedHashes.UnionWith(parityHashes);

                                        groupList.Add(new Group(correctionAlgorithm, sumLength, CollectionUtils.Unite(targetHashes, parityHashes).ToArray()));
                                    }
                                    finally
                                    {
                                        foreach (var buffer in targetBuffers)
                                        {
                                            if (buffer.Array == null)
                                            {
                                                continue;
                                            }

                                            _bufferManager.ReturnBuffer(buffer.Array);
                                        }
                                    }

                                    if (stream.Position == stream.Length)
                                    {
                                        break;
                                    }
                                }

                                depth++;
                            }
                        }
                    }
                }

                lock (_lockObject)
                {
                    if (!_contentInfoManager.ContainsFileContentInfo(path))
                    {
                        _contentInfoManager.Add(new ContentInfo(creationTime, Timeout.InfiniteTimeSpan, metadata, lockedHashes, shareInfo));

                        foreach (var hash in lockedHashes)
                        {
                            _blocksManager.Lock(hash);
                        }
                    }
                }

                return metadata;
            }, token));
        }
コード例 #4
0
            public ContentInfo(DateTime creationTime, TimeSpan lifeTime, Metadata metadata, IEnumerable <Hash> lockedHashes, ShareInfo shareInfo)
            {
                this.CreationTime = creationTime;
                this.LifeSpan     = lifeTime;

                this.Metadata = metadata;
                if (lockedHashes != null)
                {
                    this.ProtectedLockedHashes.AddRange(lockedHashes);
                }
                this.ShareInfo = shareInfo;
            }