File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes criteriaFind Implementation (Clean design vs performance) 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 "criteriaFind Implementation (Clean design vs performance)" Watch "criteriaFind Implementation (Clean design vs performance)" New topic
Author

criteriaFind Implementation (Clean design vs performance)

John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
In criteriaFind(String criteria) method of Data, I use a SearchCriteriaBuilder class to parse criteria and to store the requested field indexes and the corresponding field values in the Map, and return this Map to criteriaFind(). Subsequently, in criteriaFind() method in Data, for each record in the database, I iterate through the Map to find matching records. This works fine, but I was thinking of the performance issues, since I am getting a new iterator for the Map for every record.
To address the performance issue, I considered using two arrays (one for field indexes, one for values) instead of a Map, such as in code:

This looks much simpler and cleaner than dealing with map and iterators, but the criteria is clearly a map, not two arrays, and I guess I could be penalized for the "two arrays" approach.
How did you guys handle this?
Thanks,
Eugene Kononov.
Robin Underwood
Ranch Hand

Joined: May 01, 2002
Posts: 117
I think the original approach sounds better. I haven't finished my project yet, but my searching sounds similar to your original approach except that I'm using a HashSet. I don't think a new Iterator for every record sounds bad. But I always choose readibility over performance, until performance becomes a problem.
Robin Underwood
Ranch Hand

Joined: May 01, 2002
Posts: 117
I guess you could have an array of objects containing key value information if you wanted to avoid the iterator.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I used two arrays and an ArrayList to add the matched records.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

I used two arrays and an ArrayList to add the matched records.

Yes, that's what I had in mind as an alternative to HashMap, but again, the question is if Sun will mark it down, since the criteria is really a hashmap, not two arrays.
Eugene.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I never used HashMap here. I don't know what you mean by "criteria is really a HashMap". So Sun will not mark it down if you use couple of arrays.
[ June 20, 2002: Message edited by: Sai Prasad ]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Sai posted:
I don't know what you mean by "criteria is really a HashMap".

Sai,
What I mean is the Sun requirement to use known solutions to known problems. Our criteria is a construct known as a "Map":
Carrier->SpeedyAir
Destination->Mumbai
If you use two arrays to hold this data, you misrepresent that data intrinsic structure:
Array1 Array2
------ ---------
Carrier SpeedyAir
Destination Mumbai
To make this mispresentation more dramatic, one could also use a stack to hold the same data:
Carrier
SpeedyAir
Destination
Mumbai
Or a linked List:
Carrier->SpeedyAir->Destination->Mumbai
If I were the grader, I would penalize all the above data structures, except the natural one, a Map.
Eugene Kononov.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Eugene,
I understand your concern. I didn't have to use the Map for criteriaFind even though the criteria is nothing but name/value pair. Here is the abridged version of my implementation:
1) You get a String instance with Sun dictated delimiters
2) You use a String tokenizer and store the criteria values in a String[]. The length of this array is equal to the number of columns in the database and the column index matches the index of this array
3) When you read each row, match the index of the String[] with the database column index, if there is a value, match it with the database value and populate the ArrayList with DataInfo object.
If you feel strong about using HashMap instead of array index, use it and document it. No one will be able to guess accessor's judgemet.
[ June 20, 2002: Message edited by: Sai Prasad ]
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
I agree with Sai. As the db fields are ordered, I use only one array of values. If a field is missing in the criteria the corresponding position in the array is set to null.
I don't think the criteria is a Map. As it is used only internally in a method, this criteria is whatever we need to fulfill the request cleanly and with good performance.
If you want a good performance gain, don't compare strings:
- Convert the criteria values to bytes
- Don't call getRecord, as it creates a string for each value. Construct your own getRecord which returns a list of byte arrays, one per field
- Perform byte array comparisons
 
wood burning stoves
 
subject: criteriaFind Implementation (Clean design vs performance)