home / blog

Ubuntu 12.04 – minor updates

Slight change in syntax to mythfilldatabase, not really ubuntu 12.04 related, but that’s when the version changed.

# before ubuntu 11.10
/usr/bin/mythfilldatabase --file 1 grab.xml

# after ubuntu 12.04
/usr/bin/mythfilldatabase --file --sourceid 1 --xmlfile grab.xml
Posted in geek | Tagged , | Leave a comment

Postgresql hello world – part 3

So how about some JPA? First get a JEE eclipse build, and install JPA tools and eclipse link. Next download a postgres JDBC driver, eclipse JPA implementation and OpenJPA – JPA support for J2SE.

You should now be able to use the New… Other… JPA… “Entities from tables” option. Create a database connection and enter username/password etc. and the JDBC driver jar etc.

Here we’ve created an entity for Horse and Rider.

@Entity
@Table(name = "horses")
public class Horse {

    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "size")
    private int size;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "horses_riders", joinColumns = @JoinColumn(name = "hid"), inverseJoinColumns = @JoinColumn(name = "rid"))
    private Collection<Rider> riders;

    public Horse() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Collection<Rider> getRiders() {
        return riders;
    }
}
@Entity
@Table(name = "riders")
public class Rider {
    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "horses_riders", joinColumns = @JoinColumn(name = "rid"), inverseJoinColumns = @JoinColumn(name = "hid"))
    private Collection horses;

    public Rider() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<Horse> getHorses() {
        return horses;
    }
}

A fairly basic setup would be as follows:

    EntityManagerFactory factory = Persistence.createEntityManagerFactory(
            "horses", System.getProperties());

    EntityManager em = factory.createEntityManager();

    Query horseQuery = em.createQuery("SELECT h FROM Horse h");

    for (Horse horse : (List<Horse>) horseQuery.getResultList()) {
        System.out.println(horse.getName() + horse.getRiders());
    }

The META-INF/persistence.xml file defines the entities and postgres connection info.

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<persistence-unit name="horses">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>

<class>hellojpa.Horse</class>
<class>hellojpa.Rider</class>

<properties>

<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/horsedb" />

<property name="openjpa.ConnectionUserName" value="horse" />
<property name="openjpa.ConnectionPassword" value="password" />

<property name="openjpa.DynamicEnhancementAgent" value="true" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>

</persistence>
Posted in geek | Leave a comment

Postgresql hello world – part 2

So we’ve got a table of horses. Let’s make another table, and get get busy with an association table and some JOINs. First we have to add a primary key to our horse table so we can reference it elsewhere.

The easy way:

ALTER TABLE horses ADD COLUMN id SERIAL;

The hard way: This explains how auto-increment really works in postgres – it actually uses a sequence owned by the table column that uses it.

# Steps to make a sequence.
ALTER TABLE horses ADD COLUMN id INT UNIQUE;
ALTER TABLE horses ALTER COLUMN id NOT NULL;
ALTER TABLE horses ADD PRIMARY KEY (id);
CREATE SEQUENCE horses_id_seq;
ALTER TABLE horses ALTER COLUMN id SET DEFAULT NEXTVAL('horses_id_seq');
ALTER SEQUENCE horses_id_seq OWNED BY horses.id;

Now let’s create table of riders.

CREATE TABLE riders (name VARCHAR(255), id SERIAL PRIMARY KEY);
INSERT INTO riders (name) VALUES ('adam');
INSERT INTO riders (name) VALUES ('bob');
INSERT INTO riders (name) VALUES ('charlie');

Now we create an association table linked horses with riders.

CREATE TABLE horses_riders (hid INT REFERENCES horses(id),
                            rid INT REFERENCES riders(id));

INSERT INTO horses_riders (hid, rid) VALUES (1,1);
INSERT INTO horses_riders (hid, rid) VALUES (2,3);

Using the association table. First the naive, inefficient way. This is bad because it makes all possible comparisons.

SELECT r.name,h.name FROM horses h, riders r,horses_riders ass
                     WHERE ass.hid = h.id AND ass.rid = r.id;
  name   |     name
---------+--------------
 adam    | Mr Ed
 charlie | glue factory

Now joins, there are various varieties, but the simplest to understand is simple “table1 JOIN table2 ON condition”.

SELECT * from (horses h JOIN horses_riders ass ON ass.hid = h.id);
     name     | size | id | hid | rid
--------------+------+----+-----+-----
 Mr Ed        |  456 |  1 |   1 |   1
 glue factory |  123 |  2 |   2 |   3

SELECT * from (riders r JOIN horses_riders ass ON ass.rid = r.id);
  name   | id | hid | rid
---------+----+-----+-----
 adam    |  1 |   1 |   1
 charlie |  3 |   2 |   3

We can combine two joins to see who owns which horses.

SELECT r.name, h.name from
      (horses_riders ass JOIN horses h ON ass.hid = h.id)
      JOIN riders r ON ass.rid = r.id;

  name   |     name
---------+--------------
 adam    | Mr Ed
 charlie | glue factory
Posted in geek | Leave a comment

Postgresql hello world

1. Download and build it.

wget http://ftp.postgresql.org/pub/source/v9.1.3/postgresql-9.1.3.tar.bz2
tar xjf postgresql-9.1.3.tar.bz2
cd postgresql-9.1.3
sudo apt-get install libreadline-dev zlib1g-dev
./configure --prefix=$(pwd)/local
make install

2. Initialize and run. Setup a database, with a root user, prompt for password.

export LD_LIBRARY_PATH=local/lib:$LD_LIBRARY_PATH
export PATH=local/bin:$PATH
initdb -U root -W datadir
postgres -D datadir

3. Create a regular user and a database.

createuser -U root horse -W
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
createdb horsedb -U root -O horse

4. Connect as regular user, create a table and insert some data, then select it.

psql horsedb -U horse
CREATE TABLE horses (name VARCHAR(255), size INT);
INSERT INTO horses (name, size) VALUES('glue factory', 123);
INSERT INTO horses (name, size) VALUES('Mr Ed', 456);
SELECT * FROM horses;
     name     | size
--------------+------
 glue factory |  123
 Mr Ed        |  456
\q
Posted in geek | Leave a comment

ffmpeg x11grab compilation

For the x11grab input device to be compiled you need to run ./configure with –enable-x11grab.

When you’ve run ./configure x11grab should be listed in the indev section like this.

Enabled indevs:
dv1394			lavfi			v4l2
fbdev			oss			x11_grab_device

If x11grab was not listed, then it is most likely Xfixes.h and/or XShm.h files are not in your compiler include path. These are validated in the ./configure script, and x11grab is disabled if they are not available. This is the check inside the configure script:

enabled x11grab                         &&
check_header X11/Xlib.h                 &&
check_header X11/extensions/XShm.h      &&
check_header X11/extensions/Xfixes.h    &&
check_func XOpenDisplay -lX11           &&
check_func XShmCreateImage -lX11 -lXext &&
check_func XFixesGetCursorImage -lX11 -lXext -lXfixes

On ubuntu 11.10 the libraries are available, but the headers are not installed. XShm.h is in the libxext-dev package and Xfixes.h is in libxfixes-dev

sudo apt-get install libxfixes-dev
sudo apt-get install libxext-dev

When compiling for RHEL 4u4 I discovered the headers were available, but in non-standard locations not on the default compiler/linker include paths. It was necessary to point ./configure in the right direction.

# first locate the headers.
find / -name "Xfixes.h"
/usr/foo/bar/X11/extensions/Xfixes.h

# next locate the libs
find / -name "libXfixes.so"
/lib/foo/bar/libXfixes.so

# call configure with the compiler + linker locations
./configure \
 other options ... \
 --extra-cflags="-I/usr/foo/bar" \
 --extra-ldflags="-L../lib/foo/bar"
Posted in geek | Tagged , | Leave a comment

Chaining builders in Java

JavaFX uses chaining builders as a kind of syntactic sugar. Here’s an example. The first create() creates the builder (and inner text object), the subsequent calls set attributes on the the object, and finally it is returned via .build().

final Text text = TextBuilder.create()
    .text("hello")
    .x(50).y(100)
    .fill(Color.RED)
.build();

Advantages.

  • Slightly less code.
  • Safer (as self-documenting) than complicated constructors with same type e.g. (String, int, int, int, int)
  • No need for intermediate object if we were just passing it to another method.

I think they’re can be useful than that… What if the builder was the only way to create a new instance of your class… What if you were creating an immutable type….

Here’s a standard immutable type.

public class Horse {
    private String name;
    private int height;
    public Horse(String name, int height) {
      this.name = name;
      this.height = height;
    }
    public String getName() {
        return name;
    }
    public int getHeight() {
        return height;
    }
}

It looks harmless enough, but what happens when someone adds a new field, say age, weight? We have to extend the constructor, and because we already have code using the old constructor we end up with many different constructors leading to confusion.

public Horse(String name, int height) {
    this.name = name;
    this.height = height;
}
public Horse(String name, int height, int age) {
    this(name, height);
    this.age = age;
}
public Horse(String name, int height, int age, int weight) {
    this(name, height, age);
    this.weight = weight;
}

So how can we make this more future-proof using chaining builders. Well here’s the same class with a built in builder. Using the builder is the only way to instiate a new horse.

public class Horse {
    private String name;
    private int height;
    private Horse() {
    }
    public static class Builder {
        private Horse horse = new Horse();
        public Builder name(String name) {
            horse.name = name;
            return this;
        }
        public Builder height(int height) {
            horse.height = height;
            return this;
        }
        public Horse build() {
            return horse;
        }
    }
    public static Builder create() {
        return new Builder();
    }
    public String getName() {
        return name;
    }
    public int getHeight() {
        return height;
    }
}

