| 1041 | } |
| 1042 | |
| 1043 | void BTreeDatabase::writeRoot() { |
| 1044 | DataStreamIODevice ds(m_device); |
| 1045 | // First write the root info to whichever section we are not currently using |
| 1046 | ds.seek(BTreeRootInfoStart + (m_usingAltRoot ? 0 : BTreeRootInfoSize)); |
| 1047 | ds.write<BlockIndex>(m_headFreeIndexBlock); |
| 1048 | ds.write<StreamOffset>(m_deviceSize); |
| 1049 | ds.write<BlockIndex>(m_root); |
| 1050 | ds.write<bool>(m_rootIsLeaf); |
| 1051 | |
| 1052 | // Then flush all the pending changes. |
| 1053 | m_device->sync(); |
| 1054 | |
| 1055 | // Then switch headers by writing the single bit that switches them |
| 1056 | m_usingAltRoot = !m_usingAltRoot; |
| 1057 | ds.seek(BTreeRootSelectorBit); |
| 1058 | ds.write(m_usingAltRoot); |
| 1059 | |
| 1060 | // Then flush this single bit write to make sure it happens before anything |
| 1061 | // else. |
| 1062 | m_device->sync(); |
| 1063 | } |
| 1064 | |
| 1065 | void BTreeDatabase::readRoot() { |
| 1066 | DataStreamIODevice ds(m_device); |