Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(B&S) The read() method

 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have been a bit confused on how to go by implementing the read() method. Since it accepts an Integer, my immediate instinct is that it would use the value supplied and jump to a particular section(That is after all the schema section and header section has been skipped)!I would like it if i could get a pseudo-code of some kind!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you are basically correct. So try to write a formula for getting to the n-th record, n >= 0. Let p(n) be the file pointer position of the record you want to find. Let o be the offset to the first data record, r be the record length (including its delete flag). What's p(n)?

You may have to adjust that for your own particular file schema.
[ December 03, 2005: Message edited by: Barry Gaunt ]
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks i was able to solve the above quoted problem on the same day! Now the problem i face s this:
I intend to use a cache(An arraylist), that would have the records on the data file. My algorithm would involve the constructor of my data class read the whole records of the datafile unto an arrayList(private static: so that only one instance is available if client requests for the data file).
My question is this: Do i allow the read() method read from this cache instead of the actual file?My second question is: Is it ok to have the constructor of data.java read all the record once it is invoked?Thanks!
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My other question is: Do i sync on my cache cause i figured, a client might be trying to update, a deleted record!In any case, i would luv a push in the right direction! Thanks!
 
Ailsa Cape
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Saheed

I think you are on the right track.
Do i allow the read() method read from this cache instead of the actual file?

I do the same thing as what you want to do. You may find in this forum that using cache is valid in this exam.

Is it ok to have the constructor of data.java read all the record once it is invoked?

I also did it just like what you said.

Do i sync on my cache cause i figured, a client might be trying to update, a deleted record!

Yes, I think you have to ensure thread safety in multithreading environment by using synchronized sign in this ArrayList when your program is running in the server mode.

I hope this would be helpful.

Regards, Ailsa Cape
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic