saveriomiroddi / saveriomiroddi.github.io

My professional blog
https://saveriomiroddi.github.io
MIT License
3 stars 0 forks source link

https://saveriomiroddi.github.io/SQLIte-database-file-format-diagrams/ #77

Open utterances-bot opened 2 months ago

utterances-bot commented 2 months ago

SQLIte database file format diagrams – Saverio Miroddi – 64K RAM SYSTEM  38911 BASIC BYTES FREE

During my CodeCrafters SQLite project, I’ve found the SQLite database file format document to be rather complete, but nonetheless, for a variety of reasons, hard to use.

In this article I present easy to read diagrams, that one can refer to while developing the exercises.

Content:

Introduction Index interior page Index leaf page Table interior page Table leaf page

https://saveriomiroddi.github.io/SQLIte-database-file-format-diagrams/

elordeiro commented 2 months ago

One small error I noticed is that for:

Index [Interior | Leaf] Page -> Cell Payload -> Record Body -> RowID

You have type listed as varint and it should be int

Although I imagine that you did varint because the number of bytes that composes that int does indeed vary, but it should not be decoded as a varintbut rather it depends on the serial type of the correspoding col in the varint[] in the header.

Here's what I used for decoding a varint:

// Big-endian
func readVarInt(buf []byte) (uint64, int) {
    result := uint64(0)
    for i, b := range buf {
        result <<= 7
        result |= uint64(b & 0x7f)
        if b&0x80 == 0 {
            return result, i + 1
        }
    }
    return result, 0
}

And here's what I used for a []byte to uint64 which I used for the RowID of Index Page cells:

func bytesToInt(bytes []byte) uint64 {
    var result uint64
    for _, b := range bytes {
        result = (result << 8) | uint64(b)
    }
    return result
}

Other then that, your diagram was SUPER helpful as I was going through the project. Thank you!