This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique key issue.

 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roel De Nijs
Sheriff
Posts: 9790
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ixus See
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9790
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic