Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to .DB with RandomAccessFile

 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys.. I am doing periodic writing to .db

anyone pass using period update?

My flow would be like this

1) truncate the file using raf.setlength.(header size)
2) start writing all the records from cache into DB using raf.write()


It says All text values, and all fields (which are text), contain only 9 bit charactors, null terminated if less than maxium length for the field. May I know how to do the writing!! last hurdle for db layer.. please help!
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The writing is quite simple. Just use RandomAccessFile. Move the file pointer past all the schema information and begin writing your records.

Most people I think ignored the null terminating character and simply padded all fields (where required) so ensure that each field takes up the max space allowed by the schema for that field.
 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:The writing is quite simple. Just use RandomAccessFile. Move the file pointer past all the schema information and begin writing your records.

Most people I think ignored the null terminating character and simply padded all fields (where required) so ensure that each field takes up the max space allowed by the schema for that field.


how do I do this padding? any sample code anywhere?

Anyone pass with my current implementation of periodic update before?
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ixus See wrote:how do I do this padding? any sample code anywhere?


To pad, simply add empty spaces. If you look at the code from Andrew Monkhouse book you'll probably see sample code see here.

 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:
Ixus See wrote:how do I do this padding? any sample code anywhere?


To pad, simply add empty spaces. If you look at the code from Andrew Monkhouse book you'll probably see sample code see here.



oic thanks, so did anyone pass by using period update? If not I rather do code change =X
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what a period update is.

But I know people passed with:

1) Storing records in a cache.
2) Completely overwriting the records section of the data file with the contents of the cache.
3) Padding all fields (when required) with empty spaces to fill out the max length of the field for a record.
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I think I understand what you mean by period update now! You are periodically writing the contents of the cache to the data file.

I have read that people passed with this approach. But people also passed with the approach of only writing the contents of the cache to the file on shutdown.

I would go with the approach of only writing the contents of the cache to the data file on shutdown because you aren't going to get any extra marks for adding functionality for periodicly writing the cache to the file, so why bother?
 
Roel De Nijs
Sheriff
Posts: 10386
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A periodic update is not needed (I passed without it), but should at least be mentioned in your choices.txt to solve one of the drawbacks of the approach of using a record cache (huge data loss if e.g. server crashes)
 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:Oh, I think I understand what you mean by period update now! You are periodically writing the contents of the cache to the data file.

I have read that people passed with this approach. But people also passed with the approach of only writing the contents of the cache to the file on shutdown.

I would go with the approach of only writing the contents of the cache to the data file on shutdown because you aren't going to get any extra marks for adding functionality for periodicly writing the cache to the file, so why bother?


nvm I just change it to updating that individual record =x

by the way

I keep getting square boxes when i read the database after writing..

example elephant[][][][]

do i need to add black space to the left over strings when using string buffer?
 
Roel De Nijs
Sheriff
Posts: 10386
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ixus See wrote:do i need to add black space to the left over strings when using string buffer?

Maybe you could get some inspiration from the source code provided by Seane in one of his previous posts in this thread. I followed a similar approach for writing my records back to the database file.
 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Ixus See wrote:do i need to add black space to the left over strings when using string buffer?

Maybe you could get some inspiration from the source code provided by Seane in one of his previous posts in this thread. I followed a similar approach for writing my records back to the database file.


i did
 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int i =0; i<out.length(); i++) out.setCharAt(i, ' ');

solve using the following ;) thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic