Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Delete() implementation

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

You have once again spared me from unnecessary work.

Thanks very much!
Matt
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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>?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic