Wishful Coding

Didn't you ever wish your
computer understood you?

Understand SQL, learn NoSQL

I learned SQL when I started PHP. I found a website named Tizag, where they had SQL tutorials. I installed PhpMyAdmin, created tables, ran queries like SELECT * FROM pages WHERE foo IS bar LEFT JOIN ON comments or whatever. It was magic.

No one ever explained to me how it stored the information, or how it was so fast (or slow). They did say that indexes made stuff faster, sometimes.

On the other hand, when you read the CouchDB guide, they do not primarily teach you their query language, but also a lot about how stuff works. A lot of this also applies to SQL databases.

Storage

CouchDB uses a B-tree to store documents. This provides O(log n) lookup, update, etc. rather than O(n) scanning of all documents. It seems most SQL databases use a B-tree as well, but not always.

Indexes

When you add a WHERE clause to your query, the database has to look at all documents for a match.

If you add an index to the field, you get a sorted representation of that field. This way you can get single items or ranges(time > 123456) in logarithmic time, using binary search.

CouchDB gives you a ‘view’ of the _id of a document, but other views will have to be created to create the equivalent of a WHERE clause. (What _id is in CouchDB, is your primary key in SQL)

Locking & Transactions

NoSQL databases are infamous for their lack of locking and transactions. Why? For the sake of scalability.

Let’s ignore for a moment that 90% of all apps can run on a single server. The idea is that creating a transaction synchronously on a whole cluster is nearly impossible, let alone fast. So you you just don’t to it at all, in NoSQL land.

The flip side is that not locking at all allows reads to be faster. More on that in the next section.

Interesting to note is that both CouchDB and PostgreSQL use MVCC, allowing for reads without locking. So this is not unique to NoSQL databases.

History & Recovery

CouchDB stores its data in append-only B-trees, meaning that data is never changed.

Because old data is still there and immutable, readers can access it without waiting for a write to complete. It is even possible to read old revisions of the data.

What is maybe even more interesting is that, if the server crashes in the middle of an update, the old data is still there.

InnoDB also applies a similar technique, unlike Mysam, which needs to scan and repair the whole database.

Joins

Basically the same thing as transactions, you don’t want to scavenge your whole cluster looking for all comments referencing a blogpost.

The high-performance way to do joins is to not do joins, instead unlearn everything your learned about normalization, and denormalize.

The other way to do it teaches us more about SQL joins though. Basically you create an index or view on the ‘foreign key’, and run a separate query to get the correct documents. Here is an elaborate example.

Conlusion

Neither SQL or NoSQL databases are magic, and they are even pretty similar in most ways. Don’t follow the hype, choose wisely.

Minecraft 1.8 (P)review

Like most Minecraft addicts1, I downloaded the prerelease this weekend and played it all day. There are some really cool things in there, but also a few crapy ones, and of course a few bugs and glitches.

My new home

I rally love the new biomes. I spawned in a desert biome, and if it wasn’t for the NPC village, I would have died for sure. Rivers are also great for exploring by boat.

I really like all the stuff that is specific to certain biomes. Without compasses and maps, it’s quite an adventure to get wood, animals and later vines and mushrooms from other biomes.

There are quite a few odd new items, like Ender pearls, rotten meat, and huge mushrooms, but they win on atmosphere, so I built my home in a shroom. Beat that, treehouse!

Mineshaft

The new map features all look really neat, but I found the abandoned mineshafts impossible to navigate. They cross right through strongholds, other caves and themselves.

The only thing I rally don’t like about the Adventure Update is the food and fighting mechanics. It’s like playing Quake 3 on peaceful2.

I’m not going to say anything about experience points, because I don’t know what they will be used for, but let me say that I don’t think Minecraft is a classic RPG.

The idea of food healing you indirectly is really nice, but having it replenish my life slowly makes me play as reckless as if I’m playing on peaceful. Maybe it could heal you while sleeping?

When you run, you are able to jump 4 blocks. Though, mid-air, you go faster than running, so the fastest way to move is to jump running. I pray that running diagonally isn’t even faster.

There is a new fighting mechanic that lets you deal critical hits by jumping down on a mob while hitting. This means the best way to fight is to jump up and down.

The last thing that adds to the Quake experience is hammering the mouse button. Previously, it was possible to place blocks and hit mobs by just holding the button, now you need to hammer the mouse like a real FPS. dislike

So, overall, I really enjoy 1.8, but can I say it’s having a bit of an identity crisis?

  1. Well, I played all Sunday, but except for that I don’t play much. 

  2. I Play on hard. 

CSS tips and tricks

While working on Sombrero, I encountered a lot of things I did not know how to do properly. I hope they help someone.

width: cling;

Block elements normally fill up all the available width, except when you apply position or float to them. I needed some element to take up the minimal width, without the side effects of floating or positioning. display: table; is the solution I was looking for.

Full-width form fields

Another problem in my magic alignment layout where full-width form fields. Input elements do not take space like normal block elements, no matter the display property.

width: 100%; works, but only as long as you have absolutely no borders, margin, or padding. To get around that, CSS 3 has a property to use the IE 5 box model, which includes padding and borders in the width: box-sizing: border-box;

Collapsing margins

I can’t imagine why I never noticed before, but it turns out CSS collapses margins on “regular” block elements. It’s complicated.

display: run-in;

I never used it, but it seems like an interesting property. It makes things like headings appear inline with the content of the block below it.

Styling range input fields

Some modern browsers support input fields of type="range", for imprecise number values. However, style on these elements is normally completely ignored.

Bring in -webkit-appearance: none;, which turns the slider track back into a normal box. Then apply the knob styling to the ::-webkit-slider-thumb pseudo element.

position all four corners

Another part in my alignment puzzle. Note that not all browsers support this. If you can’t do with 2 sides plus size, it is possible to specify all 4 and leave the sizing implicit.

Published on