Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flag Bytes

 
Kedarnath Bhagwat
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Data Section for the file URLyBird

Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF 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


When I create a new record do I need to write the value for bytes into File ?

Without writing these Bytes , the program worked fine .

After I wrote the bytes 00 once and then 0xFF I saw the data was misplaced .

PLease guide me on whether I should write it or not .
 
Carlos Morillo
Ranch Hand
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you are using readUnsignedByte() since 0xFF is 255 as an int and the maximum value of a signed byte is 127.

Hope this helps,


Carlos.
 
Kedarnath Bhagwat
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carlos .

I am using the RandomAccessFile .
I will have to use DataInputStream .
But then I won't be able to use methods such as seek .

My question is , first of all do I need to write into file a byte indicating whether the record is valid or not ?
 
Naveen Narayanan
Ranch Hand
Posts: 114
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kedarnath,

Probably the question that arises here is
* You are supposed to confirm that newly created record is valid one,
So, How do you make sure that …
 
Matheus Mendes
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you must write this byte.

I mean, assume that you created a new record, how your application will know that it is a valid record ? It will check this field, and the field will be blank ( assuming that you didn't put the valid or invalid byte on this field), how does your application will check it ??

Or

You could just write on this field if it was an invalid record, BUT you must justify this decision on your choices.txt and tell why you did things this way.
 
Naveen Narayanan
Ranch Hand
Posts: 114
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Matheus,
Multiple options that may arise here are

  • If you have a null padded (0x00) db file, you might escape, without writing a valid- flag especially for a newly created record.
  • You have a space padded (0x20) db file; writing a valid-flag is mandatory.
  • If creating a new record, reusing a deleted one, here also you will overwrite the deleted-flag with a valid-flag.

  •  
    Matheus Mendes
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thats a good point

    If creating a new record, reusing a deleted one, here also you will overwrite the deleted-flag with a valid-flag.


    I already saw some friends lost points because they don't reuse the deleted records...
     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy, friends!

    Naveen Narayanan wrote:If you have a null padded (0x00) db file, you might escape, without writing a valid- flag especially for a newly created record.


    Hum... I'm not sure if I agree with that. I mean, this value has to be written under any circunstance. Remember that other applications that the guys from URLyBird and B&S use also use the same .db file that we do.

    Matheus Mendes wrote:I already saw some friends lost points because they don't reuse the deleted records...


    Hum... well, the spec doesn't really say that we must reuse deleted entries. The comments of the create() method suggest that we can reuse deleted entries, so it's pretty likely that your friends lost points due to other reasons.
     
    Kedarnath Bhagwat
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Naveen , Matheuse and Roberto .

    Reading your replies following questions come to my mind

    1:-Is soft Delete allowed ? Am I expected only to set the flag while deleting the record ?But specifications say 'Deleting record makes the record number and associated disk storage available for reuse" So as I understand I need to physically delete it .

    2:-Flag is one byte and when I write 0xFF , placement of data gets disturbed when I read that record.
    Even while declaration
    byte b = 0xFF
    I see error , "Possible Loss of Precision "
    If I print it then it prints as -1 and not 255 .
    I am not sure if one byte space in file can accommodate 0xFF


    Please advice me on how should I proceed on this .









     
    Naveen Narayanan
    Ranch Hand
    Posts: 114
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roberto,
    I mean, this value has to be written under any circunstance

    Yes, probably writing that value is secure, otherwise its a bit vulnerable alternative
    the spec doesn't really say that we must reuse deleted entries

    Fair enough, spec says "possibly reusing a deleted entry" ... , choice is on the developer
    Yes Kedarnath,
    So as I understand I need to physically delete it

    If you do that, you might have to go for these things
    * wipe out a record from file.
    * push all the following records, 1 record up.
    Why don't think like just marking a record with a flag, so that you can treat it as a deleted-one and possibly later
    reuse for a new record-creation

     
    Kedarnath Bhagwat
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Naveen for your help .

    I still not have understood how can I write the unsigned byte 0xFF to file If I need to write 0xFF to indicate that record is soft deleted.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic