*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Delete and Create Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Delete and Create" Watch "Delete and Create" New topic
Author

Delete and Create

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Hello Guys..

currently I am using hashmap/cache to store the recordNo with data ...

on start up will load the hashmap with redNo and data

and periodically I would save the entire hashmap data back to the .db file.. I will overwrite everything.. that means if another server load up.. it might have a different recNo for each record.

May I know for delete...

Should I remove the recNo totally from the hashmap by using .remove function or should I assign it as null?

2ndly for create..

am I suppose to re-use the redNo or I can just keep increase it...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Another server can NOT be running according to the instructions:
You may assume that at any moment, at most one program is accessing the database file;


For a deleted record I assigned null to the value of the according map entry. When creating records I re-used record numbers, but that's not a must requirement. So just increasing your record number with each create is just as good.


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

Joined: Jul 17, 2011
Posts: 160
Roel De Nijs wrote:Another server can NOT be running according to the instructions:
You may assume that at any moment, at most one program is accessing the database file;


For a deleted record I assigned null to the value of the according map entry. When creating records I re-used record numbers, but that's not a must requirement. So just increasing your record number with each create is just as good.


If i remove the record from the map/cache and & I only save all the record to .db periodically. (at interval timing)

won't all the recordNo changes when I restart the server and plus there is implication on the client side too.

would I be penalize marks for this?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

If you periodically save records and you'll be deleting complete entries, I'm curious how you will create uniqe record numbers for new record numbers. Because the most logical would be to use the size of the map, but you clearly can't use that one if you delete entries for the map
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Roel De Nijs wrote:If you periodically save records and you'll be deleting complete entries, I'm curious how you will create uniqe record numbers for new record numbers. Because the most logical would be to use the size of the map, but you clearly can't use that one if you delete entries for the map


Would this work?

[edit] do not post complete snippets of actual code
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Yes, that would work. But would records.size() not be a whole lot simpler (instead of 15 lines of code)
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Roel De Nijs wrote:Yes, that would work. But would records.size() not be a whole lot simpler (instead of 15 lines of code)


lf I use record.size() I won't be able to to do the following

1) re-use the keys that has been deleted

2) Imagine if a record has 30 elements, record 3 is deleted, and a new record is added...
the new record will over write element 30th!!! Serious bug ;)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:Yes, that would work. But would records.size() not be a whole lot simpler (instead of 15 lines of code)


I have a map with my records, I reuse deleted records, and that's what I do. I have a method that generates my record number when creating a new record. It does the following:



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

Joined: Jul 19, 2004
Posts: 5131
    
  12

Ixus See wrote:lf I use record.size() I won't be able to to do the following

If you decide to delete a complete entry, you won't be able to do that. But on the other hand, if you decide just to put null as value, you can simply re-use record numbers and use the map size as key for new records. But both approaches are valid and just fine.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
do i need to lock in the create method?

2 thread attempt to create a record using the same recNo. (yes i did sychronice the create unique key method) but should I just be safe? since the create unique key method doesn't add the key into the key list but just generate.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

you can't lock (as in calling lock-method) in the create-method, because you simply don't have a record number yet.

you have to make sure your Data class is thread-safe, so you can take the easy road and synchronize all methods which will guarantee thread-safety (if all threads are using same instance of the Data class). Or you could work with synchronized blocks/methods (or the new concurrency apu) when needed to have a more performant solution.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
well i created a test script.. it will create 100 threads to add new record.. so far there is no problem.. 100 records are added successfully with no jump in number.

does this means I'm safe? =X

really wish someone can take a look @ my data.java and give me a peace of mind =x

[edit] removed quote of a complete post (has no added value)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

in ScjdFaq you'll find a link to an excellent test case for your Data class. And you can always do a bit of thinking yourself and think of possible problems when your program is used with multiple threads. And you can easily simulate multiple threads when you use Thread.sleep to put thread-x to sleep and give thread-y a bit of cpu-time.

Ixus See wrote:really wish someone can take a look @ my data.java

That's against Oracle's policy (just as providing real exam questions in publci), so that will not happen. That's just the point of being a developer: being able to deliver a bug-free application based on a given set of instructions.
 
Don't get me started about those stupid light bulbs.
 
subject: Delete and Create
 
Similar Threads
NX: URLyBird 1.3.2. Question about the process of booking.
lockCookie clarification request ?
delete in B&S
B&S Database Design Structure
URLyBird: Just started, have a few questions