Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Magic Cookie & Offset-to-first-record

 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

A few questions:
- Do you guys read the magic cookie every single time before reading-from/writing-to the database file, or only once?
- and then after checking if the magic cookie corresponds to the one hard-coded, do you store the true/false in a static variable so that all instances of the class shares it?

- I also decided to use the facade design pattern where I have the Data class and then two other worker classes; now: did you guys read the "offset to first record" and "magic cookie" from the database worker class, or another util class?

Thanks!
Pieter
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, champion!

Do you guys read the magic cookie every single time before reading-from/writing-to the database file, or only once?


No, it isn't necessary. This code has just to be verified when the database file is accessed for the first time. In fact, some developers choose to ignore it!

and then after checking if the magic cookie corresponds to the one hard-coded, do you store the true/false in a static variable so that all instances of the class shares it?


It also isn't necessary. You just verify this number for the first time the file is accessed.

I also decided to use the facade design pattern where I have the Data class and then two other worker classes; now: did you guys read the "offset to first record" and "magic cookie" from the database worker class, or another util class?


Hum... I'm not sure if I got your question, but here's what I did regarding the offset of each record: I have a formula that calculates the postion of a given record, given the size of the magic cookie, the headers of the file, etc.
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

Thanks for the reply!

Cool thanks!

What I meant with the last question was - and now looking at what you did:
Did you create a utility class where you placed your formula (that you were talking about), or did you do it in (from) your database worker class (Data)?
I got my answers, thanks; it is better reading and calculating everything and stick it into static member variables.

Thanks,
Pieter
 
Carlos Morillo
Ranch Hand
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pieter,


I read the magic cookie once and if it's different from the correct one my Data class is expecting I throw and InvalidDatabaseException.
Check the SCJD FAQ.

The Database Schema of every assignment seems to be different, but once you figure out the structure of your schema
and how to read it, it's easy to compute the offset to the first record and to any other record based on the length of each individual data record.

I am using a Singleton to my Data class since in my assignment I have:

You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server.



HTH,


Carlos.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pieter Jacobs wrote:Did you create a utility class where you placed your formula (that you were talking about), or did you do it in (from) your database worker class (Data)?
Why would you put it in a utility class? Normally it should be a private method in your Data class, because it should be used only in that class.

Kind regards,
Roel
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Pieter Jacobs wrote:Did you create a utility class where you placed your formula (that you were talking about), or did you do it in (from) your database worker class (Data)?
Why would you put it in a utility class? Normally it should be a private method in your Data class, because it should be used only in that class.

I can see value in having a utility class - if you do this, you will have one class that is only concerned with CRUD operations on the data file, and another class that is only concerned with how to handle meta data. This reduces the overall complexity of each class, making them easier to maintain and easier to write unit tests for.

Similarly I am an advocate for having the Data class be a Fa├žade fronting separate classes that handle locking and file operations.

Regards, Andrew
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Monkhouse wrote:if you do this, you will have one class that is only concerned with CRUD operations on the data file, and another class that is only concerned with how to handle meta data.
God has spoken, so I have to agree (otherwise I go to SCJD-hell) Seriously, if you have a seperate class which handles the metadata you certainly will have a method which calculates the record position. I can see the benefits of such an approach (high cohesion, easy maintainable,...).
But if you are not seperating crud operations and meta data handling (in seperate classes) I think putting it in a utility class has no (or very little) added value.

Kind regards,
Roel
 
Pieter Jacobs
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thank you very much for all the help guys!!

I am busy working through it.

Thanks,
Pieter

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic