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

class Data, DataInfo, FieldInfo

 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new here. I just got the assignment. Here is questin for Checked class Data, DataInfo, FieldInfo.
I think the code that needs to be changed just implement lock(), unlock() and criteriaFind(). I don't need to make any other changes on class Data, DataInfo, FieldInfo.
Is it correct?
I think Some change maybe need to be made. For example:
1. we can delete
final static char sc = 'A';
The code never use it. for future use?
2. can I delete synchronized from readRecord()?
3. does writeRecord need to add synchronized?

I decide to not change above 1., 2., 3.
Is it OK?
Thank you for any feedback.
HAX
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct I would not change 1,2,3. But I would try to compile the data class with the -deprecated argument to see if there are any deprecated calls in the code, that you might need to fix.
Hint, Hint.
Mark
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replay.
I did it.
Today I implemeted lock, unlock and criteriaFind.
I use vector for lock a file(record = -1). But some one uses an object to lock a file. It seems have some problem. If we have a lot of clients to access the database, It seems it will spend a lot of time to lock a file.
for vector design, first put all unlocked record into vector by using a loop; then wait locked record until they release their lock by a loop. is it correct solution?
for criteriaFind method, I overloaded find method.
but i don't know why find method need synchronized?
please help me!
java2de
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, criteriaFind is a separate method. Not like find at all. so you don't want to overload that method.
second. For lock file, the only time lock is called with -1 is when the server is about to shutdown, and you want to close the database. Your algorith of getting unlocked records, locking them and waiting for other records that are locked by clients to be unlocked is good. Try to use the ArrayList object instead of Vector.
Hope that helps
Mark
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thanks,
first, criteriaFind is a separate method. Not like find at all. so you don't want to overload that method.
yes! I agree. But after get columnName and ColumnVaule from String criteria(I store them into
a Hashtable), I need to call a function. As this function looks like find method, I overload it. please see following code:
public DataInfo[] criterFind(String criteria) throws DatabaseException {
...
return find(pairsTable);
}
}
private synchronized DataInfo[] find(Hashtable pairsTable) throws DatabaseException {}
This is why I overload find. I don't know why use
synchronized ?
second. For lock file, the only time lock is called with -1 is when the server is about to shutdown, and you want to close the database.

Yes! I agree. Normally lock file only happened when you want to close the database or you want to change the database structure(we have no requirements for it).
Your algorith of getting unlocked records, locking them and waiting for other records that are locked by clients to be unlocked is good. Try to use the ArrayList object instead of Vector.
Yes! ArrayList is fast then Vector. the main difference between them is that Vector is synchronized. because we use Vector m_lock as lock. see code:
public void lock(int record) throws IOException {
synchronized(m_lock){...
}. it seems

Hope that helps
Mark
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thanks,
first, criteriaFind is a separate method. Not like find at all. so you don't want to overload that method.
yes! I agree. But after get columnName and ColumnVaule from String criteria(I store them into
a Hashtable), I need to call a function. As this function looks like find method, I overload it. please see following code:
public DataInfo[] criterFind(String criteria) throws DatabaseException {
...
return find(pairsTable);
}
}
private synchronized DataInfo[] find(Hashtable pairsTable) throws DatabaseException {}
This is why I overload find. I don't know why use
synchronized ?
second. For lock file, the only time lock is called with -1 is when the server is about to shutdown, and you want to close the database.

Yes! I agree. Normally lock file only happened when you want to close the database or you want to change the database structure(we have no requirements for it).
Your algorith of getting unlocked records, locking them and waiting for other records that are locked by clients to be unlocked is good. Try to use the ArrayList object instead of Vector.
Yes! ArrayList is fast then Vector. the main difference between them is that Vector is synchronized. because we use Vector m_lock as lock. see code:
public void lock(int record) throws IOException {
synchronized(m_lock){... m_lock.add();...
}. it seems ArrayList also can be used?

thanks
java2de
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually here's where we get to the point where my submission had something different, and this is one of the areas that I think I might have lost the points. I created a private method called

this is similar to your overloading of find. I think they are both similar. I just wonder if I had created a SearchHelper type class that could take a reference to the instance of the Data Class and the Hashtable or should have been HashSet, and finds matches, and then returns the DataInfo[] array.
This would have kept those two private methods that I added to the Data class.
Not sure, but looking back, it seems cleaner.
What does everyone else think?
Mark
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thank you for your replay,
I think we don't need any help class for find
method( your checkRecordForMatchingCriteria).
I think it is simple. do like below in checkRecordForMatchingCriteria:
for (r = 0; r < description.length ; r++){
Check names in hashtable match column names by using get(description[r].getName()).
...
}
if match number < hashtable size {
return null. because it is wrong format.
}
for (r = 1; r <= recordCount; r++) {
do a match;
}
that's done.
I think lock/unlock without Client ID will
causes a record locked by a client and unlocked
by the other client. is it corret.
by the way, in your code, you did not use synchronized. but find method uses it. could you
help me explain it.
thanks,
java2de.
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connect above message!
match number will not > hashtable size. because
hashtable can not using duplicated name key.
for a criteriaFind that may include same column name like:
"Origin = 'SFO',Origin = 'xxx'. has been checked
before adding a data into hashtable.
java2de
 
Forrest Xu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about If the client shutdown his machine but
locked a record on server side? this record seems
never been unlocked, unless server side can check
the client at a specific time to check if it is still alive.
does our assignment need to solve this problem?
can any one help me on it?
thanks,
java2de
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about If the client shutdown his machine but
locked a record on server side?

Check out the Unreferenced interface.
As far as synchronization is concerned, I am not changing any data or instance variables that another client might be using. So I don't need to worry about thread safety in this case. Hence no Synchronization.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic