Exemplo n.º 1
0
 public override VersionVector Merge(VersionVector other)
 {
     if (other is MultiVersionVector)
     {
         var vector         = (MultiVersionVector)other;
         var v2             = vector.Versions.GetValueOrDefault(Node, 0L);
         var mergedVersions = v2 >= Version ? vector.Versions : vector.Versions.SetItem(Node, Version);
         return(new MultiVersionVector(mergedVersions));
     }
     else if (other is SingleVersionVector)
     {
         var vector = (SingleVersionVector)other;
         if (Node == vector.Node)
         {
             return(Version >= vector.Version ? this : new SingleVersionVector(vector.Node, vector.Version));
         }
         else
         {
             return(new MultiVersionVector(
                        new KeyValuePair <UniqueAddress, long>(Node, Version),
                        new KeyValuePair <UniqueAddress, long>(vector.Node, vector.Version)));
         }
     }
     else
     {
         throw new NotSupportedException("SingleVersionVector doesn't support merge with provided version vector");
     }
 }
Exemplo n.º 2
0
        public override VersionVector Merge(VersionVector other)
        {
            if (other is MultiVersionVector)
            {
                var vector = (MultiVersionVector)other;
                var merged = vector.Versions.ToBuilder();
                foreach (var pair in Versions)
                {
                    var mergedCurrentTime = merged.GetValueOrDefault(pair.Key, 0L);
                    if (pair.Value >= mergedCurrentTime)
                    {
                        merged.AddOrSet(pair.Key, pair.Value);
                    }
                }

                return(new MultiVersionVector(merged.ToImmutable()));
            }
            else if (other is SingleVersionVector)
            {
                var vector = (SingleVersionVector)other;
                var v1     = Versions.GetValueOrDefault(vector.Node, 0L);
                var merged = v1 >= vector.Version ? Versions : Versions.SetItem(vector.Node, vector.Version);
                return(new MultiVersionVector(merged));
            }
            else
            {
                throw new NotSupportedException("MultiVersionVector doesn't support merge with provided version vector");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// INTERNAL API
        /// Subtract the <paramref name="vvector"/> from the <paramref name="dot"/>.
        /// What this means is that any (node, version) pair in
        /// <paramref name="dot"/> that is &lt;= an entry in <paramref name="vvector"/> is removed from <paramref name="dot"/>.
        /// Example [{a, 3}, {b, 2}, {d, 14}, {g, 22}] -
        ///         [{a, 4}, {b, 1}, {c, 1}, {d, 14}, {e, 5}, {f, 2}] =
        ///         [{b, 2}, {g, 22}]
        /// </summary>
        internal static VersionVector SubtractDots(VersionVector dot, VersionVector vvector)
        {
            if (dot.IsEmpty)
            {
                return(VersionVector.Empty);
            }

            if (dot is SingleVersionVector single)
            {
                // if dot is dominated by version vector, drop it
                return(vvector.VersionAt(single.Node) >= single.Version ? VersionVector.Empty : single);
            }

            if (dot is MultiVersionVector multi)
            {
                var acc = ImmutableDictionary <UniqueAddress, long> .Empty.ToBuilder();

                foreach (var pair in multi.Versions)
                {
                    var v2 = vvector.VersionAt(pair.Key);
                    if (v2 < pair.Value)
                    {
                        acc.Add(pair);
                    }
                }

                return(VersionVector.Create(acc.ToImmutable()));
            }

            throw new NotSupportedException("Cannot subtract dots from provided version vector");
        }