You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2023/01/11 14:31:33 UTC

[GitHub] [daffodil] stevedlawrence opened a new pull request, #908: Refactor and document SAX unparse implementation

stevedlawrence opened a new pull request, #908:
URL: https://github.com/apache/daffodil/pull/908

   - Move classes/traits out of the .api namespace since that aren't really part of the internal API. The DaffodilUnparseContentHandler and SAXInfosetInputter don't necessarily need to be implemented as coroutines, so the Producer/Consumer coroutine classes are removed from .api. Likewise, the SAXInfosetEvent is an implementation detail about how we send SAX events from the DaffodilUnparseContentHandler to the SAXInfosetInputter and also isn't part of the API. These classes are now modified to directly mixin Coroutine/MainCoroutine.
   - Modify SAXInfosetEvent so it no longer stores unparse result or a cause exception when unparse completes. Instead, the SAXInfosetInputter is changed to return the unparse result/exception directly to the DaffodilContentHandler coroutine--it no longer reuses a SAXInfosetEvent from the batch array. Making this change simplifies the logic in some places:
     * We no longer need to copy the last SAX event to the beginning of the batch event, which was needed to carry back the unparse result/error
     * We no longer need the returnedInfosetEvent variable since we return unparse result/failure causes directly
     * The size of the batch can be exactly the same of the saxUnparseEventBatchSize instead of needed to add one
   - The hasNext() function really just needs to check if current element is EndDocument or not, and the next() function handles actually getting events from the main coroutine. This allows removal of the endDocumentRecived variable
   - The DaffodilUnparseContentHandler is now fully responsible for creating SAXExceptions based on the results of the SAXInfosetInputter. The SAXInfosetInputter now knows nothing about SAX, and theoretically could be used for anything that can convert an infoset to a batch of the SAXInfosetEvent objects
   - Add an inline function called currentEvent to make it more succinct to access the event at the right index in the batched infosets array
   - Change the resolveRelativeInfosetBlobURIs variable so that it is passed into the creation of the SAXInfosetInputter instead of mutating state
   - Add many comments to describe the SAX logic in detail. The interaction between the ContentHandler and InfosetInputter coroutines is not obvious and needs thorough documentation
   - Add SAX unparse test cases to improve code coverage
   - Modify the InfosetInputter events to directly get event information instead of using orNull. These values will always be set for each event by the ContentHandler
   - Rename maybeSendToInputter() to currentEventIsFinished() since that function does more than just send to the inputter. This more generic name means it can also do more logic if needed in the future.
   
   DAFFODIL-2770


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] mbeckerle commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
mbeckerle commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067334427


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the

Review Comment:
   Good point. Maybe producer/consumer is confusing, but making one of the coroutines be the "event queuer", the other the "event puller" coroutine is perhaps better?
   
   re main and peer, perhaps main and "worker" are better. I've been using the term "peer" to refer to "the other coroutines" in general. 
   
   I do recall that it was unintuitive which coroutine ends up doing which activity. I.e., the main ends up just queuing events, the worker does all the pulling and unparsing? (Or do I have that backwards?) 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067400263


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the

Review Comment:
   And yep, you have it right, the main coroutine is the event queuer, the worker routine pulls those events and unparses them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067285755


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter consumes SAXInfosetEvent objects from the
+ * DaffodilUnparseContentHandler class and converts them to events that the
+ * DataProcessor unparse can use.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
- *
- * - the run method is called, with the first batch of events, starting with the StartDocument event,
- * already loaded on the inputter's queue.
- * This is collected and stored in the batchedInfosetEvents member, and the currentIndex is set to 0
- * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be
- * processed and then queries the event at currentIndex. The hasNext call also checks that there is
- * a next event to be processed (currentIndex+1), and if not, queues the next batch of events by
- * transferring control to the contentHandler so it can load them.
- * - After it is done with the current event, it calls inputter.next to get the next event, and that
- * increments the currentIndex and cleans out the event at the previous index
- * - This process continues until the event at currentIndex either contains an EndDocument event or
- * the currentIndex is the last in the batch. If it is the former, the endDocumentReceived flag is
- * set to true and hasNext will return false. If it is the latter, the next batch of events will be
- * queued by transferring control to the contentHandler so it can load them.
- * - This ends the unparse process, and the unparseResult and/or any Errors are set on a single element
- * array containing response events. We call resumeFinal passing along that array, terminating this
- * thread and resuming the contentHandler for the last time.
- *
- * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvent to this class
- * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the
- *           currentEvent
- * @param output  outputChannel of choice where the unparsed data is stored
+ * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvents to this class
+ * @param dp DataProcessor that we use to kickstart the unparse process to consumes the events
+ * @param output  OutputChannel where the unparsed data is written
+ * @param resolveRelaiveInfosetBlobURIs if true, elements with type xs:anyURI type and a
+ *   relativeURI are resolved relative to the classpath. This should only be true when used
+ *   via a TDMLRunner or similar testing infrastructure
  */
 class SAXInfosetInputter(
-  unparseContentHandler: DFDL.DaffodilUnparseContentHandler,
+  unparseContentHandler: DaffodilUnparseContentHandler,
   dp: DFDL.DataProcessor,
-  output: DFDL.Output)
-  extends InfosetInputter with DFDL.ConsumerCoroutine {
+  output: DFDL.Output,
+  resolveRelativeInfosetBlobURIs: Boolean)
+  extends InfosetInputter with Coroutine[Array[SAXInfosetEvent]] {
 
   /**
-   * allows support for converting relative URIs in Blobs to absolute URIs, this is necessary
-   * for running TDML tests as they allow relative URIs. Because Daffodil proper only uses
-   * absolute URIs, we hide this functionality behind this flag. It can be set to true by calling
-   * the unparseContentHandler.enableInputterResolutionOfRelativeInfosetBlobURIs(), which calls the
-   * inputter's enableResolutionOfRelativeInfosetBlobURIs() function to set the below variable to true
+   * The index into batchedInfosetEvents that the InfosetInputter is currently returning
+   * information about to the unparse()
    */
-  private var resolveRelativeInfosetBlobURIs: Boolean = false
-
-  private var endDocumentReceived = false
   private var currentIndex: Int = 0
+
+  /**
+   * The array of batched infoset events received from the ContentHandler.
+   */

Review Comment:
   Yep, there would be one `resume()` call passing the entire batch to the infoset inputter coroutine, and one `resumeFinal()` call passing the `UnparseResult` back to the ContentHandler. Unfortunately there's still a thread created so there is that same Thread overhead.
   
   In theory, if we found the EndDocument in the first batch we could potentially even avoid the coroutine/thread stuff altogether. Since the SAXInofsetINputter threated wouldn't have been started yet we could have a setter to directly set the batch, and then call the unparse() function passing in that inputter. Could potentially be a big win for small-ish infosets. But does add extra complication. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] mbeckerle commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
mbeckerle commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067292157


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter consumes SAXInfosetEvent objects from the
+ * DaffodilUnparseContentHandler class and converts them to events that the
+ * DataProcessor unparse can use.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
- *
- * - the run method is called, with the first batch of events, starting with the StartDocument event,
- * already loaded on the inputter's queue.
- * This is collected and stored in the batchedInfosetEvents member, and the currentIndex is set to 0
- * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be
- * processed and then queries the event at currentIndex. The hasNext call also checks that there is
- * a next event to be processed (currentIndex+1), and if not, queues the next batch of events by
- * transferring control to the contentHandler so it can load them.
- * - After it is done with the current event, it calls inputter.next to get the next event, and that
- * increments the currentIndex and cleans out the event at the previous index
- * - This process continues until the event at currentIndex either contains an EndDocument event or
- * the currentIndex is the last in the batch. If it is the former, the endDocumentReceived flag is
- * set to true and hasNext will return false. If it is the latter, the next batch of events will be
- * queued by transferring control to the contentHandler so it can load them.
- * - This ends the unparse process, and the unparseResult and/or any Errors are set on a single element
- * array containing response events. We call resumeFinal passing along that array, terminating this
- * thread and resuming the contentHandler for the last time.
- *
- * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvent to this class
- * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the
- *           currentEvent
- * @param output  outputChannel of choice where the unparsed data is stored
+ * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvents to this class
+ * @param dp DataProcessor that we use to kickstart the unparse process to consumes the events
+ * @param output  OutputChannel where the unparsed data is written
+ * @param resolveRelaiveInfosetBlobURIs if true, elements with type xs:anyURI type and a
+ *   relativeURI are resolved relative to the classpath. This should only be true when used
+ *   via a TDMLRunner or similar testing infrastructure
  */
 class SAXInfosetInputter(
-  unparseContentHandler: DFDL.DaffodilUnparseContentHandler,
+  unparseContentHandler: DaffodilUnparseContentHandler,
   dp: DFDL.DataProcessor,
-  output: DFDL.Output)
-  extends InfosetInputter with DFDL.ConsumerCoroutine {
+  output: DFDL.Output,
+  resolveRelativeInfosetBlobURIs: Boolean)
+  extends InfosetInputter with Coroutine[Array[SAXInfosetEvent]] {
 
   /**
-   * allows support for converting relative URIs in Blobs to absolute URIs, this is necessary
-   * for running TDML tests as they allow relative URIs. Because Daffodil proper only uses
-   * absolute URIs, we hide this functionality behind this flag. It can be set to true by calling
-   * the unparseContentHandler.enableInputterResolutionOfRelativeInfosetBlobURIs(), which calls the
-   * inputter's enableResolutionOfRelativeInfosetBlobURIs() function to set the below variable to true
+   * The index into batchedInfosetEvents that the InfosetInputter is currently returning
+   * information about to the unparse()
    */
-  private var resolveRelativeInfosetBlobURIs: Boolean = false
-
-  private var endDocumentReceived = false
   private var currentIndex: Int = 0
+
+  /**
+   * The array of batched infoset events received from the ContentHandler.
+   */

Review Comment:
   This idea: if the entire set of events fits in the first batch, bypass the whole thread/coroutines system, seems like it would be of some real value. 
   
   This is a large percentage of our use cases.  
   
   But the overhead of creating and starting one thread, once, because now they're pooled, seems like it should be pretty minimal. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067342976


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the

Review Comment:
   I like it, will make the update.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] tuxji commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
tuxji commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067529104


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/api/DFDLParserUnparser.scala:
##########
@@ -226,84 +221,22 @@ object DFDL {
   }
 
   trait DaffodilUnparseContentHandler
-    extends  ProducerCoroutine
-    with org.xml.sax.ContentHandler {
+    extends org.xml.sax.ContentHandler {
     def getUnparseResult: UnparseResult
-    def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit
+    def enableResolutionOfRelativeInfosetBlobURIs(): Unit
   }
 
   /**
    * Used in SAXInfosetEvent.causeError to identify when an unparseResult.isError is true
    */
   class DaffodilUnparseErrorSAXException(unparseResult: UnparseResult)
-    extends org.xml.sax.SAXException(unparseResult.getDiagnostics.mkString("\n"))
+    extends SAXException(unparseResult.getDiagnostics.mkString("\n"))
 
   /**
    * Used in SAXInfosetEvent.causeError to identify when an unexpected error occurs

Review Comment:
   You've moved SAXInfosetEvent from api/DFDLParserUnparser.scala to infoset/SAXInfosetInputter.scala, but these scaladocs still mention SAXInfosetEvent.  Both DaffodilUnparseErrorSAXException and DaffodilUnhandledSAXException just extend SAXException, so their own names are the only extra info they communicate to users.  It looks like Daffodil's apis (runtime1's api, daffodil-japi, daffodil-sapi) want to capture SAXException and rethrow it as either DaffodilUnparseErrorSAXException or DaffodilUnhandledSAXException.  I'm not sure why it's so important that Daffodil users receive instances of these exceptions instead of generic SAXException instances, so you might want to make the scaladocs explain why these exceptions matter to users rather than mention which internal Daffodil code uses these exceptions.



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,224 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Handle and unparse XMLReader SAX events using a provided DataProcessor and
+ * OutputChannel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we provided namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The main coroutine or "event
+ * queuer" is this DaffodilUnparseContentHandler and runs on the same thread as an
+ * XMLReader to handle and batch SAX events. The worker coroutine or "event puller" is
+ * an instance of the SAXInfosetInputter which calls the actual unparse() function to
+ * query these batched SAX events and unparse data.
  *
- * @param dp dataprocessor object that will be used to call the parse
- * @param output outputChannel of choice where the unparsed data is stored
+ * Below is a description of how this class is used and they communicate to unparse
+ * using the SAX XMLReader API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter instance.
+ *
+ * 2. An XMLReader instance is created and configured to use the
+ *    DaffodilUnparseContentHandler to handle its SAX events.
+ *
+ * 3. The DaffodilUnparseContentHandler handles events from the XMLReader, gathers
+ *    the necessary information from those events, and fills out an array of
+ *    SAXInfosetEvent objects, called a "batch".
+ *
+ * 4. When a full batch has been gathered, or an endDocument() event is handled, the
+ *    DaffodilUnparseContentHandler calls resume() to pause execution and start the
+ *    SAXInfosetInputter coroutine, sending it the batch of events.
+ *
+ * 5. Since this is the first time the SAXInfosetInputter has been given control, its
+ *    coroutine Thread is started and its run() function called. This calls
+ *    waitResume() to receive the first batch of events. It then calls the
+ *    DataProcessor unparse() function to begin the unparse.
+ *
+ * 6. The SAXInfosetInputter functions are called by the unparse, which reads the
+ *    batched events and provides the necessary information to unparse the infoset.
+ *    Once the SAXInfosetInputter has unparsed all batched events and needs a new
+ *    event in the next() function, it calls resume() to pause execution and resume
+ *    the DaffodilUnparseContentHandler coroutine, sending it Nope to signify it
+ *    needs more events.
+ *
+ * 7. The DaffodilUnparseContentHandler resumes and again continues to handle SAX
+ *    events and fill out the batched events array until it is full or endDocument()
+ *    is handled, at which point it calls resume() to pause and send the new batch of
+ *    events to the SAXInfosetInputter where it resumes control.
+ *
+ * 8. Steps 6 and 7 repeat until the SAXInfosetInputter signals back to the
+ *    DaffodilUnparseContentHandler that it is complete calling resumeFinal() and
+ *    sending a One object containing either an UnparseResult or an Exception. At
+ *    this point, the SAXInfosetInputter is complete and provide control back to the
+ *    DaffodilUnparseContentHandler--the SAXInfosetInputter coroutine is done.
+ *
+ * 9. The DaffodilUnparseContentHandler resumes, recieves and examines the result
+ *    from the SAXInfosetInputter, and either makes the UnparseResult available to
+ *    the XMLReader or throws a SAXException if there was an error.
+ *
+ * @param dp DataProcessor object that will be used to start the unparse
+ * @param output OutputChannel of where the unparsed data is written
  */
 class DaffodilUnparseContentHandler(
   dp: DFDL.DataProcessor,
   output: DFDL.Output)
-  extends DFDL.DaffodilUnparseContentHandler {
-  private lazy val inputter = new SAXInfosetInputter(this, dp, output)
-  private var unparseResult: DFDL.UnparseResult = _
+  extends MainCoroutine[Maybe[Either[Exception, DFDL.UnparseResult]]]
+  with DFDL.DaffodilUnparseContentHandler {

Review Comment:
   Just making another observation.  This line changes DaffodilUnparseContentHandler from MainCoroutine[Array[SAXInfosetEvent]] to MainCoroutine[Maybe[Either[Exception, DFDL.UnparseResult]]].  SAXInfosetInputter is still Coroutine[Array[SAXInfosetEvent]].  What this means is that a pair of coroutines can send data to each other without the data having to be the same type on both ends:
   
   ```scala
       // dataToReceive and dataToSend can be different types!
       val dataToReceive = this.resume(otherCoroutine, dataToSend)
   ```
   
   This is valuable to know - maybe worth adding to a comment somewhere.



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter worker coroutine receives batches of SAXInfosetEvent
+ * objects from a DaffodilUnparseContentHandler main coroutine and calls the
+ * unparse() function to provide these events and unparse the data.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.

Review Comment:
   This is the place that might send a programmer to the wrong place (api.DFDL.DaffodilUnparseContentHandler instead of processors.DaffodilUnparseContentHandler).



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/api/DFDLParserUnparser.scala:
##########
@@ -226,84 +221,22 @@ object DFDL {
   }
 
   trait DaffodilUnparseContentHandler
-    extends  ProducerCoroutine
-    with org.xml.sax.ContentHandler {
+    extends org.xml.sax.ContentHandler {
     def getUnparseResult: UnparseResult
-    def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit
+    def enableResolutionOfRelativeInfosetBlobURIs(): Unit
   }

Review Comment:
   I'm not very happy that Daffodil has both a DFDL.DaffodilUnparseContentHandler trait and a processors.DaffodilUnparseContentHandler class. I'm especially not happy that this DFDL.DaffodilUnparseContentHandler trait has no scaladocs to tell programmers that the real work is done in the processors.DaffodilUnparseContentHandler class. SAXInfosetInputter's scaladocs tells programmers to see DaffodilUnparseContentHandler for a detailed description of how the DaffodilUnparseContentHandler and SAXInfosetInputter classes interact.  I fear a programmer might come here by mistake and get puzzled why this DaffodilUnparseContentHandler doesn't have any scaladocs or real code.  If we really need both the trait and the class (do we really need both?), the trait needs its own scaladocs.
   
   I noticed that one half of grep -w ContentHandler's hits have an import org.xml.sax.ContentHandler while the other half don't bother to import ContentHandler. I'm just mentioning that fact; I don't have a strong opinion whether ContentHandler should be imported or qualified although it was your change at line 233 using SAXException instead of org.xml.sax.SAXException which drew my eye to line 224 still using org.xml.sax.ContentHandler.



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter worker coroutine receives batches of SAXInfosetEvent
+ * objects from a DaffodilUnparseContentHandler main coroutine and calls the
+ * unparse() function to provide these events and unparse the data.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
- *
- * - the run method is called, with the first batch of events, starting with the StartDocument event,
- * already loaded on the inputter's queue.
- * This is collected and stored in the batchedInfosetEvents member, and the currentIndex is set to 0
- * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be
- * processed and then queries the event at currentIndex. The hasNext call also checks that there is
- * a next event to be processed (currentIndex+1), and if not, queues the next batch of events by
- * transferring control to the contentHandler so it can load them.
- * - After it is done with the current event, it calls inputter.next to get the next event, and that
- * increments the currentIndex and cleans out the event at the previous index
- * - This process continues until the event at currentIndex either contains an EndDocument event or
- * the currentIndex is the last in the batch. If it is the former, the endDocumentReceived flag is
- * set to true and hasNext will return false. If it is the latter, the next batch of events will be
- * queued by transferring control to the contentHandler so it can load them.
- * - This ends the unparse process, and the unparseResult and/or any Errors are set on a single element
- * array containing response events. We call resumeFinal passing along that array, terminating this
- * thread and resuming the contentHandler for the last time.
- *
- * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvent to this class
- * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the
- *           currentEvent
- * @param output  outputChannel of choice where the unparsed data is stored
+ * @param unparseContentHandler main coroutine that sends the SAXInfosetEvents to this class
+ * @param dp DataProcessor that we use to start the unparse and provide infoset information
+ * @param output  OutputChannel where the unparsed data is written
+ * @param resolveRelaiveInfosetBlobURIs if true, elements with type xs:anyURI type and a
+ *   relativeURI are resolved relative to the classpath. This should only be true when used
+ *   via a TDMLRunner or similar testing infrastructure
  */
 class SAXInfosetInputter(
-  unparseContentHandler: DFDL.DaffodilUnparseContentHandler,
+  unparseContentHandler: DaffodilUnparseContentHandler,

Review Comment:
   After this line changing DFDL.DaffodilUnparseContentHandler to DaffodilUnparseContentHandler, I believe there is only one place left in the codebase which uses DFDL.DaffodilUnparseContentHandler in an API-visible place (DataProcessor's newContentHandlerInstance method):
   
   ```rg
   daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DataProcessor.scala
   257:  override def newContentHandlerInstance(output: DFDL.Output): DFDL.DaffodilUnparseContentHandler =
   ```
   
   When Mike and I wrote the C code generator (runtime2), we decided we didn't want or need to implement all of DFDL.DataProcessor so we split it into DFDL.DataProcessorBase and DFDL.DataProcessor.  Runtime2DataProcessor didn't need newContentHandlerInstance at all.  Now, speaking from my experience working on a second code generator and integrating it into Daffodil, I would say we didn't even need Runtime2DataProcessor either.  We needed only Runtime2TDMLDFDLProcessor to run TDML tests with DaffodilC and we could have put all of Runtime2DataProcessor's code in Runtime2TDMLDFDLProcessor instead of Runtime2DataProcessor.  I think the only parts of Daffodil we need to add or override for another code generator are another language in the "daffodil generate \<language\>" CLI, another new generator backend, and another Daffodill\<Language\>TDMLDFDLProcessor.
   
   No real point here, just making some observations such as that there doesn't seem to be any motivation for a programmer to take advantage of Daffodil's extensible DFDL.* API to replace Compiler, DataProcessor, DaffodilUnparseContentHandler, etc. (it's hard to know that except in hindsight and new use cases still might appear, though).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1068362847


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/api/DFDLParserUnparser.scala:
##########
@@ -226,84 +221,22 @@ object DFDL {
   }
 
   trait DaffodilUnparseContentHandler
-    extends  ProducerCoroutine
-    with org.xml.sax.ContentHandler {
+    extends org.xml.sax.ContentHandler {
     def getUnparseResult: UnparseResult
-    def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit
+    def enableResolutionOfRelativeInfosetBlobURIs(): Unit
   }
 
   /**
    * Used in SAXInfosetEvent.causeError to identify when an unparseResult.isError is true
    */
   class DaffodilUnparseErrorSAXException(unparseResult: UnparseResult)
-    extends org.xml.sax.SAXException(unparseResult.getDiagnostics.mkString("\n"))
+    extends SAXException(unparseResult.getDiagnostics.mkString("\n"))
 
   /**
    * Used in SAXInfosetEvent.causeError to identify when an unexpected error occurs

Review Comment:
   Good points.
   
   I think the only difference between these two exceptions is that if the user receives a DaffodilUnparseErrorSaxException, it means the unparse failed and the user can call getUnparseResult from the content handler to get the UnparseResult and get the diagnostics. With the other exception, it means there's probably a bug in Daffodil. I'll make this more clear and remove the SAXInfosetEvent stuff from the .api.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067305792


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the
+ * SAXInfosetInputter which calls the actual unparse() function to consume these
+ * batched SAX events and unparse data.
+ *
+ * Below is a description of how this class is used and the interaction between these
+ * two classes to unparse using the SAX API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter coroutine instance.
+ *
+ * 2. An XMLReader instance is created and configured to send events to the

Review Comment:
   Agreed that the distinction is important and I like the send/receive distinction for coroutines.
   
   Reading the XMLReader/ContentHandler java doc, what are you thoughts on using language like "The DaffodilUnparseContentHandler *handles SAX events* from the XMLReader". So we have unique descriptions for XMLReader to ContentHandler communication, and inter-coroutine communication.
   
   And then we can use call when one of these classes just calls some function (e.g. next()).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1068367020


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/api/DFDLParserUnparser.scala:
##########
@@ -226,84 +221,22 @@ object DFDL {
   }
 
   trait DaffodilUnparseContentHandler
-    extends  ProducerCoroutine
-    with org.xml.sax.ContentHandler {
+    extends org.xml.sax.ContentHandler {
     def getUnparseResult: UnparseResult
-    def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit
+    def enableResolutionOfRelativeInfosetBlobURIs(): Unit
   }

Review Comment:
   The trait acts as an interface so that if this .api ever does become public (which I hope happens some day, it's such a pain having to maintain daffodil-japi and daffodil-sapi) then this will be the public interface that users can interact with. But I can definitely see how that could be confusing. Maybe we rename the implementation to `DaffodilUnparseContentHandlerImpl` to make it clear it's an implementation of the DaffodilUnparseContentHandler interface/API.
   
   I'm not sure if we have a standard on fully qualified vs not, but in general I prefer to not use the fully qualified name, I guess I missed a couple places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] mbeckerle commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
mbeckerle commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067103209


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter consumes SAXInfosetEvent objects from the
+ * DaffodilUnparseContentHandler class and converts them to events that the
+ * DataProcessor unparse can use.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
- *
- * - the run method is called, with the first batch of events, starting with the StartDocument event,
- * already loaded on the inputter's queue.
- * This is collected and stored in the batchedInfosetEvents member, and the currentIndex is set to 0
- * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be
- * processed and then queries the event at currentIndex. The hasNext call also checks that there is
- * a next event to be processed (currentIndex+1), and if not, queues the next batch of events by
- * transferring control to the contentHandler so it can load them.
- * - After it is done with the current event, it calls inputter.next to get the next event, and that
- * increments the currentIndex and cleans out the event at the previous index
- * - This process continues until the event at currentIndex either contains an EndDocument event or
- * the currentIndex is the last in the batch. If it is the former, the endDocumentReceived flag is
- * set to true and hasNext will return false. If it is the latter, the next batch of events will be
- * queued by transferring control to the contentHandler so it can load them.
- * - This ends the unparse process, and the unparseResult and/or any Errors are set on a single element
- * array containing response events. We call resumeFinal passing along that array, terminating this
- * thread and resuming the contentHandler for the last time.
- *
- * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvent to this class
- * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the
- *           currentEvent
- * @param output  outputChannel of choice where the unparsed data is stored
+ * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvents to this class
+ * @param dp DataProcessor that we use to kickstart the unparse process to consumes the events
+ * @param output  OutputChannel where the unparsed data is written
+ * @param resolveRelaiveInfosetBlobURIs if true, elements with type xs:anyURI type and a
+ *   relativeURI are resolved relative to the classpath. This should only be true when used
+ *   via a TDMLRunner or similar testing infrastructure
  */
 class SAXInfosetInputter(
-  unparseContentHandler: DFDL.DaffodilUnparseContentHandler,
+  unparseContentHandler: DaffodilUnparseContentHandler,
   dp: DFDL.DataProcessor,
-  output: DFDL.Output)
-  extends InfosetInputter with DFDL.ConsumerCoroutine {
+  output: DFDL.Output,
+  resolveRelativeInfosetBlobURIs: Boolean)
+  extends InfosetInputter with Coroutine[Array[SAXInfosetEvent]] {
 
   /**
-   * allows support for converting relative URIs in Blobs to absolute URIs, this is necessary
-   * for running TDML tests as they allow relative URIs. Because Daffodil proper only uses
-   * absolute URIs, we hide this functionality behind this flag. It can be set to true by calling
-   * the unparseContentHandler.enableInputterResolutionOfRelativeInfosetBlobURIs(), which calls the
-   * inputter's enableResolutionOfRelativeInfosetBlobURIs() function to set the below variable to true
+   * The index into batchedInfosetEvents that the InfosetInputter is currently returning
+   * information about to the unparse()
    */
-  private var resolveRelativeInfosetBlobURIs: Boolean = false
-
-  private var endDocumentReceived = false
   private var currentIndex: Int = 0
+
+  /**
+   * The array of batched infoset events received from the ContentHandler.
+   */

Review Comment:
   So if we make this batch size large enough, would this consume the entire unparser input infoset events, create one batch object, and hand it off to the coroutine exactly once? That would minimize the overhead, and be sensible for almost all "small message" situations. 



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter consumes SAXInfosetEvent objects from the
+ * DaffodilUnparseContentHandler class and converts them to events that the
+ * DataProcessor unparse can use.

Review Comment:
   typo: unparser



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the

Review Comment:
   The detail that one of these coroutines is actually the main co-routine,  and the other is the "peer" coroutine object, and which one is producer and which one consumer, would help here also. 



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the
+ * SAXInfosetInputter which calls the actual unparse() function to consume these
+ * batched SAX events and unparse data.
+ *
+ * Below is a description of how this class is used and the interaction between these
+ * two classes to unparse using the SAX API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter coroutine instance.
+ *
+ * 2. An XMLReader instance is created and configured to send events to the
+ *    DaffodilUnparseContentHandler.
+ *
+ * 3. The DaffodilUnparseContentHandler receives events from the XMLReader, gathers
+ *    the necessary information from those events, and fills out an array of
+ *    SAXInfosetEvent objects, called a "batch".
+ *
+ * 4. When a full batch has been gathered, or an endDocument() event is received, the
+ *    DaffodilUnparseContentHandler calls resume() to pause execution and resume the
+ *    SAXInfosetInputter coroutine, passing it the batch of events.
+ *
+ * 5. Since this is the first time the SAXInfosetInputter has been given control, its
+ *    coroutine Thread is started and its run() function called. This calls
+ *    waitResume() to get the first batch of events which it immediately receives. It
+ *    then calls the DataProcessor unparse() function to begin the unparse.
+ *
+ * 6. The SAXInfosetInputter functions are called by unparse, which read the batched
+ *    events and provide the necessary information to unparse the infoset. Once the
+ *    SAXInfosetInputter has consumed and used the full batch of events and needs a
+ *    new event in the next() function, it calls resume() to pause execution and
+ *    resume the DaffodilUnparseContentHandler coroutine,
+ *    passing it Nope signifying it needs more events.
+ *
+ * 7. The DaffodilUnparseContentHandler resumes and again continues to receive SAX
+ *    events and fill out the batched events array until it is full or endDocument()
+ *    is received, at which point it calls resume() to pause and return control back
+ *    to the SAXInfosetInputter where it can read the new batch of events.
+ *
+ * 8. Steps 6 and 7 repeat until the SAXInfosetInputter signals back to the
+ *    DaffodilUnparseContentHandler that it is complete by returning a One object
+ *    containing either an UnparseResult or an Exception. At this point, the
+ *    SAXInfosetInputter is complete and calls resumeFinal() to provide control back
+ *    to the DaffodilUnparseContentHandler--the SAXInfosetInputter
+ *    coroutine is done.
+ *
+ * 9. The DaffodilUnparseContentHandler resumes, examines the result from the
+ *    SAXInfosetInputter, and either makes the UnparseResult available to the
+ *    XMLReader or throws a SAXException if there was an error.
  *
  * @param dp dataprocessor object that will be used to call the parse
  * @param output outputChannel of choice where the unparsed data is stored
  */
 class DaffodilUnparseContentHandler(
   dp: DFDL.DataProcessor,
   output: DFDL.Output)
-  extends DFDL.DaffodilUnparseContentHandler {
-  private lazy val inputter = new SAXInfosetInputter(this, dp, output)
-  private var unparseResult: DFDL.UnparseResult = _
+  extends MainCoroutine[Maybe[Either[Exception, DFDL.UnparseResult]]]
+  with DFDL.DaffodilUnparseContentHandler {
+
+   /**
+    * The SAXInfosetInputter coroutine that consumes the batches of SAXInfosetEvents.
+    * The coroutine thread will not be created until the
+    * DaffodilUnparseContentHandler calls resume with the first batch of events
+    */
+  private lazy val inputter: SAXInfosetInputter = new SAXInfosetInputter(this, dp, output, resolveRelativeInfosetBlobURIs)

Review Comment:
   Line too long. 



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the
+ * SAXInfosetInputter which calls the actual unparse() function to consume these
+ * batched SAX events and unparse data.
+ *
+ * Below is a description of how this class is used and the interaction between these
+ * two classes to unparse using the SAX API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter coroutine instance.
+ *
+ * 2. An XMLReader instance is created and configured to send events to the
+ *    DaffodilUnparseContentHandler.
+ *
+ * 3. The DaffodilUnparseContentHandler receives events from the XMLReader, gathers

Review Comment:
   receives = "is passed" or "is called with these"



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the
+ * SAXInfosetInputter which calls the actual unparse() function to consume these
+ * batched SAX events and unparse data.
+ *
+ * Below is a description of how this class is used and the interaction between these
+ * two classes to unparse using the SAX API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter coroutine instance.
+ *
+ * 2. An XMLReader instance is created and configured to send events to the

Review Comment:
   I want to make sure we're using the word "send" vs. "pass" consistently here if possible. 
   
   I think best usage is to keep the term "send" to mean passed over to the peer coroutine as part of resuming it. The word "pass" here is technically correct, one passes things to a coroutine, but distinguishing that from regular calling, such as to a callback handler, is useful here if we can be consistent. 
   
   The term "pass" should mean passed as an argument to a method call. 
   
   I think an XMLReader passes things to a content handler. Not send. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1068371163


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,224 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Handle and unparse XMLReader SAX events using a provided DataProcessor and
+ * OutputChannel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we provided namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The main coroutine or "event
+ * queuer" is this DaffodilUnparseContentHandler and runs on the same thread as an
+ * XMLReader to handle and batch SAX events. The worker coroutine or "event puller" is
+ * an instance of the SAXInfosetInputter which calls the actual unparse() function to
+ * query these batched SAX events and unparse data.
  *
- * @param dp dataprocessor object that will be used to call the parse
- * @param output outputChannel of choice where the unparsed data is stored
+ * Below is a description of how this class is used and they communicate to unparse
+ * using the SAX XMLReader API:
+ *
+ * 1. A DaffodilUnparseContentHandler instance is created, which initializes a
+ *    SAXInfosetInputter instance.
+ *
+ * 2. An XMLReader instance is created and configured to use the
+ *    DaffodilUnparseContentHandler to handle its SAX events.
+ *
+ * 3. The DaffodilUnparseContentHandler handles events from the XMLReader, gathers
+ *    the necessary information from those events, and fills out an array of
+ *    SAXInfosetEvent objects, called a "batch".
+ *
+ * 4. When a full batch has been gathered, or an endDocument() event is handled, the
+ *    DaffodilUnparseContentHandler calls resume() to pause execution and start the
+ *    SAXInfosetInputter coroutine, sending it the batch of events.
+ *
+ * 5. Since this is the first time the SAXInfosetInputter has been given control, its
+ *    coroutine Thread is started and its run() function called. This calls
+ *    waitResume() to receive the first batch of events. It then calls the
+ *    DataProcessor unparse() function to begin the unparse.
+ *
+ * 6. The SAXInfosetInputter functions are called by the unparse, which reads the
+ *    batched events and provides the necessary information to unparse the infoset.
+ *    Once the SAXInfosetInputter has unparsed all batched events and needs a new
+ *    event in the next() function, it calls resume() to pause execution and resume
+ *    the DaffodilUnparseContentHandler coroutine, sending it Nope to signify it
+ *    needs more events.
+ *
+ * 7. The DaffodilUnparseContentHandler resumes and again continues to handle SAX
+ *    events and fill out the batched events array until it is full or endDocument()
+ *    is handled, at which point it calls resume() to pause and send the new batch of
+ *    events to the SAXInfosetInputter where it resumes control.
+ *
+ * 8. Steps 6 and 7 repeat until the SAXInfosetInputter signals back to the
+ *    DaffodilUnparseContentHandler that it is complete calling resumeFinal() and
+ *    sending a One object containing either an UnparseResult or an Exception. At
+ *    this point, the SAXInfosetInputter is complete and provide control back to the
+ *    DaffodilUnparseContentHandler--the SAXInfosetInputter coroutine is done.
+ *
+ * 9. The DaffodilUnparseContentHandler resumes, recieves and examines the result
+ *    from the SAXInfosetInputter, and either makes the UnparseResult available to
+ *    the XMLReader or throws a SAXException if there was an error.
+ *
+ * @param dp DataProcessor object that will be used to start the unparse
+ * @param output OutputChannel of where the unparsed data is written
  */
 class DaffodilUnparseContentHandler(
   dp: DFDL.DataProcessor,
   output: DFDL.Output)
-  extends DFDL.DaffodilUnparseContentHandler {
-  private lazy val inputter = new SAXInfosetInputter(this, dp, output)
-  private var unparseResult: DFDL.UnparseResult = _
+  extends MainCoroutine[Maybe[Either[Exception, DFDL.UnparseResult]]]
+  with DFDL.DaffodilUnparseContentHandler {

Review Comment:
   Yeah, I didn't know this either until I started digging more deeply into this SAX an coroutine stuff. I'll make sure it's clear somewhere (maybe the Coroutine.scala documentation) that the type parameter is the type the coroutine expects to receive from its peer when it calls resume, which are allowed to be different.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1068369294


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/SAXInfosetInputter.scala:
##########
@@ -21,79 +21,70 @@ import java.net.URI
 import java.net.URISyntaxException
 
 import org.apache.daffodil.api.DFDL
-import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
-import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.dpath.NodeInfo
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.processors.DaffodilUnparseContentHandler
+import org.apache.daffodil.util.Coroutine
+import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.MaybeBoolean
 import org.apache.daffodil.util.Misc
 import org.apache.daffodil.xml.XMLUtils
 
 /**
- * The SAXInfosetInputter consumes SAXInfosetEvent objects from the DaffodilUnparseContentHandler
- * class and converts them to events that the DataProcessor unparse can use. This class contains an
- * array of batched SAXInfosetEvent objects that it receives from the contentHandler and the index
- * of the current element being processed.
+ * The SAXInfosetInputter worker coroutine receives batches of SAXInfosetEvent
+ * objects from a DaffodilUnparseContentHandler main coroutine and calls the
+ * unparse() function to provide these events and unparse the data.
+ * 
+ * See the DaffodilUnparseContentHandler for a detailed description of how these two
+ * classes interact.
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
- *
- * - the run method is called, with the first batch of events, starting with the StartDocument event,
- * already loaded on the inputter's queue.
- * This is collected and stored in the batchedInfosetEvents member, and the currentIndex is set to 0
- * - The dp.unparse method is called, and it calls hasNext to make sure an event exists to be
- * processed and then queries the event at currentIndex. The hasNext call also checks that there is
- * a next event to be processed (currentIndex+1), and if not, queues the next batch of events by
- * transferring control to the contentHandler so it can load them.
- * - After it is done with the current event, it calls inputter.next to get the next event, and that
- * increments the currentIndex and cleans out the event at the previous index
- * - This process continues until the event at currentIndex either contains an EndDocument event or
- * the currentIndex is the last in the batch. If it is the former, the endDocumentReceived flag is
- * set to true and hasNext will return false. If it is the latter, the next batch of events will be
- * queued by transferring control to the contentHandler so it can load them.
- * - This ends the unparse process, and the unparseResult and/or any Errors are set on a single element
- * array containing response events. We call resumeFinal passing along that array, terminating this
- * thread and resuming the contentHandler for the last time.
- *
- * @param unparseContentHandler producer coroutine that sends the SAXInfosetEvent to this class
- * @param dp dataprocessor that we use to kickstart the unparse process and that consumes the
- *           currentEvent
- * @param output  outputChannel of choice where the unparsed data is stored
+ * @param unparseContentHandler main coroutine that sends the SAXInfosetEvents to this class
+ * @param dp DataProcessor that we use to start the unparse and provide infoset information
+ * @param output  OutputChannel where the unparsed data is written
+ * @param resolveRelaiveInfosetBlobURIs if true, elements with type xs:anyURI type and a
+ *   relativeURI are resolved relative to the classpath. This should only be true when used
+ *   via a TDMLRunner or similar testing infrastructure
  */
 class SAXInfosetInputter(
-  unparseContentHandler: DFDL.DaffodilUnparseContentHandler,
+  unparseContentHandler: DaffodilUnparseContentHandler,

Review Comment:
   > Just making some observations such as that there doesn't seem to be any motivation for a programmer to take advantage of Daffodil's extensible DFDL.*
   
   Yeah, I agree with this. I think there is value in maintain a .api and having some interfaces since I hope this does someday become the public API and we can drop SAPI and JAPI.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1067290881


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DaffodilUnparseContentHandler.scala:
##########
@@ -28,156 +28,223 @@ import org.xml.sax.Locator
 import org.apache.daffodil.api.DFDL
 import org.apache.daffodil.api.DFDL.DaffodilUnhandledSAXException
 import org.apache.daffodil.api.DFDL.DaffodilUnparseErrorSAXException
-import org.apache.daffodil.api.DFDL.SAXInfosetEvent
 import org.apache.daffodil.exceptions.Assert
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.EndElement
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartDocument
 import org.apache.daffodil.infoset.InfosetInputterEventType.StartElement
+import org.apache.daffodil.infoset.SAXInfosetEvent
 import org.apache.daffodil.infoset.SAXInfosetInputter
 import org.apache.daffodil.util.MStackOf
+import org.apache.daffodil.util.MainCoroutine
 import org.apache.daffodil.util.Maybe
 import org.apache.daffodil.util.Maybe.Nope
 import org.apache.daffodil.util.Maybe.One
 import org.apache.daffodil.util.Misc
 
 /**
- * DaffodilUnparseContentHandler produces SAXInfosetEvent objects for the SAXInfosetInputter to
- * consume and convert to events that the Dataprocessor unparse can use. The SAXInfosetEvent object
- * is built from information that is passed to the ContentHandler from an XMLReader parser. In
- * order to receive the uri and prefix information from the XMLReader, the XMLReader must have
- * support for XML Namespaces
+ * Unparse SAX events received from an XMLReader using a provided DataProcessor and
+ * Output channel
  *
- * This class, together with the SAXInfosetInputter, uses coroutines to ensure that a batch of events
- * (based on the tunable saxUnparseEventBatchSize) can be passed from the former to the latter.
- * The following is the general process:
+ * Note: XMLReaders using this as their ContentHandler must have support for XML
+ * namespaces so that we receive namespace URI and prefix information that Daffodil
+ * requires to unparse.
  *
- * - an external call is made to parse an XML Document
- * - this class receives a StartDocument call, which is the first SAXInfosetEvent that should be
- * sent to the SAXInfosetInputter. That event is put onto an array of SAXInfosetEvents of size the
- * saxUnparseEventBatchSize tunable. Once the array is full, it is put on the inputter's queue,
- * this thread is paused, and that inputter's thread is run
- * - when the SAXInfosetInputter is done processing that batch and is ready for a new batch, it
- * sends a 1 element array with the last completed event via the coroutine system, which loads it on
- * the contentHandler's queue, which restarts this thread and pauses that one. In the expected case,
- * the single element array will contain no new information until the unparse complete. In the case of
- * an unexpected error though, it will contain error information
- * - this process continues until the EndDocument SAXInfosetEvent is loaded into the batch.
- * Once that SAXInfosetEvent is processed by the SAXInfosetInputter, it signals the end of batched
- * events coming from the contentHandler. This ends the unparseProcess and returns the event with
- * the unparseResult and/or any error
- * information
+ * The SAX ContentHandler API is push-based, but the Daffodil InfosetInputter unparse
+ * API is pull-based, so these two API's are at odds with one another. To link the
+ * two, we create two classes that implement a coroutine-like API to communicate and
+ * ensure that the push and pull sides of the two APIs never run at the same time
+ * (see Coroutine.scala for implementation details). The "producer" coroutine is this
+ * DaffodilUnparseContentHandler and runs on the same thread as an XMLReader to
+ * receive and batch SAX events. The "consumer" coroutine is an instance of the

Review Comment:
   Do you think we should remove the terms producer and consumer entirely?
   
   As I was writing this I was a bit concerned that there could be confusion that the DaffodilUnparseContentHandler could be considered both a producer and consumer (e.g. it "consumes" things from the XMLReader (SAX events) and from the SAXInfosetInputter (UnparseResult), and produces for the SAXInfosetInputter (SAXInfosetEvents).
   
   I wonder if just using "main" and "peer" is enough of a distiction, and the documentation makes it clear when it gets things from one class and sends things to another?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] mbeckerle commented on a diff in pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
mbeckerle commented on code in PR #908:
URL: https://github.com/apache/daffodil/pull/908#discussion_r1068387651


##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/api/DFDLParserUnparser.scala:
##########
@@ -226,84 +221,22 @@ object DFDL {
   }
 
   trait DaffodilUnparseContentHandler
-    extends  ProducerCoroutine
-    with org.xml.sax.ContentHandler {
+    extends org.xml.sax.ContentHandler {
     def getUnparseResult: UnparseResult
-    def enableInputterResolutionOfRelativeInfosetBlobURIs(): Unit
+    def enableResolutionOfRelativeInfosetBlobURIs(): Unit
   }

Review Comment:
   "Impl" suffix helps here. 
   I like to use "Mixin" suffix also when a trait provides implementation methods. 
   In fact both makes sense sometimes XYZImplMixin is useful when there is an interface XYZ. 
   
   As for qualified names,... IDEs make it hard to maintain a discipline of say, not importing a particular package because you *want* to use qualified names.
   
   The IDEs I've used all prompt you for possible matching names, showing you the choices for different qualifiers, and then insert the name in the code and the import statement at the top for you. 
   
   This is a big help when writing code and I would *not* want to turn it off for the sake of some discretionary code style issue like "I think I'd prefer to use a qualified name in this spot."  If you really insist on this you can put in the qualified name and delete the import and the IDE won't undo your change, but you have to really want it that way to go through those steps. 
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [daffodil] stevedlawrence merged pull request #908: Refactor and document SAX unparse implementation

Posted by GitBox <gi...@apache.org>.
stevedlawrence merged PR #908:
URL: https://github.com/apache/daffodil/pull/908


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org