Tuesday, November 8, 2011

Compiling JPype with openjdk-7

JPype is a requirement of neo4j-embedded and is not available through PyPI. When building from source, I ran into the following compile error:

src/native/common/include/jpype.h:45:17: fatal error: jni.h: No such file or directory

First, make sure that you have the JDK installed (sudo apt-get install openjdk-7-jdk if apt-get is available on your machine) and find where jni.h lives (was /usr/lib/jvm/java-7-openjdk-amd64 for me)

Next, make the following change to setup.py in your JPype directory:


def setupLinux(self):
    self.javaHome = os.getenv("JAVA_HOME")
    if self.javaHome is None :
        #self.javaHome = '/usr/lib/jvm/java-1.5.0-sun-1.5.0.08' # was this
        self.javaHome = '/usr/lib/jvm/java-7-openjdk-amd64' # to this
    self.jdkInclude = "linux"
    self.libraries = ["dl"]
    self.libraryDir = [self.javaHome+"/lib"]

Now, your build should complete as expected (barring any other issues local to your own machine).

Sunday, October 30, 2011

Multi-processor make jobs

Compile times (especially when not using a distributed build system) are always nice to keep down. Outside of code structure, I ran across this little ditty to help speed up your local build times:

make -j n

The -j parameter in make tells the build system to use n processors while running targets. To find out how many processors your Linux system has (should run on a Mac too), run cat /proc/cpuinfo | grep processor | wc -l.

Monday, September 19, 2011

HTML5 Video Scaling

According to w3c specs:
The dimension attributes are not intended to be used to stretch the image.
So, how do we scale the actual video? In a project that I'm working on (at home), I wanted to have an HTML5 video element that takes up the entire width and height of the browser window and resized whenever the user resized their browser. Here's what I came up with (using Yii as my PHP framework): Page script:
// you could bind to the equivalent event as well, of course
<video onloadeddata="resizeViewport()" id="background">
    <source src="<?php echo Yii::app()->request->baseUrl?>/assets/video/ed.ogv" />
</video>
.css:
#background
{
    position: absolute;
    -moz-transform-origin: 0 0;
}
.js (jQuery):
$(document).ready(function(){
});

$(window).resize(function(){
    resizeViewport();
});

function resizeViewport()
{
    var d = [$('#background').attr('videoWidth'), $('#background').attr('videoHeight')];

    $('#background').css('-moz-transform', 'scale('+
        $(window).width()/d[0]+','+
        $(window).height()/d[1]
        +')');
}
The scale CSS function is meant to change the dimensions of the image (as opposed to width and height). The other trick here is to ensure that the video has actually loaded so I can extract correct dimensions (which is only known once it's been loaded). Note: The code samples here are intended for FF only (each user agent has a different transform method).

Friday, September 16, 2011

Running Django unit tests

Not being a DBA, or someone who's even really familiar with database administration, I've been banging my head against the wall trying to get unit tests running on my dev box. I'd checked a few posts on SO, but it seems that I'd missed the one most important thing: RTFM.

Got an error creating the test database: permission denied to create database

If you're seeing the above error when running ./manage.py test, read on..

As Django creates and destroys a database when running unit tests, the user that you're using to connect to the server with needs to have permissions to create databases. This cannot be done by granting superuser permissions to the database user (within postgres of course) as the user needs to have database creation by default (you have to SET ROLE when using this method). To grant permissions to create a database by default, you should do this when creating the user account within psql:

create user myuser createdb

No more issues running unit tests.

Setting up DLink DWA-125 (USB 802.11N) on FreeBSD

So, my wife decided that she didn't like the computer and desk where they were (not that I blame her, I actually agreed), and proposed a move into another room of the house. After it was moved (she did it while I was at work one day), I definitely did like the placement better. But what did this mean for me? I either had to: a) Figure out a nice wiring solution as the router that I was previously wired to was in two rooms away, or b) Pick up a wireless card Picking up a wireless card was the obvious choice as it meant less work for me, or so I thought ;) This is more of a reminder for myself in case I have to do this again, but if it helps anyone else out, great. I ended up picking up a USB 11N stick (DWA-125 for those interested). I had forgotten to check whether or not the chipset had FreeBSD ported drivers prior to ripping the box open. Thankfully, there were drivers available. To get it up and running, here are the steps that I took: (Note, I use the run driver because of the DWA-125 chipset)

1) Added this to /etc/rc.conf:

wlans_run0="wlan0"
ifconfig_wlan0="WPA DHCP"

(I have WPA encryption on my router, so the "WPA" entry before "DHCP" is required.)

2) Added to /boot/loader.conf

if_run_load="YES"
runfw_load="YES"

3) Rebooted (you could also just kldload

4) Because I'm running WPA, I had to create /etc/wpa_supplicant.conf and add the following lines:

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
network={
ssid="myssid"
psk="mypassword"
}

5) Once this was all done, I ran wpa_supplicant to ensure everything was working properly:

wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf

And finally...

6) Reboot. And once again, I could listen to last.fm. I'll most likely end up compiling these options into the kernel, but this will do for now. Is it a good thing when you feel happy that you've set up and configured a USB network device?

Optimizing Your Web Apps (LAMP)

Note: This article assumes single server deployment and therefore doesn't cover topics such as load balancing

Code Profiling

Use xdebug's profiling option. This will give you insight into areas of your code that may need optimization.

SQL Profiling

Most of my projects use PostgreSQL, so I haven't actually used this myself, but here's some documentation on profiling your MySQL queries. Again, it'll give you insight into areas of your SQL scripts that could use some optimization.

Investigate Cache Options

If you have a lot of database queries on your pages or a lot of PHP logic in your pages, it can be expensive to generate. There are a few different methods of cacheing and, while each method helps to solve load times, each aims to solve different specialized problems:

Tuesday, January 18, 2011

PHP factory methods hacked

I came across a question on stackoverflow a little while ago that sparked an idea (whether or not it's actually a good idea is another matter) in regards to factory design pattern methods.

The question was, basically how to get around the problem of massive switch blocks that would happen if you had a lot of options in terms of what concrete classes you could create via the factory method. The question was posed with the following snippet:

class ServiceFactory { 
    public static function getInstance($token) { 
        switch($token) {
            case 'location':
                return new StaticPageTemplateService('location');
                break;
            case 'product':
                return new DynamicPageTemplateService('product');
                break;
            case 'user'
                return new UserService();
                break;
            default:
                return new StaticPageTemplateService($token);
         }
    }
}

I thought.. What if you could have every class type created by the factory method register itself, leaving the factory method to simply supply a map of identifiers to concrete classes? Here's what I came up with:

The updated ServiceFactory class:
class ServiceFactory
{
    private static $BASE_PATH = './dirname/';
    private $m_aServices;

    function __construct()
    {
        $this->m_aServices = array();

        $h = opendir(ServiceFactory::$BASE_PATH);
        while(false !== ($file = readdir($h)))
        {
            if($file != '.' && $file != '..')
            {
                require_once(ServiceFactory::$BASE_PATH.$file);
                $class_name = substr($file, 0, strrpos($file, '.'));

                $tokens = call_user_func(array($class_name, 'get_tokens'));
                foreach($tokens as &$token)
                {
                    $this->m_aServices[$token] = $class_name;
                }
            }
        }
    }

    public function getInstance($token)
    {
        if(isset($this->m_aServices[$token]))
        {
            return new $this->m_aServices[$token]();
        }
        return null;
    }
}

interface IService
{
    public static function get_tokens();
}

And a sample concrete implementation:
class UserService implements IService
{
    function __construct()
    {
        echo '__construct()';
    }

    public static function get_tokens()
    {
        return array('user', 'some_other');
    }
}

Sure, it's a little more code in the short term and the downside is that the concrete implementations need to know what the client code should be using as an identifier to return a constructed class, but there's no need for any switch block at all :)