Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves the transient annotations for an EDM element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="annotationsDictionary">The dictionary for looking up the element's annotations.</param>
        /// <returns>The transient annotations for the element, in a form managed by the annotations manager.</returns>
        /// <remarks>This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation.</remarks>
        private static object GetTransientAnnotations(IEdmElement element, VersioningDictionary <IEdmElement, object> annotationsDictionary)
        {
            object transientAnnotations;

            annotationsDictionary.TryGetValue(element, out transientAnnotations);
            return(transientAnnotations);
        }
        /// <summary>
        /// Retrieves the transient annotations for an EDM element.
        /// </summary>
        /// <param name="element">The annotated element.</param>
        /// <param name="annotationsDictionary">The dictionary for looking up the element's annotations.</param>
        /// <returns>The transient annotations for the element, in a form managed by the annotations manager.</returns>
        /// <remarks>This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation.</remarks>
        private static VersioningList <IEdmDirectValueAnnotation> GetTransientAnnotations(IEdmElement element, VersioningDictionary <IEdmElement, VersioningList <IEdmDirectValueAnnotation> > annotationsDictionary)
        {
            VersioningList <IEdmDirectValueAnnotation> transientAnnotations;

            annotationsDictionary.TryGetValue(element, out transientAnnotations);
            return(transientAnnotations);
        }
        /// <summary>
        /// Gets the serialization alias for a given namespace.
        /// </summary>
        /// <param name="model">Model that will be serialized.</param>
        /// <param name="namespaceName">Namespace the alias is needed for.</param>
        /// <returns>The alias of the given namespace, or null if one does not exist.</returns>
        public static string GetNamespaceAlias(this IEdmModel model, string namespaceName)
        {
            EdmUtil.CheckArgumentNull(model, "model");
            VersioningDictionary <string, string> mappings = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation);
            string namespaceAlias;

            if (mappings != null && mappings.TryGetValue(namespaceName, out namespaceAlias))
            {
                return(namespaceAlias);
            }

            return(null);
        }
        /// <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);
        }
 /// <summary>
 /// Retrieves the transient annotations for an EDM element.
 /// </summary>
 /// <param name="element">The annotated element.</param>
 /// <param name="annotationsDictionary">The dictionary for looking up the element's annotations.</param>
 /// <returns>The transient annotations for the element, in a form managed by the annotations manager.</returns>
 /// <remarks>This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation.</remarks>
 private static object GetTransientAnnotations(IEdmElement element, VersioningDictionary<IEdmElement, object> annotationsDictionary)
 {
     object transientAnnotations;
     annotationsDictionary.TryGetValue(element, out transientAnnotations);
     return transientAnnotations;
 }
Ejemplo n.º 6
0
        public void MapIntegerInsertionAndDeletion()
        {
            VersioningDictionary <int, int> map = VersioningDictionary <int, int> .Create(CompareIntegers);

            int value;

            Assert.IsTrue(map is VersioningDictionary <int, int> .EmptyVersioningDictionary, "Dictionary type");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            // Test conversions back and forth between EmptyDictionary, OneKeyDictionary, and TwoKeyDictionary.

            map = map.Set(1, 10);
            Assert.IsTrue(map is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 10, "Key value");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            map = map.Set(1, 100);
            Assert.IsTrue(map is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            map = map.Remove(1);
            Assert.IsTrue(map is VersioningDictionary <int, int> .EmptyVersioningDictionary, "Dictionary type");

            map = map.Set(1, 10);
            map = map.Set(2, 20);
            Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 10, "Key value");
            Assert.AreEqual(map.Get(2), 20, "Key value");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            map = map.Set(1, 100);
            Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 20, "Key value");

            map = map.Set(2, 200);
            Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 200, "Key value");

            VersioningDictionary <int, int> oneMap = map.Remove(1);

            Assert.IsTrue(oneMap is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type");
            Assert.AreEqual(oneMap.Get(2), 200, "Key value");

            Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 200, "Key value");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            oneMap = map.Remove(2);
            Assert.IsTrue(oneMap is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type");
            Assert.AreEqual(oneMap.Get(1), 100, "Key value");

            Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 200, "Key value");

            // Test conversion to a TreeDictionary.

            map = map.Set(3, 30);
            Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 200, "Key value");
            Assert.AreEqual(map.Get(3), 30, "Key value");
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            map = map.Set(3, 300);
            Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type");
            Assert.AreEqual(map.Get(1), 100, "Key value");
            Assert.AreEqual(map.Get(2), 200, "Key value");
            Assert.AreEqual(map.Get(3), 300, "Key value");

            // Test that a significant number of keys does not force conversion to a HashKeyDictionary.

            for (int i = 1; i <= 100; i++)
            {
                map = map.Set(i, i * 10);
            }
            Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type");
            for (int i = 1; i <= 100; i++)
            {
                Assert.AreEqual(i * 10, map.Get(i), "Key value");
            }
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            // Test removing half the keys.

            for (int i = 1; i < 100; i += 2)
            {
                map = map.Remove(i);
            }
            Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type");
            for (int i = 1; i < 100; i += 2)
            {
                Assert.IsFalse(map.TryGetValue(i, out value), "Odd key");
                Assert.AreEqual((i + 1) * 10, map.Get(i + 1), "Even key value");
            }

            // Test conversion to a HashTreeDictionary.

            for (int i = 1; i <= 10000; i++)
            {
                map = map.Set(i, i * 2);
            }
            Assert.IsTrue(map is VersioningDictionary <int, int> .HashTreeDictionary, "Dictionary type");
            for (int i = 1; i <= 10000; i++)
            {
                Assert.AreEqual(i * 2, map.Get(i), "Key value");
            }
            Assert.IsFalse(map.TryGetValue(0, out value), "Zero key");

            // Test removing half the keys.

            for (int i = 1; i < 10000; i += 2)
            {
                map = map.Remove(i);
            }
            Assert.IsTrue(map is VersioningDictionary <int, int> .HashTreeDictionary, "Dictionary type");
            for (int i = 1; i < 10000; i += 2)
            {
                Assert.IsFalse(map.TryGetValue(i, out value), "Odd key");
                Assert.AreEqual((i + 1) * 2, map.Get(i + 1), "Even key value");
            }
        }