techie : blog

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 :: software :: stuff]

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..

Comments

I'm now experimenting with comments on this site, they're provided by js-kit in a very web2.0 way. Just a little bit of javascript on my site, and it works. Well, I don't know if it works yet, leave a comment to test it :)

Last updated: 14:10, 20 Jul 2008 [ /techie/blog ] 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..

Monitoring progress of a big copy

Sometimes you want to know how fast some files are copying across your netcat. tar -v isn't helpful if you have lots of small files as you can't really tell how fast it's going anywhat.

Up until now I've install Chris Lightfoot's cwp, but I've now found something that's built into debian.

Unfortunately it has a terrible name, which is why nobody has found it before. This is pv.

Last updated: 10:27, 20 Feb 2008 [ /techie/blog/software/linux ] 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..

Argh!! Error 1722

I just installed Office XP on a new machine. Once it was installed users from the domain were able to run Word etc. However it popped up an annoying error three times before it would let you in. This message said:

"Error 1722. There is a problem with this Windows Installer package. A program run as part of the setup did not finish as expected. Contact your support personnel or package vendor"

It turns out that in order to run Word, users need to be an administrator on the machine. WTF? Is this just so that word macro viruses can contact the network? Someone must be smoking something at MS, or there will be a way around this. Please tell me there is a way around this.

Last updated: 15:53, 11 Oct 2004 [ /techie/blog/software/windows ] Link..

Emacs keys not working in gnome

If you are like me and use firefox, you may have recently noticed that Ctrl-U doesn't delete the content of a box, rather it shows you the source to the page you were about to navigate away from. Ctrl-W rather annoyingly closes the window, and Ctrl-A doesnt go to the beginning of the URL. Well, there is a solution to this, and it turns out not to be the fault of firefox, rather the fault of Gnome. Gnome now defaults to some weird windows-alike keys rather than the finger macros that all unix users have programmed in (well, apart from those people who are so into VI that they have their shell using VI keybindings.

The solution is to go to the Gnome control panel -> keyboard shortcuts, thenset the Text Editing box from "GNOME Default" to "EMACS".

Changing Gnome to EMACS keys

Update: Alternatively, echo gtk-key-theme-name = "Emacs" >> ~/.gtkrc-2.0

Update 2: For gnome 2.8 MozillaZine has an article on this: Emacs keybindings for firefox(Firefox)

Last updated: 12:59, 06 Oct 2004 [ /techie/blog/stuff ] Link..

Annoying IE click sound

It appears that if you reload a page with the javascript function "document.location.reload()", IE does not make that anoying clicking sound.

function Update() { window.frames["FooFrame"].document.location.reload(); window.setTimeOut("Update()", 2000); }

Or disable it locally at:

Control panel->Sounds&Audio devices->Sounds->Program Events->Explorer->Start Navigation

Last updated: 18:31, 24 Aug 2004 [ /techie/blog/stuff ] Link..

Rackmounting checklist

You need:

Before going:

Last updated: 11:49, 18 Nov 2003 [ /techie/blog/stuff ] Link..

Index plugin

When writing my People page, and serveral others. I wanted an bit of introduction to be included before all of the content on the page.

I was browsing the writebacks of the blosxom page, when I saw this

In some places I would like to be able to make an index.txt file that would then replace the automatic folder index that blosxom generates when it renders the txt files. Is this possible? To clarify, say I have /www/datadir/some/folder/index.txt and blosxom statically renders to /www/site/. When I look at mysite.net/site/some/folder/, I want to see my rendered index.txt, not the blosxom index.

This is similar to what I had hacked up, so I thought I would give it a go as my first plugin.

The plugin can be downloaded here . And I welcome feedback to the email address in the plugin.

Bugs

Last updated: 23:31, 28 Jul 2003 [ /techie/blog/software/blosxom/plugins/index ] Link..

Traffic Shaping

From the OxLUG list. For people with cable modems.

That can actually be fixed by ensuring you throttle your outgoing connection to match your upstream bandwidth. The cable modem has big buffers to aid upload throughput but introduces big delays for interactive tasks while your packets transit the buffer. Under linux some varient of:
tc qdisc add dev eth1 root tbf rate 120kbit latency 50ms burst 1540
Will shape outgoing traffic to match your real upstream bandwidth. This slightly caps your upstream bandwidth per app, but keeps everything flowing.

Last updated: 09:48, 24 Jul 2003 [ /techie/blog/stuff ] 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..

Postscript Sickness

Mandelbrot in 4 lines of postscript. One day I will expand it so that it is readable. (It wasn't written by me)

/D{def}def/P{pop}D/g{.01}D/O{-.01}D/o{rlineto}D/J{index}D/M{mul}D/i{2 copy}D 0 g
90 90 scale/C{dup M}D 4{0 g 4{0 2 J 2 sub 2 J 2 sub i 1 O 0{6 -1 roll P 5 1 roll
i M 2 M 3 J add 2 J C 2 J C sub 5 J add 4 2 roll P P exch i C exch C add 4 gt{%e
exit}if}for P P P P setgray i moveto 0 g g 0 0 O o o o fill P}for P}for showpage

Last updated: 09:40, 24 Jul 2003 [ /techie/blog/stuff ] Link..