using System; using System.Linq; using FrostFS.Netmap; using FrostFS.SDK.Client; namespace FrostFS.SDK; public struct FrostFsPlacementPolicy(bool unique, params FrostFsReplica[] replicas) : IEquatable { private PlacementPolicy policy; public FrostFsReplica[] Replicas { get; private set; } = replicas; public bool Unique { get; private set; } = unique; public override readonly bool Equals(object obj) { if (obj is null) return false; var other = (FrostFsPlacementPolicy)obj; return Equals(other); } public PlacementPolicy GetPolicy() { if (policy == null) { policy = new PlacementPolicy { Filters = { }, Selectors = { }, Replicas = { }, Unique = Unique }; foreach (var replica in Replicas) { policy.Replicas.Add(replica.ToMessage()); } } return policy; } //public static FrostFsPlacementPolicy ToModel(placementPolicy) //{ // return new FrostFsPlacementPolicy( // placementPolicy.Unique, // placementPolicy.Replicas.Select(replica => replica.ToModel()).ToArray() // ); //} public override readonly int GetHashCode() { return Unique ? 17 : 0 + Replicas.GetHashCode(); } public static bool operator ==(FrostFsPlacementPolicy left, FrostFsPlacementPolicy right) { return left.Equals(right); } public static bool operator !=(FrostFsPlacementPolicy left, FrostFsPlacementPolicy right) { return !(left == right); } public readonly bool Equals(FrostFsPlacementPolicy other) { var notEqual = Unique != other.Unique || Replicas.Length != other.Replicas.Length; if (notEqual) return false; foreach (var replica in Replicas) { if (!other.Replicas.Any(r => r.Equals(replica))) return false; } return true; } }