Yearly Archives: 2013

Code

Create large files with a specific size

In order to test the latency on a specific server configuration, we simply wanted to test the throughput rate of downloading large files. The only question is: how can you efficiently create several (large) files with an exact size?

As easy as two lines, I can tell you. The snippet below basically writes a file with spaces, using a stream, until the designated size is reached. This to accomplish an exact file size.

    /// <summary>
    /// Creates a file with a specific size.
    /// </summary>
    /// <param name="outputFilePath">The full output path for the file. Overwrites if exists.</param>
    /// <param name="length">Filesize in bytes</param>
    static void CreateFileWithSpecificSize(string outputFilePath, long length)
    {
        using (var stream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            stream.SetLength(length);
        }
    }
Code

Drop an entire collection in RavenDB

Deleting a single document in RavenDB is easy. Deleting several documents is too. Deleting an entire collection of N-documents isn’t supported by default.

Here is a great extension that deletes an entire collection from your RavenDB document store:

    /// <summary>
    /// Deletes, or 'drops', an entire collection from the datastore, by the specified type.
    /// </summary>
    public void Drop<T>()
    {
        // Determine the concrete type of T
        Type g = typeof (T);

        // Delete the entire collection
        CurrentSession.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName",
            // ouch, be weary of the 's' suffix here (pluralization thingie)
            new IndexQuery { Query = "Tag:" + g.Name + "s" },
            allowStale: true
        );
    }
Concepts

DRY

DRY, as in Don’t Repeat Yourself, is another fundamental principle in coding. And it’s all about avoiding repetition, or doing the same thing twice.

Rule of thumb: if you use the same code more than once in your project, you’re probably doing it wrong.

Code

Iterate through a hierarchical list of infinite child elements

Example that flattens a nested and, possibly infinite, hierarchical list of child elements.


    class Program
    {
        static void Main(string[] args)
        {
            // Create dummy categories - not especially pretty
            var categories = new List();
            categories.AddRange(new List()
                                    {
                                        new Category(){ Id = 1, Name = "Cat1", Children = new List()
                                                                                              {
                                                                                                  new Category(){ Id = 101, Name = "Subcat1"},
                                                                                                  new Category(){ Id = 102, Name = "Subcat2"},
                                                                                                  new Category(){ Id = 103, Name = "Subcat3WithChildren", Children = new List()
                                                                                                                                                                         {
                                                                                                                                                                             new Category() { Id = 10301, Name = "Subcat1-sub1" },
                                                                                                                                                                             new Category() { Id = 10302, Name = "Subcat1-sub2" }
                                                                                                                                                                        }
                                                                                                                }
                                                                                              }},
                                        new Category() { Id = 2, Name = "Cat2"}
                                    });

            // Flatten the nesteded list of *child* categories
            var flatCategories = new List();
            foreach (var rootCategory in categories)
                flatCategories.AddRange(IterateChildCategories(rootCategory));

            // Spit out the results
            foreach (var cat in flatCategories)
                Console.WriteLine(cat.Name);

            Console.ReadKey();

        }

        /// 
        /// Iterates through a list of child categories. 
        /// 
        /// 
        /// Note that this skips the parent categories, and focusses only on the children.
        /// 
        private static IEnumerable IterateChildCategories(Category parent)
        {
            if (parent.Children == null) yield break;

            foreach (var g in parent.Children)
            {
                yield return g;

                foreach (var sub in IterateChildCategories(g))
                    yield return sub;
            }
        }

        public class Category
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List Children { get; set; }

            public Category()
            {
                this.Children = new List();
            }
        }
    }

 

Concepts

YAGNI

Each and every piece of software will start with a design. Ranging from common sense, to a fully blown YAGNI, You ain’t gonna need it.

Rule of thumb: Don’t over-engineer things. Only write functionality when you need it right now. If you will not have a use right now for it, you will most likely not need it in the future. Even though you think else. Here are some typical statements that should ring some alarm bells when it comes to violating the YANGNI principle:

  • We may have a use for it in the future
  • This may come in handy sometime
  • I think the customer wants it this way

More often than not, when the YAGNI concept is violated it is presumably someone in the base forgot the KISS rule.