So we create a horse as follows.

Horse h = Horse.create().name("ed").height(123).build();

So how do we support future attributes? We just add new sections to the builder… We also have a clear single point of construction, the build() method. This could contain additional logic to finish the job.

public Builder weight(int weight) {
    horse.weight = weight;
    return this;
}
public Builder age(int age) {
    horse.age = age;
    return this;
}
Posted in geek | Tagged | Leave a comment

Mac wake up from Linux

On the Mac (Intel iMac 10.7), system preferences, energy saver, wake for ethernet network access.

On Linux (Ubuntu 11.04)

# Grab the MAC address out of the ARP table (assuming still on)
ping horse.lan
/usr/sbin/arp -a
horse.lan (10.5.1.62) at 00:12:23:34:45:56 [ether] on eth0

# install etherwake
sudo install etherwake
etherwake -i eth0  00:12:23:34:45:56

# now awake
ssh horse.lan

# do stuff
horse:$ scp /Users/AnnaChapman/Desktop/secret_docs.tar.gz julian@wikileaks.org:/home/julian/nextrelease

# power off again
horse:$ pmset sleepnow

This works, the mac powers on. However the screen stays off, but you can still ssh in, do whatever and put it back to sleep.

Posted in geek | Leave a comment

Obscure changes in Ubuntu 11.10

I came to resurrect a year old project last week, which previously worked fine, under Ubuntu 11.10. BOOM! nothing works. The project is a CLI TCP server to control a USB opendmx widget using FTD2XX with an Android app client.

First a random g++ fail.

g++ -I. -lpthread -lstdc++ -lftd2xx  *.cpp -o opendmxserver
DmxTransmit.cpp:(.text+0x38): undefined reference to `FT_ListDevices'
DmxTransmit.cpp:(.text+0xdb): undefined reference to `FT_Open'
/tmp/cccTckgn.o: In function `Mutex::Mutex()':
Mutex.cpp:(.text+0x1e): undefined reference to `pthread_mutexattr_init'
/tmp/ccs5TdOM.o: In function `System::currentTimeMillis()':
System.cpp:(.text+0x15): undefined reference to `clock_gettime'
/tmp/ccCEDMng.o: In function `Thread::start()':
Thread.cpp:(.text+0x6c): undefined reference to `pthread_create'

So what’s that all about, well it turns out now you have to put the linker switches after the *.cpp. So this works.

g++ -I. *.cpp -o opendmxserver -lpthread -lstdc++ -lftd2xx

Next fail. Runtime weirdness, assert fail.

opendmxserver: tpp.c:63: __pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= __sched_fifo_min_prio && new_prio <= __sched_fifo_max_prio)' failed.

YFKM, it turns out that out that you need to call pthread_mutexattr_init on the pthread_muxexattr_t structure. This did not used to be the case, although it makes sense to initialize all your structs…

pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr); // not previously required
pthread_mutex_init (&this->mutex, &attr);
Posted in geek | Leave a comment

Scrolling waterfalls in HTML5

A number of technical domains use scrolling data displays, like the COTS octopus-760 sonar system.

A typical implementation would involve a cyclic buffer (BufferedImage, pixmap etc) and two paint operations for efficiency. The means data can be inserted cheaply without the need to move the existing data.

For example, this is what a cyclic buffer of size 3 would look at each stage, as lines of data {A,B,C,D,E,F,G} are received.

. . C C C F F
. B B B E E E
A A A D D D G
Time ===>

So we can paint directly from the cyclic buffer into the main image. First we paint from {insert pos..buffer end}, and then the wrapped section {start pos .. insert pos}. The wrapped section will not occur until the buffer has “overflowed”.

So our painted image looks like this, giving the illusion of scrolling.

A B C D E F G
. A B C D E F
. . A B C D E
Time ===>

So all that boring background info over, can we implement using HTML canvas tag. Of course we can.

Here it is http://adamish.com/lab/html5waterfall working, and the code waterfall.js

This wasn’t entirely straightforward to implement. I could not find out how to create an anonymous image primitive to use as a back cyclic buffer. As a workaround I inserted a canvas element with matching dimensions as the “front” buffer, I then obtained the and used it as a source image in the drawImage() operation onto the front context.

So job done. This same technique could be used in HTML5 game programming, random effects etc.

Posted in geek | Tagged , | Leave a comment

VBA macros – word has insufficient memory

Following error message seen when manipulating bookmarks with VBA in Word.

Word has insufficient memory. You will not be able to undo this action once it is
completed. Do you want to continue?”

Fix: Clear the undo buffer as you go along.

ActiveDocument.UndoClear
Posted in geek | Leave a comment