Jan 6 2011

Running VirtualBox as 64 Bit windows service

Category: C# | Virtual Machines — Duke @ 13:19
It is some time that I’m interested in virtualization: I think it is a nice way to keep my machine healty and clean, and in the same time it allows me to experiment and do “distruptive” things.
I love the possibility to take snapshots and recover a previous state in a moment!
Probably some of you have noticed that “photoatomiclab.net” is not always online(there is a reason for this you’ll discover in a moment), right? I know it is boring and frustrating when a resource is not available when you need it, and for this I’m really sorry.
So… I’ve started thinking on how to improve the thing. First of all: my favorite virtualization environment is VirtualBox (too bad Oracle have bought it, I hope it will not ruine all this good work) I like it because is free, is fast (in my opinion far better than VMWare) and it is supported by an opensource community… and having the source code in your hands you can do great things.
Anyway… my site is not always available: that’s because it is hosted on a virtual machine on my real machine! ANd this because I always like to know the internals and from time to time I had some crazy ideas: like streaming the town fest in the square in front of my windows using my HD camera ( LOL, usually I did this on xmas night) or connect some netduino robots and drive it from a web interface… this requires me to have a host where I really can do everything. and these host services are not cheap, unfortunately Triste 
But hey! VirtualBox really saves the day! So I  have this virtualmachine “Bear” that runs my site… the one you are reading now. And it is hosted on my real machine… and my machine is housed in my bedroom… and at night I like to sleep A bocca aperta 
It is natural that I shut id down at night… and therefore the site becomes unavailable….
Again, my pc is setup to start up at about 14.00 CET every day (that’s because it have to record my favorite tv series…. but this is another story) and since I’m forgetful, I usually don’t remember to turn the VM on, so I’ve tough to improve the situation… I’ve started searching for a tool that allows me to automatically starts my VM up when the pc starts.
I’ve found VirtualBox headless mode… nice! but… I have not found a way to use it… expecially because for the shutd down part… I want my VM freezes its state when the pc is shutted down.. I don’t want to loose the last change on the site, nor to wait minutes for the virtualized machine shut down…
I’ve serched for days but nothing appears to be a strong solution…. there are some services that tries to emulate this but at the end they open a shell and invokes the command line version ov VBOX and “do things”
this have the bad behavior (in the major part of the occasions) to open console windows for some seconds, or to leave consoles opened…. and if you close the console .. the VM dies Triste
this is ugly and completely unprofessional.
But fortunately VirtualBox is opensource! and there are SDKs!!

Tags: ,

Oct 13 2010

3 ways for splitting a collection into pages with Linq

Category: C# — Duke @ 18:27
There are situations when you have a collection of objects (IEnumerable<T>) and you need to split it in chunks or pages so to process these resulting pars in different moments.
There are a lot of way to achieve this but the nicer is probably using Linq (it makes also your code looks a lot more modern and cool!)
the focus of this post is to discuss some of these ways and it’s pros and cons.
Digging around the web I've found this first example ( unfortunately I've lost the link, anyway the code in this first sample is not mine)
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> enumerable, int chunkSize) 
{ 
    var total = enumerable.Count(); 
    for (int i = 0; i * chunkSize < total; i++) 
    { 
        yield return enumerable.Skip(i * chunkSize).Take(chunkSize); 
    } 
}
 
This allows you to write code like:
foreach(var page in myCollection.Chunk(30))
{
    foreach(var item in page)
    {
         process(item);
    }
}
anyway, this work well in limited scenarios, when the amount of record are quite low. To prove it imagine to have a collection with 1 million item in it, and you want to split it in chunk of 1000 elements each.
the Chunk method will first skip zero element (hey pretty fast!) and then take 1000. the total amount of time is c, given c the size of the chunk.
but the next round the method have to skip the first 1000(as they are already taken) and then take 1000. Hum… still not so bad as the first 1000 was already taken and then we have to process the next 1000.
third round…. skip 2000 this time… and then take 1000; ok but we have skipped 0 the first time, 1000 the second time and 2000 the third time… if we continue this way at the 10th page we will have to skip 0, 1000, 2000, 3000, 4000 … 9000! oh no, this means that at the kth page we will need to skip
((k * (k-1))/2 ) * c;
Old friend Gauss appears again! plus of course (k * c) for the take part.
that’s so bad, as you can see, we have a term that grows as O(k^2) and you for sure don’t want to have such complexity if your collection have 1 million of elements… moreover, one usually want to split thinks in chunks when the number is important, so exactly our scenario… therefore this algorithm is quite poor in a general situation: we are not going to use it in our programs, isn’t it?
So, let’s move forward and try to find something better: we have to solve the problem of restarting every time from the beginning, let’s try some caching, it usually works,right?
 
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> enumerable, int chunkSize) 
{ 
    var total = enumerable.Count(); 
    var current = enumerable; 

    for (int i = 0; i * chunkSize < total; i++) 
    { 
        yield return current.Take(chunkSize); 
        current = current.Skip(chunkSize); 
    } 
}

Tags:

Sep 25 2010

Wellcome!

Category: — Duke @ 20:15

Wellcome to PhotoAtomic Research Lab.

Tags: