3

Context

This is purely for education purposes. I want to write a primitive database. Focus is NOT on performance; but just the principles behind databases. I have material already on locking / mutexes / transactions. What I know nothing about is writing to disk / guaranteeing persistence in unexpected hardware (say power) failures.

In order to have proper recovery / persistence, I need certain guarantees when writing files to disk.

Question:

For the above purposes, what types of file primitives (guarantees that file is written to disk? leaving a file open and appending to the log?) do I need? What does the JVM offer?

Thanks!

  • Consider a model like that used by SQLite (which is talked about). All that is "*really*" required is an atomic exclusive lock and sync. Also, take a peak at *existing* Java databases like H2SQL or Derby. –  Jun 27 '12 at 00:03
  • My question was not well written. I'm interested in _file system_ primitives to ensure persistence. I've updated it to focus on file system primitives. Sorry about that. –  Jun 27 '12 at 00:04
  • An atomic exclusive *file* lock and *file* sync ;-) A "flush" will ensure the *process buffers* are flushed. A true `fsync` will wait/block until the disk has actually persisted the data to media. (Or the OS lies about it.) –  Jun 27 '12 at 00:25
  • http://stackoverflow.com/questions/4072878/i-o-concept-flush-vs-sync should help to explain flush vs fsync –  Jun 27 '12 at 00:27

2 Answers2

2

It's a huge area to talk about because of the many layers of abstraction surrounding discs these days, though from the JVM's perspective you pretty much depend on fsync to actually write your bits to disc once you call fsync you depend on these bits being on the disc. the rest is built on this.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • How does fsync differ from flush? –  Jun 27 '12 at 00:16
  • some languages flush causes the buffer in that program of data waiting to be given to the OS to be flushed. after flushing it will then be included in the next fsync. – Arthur Ulfeldt Jun 27 '12 at 01:00
0

To force data to be written to disk before the call to a write returns, you must use a FileChannel and call force.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57