source code bean

By default MongoDB tries to memory map as much as possible of the database file. Given a fairly big database, this will consume all your memory. If MongoDB is running on a dedicated server this is totally fine, but when running on a shared server this will cause unnecessary swapping for your other applications. At this point you want to limit the max amount of memory MongoDB can use. On Linux I have not found a way on how to do this (if you know a way please let me know!), but on Windows Server it is possible to limit it using WSRM.

WSRM allows greater control over the CPU and Memory available to a process and is an additional feature shipped with Windows Server that can be installed from the server manager under “Features Summary”:

Once installed we need to create a new resource allocation policy from the manager:

and give it some name:

Now we need to select processes to match with the policy:

We will select process from the list of registered services:

Here we find MongoDB:

Click OK a few times to exit the process selection. On the memory tab we can limit the memory for this resource allocation policy:

The final step is to make the policy active. To do this, go to the top level page in the manager and click the “Selected Policy” link. In here you can set the active policy:

That’s it! Now MongoDB will not consume more than 500MB of memory.

26 Apr, 2012

ColourSearch – a simple image search engine

Posted by: Peter In: General

A few weeks I randomly got interested in how you match images with other images. Having a very limited background in computer graphics I started reading some research papers on how to match images. One strategy that seemed to work pretty well for most people was a histogram based comparison. Given two histograms it is possible to calculate the distance, or correlation, between the histograms. The image with the lowest distance or highest correlation would be the best match. To try out my newly learnt knowledge I created a small application, ColourSearch, which given a directory calculates a histogram for each image and stores it in memory. In the GUI you can then pick a color to search and the application will find the best matching images using 4 different algorithms and present the result side by side.

A problem with the current implementation is that I have not found any way to index histograms, so when searching the input image needs to be compared to each image in the database (O(n) yay!). For larger datasets this would of course not be feasible, but at least the problem is easy to scale over several CPUs, so I can take advantage of all my cores.

You can find ColourSearch on GitHub (code in C#, works perfect under Mono!):
https://github.com/moberg/coloursearch

If you have any ideas on how indexing can be done or on more efficient matching algorithms I would be very interested in hearing them!

It is not totally obious how to unlock your WP7 device for development. The first step is of course to sign up for the development program at http://create.msdn.com. But then? I had the exact same experience as this guy:

http://www.pitorque.de/MisterGoodcat/post/Unlocking-a-Windows-Phone-7-device-for-development.aspx

So together with the SDK a small activation program was installed, “Windows Phone Developer Registration”. Start it and login to active your device!

So for some reason it takes Nokia a long time to push out the WP7 updates. 1600.2487.8107.12070 is a new official build of WP7 that was released a while back, but Zune still tells me that there is no update for my phone. The most important features of the update to me seems to be improved battery performance and improved bass in audio performance. I have been quite happy with the current battery performance of my phone (about 1,5 days on one charge), but at the same time I know other Lumia 800 owners that are very unhappy with theirs, not even lasting one work day. It seems like there are some good and some not so great devices out there. Even better battery life would of course be great! On the audio side, the audio has been quite flat and improved bass performance would be very welcome.

This blog post has great step by step instructions on how to force the update:
http://nokiagadgets.com/2012/03/08/force-the-newest-lumia-800-update-1600-2487-8107-12070-to-your-phone/

Basically what you have to do is to download the update and install some tools for phone developers. Once you have downloaded everything you run the “WP7 Update Cab Sender.bat”:

The update process takes about 10 minutes. The first thing I tested out after updating was of course to play some music, the audio is indeed much improved. Time will tell if the battery performance got better!

25 Mar, 2012

Protothon #2 – WebRTC

Posted by: Peter In: Web

Yesterday I went to a hackathon called Protothon. Protothon describes itself as “Space for the place between code and creativity” and the idea is to bring together programmers, creatives and entrepreneurs together and under a very limited time build an application from scratch. The focus of this hackathon was WebRTC, which is a new standard for Real Time Communications using HTML5/Javascript. At the moment it is so new that no browser actually supports it yet, but there are special builds of Chrome and Firefox in which you can enable it. So the task of this hackaton was basically to do something cool with WebRTC.

The team I was in consisted of me, Tomek Augustyn, Patrik Spathon and Pebbles Lim. Tother we came up with the idea to build a multiplayer version of the classic game pong, but using motion detection to control the game. We named the game Spong:

Each player runs the game from his web browser, which will access his web cam using WebRTC. The motion detector analyzes the video stream and detects where the most amount of movement is, this gives us a coordinate on the screen. The Y part of this coordinate is used to control the position of the players’ paddle. You can see this in the image above where the cross points out where i just moved my hand. Each players’ movements is sent to the other player via a Node.js/Socket.IO relay server.

To summarize Protothon #2 I must say that I was really impressed with all the applications presented by the different teams. It was one intense day, but a lot of fun! I am looking forward to attending again in the future.

In my previous post I showed you how to compile and install mono and get your MVC site up running using the development web server xsp4. The next step is to serve your site using a real web server, my choice is nginx.

First, we need a configuration file for the nginx site. If you haven’t already got nginx installed, install it (sudo apt-get install nginx).

/etc/nginx/sites-enabled/mvc:

server {
    listen   80;
    server_name mvctest.sourcecodebean.com;
    root  /home/peter/MonoMvcDeploy/;

    location / {
      root /home/peter/MonoMvcDeploy/;
      index index.html index.htm default.aspx Default.aspx;
      fastcgi_index /Home;
      fastcgi_pass 127.0.0.1:8000;
      include /etc/nginx/fastcgi_params;
    }
}

Read the rest of this entry »

10 Mar, 2012

Deploying your ASP.NET MVC3 site on Mono

Posted by: Peter In: ASP.NET|C#|Mono

In this blog post I am going to show you how easy it is to create a new ASP.NET MVC site and deploy it on Linux/Mono.

Step 1 – Create project
Create a new ASP.NET MVC 3 project. Compile and run it.

Read the rest of this entry »

20 Feb, 2012

One week with the Nokia Lumia 800

Posted by: Peter In: Mobile

One week ago I got a Nokia Lumia 800 from my company, this is my random thoughts so far:

+ Amazing screen
+ Good batery life for being a smart phone
+ Really beautiful
+ User interface is blazing fast
+ Integration with Google, Facebook and MSN works great. All my contacts was synced from Google.
+ Fast web browser, zooming and scrolling is super smooth
+ Live tiles are really cool, but not sure if it is the best use of space
+ Hubs! This is a great concept, messages goes to the messages hub, music to the music hub and so on.

- The lack of applications, good thing i am not a big app user
- Applications are not as mature as on iOS or Android. For example the Facebook application is missing a lot of functionality found on iOS and Android.
- The default search engine is Bing and I can’t find a way to change it.
- Navigating back in the web browser. Navigating back to the previous page is done by clicking the back button in the bottom left corner, works great if you are surfing some pages, but if you leave the browser and come back, the back button will take you back to the main screen (or where you were before going back to the web browser). At this point there is no way to navigate back in your surfing history, really dumb!

But over all I must say I am very happy with it!

I have been hacking on a project in Node.js/Express.js for some time now. I am really impressed by how fast it is to code and prototype in Node, not much gets in your way. However, there is one thing I am still struggling with, the callback model of Node.js applications. It is not that it is conceptually hard to understand or use, but i feel that it keeps me from writing clean code.

Let’s imagine that we are writing a small nonsense program. The program receives a post, create some variable x, if some condition is true we call an async method to get some result and assign it to x.result. In the end we want to save x (also async). This would probably be my first attempt in node:
 

  1. app.post(‘/someurl’, function(req, res) {
  2. var x = {..}
  3.  
  4. if (some contidion) {
  5.   someOperation(function(err, result)) {
  6.     x.result = result
  7.     x.save(function(err, result) {
  8.        if (err) next(err)
  9.        res.redirect(url)
  10.     })
  11.   })
  12. } else {
  13.   x.save(function(err, result) {
  14.     if (err) next(err)
  15.     res.redirect(‘/’)
  16.   }
  17. }

 

(Edit: To clarify, both someOperation and save is doing some kind of I/O)

So is this clean code? In my opinion it is not. Read the rest of this entry »

11 Jan, 2012

MongoDB, C# and Mono

Posted by: Peter In: C#|Mono|NoSQL

MongoDB is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono.

On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No installation is required, just unzip the file and run the “mongod” binary from the directory. There is also a binary called “mongo”, this is the mongo interactive shell which is very useful.

The C# drivers are available, both in binary form and as source, from the C# language center. To get started I fired up MonoDevelop 2.8 running on Mono 2.10.8:



MonoDevelop has matured a lot since I used it the first time. It is still not as good as Visual Studio, but it is getting there. What takes most time to get used to is the totally different set of shortcuts, and the lack of all ReSharper magic.

Let me break down a simple console application example on how to use MongoDB using the official driver from 10gen: Read the rest of this entry »

About

Welcome to source code bean! On this site I post stuff that I encounter in my job and spare time. The content is mostly related to .NET development, but my interest in techonology is very broad, so often you will find posts on totally different subjects!