site_archiver@lists.apple.com Delivered-To: webobjects-dev@lists.apple.com Thanks Arturo, import com.webobjects.foundation.*; import com.webobjects.eocontrol.*; import com.webobjects.eoaccess.*; On 01/02/2005, at 14:57, Arturo Pérez wrote: On Jan 31, 2005, at 9:33 PM, Marek Wawrzyczny wrote: Can anyone point me in the right direction on this? public static Number objectCountWithFetchSpecification( EOEditingContext editingContext, EOFetchSpecification fetchSpecification) { if (fetchSpecification != null) { EOFetchSpecification rawFetchspecification; if ((results != null) && (results.count() == 1)) { NSDictionary row = (NSDictionary) results.lastObject(); return (Number)row.objectForKey(attribute.name()); } } return null; } Marek Wawrzyczny software engineer --------------------------> ish group pty ltd http://www.ish.com.au 7 Darghan St Glebe 2037 Australia phone +61 2 9660 1400 fax +61 2 9660 7400 _______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/site_archiver%40lists.... To be absolutely honest I have never used synchronized keyword in this way before. I will still create the "count" attribute dynamically, otherwise I suspect there might be a risk of a deadlock if the attribute was to be added to two different entities simultanously. So, the given entity would be locked for the duration of addition/fetching count/removal of the "count" attribute. /** * This class impements methods that allow us to count the number of * rows for a user defined fetch specification. * * <p>The count is performed directly on the database source without having to load * them into memory.</p> * * <p>Original code by Eric Noyau. This code is based on Eric Noyau's Bugtracker application.</p> * * @author Eric Noyau * @author Marek Wawrzyczny * @version 1.1 */ public class EOFetchCounting extends Object { /** * Returns the number of rows metching the fetch specifivation. * @param editingContext - editing context to perform the fetch in * @param fetchSpecification - the fetch specification */ public static Number objectCountWithFetchSpecification( EOEditingContext editingContext, EOFetchSpecification fetchSpecification) { boolean hasCountAttribute = false; EOFetchSpecification rawFetchspecification; EOEntity entity; EOQualifier schemaBasedQualifier; EOAttribute attribute; NSArray results = null; if (fetchSpecification != null) { try { rawFetchspecification = (EOFetchSpecification)fetchSpecification.clone(); } catch (Exception e) { System.out.println("\nException in objectCountWithFetchSpecification :\n"+e); return null; } entity = EOModelGroup.defaultGroup().entityNamed(rawFetchspecification.entityName ()); // Synchronize access to the entity object, the "count" attribute should 'live' only for the // duration of the fetch. synchronized (entity) { schemaBasedQualifier = entity.schemaBasedQualifier(rawFetchspecification.qualifier()); attribute = objectCountAttribute(); try { entity.addAttribute(attribute); } catch (Exception e) { NSLog.out.appendln("FetchCounting: Exception while adding attrib " + e); } try { rawFetchspecification.setQualifier(schemaBasedQualifier); rawFetchspecification.setRawRowKeyPaths(new NSArray(attribute.name())); results = editingContext.objectsWithFetchSpecification(rawFetchspecification); } catch (Exception e) { NSLog.out.appendln("FetchCounting: Exception while performing fetch " + e); } finally { entity.removeAttribute(attribute); } } if ((results != null) && (results.count() == 1)) { NSDictionary row = (NSDictionary) results.lastObject(); return (Number)row.objectForKey(attribute.name()); } } return null; } /** * Returns the number of rows metching the fetch specifivation. * @param editingContext - editing context to perform the fetch in * @param entityName - the entity to perform the count on * @param fetchSpecification - the fetch specification * @param bindings - the fetch bindings */ public static Number objectCountWithFetchSpecificationAndBindings( EOEditingContext editingContext, String entityName, String fetchSpecName, NSDictionary bindings) { EOModelGroup modelGroup = EOModelGroup.defaultGroup(); EOFetchSpecification unboundFetchSpec; EOFetchSpecification boundFetchSpec; unboundFetchSpec = modelGroup.fetchSpecificationNamed(fetchSpecName, entityName); if (unboundFetchSpec == null) { return null; } boundFetchSpec = unboundFetchSpec.fetchSpecificationWithQualifierBindings(bindings); return objectCountWithFetchSpecification(editingContext, boundFetchSpec); } /** * Returns the "count" entity attribute to perform the fetch on. * @return "count" entity attribute */ private static EOAttribute objectCountAttribute() { EOAttribute objectCountAttribute = new EOAttribute(); objectCountAttribute.setName("p_objectCountAttribute"); objectCountAttribute.setColumnName("p_objectCountAttribute"); objectCountAttribute.setClassName("Number"); objectCountAttribute.setValueType("i"); objectCountAttribute.setReadFormat("count(*)"); return objectCountAttribute; } } This is actually where I am stuck. I don't understand the EOModel and EOAttribute mechanism very well. One solution I tried was to add the count attribute permanently, that crashed the application. So I tried creating the attributes on the fly and removing them once finished this worked. However, it doesn't appear I can make this thread-safe since there is no locking mechanism on the EOModel (I think this is why Eric's solution used a static var) and I cannot easily test this concept under duress (when the exceptions actually start occurring). EOF may not provide a locking mechanism but perhaps the Java one will suffice? Have you tried synchronizing on the entity, something like: try { rawFetchspecification = (EOFetchSpecification)fetchSpecification.clone(); } catch (Exception e) { System.out.println("\nException in objectCountWithFetchSpecification :\n"+e); return null; } EOEntity entity = EOModelGroup.defaultGroup().entityNamed(rawFetchspecification.entityNam e()); synchronized (entity) { // ADDED SYNCHRONIZED EOQualifier schemaBasedQualifier = entity.schemaBasedQualifier(rawFetchspecification.qualifier()); EOAttribute attribute = EOFetchCounting.objectCountAttribute(); NSArray results = null; entity.addAttribute(attribute); // Fix to ensure that fetch counting does not trip the site when // the fetch fails for some reason try { rawFetchspecification.setQualifier(schemaBasedQualifier); rawFetchspecification.setRawRowKeyPaths(new NSArray(attribute.name())); results = editingContext.objectsWithFetchSpecification(rawFetchspecification); } finally { // IMPORTANT!!! // This needs to run every time, exception or not entity.removeAttribute(attribute); } } // CLOSE SYNCHRONIZED This email sent to site_archiver@lists.apple.com
participants (1)
-
Marek Wawrzyczny