• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

criteriaFind Implementation (Clean design vs performance)

 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you could have an array of objects containing key value information if you wanted to avoid the iterator.
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used two arrays and an ArrayList to add the matched records.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic