techie : blog : programming

Menu

Diary
OldIRC
URLs
misc
techie
writing

More content will be added to fill this space at some point in the future.

home :: techie :: blog :: programming [agile :: html :: java :: sql]

Opera 9.51 shows noscript tags

It seems that it's possible to get opera to show the content of a <noscript> tag, even without the user disabling javascript.

It took me a long time to work out why there was the content of <noscript> tags popping up on our site for opera users. They were showing the raw html of the text inside the tag, but it was showing up.

See the example of opera displaying noscript tags

It turned out that someone had added CSS to style noscript elements, and set them to display: block. From this, it seems that Opera (9.51) treats noscript elements as if they were a <pre> tag, styled with display: none.

This seems quite sensible, and it was a bit silly for us to style the noscript element as displayed. However, the HTML spec says that the browser shouldn't display the noscript except in 2 specific cases - scripting being turned off or it not having run a previous script on the page. As neither of these have happend, it should not display the text, putting Opera in the wrong.

Last updated: 14:39, 20 Jul 2008 [ /techie/blog/programming/html ] Link..

Quicker Table Mutations

When adding a new column and assigning values to it, you may be tempted to write something like: (I'm using postgres)

-- Add cost column
ALTER TABLE products add COLUMN cost DECIMAL;
update products set cost = 0.00;
ALTER TABLE products ALTER COLUMN cost SET NOT NULL;

These three queries are not the fastest way of doing this. It requires several passes through the table on disk, the first time it adds the column, assigning null to each row. The next time it goes through them all changing the value to be "0.00". It then sets the table to be not-null.

It can be done a lot faster by assigning the default value at the same time as adding the column.

-- Add cost column
ALTER TABLE products add COLUMN cost DECIMAL NOT NULL DEFAULT 0.00;
ALTER TABLE products ALTER COLUMN cost DROP DEFAULT;

the second line here can even be omitted if you don't mind the default still being there, not that it takes a long time.

Similarly I saw recently one where all the existing data had to have a different value from the new default. This was implemented as:

ALTER TABLE products ADD COLUMN new_flag BOOLEAN DEFAULT false;
UPDATE products SET new_flag=true;

It was made significantly faster by changing it to

ALTER TABLE products ADD COLUMN new_flag BOOLEAN DEFAULT true;
ALTER TABLE products ALTER COLUMN new_flag SET DEFAULT false;

which does the same thing, but only scanning through the table once.

Last updated: 18:12, 03 Jul 2008 [ /techie/blog/programming/sql ] Link..

Ordered updates in SQL

Why doesn't the update command in SQL support an "order by" clause?

Logically you'd think "That'll be because the order doesn't matter" - SQL is all mathematical set operations. Unfortunately it does.

Think of the case (that you shouldn't do), of altering the primary key of a table:

  update my_table set id = id + 1;

Think of this as being like Hilbert's Hotel - if there were no collisions before running it (and nothing added to it whilst the update runs) then there should be no collisions afterwards.

Unfortunately you do get an error indicating a violation of the primary key uniqueness constraint. Obviously it sets the id of the row which used to have id=1 to be id=2 then checks for the constraint at this point there is already a row with id=2 (that we're intending to make id=3). If we updated them in order from the highest id first, then there wouldn't be any problem as we would free up a "room" before trying to put anyone else in it.

Last updated: 12:06, 25 Jun 2008 [ /techie/blog/programming/sql ] Link..

ACEGI FilterChainProxy and included requests

In our Spring application we use the FilterChainProxy in order to use beans as filters in our tomcat web.xml file. This moves the configuration into a different bit of XML, and allows us to configure the URLs that our filters apply to in a more flexable way than that available in the Servlet 2.4 spec.

For example, we can cache some URLs by defining

  <bean id="includeFilter" class="org.acegisecurity.util.FilterChainProxy">
    <property name="filterInvocationDefinitionSource">
      <value>
        CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
        /slowpages/.*=cachingFilter
        /something/complicated=cachingFilter
      </value>
    </property>
  </bean>

I want to use this for included requests by using the following (and a bit more) in my web.xml.

 <filter-mapping>
  <filter-name>includeFilter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>INCLUDE</dispatcher>
 </filter-mapping>

Unfortunately the URLs that are compared in the FilterChainProxy, are those of the original page, not that of the include.

To solve this, I've written IncludedFilterChainProxy.java which extends FilterChainProxy and wraps the setter of the filterInvocationDefinitionSource, so that the source that gets injected has been altered. It will make sure that the FilterInvocation passed to getAttributes will return the included URL, rather than the orginal URL.

I'm not sure that this is the best way, but it seemed the least invasive way of getting the result I want. If anyone knows of a better hook into ACEGI then please let me know.

I also posted to the spring forum about this.

Last updated: 21:29, 11 Mar 2008 [ /techie/blog/programming/java ] Link..

Computer scientists and trees

Computer science is all about metaphore, and how it enables us to visualise what we are doing, and discuss it with others. A lot of programming experience is about applying metaphore and recognising progamming idioms.

One that we were discussing at work this week was trees. Computer scientists and programmers talk about trees quite often. Trees are the metaphore used for some of the most often used data structures. However we noticed that these are some very confusing trees. We start at the root of the tree, then descend it (depth first) to reach the leaves. Maybe computer scientists spend too much time locked in darkened rooms programming, and never seeing the outside world, but if I were at the root of a tree, and wanted to find leaves I would head upwards, definitely not downwards.

Last updated: 12:19, 22 Jan 2005 [ /techie/blog/programming ] Link..

Mock Objects

I am always surprised how many programmers that I speak to have not heard of Mock Objects. Some can get away with it, they are those that wouldn't know what a unit test was if it bit them on the bum. Those that are writing unit tests have less of an excuse. People at an agile event I attended recently have no excuse at all, but at least they were interested in learning.

Well, lets see if I can explain what they are and why you would want mock objects, before you go and read the entry on the c2 wiki.

When writing code, or testing it you may not want to use the full production version of another class. This may be because you have not implemented the other class yet, but still need to run your code. You can compile ågainst an interface, but you can't test that your code behaves as expected without running it, and it is faster to implement a mock version of the final class than to wait for a full version. The other time you might use mock objects (and the time when I tend to use them) is when you are writing unit tests. You probably don't want to interact with a full-blown database that takes time to set-up, instead you can work with a facade of simple business objects that are more predictable and faster to use. You can also use mock versions of classes to introduce problems, such as thrown exceptions, to test how your class handles them.

There are two types of mock objects. There are the simple static mock objects, and the slightly more complicated dynamic mocks.

Static mock objects are where a programmer has written a class implementing an interface, but doing as little as possible, or maybe adding ways of throwing exceptions and that sort of things. You can download mock implementations of most of j2ee from mockobjects.com . This will probably lead to evem more people accusing you of having more tests than code, as you will have extra implementations of all of your interfaces that don't do anything. This can lead to well-implemented mocks, and you can even distribute them (possibly in a different jar), allowing people who use your code as a library, or otherwise use it in their code can use your mocked out versions of your classes. The advantage of static mocks is that they only need the language to support interfaces with multiple interfaces, and can probably be fakes at the link stage for more primative languages.

Dynamic mocks are generated at runtime using techniques such as reflection or emitting dynamic bytecode. Here you programmatically create an object that implements your interface (and some can use funky tricks to create fakes of any object), and tell it how to behave in this situation. This is followed by using this proxy object in the class after which you can ask it if it did what was expected of it. There can be a little more overhead when using dynamic mocks as all of the reflection takes time for methods that are called many times. This isn't overly likely to happen depending on how you write your unit tests but it can.

I suggest that any programmers that are either working on large projects where you have to write code against an interface, or who are writing unit tests, particularly those doing test-driven development, in Java or C# at least. To check out the use of mock objects to make life a little easier.

Last updated: 11:49, 21 Nov 2004 [ /techie/blog/programming/agile ] Link..

Sorting with Augmentation

When sorting a list of objects, objects will tend to be compared more than once each. With a CPU intensive compare operation this can be inefficient. It tends to be better to produce a new list of simple keys by which the objects canbe sorted and a link to the object, which can then be resolved over the sort. This means that the complex operation is only don O(n) times, rather than O(n log n). And a more simple thing happens O(n log n) times.

This can also be used to great effect with the command line "sort" utility. Say you have a set of log lines, starting with dates of the form DD-MM-YYYY. This is not easy to sort. However, we can augment each line with the date of the form YYYYMMDD which can then be compared either numerically or lexographically to sort the dates. After sorting, the augmentation can be removed.

 $ cat .... | perl -pe 's/(([0-9]{2})-([0-9]{2})-([0-9]{4}))/\4\3\2|\1/' |
              sort  | cut -d \| -f 2

Last updated: 09:41, 24 Jul 2003 [ /techie/blog/programming ] Link..