Archive for August, 2007

PHP

Semaphore with APC

Sometimes it’s important that the same event doesn’t occur at the same moment. For example a an order conformation is reposted. Normally you would see that the status is already set to processed and skip it. But what it the second HTTP request is quickly after the first (because the user pressed refresh or a proxy had a hick up or …) and the order has not yet been fully processed. To fix this you need a semaphore.

You can use the system V semaphore functions, but another option is to use APC.

/**
 * A very simple semaphore for mutual exclusion.
 * It will hold it for max 60 sec. Remember to release when done.
 *
 * @param string $key
 */
function getSemaphore($key)
{
	while (!apc_add("semaphore:{$key}", getmypid(), 60)) sleep(0.5);
}
 
/**
 * Release the semaphore, grabbed with getSemaphore($key).
 *
 * @param string $key
 */
function releaseSemaphore($key)
{
	apc_delete("semaphore:{$key}");
}

MySQL

LOAD XML contribution added to MySQL

It’s very nice that the LOAD XML patch finally got some attention. I wasn’t expecting it anymore. I worked on this with Erik a year ago. My C programming skill were to rusty at that time, that only my BNF code contribution was used and a lot of issues were never solved. With my C prog skills back up to a decent level, I;m planning to pick up the project. Now seems to be a better time than ever.

The current solution works well in certain situations, but definitely not in all. To insert referential data, you need to specify the id value at forehand, look at project_id in the example. That doesn’t seem like a real world scenario. You should be able to insert the whole XML tree at once creating the reference as you go. This unfortunately doesn’t fit in the current LOAD DATA scheme, on which LOAD XML is based, and therefor requires a lot of additional work.
Continue Reading »

Javeline / Ajax.org, PHP, Replies

Submit a form with AJAX using TelePort

Yesterday, PHPBuilder posted an article about sending a form using AJAX. This article shows how to post a simple form. But looking closely at the example, you can also see what the problem is using the plain XMLHttpRequest object and writing an implementation yourself.

First of all you to write some rather difficult javascript code. Next you need to completely rewrite your page, looking nor working like a normal HTML form. Last and most important, the function fetches values using getElementById(). This is not really very flexible, because you will need not only the form, but the function as well when you add a field.

Using an AJAX library can really help you here. Javeline TelePort has got a brand new method, which automatically creates a HTTP post request of a form and sends it to the server.
Continue Reading »

PHP

PHP != Ruby (and why PHP needs more advanced OO stuff)

Today I read an article about how Active Record is implemented in Ruby. In this article he lays down how things are done in Ruby and how that is not possible on PHP. Though I agree with him on a large part, there are a few site notes to make here.

First of all it is possible to add methods to an existing class. There has been an extension called runkit out for years. But so little people have shown interest in it, that it has never grown out of the experimental stage.

If I understood the Ruby code correctly, acts_as_list specifies that the item is part of a sortable list with the position of the item known to the object. The has_many specifies that the object has a list with children. To translate this to PHP, you should be able to do something like:

  $list = new TaskList();
  $list->tasks[] = new Task('Buy bread');
  $list->tasks[3] = new Task('Become a famous');
  $list->tasks[] = new Task('Have a succesfull blog');
  $list->tasks[4]->setIndex(2);

Continue Reading »

MySQL, PHP

Splitting queries to improve performance

I often read articles saying to combine statements and send less queries. But you seldom see advise about splitting queries to improve performance. Here is what I came across just the other day:

SELECT IF(`uid_to`=@user, `uid_from`, `uid_to`) AS `uid`, `message`, `date`, `uid_to`=@user AS received
  FROM `beepme_msg`
  WHERE (`uid_from`=@user AND `uid_to`!=@user) OR (`uid_to`=@user AND `uid_from`!=@user)
  ORDER BY `uid_to`, `date`;

The query gets all messages send and received by the user, filtering our message the user send to himself. This looks like a good query since you’ll get all you data in one call. In reality it will mess up your performance, since it can’t use any indexes and will therefor use a table scan. And as we know, tables scans are slow.

The ‘OR’ statement basically messes this up, since only one index can be used per table. We can see what happens a bit better if we rewrite the query (not changing behaviour):

SELECT IF(`uid_to`=@user, `uid_from`, `uid_to`) AS `uid`, `message`, `date`, `uid_to`=@user AS received
 FROM `beepme_msg`
 WHERE (`uid_from`=@user OR `uid_to`=@user) AND NOT (`uid_from`=@user AND `uid_to`=@user)
 ORDER BY `uid_to`, `date`;

We see that (`uid_from`=@user and `uid_to`=@user) is just an additional filter and doesn’t have to much impact. It is clear that neither an index on uid_from or uid_to can be used, because neither can get a set with all records which fit within the criteria. If an index gets to many records, the selection is filtered to get the correct result. But if an index gets to few records, it simply can not be used.

To solve this we need to split up this query and use a UNION SELECT:

SELECT `uid_to` AS `uid`, `message`, `date`, 0 AS `received` FROM `beepme_msg` WHERE `uid_from`=@user AND `uid_to`!=@user
UNION SELECT `uid_to` AS `uid`, `message`, `date`, 1 AS `received` FROM `beepme_msg` WHERE `uid_to`=@user AND `uid_from`!=@user
ORDER BY `uid_to`, `date`;

This is a nice example of how common rules like ‘avoid subqueries and union selects’ are not alway true. So keep using your head (and EXPLAIN) while writing queries.

Einstein

Improved version of the ‘Did You Pass Your Math?’ WP plugin

Screenshot fourier

Einstein

Alternative career: super model

Today I started the first day of my alternative career. I might not make it in computers, but at least I have my looks :p. So I decided it’s time to give modeling a shot. No enough laughs, basically I helped a friend setting up the lighting in her studio. Though she will make some professional photos of me later on, so keep posted.

But just for all you who always wanted to know what I look like (because I never show a recognizable photo), here it is:

small

ivonka_ps1_0775_small.jpg

small