This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Unique key issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Unique key issue." Watch "Unique key issue." New topic
Author

Unique key issue.

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
I got a problem with my unique key generator.

firstly all deleted record will be store as null.

according to the assignment deleted keys must be re-use(i assume that the generator must re-use deleted records)

I can't seem to find come out a uniq key generator to meet this requirement...

correctly I am testing if the key is less than integer.MAX value and if it contains in the records... (doesn't cater for deleted record)

I can't use if it contains and get value = null. > reason is because on start if a record is deleted retrieve from DB is deleted, I get into infinity loop.

how do you guys solve this problem?

I'm thinking on load it will just increment the unique key.. then the unique key generator is use for creation during run time. would there be an issue?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Ixus!

according to the assignment deleted keys must be re-use(i assume that the generator must re-use deleted records)


Hum... are you sure you have this requirement, champ? In my assignment, it was said that deleted entries could be reused, but this was not mandatory.

I can't seem to find come out a uniq key generator to meet this requirement...


Hum... generator?! How's the layout of your .db file? Does it explicitly include a primary key or something? It's just that you shouldn't generate a unique key. What you can do is follow what is suggested by your create and update methods. For instance, if the create method of your interface includes DuplicateKeyException, then you can verify if a field (or combination of fields) can be faced as unique. If so, then you just verify if a record with the value to be created already exists; if so, then you throw DuplicateKeyException. You can do the same for the update method.


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

Joined: Nov 03, 2010
Posts: 581

Ixus See wrote:I'm thinking on load it will just increment the unique key.. then the unique key generator is use for creation during run time.


Sounds good to me - it works, so problem solved


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

Joined: Jul 19, 2004
Posts: 5125
    
  12

You would expect a simple algorithm: take the number of records in the file (or the cache) as unique key. If you want to re-use entries of deleted records, you'll first iterate over all records in the file (or in the cache). When you encounter a deleted record, you take its unique key (the record number), otherwse the number of records in the cache/file


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

Joined: Jul 17, 2011
Posts: 160
actually I use a simple approach.. The counter will only reset when it reaches max integer.. there after I will start re-using the keys

the key generator will test for max integer and will keep increment only if it !=null...

this way I will re-use the keys ;) and don't have to waste precious run time resources looping the records.

if you wanna save time... you can set nextUnique key = recNo when you delete an record. this way the record will be instantly re-use on the next creation. (strongly advise against becos it will take some skill to implement it)

* keep it simple * you can't score more than 400 points =x
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5125
    
  12

Ixus See wrote:* keep it simple * you can't score more than 400 points =x

If you want to keep it simple, you just use records.size() as the record number for a new record. And indeed you can't score more than 400 points, but you'll never know how much you have scored on the assignment. So you can have passed with 320 or 400 (and of course anything in between )

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unique key issue.
 
Similar Threads
Dealing with Deleted Records
Bodgit ans Scraper DuplicateKeyException
which generator type to use..?
I Can still be wrong?, or no? (almost complete my assignment)
Can we write Entity Bean for a table which does not have a Primary Key defined?