1 Cocoli DB - File Format
2 =======================
12 db-header = { /* 8 octets */
17 db-version = 0 :: Word16
20 db-state = clean-state
31 , last-file-size-when-the-db-was-clean :: Word64
35 compacting-state = { /* 21 octets */
42 record = hole | pair | necrology
44 hole = { /* at a minimum of 9 octets */
55 , value-size :: Word32
64 , necrology-size :: Word32
66 , necrology-size :: Word32
77 1. Create an empty file.
78 2. Write a DB header and clean-state.
85 2. See if the DB header is correct.
86 3. See if the file is at least 29 octets long.
87 4. Read the db-state and redo any interrupted operations.
93 1. Write the current file size to the writing-state.
94 2. Write "1" to the beginning of db-state.
95 3. Append a new pair to the end of file.
96 4. Write "0" to the beginning of db-state.
102 1. Write the current file size to the writing-state.
103 2. Write "1" to the beginning of db-state.
104 3. Append a new necrology to the end of file.
105 4. Write "0" to the beginning of db-state.
111 1. Seek to the end of file.
112 2. Find the first pair with the given key.
118 1. Find the first contiguous holes from the beginning of file, turning
119 any inactive pairs and necrologies into holes at the same time.
120 2. Find the first active pair from the beginning of file.
121 3. If the contiguous holes is bigger than the active pair, move the
122 pair to the beginning of holes.
123 4. If the contiguous holes is smaller than the active pair, move the
124 pair to the end of file, then move it again to the beginning of
131 1. Write move-from, move-to, and amount to the compacting-state.
132 2. Write "2" to the db-state.
133 3. Copy the pair to the new location, eliminating padding octets if
134 any. If the destination hole is at least 9 octets larger than the
135 pair, turn the surplus into a new hole. If the surplus isn't large
136 enough to be a hole, pad the pair to fill the surplus.
137 4. Turn the original pair into a hole by writing "0" to the beginning
139 5. Write "0" to the db-state.
140 6. If the original pair was at the end of file, truncate the file to
144 Recovering from a writing-state
145 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147 1. Truncate the file to the size information in the writing-state.
148 2. Write "0" to the db-state.
151 Recovering from a compacting-state
152 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154 1. Copy the pair to the new location as the same way as the move
155 operation. Be aware that the original pair might already be turned
157 2. Turn the original pair into a hole.
158 3. Write "0" to the db-state.
159 4. If the original pair was at the end of file, truncate the file to
167 Any operations involving db-state changes can not be performed
170 It's possible to find a pair while inserting or removing another
171 pair. But it's impossible to do the compaction in that situation,
172 because the compaction process needs to tell active pairs from
175 Finding a pair while doing the compaction is impossible, because the
176 compaction process may move an active pair back and forth.