user152949
user152949

Reputation:

Writing to a files raw disk sectors

I'm working on a OS portable database server and I want to know what is the best way to read/write to a index file disk sectors without locking the entire file. My database disk manager have a bitmap that keep track of where all used/unused the sectors are inside the index file. Also, is there a OS portable way of doing this?

Upvotes: 0

Views: 1521

Answers (1)

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

...read/write to a index file disk sectors without locking the entire file.

You can lock just the portion (of the file) you are writing to. Under Windows, this would be accomplished using LockFile. BTW, why is it important not to lock the whole file? Your server will have exclusive access to it anyway, and it can manage the locking internally (unless you want a server-less/embedded database while still allowing multiple client connections to it).

To achieve transaction durability, you'll also need to make sure the data you write doesn't linger in the cache/buffers and is being physically written to the disk at correct times. Under Windows, consider passing FILE_FLAG_WRITE_THROUGH (and possibly FILE_FLAG_NO_BUFFERING, assuming you do your own cache management) to CreateFile.

I don't think there are OS-independent APis for any of this - you'll need to do it in a special way under each supported OS. Of course, nothing prevents you from encapsulating OS-specific code and presenting the uniform interface toward the rest of your system.

Upvotes: 1

Related Questions