Monday, November 05, 2007

Copy-On-Write: the root logical block

Well, I finished drafting the PhysicalBlock class, as well as a bunch of supporting classes. Seems like a lot of code for a simple idea, but it usually works out that way.

Now the idea behind copy-on-write is that you never overwrite a block while processing a transaction. This means that each time you update a logical block of data, it is moved to a new physical block number. Ouch! How do you keep track of where things are?

Lets start with the root logical block. The location of all other logical blocks can be determined starting with the root logical block. This block is updated with every transaction and is updated last. Writing the root logical block to disk is the transaction's commit operation.

We will reserve physical block 0 and physical block 1 for holding the root logical block. And as with other physical blocks, we never update both block 0 and block 1 in the same transaction.

When starting COWDB, we will read and validate both block 0 and block 1. If only one block is valid, we will take the root logical block from that. Otherwise we will use the more reciently updated block.

Note that if we follow these few simple rules, we end up with a crash-proof database. And it should be very fast as well--there's no need for any additional supporting files, and never any need to recover anything.

Well, that was brief. Now it is back to writing code to implement it.

0 Comments:

Post a Comment

<< Home