Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Magic Cookie & Offset-to-first-record

 
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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • 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
 
Ranch Hand
Posts: 221
Scala Python Java
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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

 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic