///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Creates the index.
        /// </summary>
        /// <exception cref="NotSupportedException">
        ///  Thrown when the requested operation is not supported.
        /// </exception>
        /// <exception cref="Exception">
        ///  Thrown when an exception error condition occurs.
        /// </exception>
        /// <exception cref="DuplicateIndexException">
        ///  Thrown when a Duplicate Index error condition occurs.
        /// </exception>
        /// <param name="metaclass">
        ///  The metaclass.
        /// </param>
        /// <param name="name">
        ///  The name.
        /// </param>
        /// <param name="unique">
        ///  true to unique.
        /// </param>
        /// <param name="propertyNames">
        ///  List of names of the properties.
        /// </param>
        /// <returns>
        ///  The new index.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public IIndex CreateIndex(ISchemaElement metaclass, string name, bool unique, params string[] propertyNames)
        {
            Contract.Requires(metaclass, "metaclass");
            Contract.Requires(propertyNames.Length > 0, "propertyName");

            if (propertyNames.Length > 1)
            {
                throw new NotSupportedException();
            }

            var property = metaclass.GetProperty(propertyNames[0]);

            if (property == null)
            {
                throw new Hyperstore.Modeling.Metadata.PropertyDefinitionException(string.Format(ExceptionMessages.PropertyNameNotValidForMetaclassFormat, metaclass.Name));
            }

            Conventions.CheckValidName(name);
            IndexDefinition def;

            _sync.EnterWriteLock();
            try
            {
                if (IndexExists(name))
                {
                    throw new DuplicateIndexException(string.Format(ExceptionMessages.DuplicateIndexFormat, name));
                }

                List <IndexDefinition> list = null;
                if (!_indexByMetaClass.TryGetValue(metaclass.Id, out list))
                {
                    list = new List <IndexDefinition>();
                    _indexByMetaClass.Add(metaclass.Id, list);
                }

                def = new IndexDefinition(_graph, name, metaclass, unique, propertyNames);
                list.Add(def);
                _indexByNames.TryAdd(name, def);
            }
            finally
            {
                _sync.ExitWriteLock();
            }

            // Build index
            if (_graph.DomainModel != null)
            {
                foreach (var mel in _graph.GetElements(metaclass))
                {
                    def.Index.Add(mel.Id, mel.GetPropertyValue(property).Value);
                }
            }

            return(def.Index);
        }
Ejemplo n.º 2
0
        public void UtilValidateNameTest()
        {
            Conventions.CheckValidName("a_b$");
            Conventions.CheckValidName("a_b$.a10", true);

            Assert.Throws <InvalidNameException>(
                () =>
                Conventions.CheckValidName("a_b$.")
                );

            Assert.Throws <InvalidNameException>(
                () =>
                Conventions.CheckValidName("a_b$:.")
                );

            Assert.Throws <InvalidNameException>(
                () =>
                Conventions.CheckValidName("a_b$.:", true)
                );
        }