// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
Q2. Initially a collection type is used to store matching record number. It would be nice to put the matched record number straight into array of long and return that at the end of method. But apriori, we do not know how big the array of long should be initialized to. What type of collection should i use? Set, ArrayList?
Q3. Should the Collection type be synchronized as in code? Is it necessary?
Q4a. Should i validate the criteria argument that it should have the correct number of fields? For B&S, it should have 6 (name, location, specialties, size, rate, and owner).
Q4b. If validate is necessary, the validateArgument method throws a subclass of Runtime exception, but the findByCriteria signature does not throw any exception. In this case, i will not change the signature, but document the possibility of this exception being thrown in javadoc. Is this ok?
Q5. Should i add the synchronized modifier to the findByCriteria method or should i synchronize on the cacheDB mutex?
quote:
--------------------------------------------------------------------------------
Q4b. If validate is necessary, the validateArgument method throws a subclass of Runtime exception, but the findByCriteria signature does not throw any exception. In this case, i will not change the signature, but document the possibility of this exception being thrown in javadoc. Is this ok?
--------------------------------------------------------------------------------
[Phil]: You may throw some IllegalArgumentException in that case
[Phil]: I'd use a HashMap instead of a HashSet, to avoid a useless overhead. ArrayList doesn't suit well for your purpose, IMO.
You may synchronized on "this" or on your cache. The correct answer depends on what you do elsewhere in your code.
Q1: Is code ok?
Q3. Can you explain why HashMap is better suited than ArrayList? For HashMap, i need both key and value to add an Object into the Collection. For ArrayList, i can do ArrayList.add(new Integer(recNo)). For HashMap, how do i add the matching recNo? HashMap.put(new Integer(recNo),new Integer(recNo)) ??
Q4. I am using cache which is populated when the db file was first accessed. The cache will be used for all read-write access. The db file is updated for write access. The find method is not called any other method in Data class. In this case, is it necessary to sync the find method? What do i have to consider?
Q5. My B&S assignment has long for recNo and my cache is an ArrayList. The methods in ArrayList that i use include get(int index) for readRecord, and set(int index, Object element) for updateRecord. This forces me to cast the long recNo to int. By doing so, i am effectively reducing the database storage size from 2^64 to 2^32. Would i be penalized for this design?
So that means the array of Strings passed into the find() method would only contain values for the name and location and the rest would be null?
If this were the case wouldn't it return all records since "A null value in criteria[n] matches any field value.?"
[Phil]I don't think you took this into account : "you could copy the non null criteria values in a (smaller) local array before the loop, and avoid testing for null criteria inside the loop".
Many people here have a full caching system backed by an ArrayList as you do
The only thing I can tell you from your code excerpt is that there is no synchronization at all, which is probably not enough. What does your update(), delete(), create() and read() methods as far as synch is concerned ? What you must do really depends on your design.
Q1: Is this what you mean?
Hope examiner don't find it too complex to read?
Danger, 10,000 volts, very electic .... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|