Data safety
What NHyDB has committed is preserved – even after a crash. Checksums detect damaged blocks, and dedicated tools check and repair the file.
Committing
NHyDB appends new data to the end of the file. Only a commit makes the new data valid: it writes the data to disk and then moves the commit boundary behind it. Everything before the boundary counts as stored.
Discarding changes
Until the next commit, changes can be undone. rollback() discards everything written since the last commit, including schema changes. The database stays open.
After a crash
If a program stops before the commit, the last committed state is there at the next opening. NHyDB cuts off incomplete remains behind the commit boundary. Committed data is never silently discarded in the process.
import nhydb
db = nhydb.create("account.nhydb")
db.create_realm(1, "account")
db.put_cell(1, 1, 1, "deposit 100", type="utf8", commit=True) # committed
db.put_cell(1, 2, 1, "deposit 200", type="utf8") # not yet committed
db.rollback() # discards u = 2
print(db.get_value(1, 2, 1))
db.put_cell(1, 3, 1, "withdrawal 50", type="utf8") # not yet committed
db.close(commit=False) # like a crash
db = nhydb.open("account.nhydb")
print(db.recovered_tail_bytes > 0) # remainder cut off
print(db.get_value(1, 1, 1), db.get_value(1, 3, 1))
v = db.verify(deep=True) # check checksums
print(v["ok"], v["error_count"])
db.close()
Output:
None
True
deposit 100 None
True 0
The committed deposit is there, the uncommitted withdrawal is not. The check finds no errors.
Durability level when writing
You choose how thoroughly a commit writes to disk: "fsync" (default) uses the operating system’s usual synchronisation. On macOS, "full" also flushes the drive’s cache before the commit returns – this is slower and additionally protects against losses in this cache. On Linux and Windows both levels behave the same.
Checking and repairing
Every block carries checksums. NHyDB checks a block before using it for the first time and rejects damaged data instead of silently using it. In addition there is a full check of the file, a health report and a repair that cuts off a damaged remainder of the file – with a backup copy and without silently sacrificing committed data.
What is committed is valid – and NHyDB never silently discards committed data.