Example #1
0
        public static bool HappensAfter(VectorClock first, VectorClock second)
        {
            // Create immutable other to protect from concurrent accesses
            ImmutableVectorClock immutableFirst  = first.ToImmutable();
            ImmutableVectorClock immutableSecond = second.ToImmutable();

            // Get all possible ids
            ImmutableHashSet <string> firstIds  = immutableFirst.Ids;
            ImmutableHashSet <string> secondIds = immutableSecond.Ids;

            HashSet <string> allIds = new HashSet <string>(firstIds);

            allIds.UnionWith(secondIds);

            // For all ids, at least one has to be greater than
            // other and none can be less
            bool oneGreater = false;

            foreach (string id in allIds)
            {
                int firstClock  = immutableFirst[id];
                int secondClock = immutableSecond[id];

                if (firstClock > secondClock)
                {
                    oneGreater = true;
                }
                else if (firstClock < secondClock)
                {
                    return(false);
                }
            }
            return(oneGreater);
        }
        public void Merge(VectorClock other)
        {
            // Create immutable other to protect from concurrent accesses
            ImmutableVectorClock immutableOther = other.ToImmutable();

            ImmutableHashSet <string> secondIds = immutableOther.Ids;

            // Dont let concurrent updates while merging
            lock (this) {
                // Get all possible ids
                HashSet <string> allIds = new HashSet <string>(vc.Keys);
                allIds.UnionWith(secondIds);

                foreach (string id in allIds)
                {
                    int otherClock = immutableOther[id];

                    // Add other value if not there or
                    // update with max between both values
                    vc.AddOrUpdate(id, otherClock, (key, prev) => Math.Max(prev, otherClock));
                }
                // Invalidate immutable this
                immutableThis = null;
            }
        }
Example #3
0
        public static bool Equal(VectorClock first, VectorClock second)
        {
            // Create immutable other to protect from concurrent accesses
            ImmutableVectorClock immutableFirst  = first.ToImmutable();
            ImmutableVectorClock immutableSecond = second.ToImmutable();

            // Get all possible ids
            ImmutableHashSet <string> firstIds  = immutableFirst.Ids;
            ImmutableHashSet <string> secondIds = immutableSecond.Ids;

            if (firstIds.Count != secondIds.Count)
            {
                return(false);
            }

            foreach (string id in firstIds)
            {
                if (immutableFirst[id] != immutableSecond[id])
                {
                    return(false);
                }
            }
            return(true);
        }