• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

URLy 1.1.2 - Reading the DB

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to read the db file... yes I am very happy that I managed to did it! I can read the magic cookie, the offset and number of fields.

While reading the schema, everything is fine until I reach the maxOccupancy. I believe somehow I miss out something or misread something. The rest of the data like smokingRestriction, price and etc have wrong/incorrect value.

Data File Format

Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
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. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 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

All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII.





Can someone help me on this? Did I set the offset incorrectly?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think the problem is connected with the values of the field name length (i.e. the actual length of the String that represents the name of the field) you are using - I can't tell how you are calculating these values with the code provided, but I would recommend something like the following pseudocode to calculate the length of the field name correctly before attempting to read:



You could adapt this code to only synchronized on the RAF for a short period of time (which is a very good idea), but as I only build the schema once at the start of the application (when no-one should be reading a record), I don't worry about this. I hope this helps.

Best wishes,

Daniel
 
Shan Jun Hao
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright... something weird here. I managed to solve the question I raised earlier on. However, I don't think that's the right solution.

The problem lies with the length of my smoking restriction. In my instruction, it mentioned the field length is 1. However, I realise that I need to set it to 3, then the fields that come after smoking restriction will not display wrong output(bcos of some positioning).

I feel that this is kind of weird. Either SUN has an error in their instruction (which I don't think so) or something wrong with my logic. Can anyone advise?

I am also wondering if it got to do with:

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 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



How do I read this section?
[ August 09, 2006: Message edited by: Shan Jun Hao ]
 
Daniel Bryant
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shan,

Don't forget the two bytes indicating a deleted record will appear at the front of every record in the DB and so you will have to read the record from the file accordingly. This may help...

0. Position file pointer to location directly after the header information
1. Read record completely: length of record in bytes + 2 bytes (for deleted flag)
2. Read the first two bytes - determine if deleted
3a. If not continue to extract data from the record (do not reset the file pointer or you will read the deleted bytes again!)
3b. If deleted move file pointer to next record
4. Repeat from 1

Best wishes,

Daniel
 
Shan Jun Hao
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel thanks for the information! Yes I am able to do it correctly now!! Cheers!
 
Skool. Stay in. Smartness. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic