Manipulating a large message tree can demand a lot of storage. If you design a message flow that handles large messages made up of repeating structures, you can code ESQL statements that help to reduce the storage load on the broker. These statements support both random and sequential access to the message, but assume that you do not need access to the whole message at one time.
These ESQL statements cause the broker to perform limited parsing of the message, and to keep only that part of the message tree that reflects a single record in storage at a time. If your processing requires you to retain information from record to record (for example, to calculate a total price from a repeating structure of items in an order), you can either declare, initialize, and maintain ESQL variables, or you can save values in another part of the message tree, for example LocalEnvironment.
This technique reduces the memory used by the broker to that needed to hold the full input and output bit streams, plus that needed for just one record's trees, and provides memory savings when even a small number of repeats is encountered in the message. The broker uses partial parsing and the ability to parse specified parts of the message tree to and from the corresponding part of the bit stream.
-- Copy the MQMD header SET OutputRoot.MQMD = InputRoot.MQMD; -- Create a special folder in the output message to hold the input tree -- Note : SourceMessageTree is the root element of an XML parser CREATE LASTCHILD OF OutputRoot.XMLNS.Data DOMAIN 'XMLNS' NAME 'SourceMessageTree'; -- Copy the input message to a special folder in the output message -- Note : This is a root to root copy which will therefore not build trees SET OutputRoot.XMLNS.Data.SourceMessageTree = InputRoot.XMLNS; -- Create a special folder in the output message to hold the output tree CREATE FIELD OutputRoot.XMLNS.Data.TargetMessageTree; -- Prepare to loop through the purchased items DECLARE sourceCursor REFERENCE TO OutputRoot.XMLNS.Data.SourceMessageTree.Invoice; DECLARE targetCursor REFERENCE TO OutputRoot.XMLNS.Data.TargetMessageTree; DECLARE resultCursor REFERENCE TO OutputRoot.XMLNS.Data; DECLARE grandTotal FLOAT 0.0e0; -- Create a block so that it's easy to abandon processing ProcessInvoice: BEGIN -- If there are no Invoices in the input message, there is nothing to do IF NOT LASTMOVE(sourceCursor) THEN LEAVE ProcessInvoice; END IF; -- Loop through the invoices in the source tree InvoiceLoop : LOOP -- Inspect the current invoice and create a matching Statement SET targetCursor.Statement = THE ( SELECT 'Monthly' AS (XML.Attribute)Type, 'Full' AS (0x03000000)Style[1], I.Customer.FirstName AS Customer.Name, I.Customer.LastName AS Customer.Surname, I.Customer.Title AS Customer.Title, (SELECT FIELDVALUE(II.Title) AS Title, CAST(II.UnitPrice AS FLOAT) * 1.6 AS Cost, II.Quantity AS Qty FROM I.Purchases.Item[] AS II WHERE II.UnitPrice> 0.0 ) AS Purchases.Article[], (SELECT SUM( CAST(II.UnitPrice AS FLOAT) * CAST(II.Quantity AS FLOAT) * 1.6 ) FROM I.Purchases.Item[] AS II ) AS Amount, 'Dollars' AS Amount.(XML.Attribute)Currency FROM sourceCursor AS I WHERE I.Customer.LastName <> 'White' ); -- Turn the current Statement into a bit stream DECLARE StatementBitStream BLOB CAST(ASBITSTREAM(targetCursor.Statement OPTIONS FolderBitStream) AS BLOB); -- If the SELECT produced a result (that is, it was not filtered out by the WHERE -- clause), process the Statement IF StatementBitStream IS NOT NULL THEN -- create a field to hold the bit stream in the result tree CREATE LASTCHILD OF resultCursor Type XML.BitStream NAME 'StatementBitStream' VALUE StatementBitStream; -- Add the current Statement's Amount to the grand total -- Note that the cast is necessary because of the behavior of the XML syntax element SET grandTotal = grandTotal + CAST(targetCursor.Statement.Amount AS FLOAT); END IF; -- Delete the real Statement tree leaving only the bit stream version DELETE FIELD targetCursor.Statement; -- Step onto the next Invoice, removing the previous invoice and any -- text elements that might have been interspersed with the Invoices REPEAT MOVE sourceCursor NEXTSIBLING; DELETE PREVIOUSSIBLING OF sourceCursor; UNTIL (FIELDNAME(sourceCursor) = 'Invoice') OR (LASTMOVE(sourceCursor) = FALSE) END REPEAT; -- If there are no more invoices to process, abandon the loop IF NOT LASTMOVE(sourceCursor) THEN LEAVE InvoiceLoop; END IF; END LOOP InvoiceLoop; END ProcessInvoice; -- Remove the temporary source and target folders DELETE FIELD OutputRoot.XMLNS.Data.SourceMessageTree; DELETE FIELD OutputRoot.XMLNS.Data.TargetMessageTree; -- Finally add the grand total SET resultCursor.GrandTotal = grandTotal;
<Data> <Statement Type="Monthly" Style="Full"> <Customer> <Name>Andrew</Name> <Surname>Smith</Surname> <Title>Mr</Title> </Customer> <Purchases> <Article> <Title>The XML Companion </Title> <Cost>4.472E+1</Cost> <Qty>2</Qty> </Article> <Article> <Title>A Complete Guide to DB2 Universal Database</Title> <Cost>6.872E+1</Cost> <Qty>1</Qty> </Article> <Article> <Title>JAVA 2 Developers Handbook</Title> <Cost>9.5984E+1</Cost> <Qty>1</Qty> </Article> </Purchases> <Amount Currency="Dollars">2.54144E+2</Amount> </Statement> <GrandTotal>2.54144E+2</GrandTotal> </Data>