File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Delete() implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Delete() implementation" Watch "Delete() implementation" New topic
Author

Delete() implementation

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Hi everyone,

I currently have this method implemented such that it writes " " for the length of the record to be deleted. However, I am contemplating truncating it instead so I am hoping to gather a few opinions as to whether or not just over-writing the record with " " satisfies what the interface calls for: "Deletes a record, making the record number and associated disk storage available for reuse." I could make an argument for and against using " " to just over-write. Thoughts?

Thanks.
Matt
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Some people just change the deleted flag and that's it, other ones just change deleted flag and write an empty record. That's simply up to you and what you prefer.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 143

I would just write the same record back to the file with the deleted flag set to 1 (instead of 0). The deleted flag is present in every record, so why not just use it? This way you don't have to deal with things like reusing deleted records and stuff.


SCJP, SCWCD, SCJD
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

I myself used a record cache (Map<Long, Room>), so when a record is deleted, I first replace its entry in the map with null. When the application finishes and the records are written back to the file, for each entry whose values are null, I just write the deleted flag for each particular record.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Guys,

You have once again spared me from unnecessary work.

Thanks very much!
Matt
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

I worked directly with the file rather than using a cache. For deletes, I checked for the existence of the record and if I found it, I set the flag. For saves, then, I reused record space in the file where possible and padded to the end of the record to overwrite any debris.


OCMJD 6, OCPJP7, Baroque Potion, G+
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roberto Perillo wrote:I myself used a record cache (Map<Long, Room>), so when a record is deleted, I first replace its entry in the map with null. When the application finishes and the records are written back to the file, for each entry whose values are null, I just write the deleted flag for each particular record.


Is the key in the record cache the record number? If it is, then this seems strange that a Long would be used as the key.

All of the assignments use Integer (or more precisely int) as the record number - see list of unlock methods here. For example:

Based on this, shouldn't your record cache instead be an Integer as the key i.e. Map<Integer, Room>?

SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Sean Keane wrote:All of the assignments use Integer (or more precisely int) as the record number - see list of unlock methods here.

I think you definitely need a pair of glasses (or maybe you just have to go and put the assignment aside for a bit )
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Roel De Nijs wrote:I think you definitely need a pair of glasses (or maybe you just have to go and put the assignment aside for a bit


That's my good buddy Roel!
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Apologies Roberto, I knew I should never have doubted you! I blame it on sleep deprivation

Of course this was my error. The full list of unlock methods listed on the FAQ page is:

Version Signature of Unlock method
1.1.1 public void unlock(int recNo, long cookie) throws RecordNotFoundException, SecurityException;
1.2.1 public void unlock(long recNo, long cookie) throws SecurityException;
1.2.2 public void unlock(long recNo, long cookie) throws SecurityException;

1.3.1 public void unlock(int recNo) throws RecordNotFoundException;
1.3.3 public void unlock(int recNo) throws RecordNotFoundException;


As can be seen, when I wake up , two of the methods actually take long as the record number.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete() implementation