• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Magic Cookie checking and deleted flag

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I've two questions about the code accessing the db file:

1) It is correct to check the corrispondance between magic cookie and the current version of the DAO? In my case, for version 2x2, I use readInt (4 bytes) then I compare with 0x0202 (constant of my DAO Class), throwing an exception if it fails.

2) The format of the file is quite unclear on Sun's instructions. Based on this sentence:

"2 byte flag. 00 implies valid record, 0x8000 implies deleted record".

This flag (why two bytes for a boolean flag??) is expressend in two different formats. It is correct to use these two constants?

public final static short FLAG_DELETED_RECORD = 0x80;
public final static short FLAG_VALID_RECORD = 0x00;

I can't use 0x8000, like in instructions, because I can't assign 0x8000 to a short value, but to read the field I use readShort (two bytes) and I need to compare this value with the two constants.

Thanks in advance.
 
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't use 0x8000, like in instructions, because I can't assign 0x8000 to a short value, but to read the field I use readShort (two bytes) and I need to compare this value with the two constants.

On the contrary you can, try using an explicit cast:


It is correct to check the correspondence between magic cookie and the current version of the DAO?
Yes as you want to ensure the select database file conforms to the schema of your implementation.
 
Roby Kappa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhm... I'm not sure about this solution...
instructions specifyes that 0x8000 is 2-bytes value, in fact it is equivalent to 1000000000000000, but I cannot assign it to a short value...

Your solution ((short)0x8000) is equivalent to 11111111111111111000000000000000, that is not 2-bytes...
[ September 21, 2008: Message edited by: Roby Kappa ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I've declared my flag as:


And I use
and
to write and read the flag.

* database is an instance of RandomAccessFile.

I think it works!

See ya!
 
Justin Rundle
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution ((short)0x8000) is equivalent to 11111111111111111000000000000000, that is not 2-bytes...

Lets just make this quick... ((short)0x8000) is NOT equivalent to 11111111111111111000000000000000 but more accurately 0x8000 cast to short presents a value that is in fact equal to -32768.

Therefore as the flag is now of type Short we can complete IO operations similar to this:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are we always required to check the magic cookie? Or, is this dependent on the assignment. My assignment is the B&S ver. 2.2.3, and nowhere in my assignment can I find anything about checking the magic cookie, its existence is only revealed as part of the database file description.

If I need to check it, how would I know which value I should expect? Is it the value I get when I run it against the file received as part of the assignment?

Hope anyone can help me on this.
 
Ranch Hand
Posts: 100
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tobias,

That's what I've done!

I'm on B&S 2.2.1 and there's also no explicit requirement to check the magic cookie value. Although I figure it makes a neater solution if your application gracefully handles the situation where a non-B&S data file is selected.

Likewise there's no requirement to handle the situation where your network client can't connect to your server. But it's much neater if you pop up a message to inform the user of this rather than spewing out lots of exception traces.

I hope that helps!

Chris
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic