Exemplo n.º 1
0
        static async Task CreateCombinedIndex <TEntity> (IMongoCollection <TEntity> collection, CreateOneIndexOptions createOneIndexOptions = null,
                                                         IndexKeyType indexKeyType = IndexKeyType.Ascending, ILogger <TContext> logger = default, CancellationToken cancellationToken = default, params IndexKeysDefinition <TEntity>[] keys) where TEntity : Entity
        {
            if (keys == null)
            {
                throw new ArgumentNullException($"Field expression cannot be empty");
            }

            var indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Combine(keys));

            await collection.Indexes.CreateOneAsync(indexModel, createOneIndexOptions, cancellationToken).ConfigureAwait(false);
        }
Exemplo n.º 2
0
    public Index CreateIndex(string name, bool isClustered, IndexKeyType type,
                             string[] columnNameList)
    {
        Index index = new Index();

        index.Name         = name;
        index.IndexKeyType = type;
        index.IsClustered  = isClustered;

        foreach (string columnName in columnNameList)
        {
            index.IndexedColumns.Add(new IndexedColumn(index, columnName));
        }

        return(index);
    }
        /// <summary>
        /// Recreates primary key or index.
        /// </summary>
        /// <param name="table">Current table <see cref="Table"/></param>
        /// <param name="indexName">Name of the index/primary key</param>
        /// <param name="indexKeyType">Type of index</param>
        /// <param name="columnNames">Columns in the index</param>
        private static void CreateNewPrimaryKey(Table table, string indexName, IndexKeyType indexKeyType,
                                                params string[] columnNames)
        {
            var primaryKeyIndex = new Index(table, indexName)
            {
                IndexKeyType = indexKeyType,
                IsClustered  = false,
                FillFactor   = 50
            };

            foreach (string columnName in columnNames)
            {
                primaryKeyIndex.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex, columnName));
            }
            primaryKeyIndex.Create();
            primaryKeyIndex.DisallowPageLocks = true;
            primaryKeyIndex.Alter();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="IndexingStrategy"/> class.
 /// </summary>
 /// <param name="unique">if <c>true</c>, indicates that the values in the index are unique.</param>
 /// <param name="pathType">sets the <seealso cref="IndexPathType"/> property of the index.</param>
 /// <param name="nodeType">indicates whether the node is an element, attribute, or metadata.</param>
 /// <param name="keyType">sets the types of query supported by the index.</param>
 /// <param name="indexValueSyntax">sets the intended datatype of the item being indexed.</param>
 public IndexingStrategy(
     bool unique, IndexPathType pathType,
     IndexNodeType nodeType, IndexKeyType keyType,
     XmlDatatype indexValueSyntax)
 {
 }
Exemplo n.º 5
0
        //https://stackoverflow.com/questions/35019313/checking-if-an-index-exists-in-mongodb
        static async Task CreateIndexAsync <TEntity> (IMongoCollection <TEntity> collection, Expression <Func <TEntity, object> > field,
                                                      CreateOneIndexOptions createOneIndexOptions = null, IndexKeyType indexKeyType = IndexKeyType.Ascending, ILogger <TContext> logger = default, CancellationToken cancellationToken = default) where TEntity : Entity
        {
            if (field == null)
            {
                throw new ArgumentNullException($"Field expression cannot be empty");
            }

            CreateIndexModel <TEntity> indexModel;

            switch (indexKeyType)
            {
            case IndexKeyType.Descending:
                indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Descending(field));
                break;

            case IndexKeyType.Text:
                indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Text(field));
                break;

            case IndexKeyType.Hashed:
                indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Hashed(field));
                break;

            default:
                indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Ascending(field));
                break;
            }
            try {
                await collection.Indexes
                .CreateOneAsync(indexModel, createOneIndexOptions, cancellationToken)
                .ConfigureAwait(false);
            } catch (MongoConnectionException mongoConnectionException) {
                logger?.LogError(mongoConnectionException, "Passbridge MongoDb database connection error");
            } catch (TimeoutException timeoutException) {
                logger?.LogError(timeoutException, "Timeout Exception in CreateIndexAsync method");
            } catch (Exception ex) {
                logger?.LogError(ex, "Something is wrong is MongoDB");
            }
        }
Exemplo n.º 6
0
 private static IEnumerable<string> DropConstraint(string tableName, string constraintName, IndexKeyType keyType)
 {
     Table table = GetTable(tableName);
     Index uniqueConstraint = new Index(table, constraintName) { IndexKeyType = keyType };
     uniqueConstraint.Drop();
     return ScriptChanges(table.Parent.Parent);
 }
Exemplo n.º 7
0
 private static IEnumerable<string> AddConstraint(string tableName, string constraintName, IndexKeyType keyType, IEnumerable<string> columnNames)
 {
     Table table = GetTable(tableName);
     Index uniqueConstraint = new Index(table, constraintName) { IndexKeyType = keyType };
     foreach (string columnName in columnNames)
     {
         Microsoft.SqlServer.Management.Smo.Column column = new Microsoft.SqlServer.Management.Smo.Column(table, columnName);
         column.DataType = Microsoft.SqlServer.Management.Smo.DataType.Bit;
         table.Columns.Add(column);
         uniqueConstraint.IndexedColumns.Add(new IndexedColumn(uniqueConstraint, columnName));
     }
     uniqueConstraint.Create();
     return ScriptChanges(table.Parent.Parent);
 }
 /// <summary>
 /// Recreates primary key or index.
 /// </summary>
 /// <param name="table">Current table <see cref="Table"/></param>
 /// <param name="indexName">Name of the index/primary key</param>
 /// <param name="indexKeyType">Type of index</param>
 /// <param name="columnNames">Columns in the index</param>
 private static void CreateNewPrimaryKey(Table table, string indexName, IndexKeyType indexKeyType,
     params string[] columnNames)
 {
     var primaryKeyIndex = new Index(table, indexName)
     {
         IndexKeyType = indexKeyType,
         IsClustered = false,
         FillFactor = 50
     };
     foreach (string columnName in columnNames)
         primaryKeyIndex.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex, columnName));
     primaryKeyIndex.Create();
     primaryKeyIndex.DisallowPageLocks = true;
     primaryKeyIndex.Alter();
 }
Exemplo n.º 9
0
        private static IEnumerable <string> DropConstraint(string tableName, string constraintName, IndexKeyType keyType)
        {
            Table table            = GetTable(tableName);
            Index uniqueConstraint = new Index(table, constraintName)
            {
                IndexKeyType = keyType
            };

            uniqueConstraint.Drop();
            return(ScriptChanges(table.Parent.Parent));
        }
Exemplo n.º 10
0
        private static IEnumerable <string> AddConstraint(string tableName, string constraintName, IndexKeyType keyType, IEnumerable <string> columnNames)
        {
            Table table            = GetTable(tableName);
            Index uniqueConstraint = new Index(table, constraintName)
            {
                IndexKeyType = keyType
            };

            foreach (string columnName in columnNames)
            {
                Microsoft.SqlServer.Management.Smo.Column column = new Microsoft.SqlServer.Management.Smo.Column(table, columnName);
                column.DataType = Microsoft.SqlServer.Management.Smo.DataType.Bit;
                table.Columns.Add(column);
                uniqueConstraint.IndexedColumns.Add(new IndexedColumn(uniqueConstraint, columnName));
            }
            uniqueConstraint.Create();
            return(ScriptChanges(table.Parent.Parent));
        }