aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird data file and DBMain interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird data file and DBMain interface" Watch "URLyBird data file and DBMain interface" New topic
Author

URLyBird data file and DBMain interface

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2511
    
    8

I have a question about the data file. The format section include stuff saying x byte numeric bla-bla-bla (quoted below). Then it mentions the database schema.

Start of file
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, total overall length in bytes of each record
2 byte numeric, number of fields in each record

Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

Data section.
Repeat to end of file:
1 byte "deleted" flag. 0 implies valid record, 1 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information

End of file


Does those x byte information mean anything when reading the file?

Also the UB system has intent to move the system to web, which means the data file can become relational database table(s). Does anyone think about using the data "as if it were relational database"?

This comes to DBMain interface read() method (public String[] read(int recNo) throws RecordNotFoundException). How to I know which recNo the record represent? Do I use a HashMap<Integer, Object> and hardcode the Integer part as I read each row from the data file? Because if it were relational, there would be an auto-increment field which indeed represents the recNo.

If the data row is read like an object (relational DB sense), how easy is it to turn the row's object toString() method to a String array for the read() method? I'm thinking once I have the toString() method output say a Hotel object with name and location (format name,location), I split that up using the comma and store into array. But would this be too much work?
[ December 26, 2008: Message edited by: K. Tsang ]

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Before reading the entire database, of course you have to decide that the file is the right file by reading its magic cookie. The magic cookie and each record is the content in the database file that I actually read when accessing the database file. The schema tells you how many bytes that you must skip to read the first record until the end of the record.


Also the UB system has intent to move the system to web, which means the data file can become relational database table(s). Does anyone think about using the data "as if it were relational database"?


I just ignored it.

Regarding the number of records is based on the order they appear. The first record is numbered by one and so on.


If the data row is read like an object (relational DB sense), how easy is it to turn the row's object toString() method to a String array for the read() method? I'm thinking once I have the toString() method output say a Hotel object with name and location (format name,location), I split that up using the comma and store into array. But would this be too much work?


I created the Hotel object that represents a single hotel object and provide the method toArray() to convert each field to array of String

Before doing your assignment, you better try to read all the value contained in the database file.Don't forget to make the backup of the database file.

Good Luck !!!


Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
I have a question about the data file. The format section include stuff saying x byte numeric bla-bla-bla (quoted below). Then it mentions the database schema.

quote:
Start of file
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, total overall length in bytes of each record
2 byte numeric, number of fields in each record

Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

Data section.
Repeat to end of file:
1 byte "deleted" flag. 0 implies valid record, 1 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information

End of file



Does those x byte information mean anything when reading the file?

Yes. You have to read the data file in the same order as mentioned in the requirement. i.e you have to read first 4 bytes which is a magic cookie etc.


Also the UB system has intent to move the system to web, which means the data file can become relational database table(s). Does anyone think about using the data "as if it were relational database"?

In my implementation, Data class will delegate the method calls to appropriate DBReader methods. i.e a new class file which is specific to the particular database can be added safely without affecting the existing implementation. For example, if oracle or sql or unknown database is used in future, all we/developers need to write a database specific reader and add it to project. For example, I have a FileReader which provides an implementation which is specific to Flat File.

This comes to DBMain interface read() method (public String[] read(int recNo) throws RecordNotFoundException). How to I know which recNo the record represent? Do I use a HashMap<Integer, Object> and hardcode the Integer part as I read each row from the data file? Because if it were relational, there would be an auto-increment field which indeed represents the recNo.

If you read the file in the specified order, you will know how many records in your database. Then you can decide easily.
My requirement states that the record will be marked when it is deleted and it should not be deleted from database. New record will be added in last row. Update will also be in the same location. Hence I assume, the record structure will not be changed and it will be in the same order. In this case, logical Record No will be helpful. I have used a map to store the record no as well as its location in order to traverse the db. As I already mentioned in the previous paragraph, the corresponding DBReader will take care of how to get all info from DB if it were a relational db.



If the data row is read like an object (relational DB sense), how easy is it to turn the row's object toString() method to a String array for the read() method? I'm thinking once I have the toString() method output say a Hotel object with name and location (format name,location), I split that up using the comma and store into array. But would this be too much work?

It's upto you. .

[ December 26, 2008: Message edited by: K. Tsang ]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2511
    
    8

Thanks Jeffrey and satishkumar. Now I got this problem/query: since the data is read as bytes, and I'm using RandomAccessFile to read the file, how can I read characters or strings so that it output ASCII? Currently I'm using readBtye() for everything. Hence my field name display a series of number instead of the actual name in English.

magic cookie value = 0011
total overall length of each record = 000-97
number of fields in each record = 07
length of field name = 04
field name = 11097109101 //should be "name"
field length = 064
delete flag = 0
data = 8 //blablabla

I'm using a for loop like for(i=1;i<=4;i++) raf.readBtye();
If I use readChar(), I get weird output like ? and @.
How many bytes make up a character or letter? Do I loop 4 times to get the field name, hence loop 64 times to get the data?

After this if I loop again will I be looking for "length of field name"?

Also the magic cookie value identifies it's a data file, how can I tell that 0011 is indeed a data file?
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Thanks Jeffrey and satishkumar. Now I got this problem/query: since the data is read as bytes, and I'm using RandomAccessFile to read the file, how can I read characters or strings so that it output ASCII? Currently I'm using readBtye() for everything. Hence my field name display a series of number instead of the actual name in English.


You can create a String object using new String(byte[] bytes) method. The number of bytes you must read is depend on the database schema.

magic cookie value = 0011


The magic cookie is 4 bytes. remember from the basic programming language, 4 bytes is integer right, you can use the randomAccessFile's readInteger() method to read it. Try to read the two bytes using readShort() method. 2 bytes means it is a short.

Good Luck !!!

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2511
    
    8

Originally posted by Jeffry Kristianto Yanuar:


The magic cookie is 4 bytes. remember from the basic programming language, 4 bytes is integer right, you can use the randomAccessFile's readInteger() method to read it. Try to read the two bytes using readShort() method. 2 bytes means it is a short.

Good Luck !!!

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)


Ah this makes my code more direct

Then whenever I call the read() method, it will read the file header (cookie value etc) before I get to the actual data. Is this true?

How to jump to the starting position without first reading the header info or simple without loop n times where n is the number of records? RandomAccessFile seek() or skipByte() method needs that magic number. I thought of using the recNo * recordLen where recordLen is the 2nd 4-byte number plus the offset after reading the header info. But doesn't quite get there.

Or should I just put everything into a hashmap in some private method and fetch the value of that hashmap for the read method? But then I will regenerate the hashmap everytime?
[ December 26, 2008: Message edited by: K. Tsang ]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2511
    
    8

Never mind, I figured it out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URLyBird data file and DBMain interface