/// <summary>
 /// Converts the <see cref="sourceValue" /> parameter to the <see cref="destinationType" /> parameter using <see cref="formatProvider"
 /// /> and <see cref="ignoreCase" />
 /// </summary>
 /// <param name="sourceValue">the value to convert into an instance of <see cref="ClusterMetadata" />.</param>
 /// <returns>
 /// an instance of <see cref="ClusterMetadata" />, or <c>null</c> if there is no suitable conversion.
 /// </returns>
 public static object ConvertFrom(dynamic sourceValue)
 {
     if (null == sourceValue)
     {
         return(null);
     }
     try
     {
         ClusterMetadata.FromJsonString(typeof(string) == sourceValue.GetType() ? sourceValue : sourceValue.ToJsonString());
     }
     catch
     {
         // Unable to use JSON pattern
     }
     try
     {
         return(new ClusterMetadata
         {
             Name = sourceValue.Name,
             Categories = sourceValue.Categories,
             CreationTime = sourceValue.CreationTime,
             Kind = sourceValue.Kind,
             LastUpdateTime = sourceValue.LastUpdateTime,
             OwnerReference = UserReferenceTypeConverter.ConvertFrom(sourceValue.OwnerReference),
             ProjectReference = ProjectReferenceTypeConverter.ConvertFrom(sourceValue.ProjectReference),
             SpecHash = sourceValue.SpecHash,
             SpecVersion = sourceValue.SpecVersion,
             Uuid = sourceValue.Uuid,
         });
     }
     catch
     {
     }
     return(null);
 }
Exemplo n.º 2
0
        public void SnowballDescribeCluster()
        {
            #region to-describe-a-cluster-1482864218396

            var response = client.DescribeCluster(new DescribeClusterRequest
            {
                ClusterId = "CID123e4567-e89b-12d3-a456-426655440000"
            });

            ClusterMetadata clusterMetadata = response.ClusterMetadata;

            #endregion
        }
Exemplo n.º 3
0
        public bool TrySet(OmniHash hash, ReadOnlySpan <byte> value)
        {
            if (!EnumHelper.IsValid(hash.AlgorithmType))
            {
                throw new ArgumentException($"Incorrect HashAlgorithmType: {hash.AlgorithmType}");
            }

            if (value.Length > 1024 * 1024 * 32)
            {
                _logger.Debug($"{nameof(value)} too large.");

                return(false);
            }

            if (hash.AlgorithmType == OmniHashAlgorithmType.Sha2_256 &&
                !BytesOperations.SequenceEqual(Sha2_256.ComputeHash(value), hash.Value.Span))
            {
                _logger.Debug("Broken block.");

                return(false);
            }

            lock (_lockObject)
            {
                if (this.Contains(hash))
                {
                    _logger.Debug($"Already exist.");

                    return(true);
                }


                if (!this.TryGetFreeSectors((int)((value.Length + (SectorSize - 1)) / SectorSize), out var sectors))
                {
                    _errorReportEventQueue.Enqueue(new ErrorReport(Timestamp.FromDateTime(DateTime.UtcNow), ErrorReportType.SpaceNotFound));

                    _logger.Debug("Space not found.");

                    return(false);
                }

                try
                {
                    uint remain = (uint)value.Length;

                    for (int i = 0; i < sectors.Length && 0 < remain; i++, remain -= SectorSize)
                    {
                        ulong position = sectors[i] * SectorSize;

                        if ((ulong)_fileStream.Length < position + SectorSize)
                        {
                            const uint unit = 1024 * 1024 * 256; // 256MB
                            ulong      size = MathHelper.Roundup((position + SectorSize), unit);

                            _fileStream.SetLength((long)Math.Min(size, this.Size));
                        }

                        if ((ulong)_fileStream.Position != position)
                        {
                            _fileStream.Seek((long)position, SeekOrigin.Begin);
                        }

                        uint length = Math.Min(remain, SectorSize);

                        BytesOperations.Copy(value.Slice((int)(SectorSize * i)), _sectorBuffer, (int)length);
                        BytesOperations.Zero(_sectorBuffer.AsSpan((int)length, (int)(_sectorBuffer.Length - length)));

                        _fileStream.Write(_sectorBuffer, 0, _sectorBuffer.Length);
                    }

                    _fileStream.Flush();
                }
                catch (Exception e)
                {
                    _logger.Debug(e);

                    return(false);
                }

                _clusterMetadataMap[hash] = new ClusterMetadata(sectors, (uint)value.Length, Timestamp.FromDateTime(DateTime.UtcNow));

                // Event
                _addedBlockEventQueue.Enqueue(hash);

                return(true);
            }
        }
Exemplo n.º 4
0
        public bool TryGet(OmniHash hash, out IMemoryOwner <byte>?memoryOwner)
        {
            if (!EnumHelper.IsValid(hash.AlgorithmType))
            {
                throw new ArgumentException($"Incorrect HashAlgorithmType: {hash.AlgorithmType}");
            }

            memoryOwner = null;
            bool success = false;

            try
            {
                lock (_lockObject)
                {
                    if (_clusterMetadataMap.TryGetValue(hash, out var clusterInfo))
                    {
                        clusterInfo = new ClusterMetadata(clusterInfo.Sectors.ToArray(), clusterInfo.Length, Timestamp.FromDateTime(DateTime.UtcNow));
                        _clusterMetadataMap[hash] = clusterInfo;
                    }

                    if (clusterInfo == null)
                    {
                        return(false);
                    }

                    memoryOwner = _bufferPool.Rent((int)clusterInfo.Length);

                    try
                    {
                        uint remain = clusterInfo.Length;

                        for (int i = 0; i < clusterInfo.Sectors.Count; i++, remain -= SectorSize)
                        {
                            ulong position = clusterInfo.Sectors[i] * SectorSize;

                            if (position > (ulong)_fileStream.Length)
                            {
                                _logger.Debug($"position too large: {position}");

                                return(false);
                            }

                            if ((ulong)_fileStream.Position != position)
                            {
                                _fileStream.Seek((long)position, SeekOrigin.Begin);
                            }

                            uint length = Math.Min(remain, SectorSize);

                            _fileStream.Read(_sectorBuffer, 0, _sectorBuffer.Length);
                            BytesOperations.Copy(_sectorBuffer, memoryOwner.Memory.Span.Slice((int)(SectorSize * i)), (int)length);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.Debug(e);

                        return(false);
                    }
                }

                if (hash.AlgorithmType == OmniHashAlgorithmType.Sha2_256 &&
                    BytesOperations.SequenceEqual(Sha2_256.ComputeHash(memoryOwner.Memory.Span), hash.Value.Span))
                {
                    success = true;

                    return(true);
                }
                else
                {
                    _logger.Debug("Broken block.");

                    return(false);
                }
            }
            finally
            {
                if (!success)
                {
                    if (memoryOwner != null)
                    {
                        memoryOwner.Dispose();
                        memoryOwner = null;
                    }

                    this.Remove(hash);
                }
            }
        }