• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX:[URLyBird]Focus in the COOKIE

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my assignment,I just saw three different cookie:
1st-magic cookie,show as

4 byte numeric, magic cookie value. Identifies this as a data file


2nd-lock cookie,occurs in

public void deleteRecord(long recNo, long lockCookie)
throws RecordNotFoundException, SecurityException;


3rd-cookie,as follows

public void unlock(long recNo, long cookie)
throws SecurityException;


A)What differences have they had?
B)How should I use them?
Regards,
Richard
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,
You know already what 1st is : it identifies the file as a data file.
And 2nd and 3rd are both lock cookies.
Best,
Phil.
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Philippe
I understand what you mean.But I don't know the use of them.
For example,the magic cookie is only used to identify the data file?
What uses to access database?
Regards,
Richard
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

For example,the magic cookie is only used to identify the data file?


Correct. Any time you open the data file, you can check to see if the magic cookie is there and if it matches the magic cookie you believe should be there. This means that you will have to note the current value and store it as a constant in your code so you can compare it later.
The lock cookies are used to validate who owns the lock / who is allowed to update a record.
So when a client wishes to update a record, they must first lock the record. The lock method will return a (lock) cookie to the client. The client must then use that lock cookie as a parameter in the update method. If the wrong cookie is used, the update method should not allow the update to continue.
Regards, Andrew
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

I understand what you mean.But I don't know the use of them.


Well, for the "magic cookie", you may test its value and throw some exception if the value read is not the one you expect (as you read the file header already you should know which value it is). The aim here is just to avoid your database system trying to interpret a wrong file.
As far as the lock cookies are concerned, your question is too general to get answered IMO.
I would suggest you to read your instructions carefully once more, than make some personal research and tests (it's your learning process isn't it ?) and search on this forum with keywords like "lock", "cookie", "LockManager". After that, you should have sharper questions which are easier to get answered.
Good luck and good job,
Phil.
PS: It seems that my reply crossed Andrew's one. Sorry.
[ August 27, 2003: Message edited by: Philippe Maquet ]
[ August 27, 2003: Message edited by: Philippe Maquet ]
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Andrew and Philippe!

This means that you will have to note the current value and store it as a constant in your code so you can compare it later.


As Andrew said,I can declare one static member

How to initialize or assign a value for them?

you can check to see if the magic cookie is there and if it matches the magic cookie you believe should be there.


Should I use a condition statement to do a analysis if matching magic or not?
Regards,Richard
 
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,
You think too much about a magiccookie.
It is simple. I hard-coded a value of magic cookie.
When I read file header I read its value and compare it with my constant.
If the value doesn't match I throw an Exception indicating not valid file format:

Regards,
Vlad
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Vlad,
Thanks for your reply.

If the value doesn't match I throw an Exception indicating not valid file format:


Yes,all you said is clear.
But what value do you want to assign the MAGIC_COOKIE constant?
If I don't know the initial value,obviously,I am not able to compare to them and know if they match each other.
I think of this,isn't it?
Welcome other new explanation!
Regards,Richard
------------------------------------------------------------------
It is valuable for the efforts of knowing any confused points well.
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,


Execute first you program without cookie to check (you don't the value yet). Let the value to be printed on your screen. Now you will get the value and hard-code it in your Data class. That is what I have done to get it.
Regards,
Vlad
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Vlad
In your post,I just saw that following:

Execute first you program without cookie to check (you don't the value yet). Let the value to be printed on your screen.


The initial database file has a unique value(magic cookie).
If declare it as a constant,it must be initialized.What value is it?
I just did a exercise,and the sample code shows here,

Two situations:
1)When running the program,it will throw a exception.
2)If MAGIC_COOKIE is a static member,it still must be initialized firstly.
It is the unique way that I can test the .db file is existed or not?
Regards,
Richard
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,
Change your program so that it prints out the value of the magic cookie if it doesn't match the expected value as well as throwing an exception. Then you will know what the magic cookie's value is. And you can then put this value into your application.
Regards, Andrew
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Andrew
Thanks for your patience to explain.
I just modified my code,please comment on

I can get the cookie value before throwing the exception,and then...How can I do to finish writing rest code?
If I want to read some record,what things should I do?
Regards,Richard
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,
In your code, MAGIC_COOKIE should be initialized with the value you want to check against.


How can I do to finish writing rest code?
If I want to read some record,what things should I do?


If you cannot answer those questions yourself, you should study a bit before anybody here may help you.
There are a few standard Java classes in the Java API you should know before getting at work : RandomAccessFile at first, and maybe Charset and FileChannel so you can make a choice between io and nio.
After that first step, you should be able to make some tests against the given db file (make a copy of the original !) and come back with a few useful questions many people here will be willing to answer.
But before that first study step, I do think that there is nothing useful you can do.
Best,
Phil.
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Philippe
Thanks for your response and critical statements!

In your code, MAGIC_COOKIE should be initialized with the value you want to check against.


Assuming that the MAGIC_COOKIE has been initialized to a value,what things that I do can compare to them?At next step,I should consider what?
Regards,
Richard
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

Thanks for your response and critical statements!


My intent was not to be critical but to give you a sincere advice...

Assuming that the MAGIC_COOKIE has been initialized to a value,what things that I do can compare to them?


The answer is already in the code you wrote.
Best,
Phil.
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Philippe
Maybe I used a unsuitable word in last post.I'm so sorry to say that.
I really feel happy to meet you and other friends in the forum although we never see each other.
Like above code,the MAGIC_COOKIE can be initialized what value?
Can I assign it to

It's ok?If so,I can do more things in next code.
Regards,Richard
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

private final int MAGIC_COOKIE=0;


It is almost right.
You have to read your file header to find out the real magicvalue. It will be an int. Then, after you get at first time, you must card.code this value.
Scenario:
1.) You read first int value from your file header (as it is described in database schema in your assignement) and print the value on the screen. For example this value can be 121.
2.) Then you hard-code the value in your Data class:

3) Now you can change the code so, that it reads the fileheader gets magic cookie and compares with your constant MAGIC_COOKIE (which is 121). If these values are not equals you throw an Exception.
Regards,
Vlad
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Vlad,thank you!

3) Now you can change the code so, that it reads the fileheader gets magic cookie and compares with your constant MAGIC_COOKIE (which is 121). If these values are not equals you throw an Exception.


Yes,I want to modify the code correctly.
1)But the MAGIC_COOKIE value should be initialized to what value?
2)And then,how and when I can know how many the value is?
Regards,Richard
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

But the MAGIC_COOKIE value should be initialized to what value?


Look at step #1 described by me. The answer is there.
It looks like you don't understand what is magic cookie.
There is a file db.db. It contains fileheader and records.
One of the values in fileheader is magiccokkie (probably the first one, look at your instructions). This is a value which you have to find out first. And this is the value your MAGIC_COOKIE must be initiated to. That is why you have to execute step 1 to find out THIS MAGIC COOKIE VALUE!!!
Open stream or whatever you want to the file db.db:
RandomizeAccessFile raf = new ...
int magic = raf.readInt();
System.out.println("Magic cookie. This value I will use to initializte my MAGIC_COOKIE: " + magic);
Output sample:
Magic cookie. This value I will use to initializte my MAGIC_COOKIE: 121
Now you forget about this code and start implementing your your Data class.
private int final MAGIC_COOKIE = 121.
and implement your if condition: if the cookie in the file your read is correct - Ok, if not throw an Exception.
If you execute your database (Data class) on db.db of course the value of your constant MAGIC_COOKIE will be equal to the from db.db (because you have got the cookie value from this db.db earlier).
If you try to run your Database using another file instead of db.db (e.g. instrctions.html) the value you read there will not be equal to your magic cookie identifying that this file is not your database file. So your programm will throw an exception.
Richard,
I will be out of this discussion forum for a week, so if you still don't understand the idea, I suggest you:
- try to understand what is the magic cookie for (as soon as you understand it you will automatically understand how to handle it)
- ask somebody else, since I will not read mails next week
Good Luck,
Vlad
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Vlad
Thank you again!
As you just said,

1.) You read first int value from your file header (as it is described in database schema in your assignement) and print the value on the screen. For example this value can be 121.


OK,I did.And my cookie value is 259.

2.) Then you hard-code the value in your Data class:

code:
--------------------------------------------------------------------------------
private final int MAGIC_COOKIE=121;
--------------------------------------------------------------------------------


You mean that I must use the value just got and assign it to the magic cookie firstly?

3) Now you can change the code so, that it reads the fileheader gets magic cookie and compares with your constant MAGIC_COOKIE (which is 121). If these values are not equals you throw an Exception.


In your wrote,I assume the code like this:

Is that right above?
Wait for your analysis about it.
Regards,
Richard
 
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Richard,
You wrote:


OK,I did.And my cookie value is 259.
quote:
--------------------------------------------------------------------------------
2.) Then you hard-code the value in your Data class:
code:
--------------------------------------------------------------------------------
private final int MAGIC_COOKIE=121;
--------------------------------------------------------------------------------
You mean that I must use the value just got and assign it to the magic cookie firstly?


I think Vlad is out on a well-deserved vacation. Therefore, I am answering in his place.
Yes, as Phil and Vlad are suggesting, you say in your data class
private final int MAGIC_COOKIE=259;
Since that is the value that you are getting right? Once, you do that, your pseudo-code seems correct to me.
Regards.
Bharat
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,




I think that is not the right exception to throw, after all the file specified was found. A "not found" exception will just cause confusion and calls to your support desk ('the program says the file is not there but I can see it is there - your program is buggy!').
Perhaps you could throw an "invalid format" exception?
Regards, Andrew
[ August 30, 2003: Message edited by: Andrew Monkhouse ]
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Bharat and Andrew
Thanks a lot!

private final int MAGIC_COOKIE=259;


Bharat,
I have gotten exactly the MAGIC_COOKIE=259,but I have not declared the value as the constant before ensuring that.
Can I use the value directly to declare a constant?
If so,I am going to try it.
Andrew,

I think that is not the right exception to throw, after all the file specified was found.


You just said is right,I think.I have not ensure the exception yet when I am only describing simply in pseudo-code.
So,I will research the exception when making use of it.
Regards,
Richard
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

have gotten exactly the MAGIC_COOKIE=259,but I have not declared the value as the constant before ensuring that.
Can I use the value directly to declare a constant?


Yes. That is exactly right.
Regards, Andrew
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Richard,
You are in good hands (Andrew's). Keep up the good work.
Regards.
Bharat
reply
    Bookmark Topic Watch Topic
  • New Topic