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.
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
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
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 )