Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DB: Keeping track of deleted records

 
Mickey Mulligan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, newbie Java programmer needing help here!

There's a boolean in my datafile that marks whether a record is deleted or not. When my app starts up it scans the list of records, and keeps a list of the ones that haven't been deleted.

I'd like to keep another list of the ones that have been deleted (just an index for their position in the file, eg. Record 1, 2, 3 etc.) This is so my app will know the records it can overwrite and only have to search for them once on startup. The deleteRecord method will add records to this list as they're deleted.

This sound like a good idea? I presume it's reasonable to overwrite deleted records in the data file.

So I'll have two LinkedHashSets; one containing the record index numbers, the other containing deleted record index numbers. Then one ReadWriteLock to cater for both of these.

Sound like a good design?
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends, but it looks redundant to me.
If you are not caching the records, there is no reason for keeping two sets, since you can use set.contains() and !set.containg().
I am using one Queue for deleted records only.
 
Mickey Mulligan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found my solution: A TreeSet<Long> to hold my record numbers and an HashSet<Long> to hold the numbers of deleted records.

So, say my collections hold:

TreeSet: 0, 1, 2, 3, 4
HashSet:

I delete record 3 and now they look like:

TreeSet: 0, 1, 2, 4
HashSet: 3

Now, instead of searching the TreeSet to find an empty space, I can pop a number out of the HashSet and use that. Excellente.
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it depends on your overall approach, i.e caching the records or not.

I've personally gone for a simple approach and just loop through the records until I find a record that has the deleted flag.

The thing to remember when doing this assignment is to keep it simple, only implement what is required, you don't get any extra points for additional functionality, and could even lose yourself marks!

Jason.
 
Xiao Di
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jason, just implement what is required, and buy yourself some rest.

During runtime I do not reuse any deleted records. All new records are appended to the end of data file. I only purges deleted records from data file at startup, and then build up a full cache in memory.

This database is not a SQL database, so the record number is very important. Because a client may keep a record number and access it later again. Based on this concept, I do not even allow a client to change the key of a record (I assume name and location are key fields).
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comment for my create method says


Creates new record in the database (Possibly reusing a deleted entry)


I interpreted this to mean that we should reuse record locations that are marked for delete so that you don't unnecessarily increase the size of the database.

Regarding the client, I don't think the record id should be a problem as it shouldn't know about the deleted records, and if you implement delete functionality in your GUI, deleted records could be handled by the RecordNotFoundException.

Jason.
[ March 13, 2006: Message edited by: Jason Moors ]
 
Mickey Mulligan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm happy enough with my solution, I think it can be explained through the code quite easily. I'm not caching records, just reusing deleted records on the fly.

Another question though, somewhat unrelated:

In Java 1.5, is there a quicker/more efficient/better-looking way of moving elements from a list into an array than this:



I can't call arrayList.toArray() there, as 'array' is an array of primitives. Do I need that count variable there, or is there some hidden trick I'm unaware of?
[ March 14, 2006: Message edited by: Mickey Mulligan ]
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you still need to use the loop, but you can use the Autoboxing functionality so you don't need the shortValue() methodas the autoboxing will will convert to primitive type.



[ March 14, 2006: Message edited by: Jason Moors ]
[ March 14, 2006: Message edited by: Jason Moors ]
 
Mickey Mulligan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, that works a treat.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic