Beispiel #1
0
        public async ValueTask <IMemoryOwner <byte>?> ReadAsync(OmniHash rootHash, OmniHash targetHash, CancellationToken cancellationToken = default)
        {
            using (await _asyncLock.LockAsync())
            {
                if (!_wantFileStatusMap.ContainsKey(rootHash))
                {
                    return(null);
                }

                var filePath = Path.Combine(Path.Combine(_configPath, this.OmniHashToFilePath(rootHash)), this.OmniHashToFilePath(targetHash));

                if (!File.Exists(filePath))
                {
                    return(null);
                }

                using (var fileStream = new UnbufferedFileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, FileOptions.None, _bytesPool))
                {
                    var memoryOwner = _bytesPool.Memory.Rent((int)fileStream.Length);
                    await fileStream.ReadAsync(memoryOwner.Memory);

                    return(memoryOwner);
                }
            }
        }
Beispiel #2
0
    public async ValueTask <bool> TryExportFileToStorageAsync(OmniHash rootHash, string filePath, CancellationToken cancellationToken = default)
    {
        var input  = new TryExportFileToStorageRequest(rootHash, filePath);
        var output = await _axisService.TryExportFileToStorageAsync(input, cancellationToken);

        return(output.Success);
    }
Beispiel #3
0
        public OmniSignature(string name, OmniHash hash)
        {
            if (name is null)
            {
                throw new System.ArgumentNullException("name");
            }
            if (name.Length > 32)
            {
                throw new System.ArgumentOutOfRangeException("name");
            }
            this.Name = name;
            this.Hash = hash;

            {
                var __h = new System.HashCode();
                if (this.Name != default)
                {
                    __h.Add(this.Name.GetHashCode());
                }
                if (this.Hash != default)
                {
                    __h.Add(this.Hash.GetHashCode());
                }
                __hashCode = __h.ToHashCode();
            }
        }
Beispiel #4
0
 public async ValueTask RemoveWantFileAsync(OmniHash rootHash, CancellationToken cancellationToken = default)
 {
     using (await _asyncLock.LockAsync())
     {
         _wantFileStatusMap.Remove(rootHash);
     }
 }
Beispiel #5
0
    public async ValueTask <ReadOnlyMemory <byte>?> TryExportFileToMemoryAsync(OmniHash rootHash, CancellationToken cancellationToken = default)
    {
        var input  = new TryExportFileToMemoryRequest(rootHash);
        var output = await _axisService.TryExportFileToMemoryAsync(input, cancellationToken);

        return(output.Memory);
    }
Beispiel #6
0
        public async ValueTask WriteAsync(OmniHash rootHash, OmniHash targetHash, ReadOnlyMemory <byte> memory, CancellationToken cancellationToken = default)
        {
            using (await _asyncLock.LockAsync())
            {
                if (!_wantFileStatusMap.TryGetValue(rootHash, out var status) ||
                    !status.WantBlocks.Contains(targetHash))
                {
                    return;
                }

                var filePath = Path.Combine(Path.Combine(_configPath, this.OmniHashToFilePath(rootHash)), this.OmniHashToFilePath(targetHash));

                if (File.Exists(filePath))
                {
                    return;
                }

                using (var fileStream = new UnbufferedFileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, FileOptions.None, _bytesPool))
                {
                    await fileStream.WriteAsync(memory);
                }

                status.WantBlocks.Remove(targetHash);
            }
        }
Beispiel #7
0
    private static OmniHash CreateOmniHash(string name, ReadOnlySpan <byte> publicKey, OmniHashAlgorithmType hashAlgorithmType)
    {
        if (name == null)
        {
            throw new ArgumentNullException(nameof(name));
        }

        using var bytesPipe = new BytesPipe();
        {
            var writer = new RocketMessageWriter(bytesPipe.Writer, BytesPool.Shared);

            writer.Write(name);
            writer.Write(publicKey);
        }

        if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256)
        {
            var result = new OmniHash(hashAlgorithmType, Sha2_256.ComputeHash(bytesPipe.Reader.GetSequence()));

            return(result);
        }
        else
        {
            throw new NotSupportedException();
        }
    }
Beispiel #8
0
            public OmniSignature Deserialize(Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new System.FormatException();
                }

                // Read property count
                uint propertyCount = r.GetUInt32();

                string   p_name = string.Empty;
                OmniHash p_hash = OmniHash.Empty;

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:     // Name
                    {
                        p_name = r.GetString(32);
                        break;
                    }

                    case 1:     // Hash
                    {
                        p_hash = OmniHash.Formatter.Deserialize(r, rank + 1);
                        break;
                    }
                    }
                }

                return(new OmniSignature(p_name, p_hash));
            }
Beispiel #9
0
        private static OmniHash CreateOmniHash(string name, ReadOnlySpan <byte> publicKey, OmniHashAlgorithmType hashAlgorithmType)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            using (var hub = new Hub())
            {
                {
                    var writer = new RocketPackWriter(hub.Writer, BufferPool.Shared);

                    writer.Write(name);
                    writer.Write(publicKey);

                    hub.Writer.Complete();
                }

                if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256)
                {
                    var result = new OmniHash(hashAlgorithmType, Sha2_256.ComputeHash(hub.Reader.GetSequence()));
                    hub.Reader.Complete();

                    return(result);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
Beispiel #10
0
            public XeusClue Deserialize(global::Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new global::System.FormatException();
                }

                uint propertyCount = r.GetUInt32();

                OmniHash p_hash  = OmniHash.Empty;
                byte     p_depth = 0;

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:
                    {
                        p_hash = OmniHash.Formatter.Deserialize(r, rank + 1);
                        break;
                    }

                    case 1:
                    {
                        p_depth = r.GetUInt8();
                        break;
                    }
                    }
                }

                return(new XeusClue(p_hash, p_depth));
            }
Beispiel #11
0
        public void Add(OmniHash hash)
        {
            _lockedHashMap.TryGetValue(hash, out int count);

            count++;

            _lockedHashMap[hash] = count;
        }
Beispiel #12
0
    public static string SignatureToString(OmniSignature signature)
    {
        using var bytesPipe = new BytesPipe(BytesPool.Shared);
        signature.Export(bytesPipe.Writer, BytesPool.Shared);
        var hash = new OmniHash(OmniHashAlgorithmType.Sha2_256, Sha2_256.ComputeHash(bytesPipe.Reader.GetSequence()));

        return(hash.ToString(ConvertStringType.Base16));
    }
Beispiel #13
0
        private async ValueTask WriteAsync(string basePath, OmniHash hash, ReadOnlyMemory <byte> memory)
        {
            var filePath = Path.Combine(basePath, this.OmniHashToFilePath(hash));

            using (var fileStream = new UnbufferedFileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, FileOptions.None, _bytesPool))
            {
                await fileStream.WriteAsync(memory);
            }
        }
Beispiel #14
0
        public bool Exists(OmniHash rootHash)
        {
            lock (_lockObject)
            {
                var rootHashEntity = OmniHashEntity.Import(rootHash);

                var col = this.GetCollection();
                return(col.Exists(n => n.RootHash == rootHashEntity));
            }
        }
        public SubscribedFileItem?FindOne(OmniHash rootHash, string registrant)
        {
            lock (_lockObject)
            {
                var rootHashEntity = OmniHashEntity.Import(rootHash);

                var col = this.GetCollection();
                return(col.FindOne(n => n.RootHash == rootHashEntity && n.Registrant == registrant)?.Export());
            }
        }
        public IEnumerable <SubscribedFileItem> Find(OmniHash rootHash)
        {
            lock (_lockObject)
            {
                var rootHashEntity = OmniHashEntity.Import(rootHash);

                var col = this.GetCollection();
                return(col.Find(n => n.RootHash == rootHashEntity).Select(n => n.Export()).ToArray());
            }
        }
Beispiel #17
0
        public void Delete(OmniHash rootHash)
        {
            lock (_lockObject)
            {
                var rootHashEntity = OmniHashEntity.Import(rootHash);

                var col = this.GetCollection();
                col.DeleteMany(n => n.RootHash == rootHashEntity);
            }
        }
Beispiel #18
0
        public void Delete(OmniHash rootHash, string registrant)
        {
            lock (_lockObject)
            {
                var rootHashEntity = OmniHashEntity.Import(rootHash);

                var col = this.GetCollection();
                col.DeleteMany(n => n.RootHash == rootHashEntity && n.FilePath == null && n.Registrant == registrant);
            }
        }
            public SharedBlocksMetadata?Get(OmniHash hash)
            {
                this.Update();

                if (_map !.TryGetValue(hash, out var metadata))
                {
                    return(metadata);
                }

                return(null);
            }
Beispiel #20
0
        public SessionStatus(ISession session, ExchangeType exchangeType, OmniHash rootHash, IBatchActionDispatcher batchActionDispatcher)
        {
            this.Session               = session;
            this.ExchangeType          = exchangeType;
            this.RootHash              = rootHash;
            this.BatchActionDispatcher = batchActionDispatcher;
            this.LastReceivedTime      = DateTime.UtcNow;

            this.SentBlockHashes         = new(TimeSpan.FromMinutes(3), TimeSpan.FromSeconds(30), batchActionDispatcher);
            this.ReceivedWantBlockHashes = new(TimeSpan.FromMinutes(3), TimeSpan.FromSeconds(30), batchActionDispatcher);
        }
Beispiel #21
0
            public SharedBlocksMetadata Deserialize(global::Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new global::System.FormatException();
                }

                uint propertyCount = r.GetUInt32();

                string p_path        = string.Empty;
                ulong  p_length      = 0;
                uint   p_blockLength = 0;

                OmniHash[] p_hashes = global::System.Array.Empty <OmniHash>();

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:
                    {
                        p_path = r.GetString(1024);
                        break;
                    }

                    case 1:
                    {
                        p_length = r.GetUInt64();
                        break;
                    }

                    case 2:
                    {
                        p_blockLength = r.GetUInt32();
                        break;
                    }

                    case 3:
                    {
                        var length = r.GetUInt32();
                        p_hashes = new OmniHash[length];
                        for (int i = 0; i < p_hashes.Length; i++)
                        {
                            p_hashes[i] = OmniHash.Formatter.Deserialize(r, rank + 1);
                        }
                        break;
                    }
                    }
                }

                return(new SharedBlocksMetadata(p_path, p_length, p_blockLength, p_hashes));
            }
Beispiel #22
0
            public BlockStorageConfig Deserialize(global::Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new global::System.FormatException();
                }

                uint propertyCount = r.GetUInt32();

                uint  p_version = 0;
                ulong p_size    = 0;

                global::System.Collections.Generic.Dictionary <OmniHash, ClusterMetadata> p_clusterMetadataMap = new global::System.Collections.Generic.Dictionary <OmniHash, ClusterMetadata>();

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:
                    {
                        p_version = r.GetUInt32();
                        break;
                    }

                    case 1:
                    {
                        p_size = r.GetUInt64();
                        break;
                    }

                    case 2:
                    {
                        var length = r.GetUInt32();
                        p_clusterMetadataMap = new global::System.Collections.Generic.Dictionary <OmniHash, ClusterMetadata>();
                        OmniHash        t_key   = OmniHash.Empty;
                        ClusterMetadata t_value = ClusterMetadata.Empty;
                        for (int i = 0; i < length; i++)
                        {
                            t_key   = OmniHash.Formatter.Deserialize(r, rank + 1);
                            t_value = ClusterMetadata.Formatter.Deserialize(r, rank + 1);
                            p_clusterMetadataMap[t_key] = t_value;
                        }
                        break;
                    }
                    }
                }

                return(new BlockStorageConfig(p_version, p_size, p_clusterMetadataMap));
            }
Beispiel #23
0
        public async ValueTask AddWantFileAsync(OmniHash rootHash, CancellationToken cancellationToken = default)
        {
            using (await _asyncLock.LockAsync())
            {
                if (_wantFileStatusMap.ContainsKey(rootHash))
                {
                    return;
                }

                var status = new WantFileStatus(rootHash);
                status.CurrentDepth = 0;
                status.WantBlocks.Add(rootHash);

                _wantFileStatusMap.Add(rootHash, status);
            }
        }
Beispiel #24
0
            public void Set(OmniHash key, bool state)
            {
                if (!_bloomFilter.Contains(key))
                {
                    return;
                }

                State info;

                if (!_dic.TryGetValue(key, out info))
                {
                    return;
                }

                info.IsEnabled = state;
            }
Beispiel #25
0
            public MerkleTreeSection Deserialize(global::Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new global::System.FormatException();
                }

                uint propertyCount = r.GetUInt32();

                CorrectionAlgorithmType p_correctionAlgorithmType = (CorrectionAlgorithmType)0;
                ulong p_length = 0;

                OmniHash[] p_hashes = global::System.Array.Empty <OmniHash>();

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:
                    {
                        p_correctionAlgorithmType = (CorrectionAlgorithmType)r.GetUInt64();
                        break;
                    }

                    case 1:
                    {
                        p_length = r.GetUInt64();
                        break;
                    }

                    case 2:
                    {
                        var length = r.GetUInt32();
                        p_hashes = new OmniHash[length];
                        for (int i = 0; i < p_hashes.Length; i++)
                        {
                            p_hashes[i] = OmniHash.Formatter.Deserialize(r, rank + 1);
                        }
                        break;
                    }
                    }
                }

                return(new MerkleTreeSection(p_correctionAlgorithmType, p_length, p_hashes));
            }
Beispiel #26
0
            public ContentMetadata Deserialize(global::Omnix.Serialization.RocketPack.RocketPackReader r, int rank)
            {
                if (rank > 256)
                {
                    throw new global::System.FormatException();
                }

                uint propertyCount = r.GetUInt32();

                XeusClue p_clue = XeusClue.Empty;

                OmniHash[]           p_lockedHashes         = global::System.Array.Empty <OmniHash>();
                SharedBlocksMetadata?p_sharedBlocksMetadata = null;

                for (; propertyCount > 0; propertyCount--)
                {
                    uint id = r.GetUInt32();
                    switch (id)
                    {
                    case 0:
                    {
                        p_clue = XeusClue.Formatter.Deserialize(r, rank + 1);
                        break;
                    }

                    case 1:
                    {
                        var length = r.GetUInt32();
                        p_lockedHashes = new OmniHash[length];
                        for (int i = 0; i < p_lockedHashes.Length; i++)
                        {
                            p_lockedHashes[i] = OmniHash.Formatter.Deserialize(r, rank + 1);
                        }
                        break;
                    }

                    case 2:
                    {
                        p_sharedBlocksMetadata = SharedBlocksMetadata.Formatter.Deserialize(r, rank + 1);
                        break;
                    }
                    }
                }

                return(new ContentMetadata(p_clue, p_lockedHashes, p_sharedBlocksMetadata));
            }
Beispiel #27
0
        public XeusClue(OmniHash hash, byte depth)
        {
            this.Hash  = hash;
            this.Depth = depth;

            {
                var __h = new global::System.HashCode();
                if (this.Hash != default)
                {
                    __h.Add(this.Hash.GetHashCode());
                }
                if (this.Depth != default)
                {
                    __h.Add(this.Depth.GetHashCode());
                }
                __hashCode = __h.ToHashCode();
            }
        }
Beispiel #28
0
        public void Remove(OmniHash hash)
        {
            if (!_lockedHashMap.TryGetValue(hash, out int count))
            {
                throw new KeyNotFoundException();
            }

            count--;

            if (count == 0)
            {
                _lockedHashMap.Remove(hash);
            }
            else
            {
                _lockedHashMap[hash] = count;
            }
        }
Beispiel #29
0
        public bool TryGetIndex(OmniHash targetHash, out long index)
        {
            if (_indexMap == null)
            {
                lock (_lockObject)
                {
                    if (_indexMap == null)
                    {
                        _indexMap = new Dictionary <OmniHash, long>();

                        for (int i = 0; i < this.Hashes.Count; i++)
                        {
                            _indexMap[this.Hashes[i]] = i;
                        }
                    }
                }
            }

            return(_indexMap.TryGetValue(targetHash, out index));
        }
Beispiel #30
0
        public int GetIndex(OmniHash hash)
        {
            if (_hashMap == null)
            {
                _hashMap = new Dictionary <OmniHash, int>();

                for (int i = 0; i < this.Hashes.Count; i++)
                {
                    _hashMap[this.Hashes[i]] = i;
                }
            }

            {
                if (!_hashMap.TryGetValue(hash, out int result))
                {
                    return(-1);
                }

                return(result);
            }
        }