Tuesday, December 13, 2011

File size in IntelliJ

I'm currently working on a java project and using IntelliJ as my prefered development environment. Yesterday I was generating a jUnit test class with 401 test methods. I realize that a java class with 401 methods in it is an abomination of sorts but it was so much easier to generate it as one file instead of several so I chose to do that nevertheless. Unfortunatly IntelliJ refused to accept my class as code and simply refused to recognise it for what is was. It drove me crazy but the nice folks at jetbrains helped me out. As it turns out there is a setting in idea.properties for this.
#---------------------------------------------------------------------
# Maximum file size (kilobytes) IDE should provide code assistance for.
# The larger file is the slower its editor works and higher overall system memory requirements are
# if code assistance is enabled. Remove this property or set to very large number if you need
# code assistance for any files available regardless their size.
#---------------------------------------------------------------------
idea.max.intellisense.filesize=2500 
So, I simply increased the filesize and that was it.

Friday, December 9, 2011

Speeding up inserts using Linq-To-Sql - Part 2

The latest post in this serie can be found at

Speeding up inserts using Linq-To-Entities - Part 4

Part one is found at http://blog.tanneryd.com/2011/11/speeding-up-inserts-using-linq-to-sql.html.

This version works better if you have nullable columns in your table and it also handles foreign keys. I.e. it will ignore foreign key objects but it requires that you set the foreign key id instead.
    partial class MyDataContext
    {
        partial void OnCreated()
        {
            CommandTimeout = 5 * 60;
        }

        public void BulkInsertAll<T>(IEnumerable<T> entities)
        {
            entities = entities.ToArray();

            string cs = Connection.ConnectionString;
            var conn = new SqlConnection(cs);
            conn.Open();

            Type t = typeof(T);

            var tableAttribute = (TableAttribute)t.GetCustomAttributes(typeof(TableAttribute), false).Single();
            var bulkCopy = new SqlBulkCopy(conn) { DestinationTableName = tableAttribute.Name };

            var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
            var table = new DataTable();
            
            foreach (var property in properties)
            {
                Type propertyType = property.PropertyType;
                if (propertyType.IsGenericType &&
                    propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                table.Columns.Add(new DataColumn(property.Name, propertyType));
            }

            foreach (var entity in entities)
            {
                table.Rows.Add(properties.Select(property => GetPropertyValue(property.GetValue(entity, null))).ToArray());
            }

            bulkCopy.WriteToServer(table);
            conn.Close();
        }

        private bool EventTypeFilter(System.Reflection.PropertyInfo p)
        {
            var attribute = Attribute.GetCustomAttribute(p, typeof (AssociationAttribute)) as AssociationAttribute;

            if (attribute == null) return true;
            if (attribute.IsForeignKey == false) return true; 
            
            return false;
        }

        private object GetPropertyValue(object o)
        {
            if (o == null)
                return DBNull.Value;
            return o;
        }
    }