• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read DB file into memory

 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In a previous post the following was written :

"Is it ok for the database implementation to keep a copy of the whole database in memory? i.e. at startup, read the whole db file into some data structure and use that as the database, only interacting with the undelying file when changes are made."

The answer was yes. I am wondering which way to go myself.
I am thinking that for code readability it would be easier to read into memory rather than dealing with a lot of File I/O.

How anyone got any arguments for/against the idea of reading the file into memory at the start and writing it out at the end ?

Cheers,
Alan.
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by alanwmorgan @Y:
How anyone got any arguments for/against the idea of reading the file into memory at the start and writing it out at the end ?

Hi Alan,

This topic has been debated here many times and as far I can tell there is no general consensus on what's better.

A pro for caching is performance, but you will need to take scalability into mind (what if the database is larger than you will have memory available? Although many consider this to be beyond the scope of the assignment).
Pros for handling the file directly is simplicity, which is an explicit requirement for the assignment. For me this was the reason to chose not to cache. And furthermore I assumed that the database file will also be cached by the operating system and that whatever caching system I develop, will probably not beat the OS.

Whatever design you choose, I think that "writing it out at the end" is never a good idea. What if the server system crashes or is killed or there is a power failure? You will probably want to write updates to the database immediately to disk.

Frans.
 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frans Janssen:

Hi Alan,

This topic has been debated here many times and as far I can tell there is no general consensus on what's better.

A pro for caching is performance, but you will need to take scalability into mind (what if the database is larger than you will have memory available? Although many consider this to be beyond the scope of the assignment).
Pros for handling the file directly is simplicity, which is an explicit requirement for the assignment. For me this was the reason to chose not to cache. And furthermore I assumed that the database file will also be cached by the operating system and that whatever caching system I develop, will probably not beat the OS.

Whatever design you choose, I think that "writing it out at the end" is never a good idea. What if the server system crashes or is killed or there is a power failure? You will probably want to write updates to the database immediately to disk.

Frans.



Frans,

I take your point about "writing it out at the end".
I initially started out with the non-caching approach but I soon found that the code I was writing to implement for example find(String[] criteria) was getting overly complex.

OK maybe complex is overstating it but I certainly thought that interrogating the methods of an object representing a record would be cleaner.

Maybe its just the way I am coding. I'm not too sure really.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alanwmorgan @Y,

Welcome to JavaRanch. You may want to take a look at the JavaRanch naming policy and change your screen name accordingly. Thanks!
 
Alan W Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume the @Y was the problem.

It appeared someone else had already taken my name so I just added @Y to the end out of frustration having tried about 10 different combinations.

Ok now ?
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep! We just like to know who we are talking to here!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic