wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird question regarding db method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird question regarding db method" Watch "URLyBird question regarding db method" New topic
Author

URLyBird question regarding db method

Debs Bharadia
Greenhorn

Joined: Jun 22, 2009
Posts: 5
Hi Ranchers!

This forum is vey helpful. Already got quite a few of my questions answered. Thanks!!

I am working on URLyBird1.2.2. Update method signature in the interface provided by Sun is like -

"
// Modifies the fields of a record. The new value for field n
// appears in data[n]. Throws SecurityException
// if the record is locked with a cookie other than lockCookie.
public void updateRecord(long recNo, String[] data, long lockCookie)
"
Every record has fields like - Status code(2), Hotel Name(64), City(64), Max Occupancy(4), Smoking(1), Price Per Night(8), Dates Available(10), Customer Holding(8) in that order.

It is not clear from the instruction whether or not I should consider the status flag as part of a record. In other word, the String[] parameter in the updateRecord method, should have a value corresponding to the status flag or not?? (Is it an array of size 7 or 8)?

From the instruction, I am guessing it should be an array of 7 without the status flag, but I am not sure.
Also, if that's true, how am I going to update the status flag? The delete method is talking about a physical delete.

Hoping someone has an answer. I am not very clever.

Thanks in advance.
Debs
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Debs! First of all, I'd like to welcome you to the JavaRanch family!

From the instruction, I am guessing it should be an array of 7 without the status flag, but I am not sure.


You are right. I myself didn't include it in the String array. I only included the fields that have more to do with data related to rooms. The flag is more of a "status" of the record, so I didn't include it.

Hoping someone has an answer. I am not very clever.


Come on, partner! I bet you are more clever than you think you are!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Debs Bharadia
Greenhorn

Joined: Jun 22, 2009
Posts: 5
Thanks Roberto! I am feeling more confident now.

On a different note:
How do I update any field of the database file??
I am using RandomAccessFile and seems like the only way is to rewrite the file..is that true..isn't there a smarter way??
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Debs,

From me also a warm welcome to the ranch

RandomAccessFile has the words random and access, so you don't have to process (read/write) the whole record just to update a field. So if you just want to change some value of a field, you can do something like this:

and the value will be changed without reading the complete file. You have to calculate of course this offset (and be sure to not make mistakes or you might corrupt your database file

You have also the method raf.getFilePointer(), this method returns you the position of the file pointer (and this value you can use as offset). So you could read the header (with the magic cookie, database schema,...) of your database file and then add a System.out.println(raf.getFilePointer()); so you'll now at which position the record section starts.
The next time you want to start at the records section, you can call

and you will be direct positioned at the first record.

A final important remark: Before you start updating the file, make sure you have a backup of your database file, because when you submit your assignment, you have to provide an original, unchanged database file.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Roel De Nijs wrote:A final important remark: Before you start updating the file, make sure you have a backup of your database file, because when you submit your assignment, you have to provide an original, unchanged database file.


That's a great tip! Keep that in mind.

Now, RandomAccessFile is like a unanimity, everybody uses it. It is simple to deal with it and it does the job well. I think this discussion also brings up a question (and Debs, think about it): how are you going to deal with the database records? Basically you have 2 options: dealing with them directly in the .db file, or dealing with them in memory. My good buddy Roel and I had the same approach in this respect; we keep a cache that reflects the database in memory, so when we have to deal with them (when we have to update a record, for instance), we read the memory cache. And then, we just deal with the physical .db file 2 times: when the application starts, and when the application finishes. Although there isn't a "right" approach (each one has its pros and cons), we think this is the easiest one.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Roberto Perillo wrote:we think this is the easiest one.

Indeed, and because the assignment is already hard enough I decided to keep it simple wherever that's possible

Of course with that approach you may have some memory issues in the future (when your database keeps getting bigger and bigger). But I mentioned that in the choices.txt and also gave some alternatives to solve that issue in future releases.

So that's everything this assignment is about: if you have several options, choose 1 and defend that properly in your choices.txt (and even mention a con of the option you chose and describe a possible solution in a future release). I did that every time, because like Roberto already said: each option has its pros and cons. Another obvious example would be choice between RMI and sockets. I think 95% (or even more) of the people on this forum used RMI as network protocol (I'm also one of them), but RMI does have cons.

Kind regards,
Roel
Debs Bharadia
Greenhorn

Joined: Jun 22, 2009
Posts: 5
Thanks a lot Roel and Roberto!
Those were really helpful tips.
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
Hi ranchers

Roberto Perillo wrote:we keep a cache that reflects the database in memory, so when we have to deal with them (when we have to update a record, for instance), we read the memory cache. And then, we just deal with the physical .db file 2 times: when the application starts, and when the application finishes.


Roberto, but wouldn't that mean that if the application crushes (or the wicked Assessor presses reboot ) then ALL data entered after app start which was supposed to be persisted will be lost forever? I am also considering holding map of records in memory, but only as a convenience helper for record locking, fast searching, data manipulation etc. (direct operations in the file is a kind of, well, weird stuff). Of course its not a real life application, but still I have a strong feeling that every write operation must be persisted. After all, RandomAccessFile is a great thing for this. What do you think?


SCJP, SCWCD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Garik,

Check my first post in this thread. A similar question was asked there.

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Garik!!!

Basically, we have 2 options: we can deal with the .db file directly on every operation, or we can deal with the records in memory. Each approach has its pros and cons. In my case, I justified this choice based on the pros I could think of, you know... it has several benefits, such as not having to deal with IOExceptions (and thus the restricted interface and the fact that we can't change it wouldn't be a problem) and the other things you also addressed. This approach also has its cons... but maybe this is the biggest deal of this certification: facing ambiguities and finding the most adequate path to solve them, having 2 solutions for something and choosing only 1 based on concrete facts... it is about choosing and justifing. Ah, and a good tip is to always try to keep things as simple as possible.
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi for all
There is any problem in put delete flag inside array[] like Debs Bharadia ask ? I mean, consider it like field ? Wich will be the problems ?
I dont see any problem....


Fernando Franzini - Java Blog
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Fernando,

I have following remarks:
- when you create a record will your array have an extra field with the deleted flag or not? if you provide such a deleted flag in the array, you can create a record and set the deleted flag to true How do you handle such a situation: don't create the record or create the new record and then set status=deleted (record will never be visible, so why create) or just ignoring the flag (why then add the flag to array)?
- same remark applies for the update method. How do you handle such a situation: don't update the record, just set status=deleted or update the record and then set status=deleted (record will never be visible, so why update) or just ignoring the flag (why then add the flag to array)?
- if you decide not to add the flag in the create method, you will have arrays of different sizes, and that will cause confusing (certainly for junior programmer, do i have to provide an array with or without the flag) and your program will lack univocity.

What is your motivation/argument(s) to add the deleted flag to the array? Because I think it has to be rock solid, because like the great and wise Roberto already said the delete flag has nothing to do with the data related to a hotel room (contractor), it's just telling you something about the state of that record (it's deleted or it's not). And adding it therefor to the data array (without having good arguments) might be graded as a poor designing decision.

Kind regards,
Roel
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Yeahh Roel..I agree with you.
Thanks for wise opinion.
Dennis Leong
Greenhorn

Joined: Jul 08, 2009
Posts: 9
Hiya Roel,
Thanks again for your thoughts on this one,
as i'm working through the Data class now, and i'd interpreted it the opposite way, and give a few reasons why.

for your remarks;
1. include flag and all data as is in a create. why not? this is your low level FileDB class that is writing data to a defined schema, not the place to be doing any validation or logic. The flag is a 1-1 per record, so why would they even bother to include it at all if it shouldn't be written (as you say).
2. same.
3. agreed, you want to be consistent.

I suppose i'm looking carefully at the Data class from the automated testing viewpoint and what it would do. I'd have assumed they'd include this flag in tests as why would they go in detail of mentioning it at all. This mixes in with other threads that talk about the fact that the GUI requirements mean that you never even use the create/delete/update (other than owner field), but this does not mean you can get away with not implementing these methods. In the same way, just because the GUI requirements don't use the flag, i would have thought it doesn't mean you can get away with not creating/updating it in your low level DB class. I had designed to use a value object that makes use of a converter from the array -> value object and back, that will ignore this flag completely, because it is never used in your business or GUI layer.

Then again, you've passed so i might change it back, (and i'm probably just annoyed as i've wasted 2 days figuring out about reading/writing unsigned bytes and getting the right charsets!!).


cheers,
D
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Dennis!!

1. include flag and all data as is in a create. why not?


So champion, if I understood correctly, you will include the status flag in your String array, in the create method, right? It's just that... why would you create a deleted record? It's like, the record is born dead! As my good buddy Roel asked, what is the motivation to add the status flag to the String array?
Dennis Leong
Greenhorn

Joined: Jul 08, 2009
Posts: 9
Hiya Roberto,

yes, i see your point it doesn't make much logical sense to do that - i'm probably just over thinking too much from the automated test program and an automatic failure viewpoint. I guess most people just hard-code a valid flag 0x00 and then write the rest of the fields for a create, and ignore completely for an update, so the arrays are matched in size.

so thanks for the clarification, i'll change it to just use the data fields. My original db file does not contain any deleted records, but i could just use a hex editor to set the deleted flag to test the 'create' method that first searches for a deleted record to replace.
thanks,
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Dennis,

Ah, someone with another opinion This will lead to some interesting valuable discussions. Let me try again to convince you of this idea being . If you add the status flag to the array of create (and other methods), you have following possibilities:
- just ignore the flag: why would you add the flag if you ignore it (together with consistency flag would be not included in any method)
- use the flag with no checks: you are able to create a deleted record, so like Roberto said it's a born dead record, so no added value at all
- use the flag with a check to see if it has the value for a valid record and if a deleted flag is in the array an IllegalArgumentException is thrown: this has no added value at all because you expect from a record that you just create to be a valid (not-deleted) one, why else would you create it?

If we think about future enhancements, we may use a rdbms (like MySQL) instead of the flat file. And if a record is deleted, it will be (probably) really deleted and so there will be only valid records and your status flag will become obsolete (and you will have to make some changes to remove it from your program unless your converter class takes care of all this clutter).

Kind regards,
Roel
Elton Kuzniewski
Ranch Hand

Joined: Dec 06, 2007
Posts: 44
Roberto Perillo wrote:
Now, RandomAccessFile is like a unanimity, everybody uses it. It is simple to deal with it and it does the job well. I think this discussion also brings up a question (and Debs, think about it): how are you going to deal with the database records? Basically you have 2 options: dealing with them directly in the .db file, or dealing with them in memory.


Hi Roberto!

In your decision to use the data base in memory, what kind of approuch did you use to instanciate all data into memory?
Some kind of singleton that is executed in the first data access?

Thanks man!


Elton Kuzniewski<br>SCJA 1.0, SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD (B&S - working on project),
<br><a href="http://www.eltonk.com.br/" target="_blank" rel="nofollow">www.eltonk.com.br</a>
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Elton,

More info about Roberto did it and how I did it (different approaches) can be found in this thread.

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Elton!

In your decision to use the data base in memory, what kind of approuch did you use to instanciate all data into memory?
Some kind of singleton that is executed in the first data access?


Yup. That's it. I decided to make my Data class a singleton, which deals with reading and locking records. For instance, in the the server mode, as long as the server is running, it is keeping the unique reference to the Data class in memory, and so records are accessed from there. You can also find some discussions about this approach in this thread or in this thread.
Elton Kuzniewski
Ranch Hand

Joined: Dec 06, 2007
Posts: 44
Thank's dude!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird question regarding db method
 
Similar Threads
B&S question about new values in updateRecord
Is checking for SecurityException in updateRecord method necessary?
Can I use another db file for optimistic locking?
recNo in Bodgitt and Scarper
Lock question!