示例#1
0
        public EdmDirectValueAnnotationsManager()
        {
            this.annotationsDictionaryLock = new object();
            this.unsortedElements          = VersioningList <IEdmElement> .Create();

            this.unsortedElementsLock  = new object();
            this.annotationsDictionary = VersioningDictionary <IEdmElement, object> .Create(new Func <IEdmElement, IEdmElement, int>(this.CompareElements));
        }
        private static void SetAnnotation(IEnumerable <IEdmDirectValueAnnotation> immutableAnnotations, ref VersioningList <IEdmDirectValueAnnotation> transientAnnotations, string namespaceName, string localName, object value)
        {
            bool needTombstone = false;

            if (immutableAnnotations != null)
            {
                foreach (IEdmDirectValueAnnotation existingAnnotation in immutableAnnotations)
                {
                    if (existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName)
                    {
                        needTombstone = true;
                        break;
                    }
                }
            }

            if (value == null)
            {
                // "Removing" an immutable annotation leaves behind a transient annotation with a null value
                // as a tombstone to hide the immutable annotation. The normal logic below makes this happen.
                // Removing a transient annotation actually takes the annotation away.
                if (!needTombstone)
                {
                    RemoveTransientAnnotation(ref transientAnnotations, namespaceName, localName);
                    return;
                }
            }

            IEdmDirectValueAnnotation newAnnotation = value != null ?
                                                      new EdmDirectValueAnnotation(namespaceName, localName, value) :
                                                      new EdmDirectValueAnnotation(namespaceName, localName);

            if (transientAnnotations == null)
            {
                transientAnnotations = VersioningList <IEdmDirectValueAnnotation> .Create().Add(newAnnotation);

                return;
            }

            for (int index = 0; index < transientAnnotations.Count; index++)
            {
                IEdmDirectValueAnnotation existingAnnotation = transientAnnotations[index];
                if (existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName)
                {
                    transientAnnotations = transientAnnotations.RemoveAt(index);
                    break;
                }
            }

            transientAnnotations = transientAnnotations.Add(newAnnotation);
        }
        /// <summary>
        /// Sets the serialization alias for a given namespace(including current model's schemas namespace-alias, and referenced models' schemas namespace-alias)
        /// TODO: REF make sure no duplicated alias.
        /// </summary>
        /// <param name="model">Model that will be serialized.</param>
        /// <param name="namespaceName">The namespace to set the alias for.</param>
        /// <param name="alias">The alias for that namespace.</param>
        public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, string alias)
        {
            VersioningDictionary <string, string> mappings = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation);

            if (mappings == null)
            {
                mappings = VersioningDictionary <string, string> .Create(string.CompareOrdinal);
            }

            if (EdmUtil.IsNullOrWhiteSpaceInternal(alias))
            {
                string val;
                if (mappings.TryGetValue(namespaceName, out val))
                {
                    mappings = mappings.Remove(namespaceName);
                }
            }
            else
            {
                mappings = mappings.Set(namespaceName, alias);
            }

            model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation, mappings);

            var list = model.GetAnnotationValue <VersioningList <string> >(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation);

            if (list == null)
            {
                list = VersioningList <string> .Create();
            }

            if (!string.IsNullOrEmpty(namespaceName) && !list.Contains(namespaceName))
            {
                list = list.Add(namespaceName);
            }

            model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation, list);
        }
示例#4
0
        public void StringVersioningLists()
        {
            VersioningList <string> list1 = VersioningList <string> .Create();

            string a = "";

            for (int i = 0; i < 1000; i++)
            {
                a     = a + "a";
                list1 = list1.Add(a);
            }

            a = "";
            int count = 0;

            foreach (string s in list1)
            {
                a = a + "a";
                Assert.AreEqual(a, s, "Element value");
                count++;
            }

            Assert.AreEqual(1000, count, "Element count");
        }
示例#5
0
        private static void SetAnnotation(IEnumerable <IEdmDirectValueAnnotation> immutableAnnotations, ref object transientAnnotations, string namespaceName, string localName, object value)
        {
            bool needTombstone = false;

            if (immutableAnnotations != null)
            {
                if (immutableAnnotations.Any(existingAnnotation => existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName))
                {
                    needTombstone = true;
                }
            }

            if (value == null)
            {
                // "Removing" an immutable annotation leaves behind a transient annotation with a null value
                // as a tombstone to hide the immutable annotation. The normal logic below makes this happen.
                // Removing a transient annotation actually takes the annotation away.
                if (!needTombstone)
                {
                    RemoveTransientAnnotation(ref transientAnnotations, namespaceName, localName);
                    return;
                }
            }

            if (namespaceName == EdmConstants.DocumentationUri && value != null && !(value is IEdmDocumentation))
            {
                throw new InvalidOperationException(Edm.Strings.Annotations_DocumentationPun(value.GetType().Name));
            }

            IEdmDirectValueAnnotation newAnnotation = value != null ?
                                                      new EdmDirectValueAnnotation(namespaceName, localName, value) :
                                                      new EdmDirectValueAnnotation(namespaceName, localName);

            if (transientAnnotations == null)
            {
                transientAnnotations = newAnnotation;
                return;
            }

            IEdmDirectValueAnnotation singleAnnotation = transientAnnotations as IEdmDirectValueAnnotation;

            if (singleAnnotation != null)
            {
                if (singleAnnotation.NamespaceUri == namespaceName && singleAnnotation.Name == localName)
                {
                    transientAnnotations = newAnnotation;
                }
                else
                {
                    transientAnnotations = VersioningList <IEdmDirectValueAnnotation> .Create().Add(singleAnnotation).Add(newAnnotation);
                }

                return;
            }

            VersioningList <IEdmDirectValueAnnotation> annotationsList = (VersioningList <IEdmDirectValueAnnotation>)transientAnnotations;

            for (int index = 0; index < annotationsList.Count; index++)
            {
                IEdmDirectValueAnnotation existingAnnotation = annotationsList[index];
                if (existingAnnotation.NamespaceUri == namespaceName && existingAnnotation.Name == localName)
                {
                    annotationsList = annotationsList.RemoveAt(index);
                    break;
                }
            }

            transientAnnotations = annotationsList.Add(newAnnotation);
        }
示例#6
0
        public void IntegerVersioningLists()
        {
            VersioningList <int> list1 = VersioningList <int> .Create();

            Assert.IsTrue(list1 is VersioningList <int> .EmptyVersioningList, "List type");
            TestList(list1, 0);

            VersioningList <int> list2 = list1.Add(0);

            Assert.IsTrue(list2 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list2, 1);

            VersioningList <int> list3 = list1.Add(0);

            Assert.IsTrue(list3 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list3, 1);

            VersioningList <int> list4 = list2.Add(10);

            Assert.IsTrue(list4 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list4, 2);

            VersioningList <int> list5 = list4.Add(20);

            Assert.IsTrue(list5 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list5, 3);

            VersioningList <int> list6 = list5.Add(30);

            Assert.IsTrue(list6 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list6, 4);

            VersioningList <int> list7 = list6.Add(40);

            Assert.IsTrue(list7 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list7, 5);

            VersioningList <int> list8 = list7.Add(50);

            Assert.IsTrue(list8 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list8, 6);

            VersioningList <int> list9 = list8.Add(60);

            Assert.IsTrue(list9 is VersioningList <int> .ArrayVersioningList, "List type");
            TestList(list9, 7);

            TestList(list8, 6);
            TestList(list7, 5);
            TestList(list6, 4);
            TestList(list5, 3);
            TestList(list4, 2);
            TestList(list3, 1);
            TestList(list2, 1);
            TestList(list1, 0);

            for (int index = 0; index < list9.Count; index++)
            {
                VersioningList <int> list00 = list9.RemoveAt(index);
                TestList(list00, 7, index);
            }

            TestList(list9, 7);

            VersioningList <int> shrinker = list9;
            int expectedCount             = list9.Count;

            for (int index = 0; index < list9.Count; index++)
            {
                Assert.AreEqual(index * 10, shrinker[0], "Shrinking value");
                expectedCount--;
                shrinker = shrinker.RemoveAt(0);
                Assert.AreEqual(expectedCount, shrinker.Count, "Shrinking count");
            }

            shrinker      = list9;
            expectedCount = list9.Count;
            for (int index = list9.Count - 1; index >= 0; index--)
            {
                Assert.AreEqual(index * 10, shrinker[index], "Shrinking value");
                expectedCount--;
                shrinker = shrinker.RemoveAt(index);
                Assert.AreEqual(expectedCount, shrinker.Count, "Shrinking count");
            }

            TestList(list9, 7);
        }
示例#7
0
        private static void SetAnnotation(IEnumerable <IEdmDirectValueAnnotation> immutableAnnotations, ref object transientAnnotations, string namespaceName, string localName, object value)
        {
            IEdmDirectValueAnnotation edmDirectValueAnnotation;
            Func <IEdmDirectValueAnnotation, bool> func = null;
            bool flag = false;

            if (immutableAnnotations != null)
            {
                IEnumerable <IEdmDirectValueAnnotation> edmDirectValueAnnotations = immutableAnnotations;
                if (func == null)
                {
                    func = (IEdmDirectValueAnnotation existingAnnotation) => {
                        if (existingAnnotation.NamespaceUri != namespaceName)
                        {
                            return(false);
                        }
                        else
                        {
                            return(existingAnnotation.Name == localName);
                        }
                    }
                    ;
                }
                if (edmDirectValueAnnotations.Any <IEdmDirectValueAnnotation>(func))
                {
                    flag = true;
                }
            }
            if (value != null || flag)
            {
                if (!(namespaceName == "http://schemas.microsoft.com/ado/2011/04/edm/documentation") || value == null || value as IEdmDocumentation != null)
                {
                    if (value != null)
                    {
                        edmDirectValueAnnotation = new EdmDirectValueAnnotation(namespaceName, localName, value);
                    }
                    else
                    {
                        edmDirectValueAnnotation = new EdmDirectValueAnnotation(namespaceName, localName);
                    }
                    IEdmDirectValueAnnotation edmDirectValueAnnotation1 = edmDirectValueAnnotation;
                    if (transientAnnotations != null)
                    {
                        IEdmDirectValueAnnotation edmDirectValueAnnotation2 = transientAnnotations as IEdmDirectValueAnnotation;
                        if (edmDirectValueAnnotation2 == null)
                        {
                            VersioningList <IEdmDirectValueAnnotation> edmDirectValueAnnotations1 = (VersioningList <IEdmDirectValueAnnotation>)transientAnnotations;
                            int num = 0;
                            while (num < edmDirectValueAnnotations1.Count)
                            {
                                IEdmDirectValueAnnotation item = edmDirectValueAnnotations1[num];
                                if (!(item.NamespaceUri == namespaceName) || !(item.Name == localName))
                                {
                                    num++;
                                }
                                else
                                {
                                    edmDirectValueAnnotations1 = edmDirectValueAnnotations1.RemoveAt(num);
                                    break;
                                }
                            }
                            transientAnnotations = edmDirectValueAnnotations1.Add(edmDirectValueAnnotation1);
                            return;
                        }
                        else
                        {
                            if (!(edmDirectValueAnnotation2.NamespaceUri == namespaceName) || !(edmDirectValueAnnotation2.Name == localName))
                            {
                                transientAnnotations = VersioningList <IEdmDirectValueAnnotation> .Create().Add(edmDirectValueAnnotation2).Add(edmDirectValueAnnotation1);

                                return;
                            }
                            else
                            {
                                transientAnnotations = edmDirectValueAnnotation1;
                                return;
                            }
                        }
                    }
                    else
                    {
                        transientAnnotations = edmDirectValueAnnotation1;
                        return;
                    }
                }
                else
                {
                    throw new InvalidOperationException(Strings.Annotations_DocumentationPun(value.GetType().Name));
                }
            }
            else
            {
                EdmDirectValueAnnotationsManager.RemoveTransientAnnotation(ref transientAnnotations, namespaceName, localName);
                return;
            }
        }