cdb(5)
๐ cdb(5) โ the file format manual, bestie edition ๐
okay so this is cdb(5), from the File Formats Manual section. It's literally the blueprint for the Constant DataBase file format, and I'm gonna walk you through it like we're texting at 2am. ๐ฆ
โจ NAME โจ
cdb โ Constant DataBase file format. That's it, that's the whole vibe. ๐
๐ธ DESCRIPTION ๐ธ
A cdb database is a single file that maps keys to values, and it does that by storing a bunch of (key,value) pair records. No cap. The file is made of 3 parts:
- toc โ the table of contents ๐
- data โ the actual records ๐
- index โ the hash tables ๐ฅ
๐๏ธ the toc section
Toc has a fixed length of 2048 bytes, and inside it there are 256 pointers pointing to hash tables inside the index section. Every single pointer is made of two things:
- the position of a hash table in bytes from the beginning of a file
- the size of a hash table in entries
Both of those are 4-byte (32 bits) unsigned integers in little-endian form. And lowkey important: a hash table length may be zero, which means that corresponding hash table is empty. It's giving "nothing to see here". ๐
๐ฆ the data section
Right after the toc section, the data section follows without any alignment. It's just a series of records, and each record is:
- a key length
- a value (data) length
- the key
- the value
Again, key and value length are 4-byte unsigned integers. And each next record follows the previous one without any special alignment โ they're just snuggled up next to each other, no personal space. ๐ซถ
๐ the index (hash tables) section
After the data section comes the index section. You must read it together with the toc section, because that's where each of the max 256 hash tables is defined. The index section is a series of hash tables, and their starting position and length are defined in the toc section.
Every hash table is a sequence of records, and each record holds two numbers:
- the key's hash value
- the record position inside the data section โ that's the number of bytes from the beginning of a file to the first byte of the key length that starts the data record
If the record position is zero, then this is an empty hash table slot, pointed to nowhere. Not it. ๐
๐งฎ the CDB hash function, periodt
The CDB hash function is literally this:
hv = ((hv << 5) + hv) ^ c
for every single c byte of a key, starting with hv = 5381. Slay. โจ
The toc section is indexed by (hv % 256), i.e. the hash value modulo 256 (which is the number of entries in the toc section).
๐ต๏ธ how to actually find a record
In order to find a record, one should do this, in order:
- First, compute the hash value (
hv) of a key. - Second, look to hash table number
hvmodulo256. If it is empty, then there is no such key, exists no more, gone. If it is not empty, thenโฆ - Third, loop by slots inside that hash table, starting from the slot with number
hvdivided by256modulo the length of that table, or((hv / 256) % htlen), searching for thishvin the hash table. Stop the search on an empty slot (if record position is zero) or when all slots have been probed.
โ ๏ธ Note the search is cyclic โ it jumps from the end back to the beginning of a table. Don't forget that part, bestie, it's a whole thing. ๐ญ
When the hash value in question is found in the hash table, look to the key of the corresponding record and compare it with the key in question. If they're of the same length and equal to each other, then the record is found ๐ โ otherwise, repeat with the next hash table slot.
๐ And one more thing: there may be several records with the same key. Not a bug, just the format living its truth.
๐ SEE ALSO ๐
๐ AUTHOR ๐
The tinycdb package, written by Michael Tokarev <mjt+cdb@corpit.ru>, based on ideas and shares file format with the original cdb library by Dan Bernstein. Icons only, fr fr. ๐ซถ
๐ LICENSE ๐
Tinycdb is licensed under the MIT license. ๐
And that's the whole page, bestie โ this was cdb(5) from the tinycdb package, dated Apr, 2005. Go forth and hash responsibly. ๐ธโจ๐
๐ฆ where this page comes from
- arch rolling: tinycdb 0.81-2 ยท MIT
- debian trixie: libcdb1 0.81-2 ยท MIT
- nixpkgs unstable: tinycdb 0.81
- ubuntu noble: libcdb1 0.81-1build1 ยท MIT
- ubuntu questing: libcdb1 0.81-2
- ubuntu resolute: libcdb1 0.81-2build1 ยท MIT
- ubuntu stonking: libcdb1 0.81-2build1 ยท MIT
- Copyright (C) 2001-2023 Michael Tokarev <mjt+cdb@corpit.ru>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms