• 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

B&S How to read numeric values from database file

 
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
In the beginning of my database file i have the following:
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

In order to read this from the file I'm using a RandomAccessFile in the following way:

int magicValue = raf.readInt();
int recordLength = raf.readInt();
short fieldsInRecord = raf.readShort();

Should I instead read the specified number of bytes? Or is it ok to use these "shortcuts" ?

Thanks
 
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You and I have the same problem. Did you already solved that problem?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

It is depends on your design. You can use anything but do not forget to state the reason of using in the document. But in my opinion, reading specified no. of byte via readXXX() is not the good way. Because if the structure of database is changed, you program will be immediately unusable.

Hope this help.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

reading specified no. of byte via readXXX() is not the good way. Because if the structure of database is changed, you program will be immediately unusable.



How you read that bytes? I assume that the structure of file from the start fo file until the schema section will never change. It is not written in my instruction that the strucure of database will change.

thanks

Jeffry Kristianto Yanuar
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2)
 
Soroj Margun
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeffry,

How fast !!! You're right, the spec. is not say about structure changing at all. But spec. said, your program should be easily to maintain by a junior programmer. Using readXXX() may not be easily to find by a junior when your program behave abnormal. Mine solution for reading structured database is using config file. With the configable file, my junior will easily check and change it to make it compatible with the current database structure if changing is occured.

Cheers
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution is not bad either, but when I was a junior programmer, I knew what the readXXX() method does. If I couldn't know that method, I could easily find that method on Java API library.

the term "junior programmer" has more than one definitions. Right?
 
Soroj Margun
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeffry,

Absolutely yes. But my point is, changing the code everytime database structure changed (especially a minor one like changing length of the field) may not be a good choice. With config file, maintainer can easily understand and possible to change program's behavior at run-time. Changing code is a little bit more complex (and sometime impossible or illegal if using binary lib).

Just share idea.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How long you finish your assignment?
 
Soroj Margun
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About 2 months of a few hours after day-work and no-busy weekends.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do yo have a particular reason for taking SCJD ?
 
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

Originally posted by Soroj Margun:
But my point is, changing the code everytime database structure changed (especially a minor one like changing length of the field) may not be a good choice.


Ahh, but it is a choice, and as long as it can be justified then it is fine.

Personally I think that changing the length of a field is not such a minor change. If it were just a minor field (e.g. cookie value length) then you might be able to get away with just writing a quick and dirty program to read the old file and write the new file without caring about contents. But most other fields (e.g. bytes in a record, number of fields in a record) would require some sort of conversion program - either a program that simultaneously understands both old and new formats, or 2 programs (one that writes records to an interim file (CSV?) and one that reads that file and creates the new database). Either way there is a decent amount of work involved. So changing something like a readShort to readInt is (in my opinion) the least of the problems.

As for my personal opinion on the original question - I would certainly prefer to see code that used the "shortcuts" than having to read code that essentially duplicates the J2SE standard library. (Isn't there a clause in the instructions about not writing your own code when there is a standard method that does the same function?)

Regards, Andrew
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew, the instructions said that there is 2 byte flag with each record. 00 for valid record and 0x8000 for deleted record. The issue is, 0x8000 is integer (4 byte).

Do I have to cast that int to short (2 byte) and document my assumptions? My assumptions is 0x8000 must be cast to short which value will become
-32768. This value is just for mark, not for calculation.

Thanks

Jeffry Kristianto Yanuar
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2)
 
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
Jeffry

This is a different question than asked by the original poster, and as such should really be in it's own thread. You might want to read "Use One Thread Per Question".

I thought you had your answer in your post "magic cookie and 0x800 flag doubt"

For more reading and ideas, take a look at:
  • NX: 0x8000?
  • NX: [Help] read & write flag on database


  • Regards, Andrew
     
    Jeffry Kristianto Yanuar
    Ranch Hand
    Posts: 759
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Andrew, Those 2 thread is old thread. Can I access the other old thread?
     
    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
    I'm not entirely sure I understand your question, but here goes ...

    When looking at the SCJD forum landing page (or any of the forum pages) you should be able to see a pull-down list of how many posts you want to see. Many people keep it to the default of "Show topics from last 5 days", but you can set it to show more topics if you so desire - even set it to show all topics that have ever been posted to this forum.

    There is also a "Search" option on the line immediately below the "Post New Topic" button. Using the search feature you can find posts from anytime in the past in any forum.

    If I have answered a question you have not asked, I apologize.

    Regards, Andrew
    reply
      Bookmark Topic Watch Topic
    • New Topic