Skip to Content

Note:  The code for the sample application is now published.


In this blog, I will be talking about batch processing which allows grouping multiple operations into a single HTTP request payload.  If network bandwidth is a concern, then batch processing allows you to send a single HTTP request to the backend server thereby avoiding multiple round trips.  A request within a ChangeSet can be referenced by subsequent requests within the same ChangeSet by referring to the Content-ID. This allows developers to implement deep inserts between entity sets that have parent child relationships.  The other advantage using batch processing is that you can provide transactional support within a ChangeSet. Transactional support however requires additional modifications on the backend and therefore not discussed in this blog.

What can a batch request contain?

The body of the batch request must be made up of an ordered series of Query operations and / or ChangeSets. Based on this, it is important to note that the sequence in which you add the Query operations and / or ChangeSets to the body of the batch request matters.  Now, let us look into what constitutes a Query operation (in the context of a batch request).  Query operations can consist of READ operations to read 1 or more entities. Query operations can also contain Function invocations.   Ok, now that we understand what a Query operation is, let us look into what constitutes a ChangeSet.  ChangeSet can consist of an unordered group of 1 or more CREATE, UPDATE or DELETE operation.   You can also reference requests in a ChangeSet.  One quick note is that the sequence of the CREATE, UPDATE or DELETE operation inside a ChangeSet does not matter, unless you reference a request in a ChangeSet.

/wp-content/uploads/2015/05/batch1_696352.png

How to create a ChangeSet and add operations to it?

Creating a ChangeSet is fairly easy in Windows SMP SDK.  There are no parameters for the constructor.

this.ChangeSet = new ODataRequestChangeSet();

Adding operations to the ChangeSet is also fairly easy.  Note that you can only add CREATE, UPDATE or DELETE operations to a ChangeSet.  

Adding CREATE operation


var item = new ODataRequestParametersSingle(“Suppliers”, RequestMode.Create, entity);

this.ChangeSet.Add(item);

Adding UPDATE operation


var item = new ODataRequestParametersSingle(“Suppliers(“ + id + “)”, RequestMode.Update, entity);

this.ChangeSet.Add(item);

Adding DELETE operation


var item = new ODataRequestParametersSingle(“Suppliers(“ + id + “)”, RequestMode.Delete);

this.ChangeSet.Add(item);

ChangeSet can have 1 or more CREATE, UPDATE or DELETE operations.  The order in which you add the operations does not matter.

How to create a Query operation?

A Query operation is basically a READ operation or a Function invocation.  Both of them can be created in a similar fashion.

var item = new ODataRequestParametersSingle(collectionName + “(“ + readId + “)”, RequestMode.Read);

How to create a batch request and add ChangeSets and Query operations to it?

Creating a batch request is fairly straight forward.  There are no parameters for the constructor.

this.BatchRequest = new ODataRequestParametersBatch ();

Adding a ChangeSet to a batch request

This assumes that you have already added 1 or more operations to this ChangeSet.

if (this.ChangeSet.Count > 0)

{

   this.BatchRequest.Add(ChangeSet);

}

Adding a Query operation to a batch request


var item = new ODataRequestParametersSingle(collectionName + “(“ + readId + “)”, RequestMode.Read);

this.BatchRequest.Add(item);

The sequence in which the ChangeSets and Query operations are added to a batch request is important.

How to execute a batch request?

Once all the ChangeSets and Query operations have been added to the batch request, the batch request can be submitted as a single HTTP POST request.  This is done asynchronously and the corresponding response can be parsed. This assumes that you already have an Online Store opened.  You simply call the ScheduleRequest method of the Online Store and pass the batch request as a parameter.

this.ResponseList = ((IReadOnlyCollection<IODataResponseBatchItem>)((IODataResponseBatch)((await this.Store.ScheduleRequest(this.BatchRequest).Response))).Responses);

           

How to parse the response?

The response from the server will be in the same exact order as the batch request.  For example, if a batch request contained the following…

Batch request

  1. ChangeSetA
  2. Query operation
  3. Query operation
  4. ChangeSetB
  5. Query operation
  6. ChangeSetC
  7. ChangeSetD

Then the response will also be in the same order…

  1. ChangeSetA
  2. Query operation
  3. Query operation
  4. ChangeSetB
  5. Query operation
  6. ChangeSetC
  7. ChangeSetD

However, the operations within a ChangeSet may not be in the same order.

Simply iterate through the response collection.  For each item, check if it is a ChangeSet or a Query operation.

If it’s a ChangeSet


A ChangeSet has 1 or more operations inside of it.  So iterate through the ChangeSet to find the response for each operation.

if (item is IODataResponseChangeSet)

{

   foreach (var element in ((ODataResponseChangeSet)item).Responses)

   {

      BatchRequestItems.Add(“ODataResponseChangeSet Status Code: “ + element.Headers[“http.code”].ElementAt(0));

   }

}

The batch response is parsed.  The first check is to determine if the item in the response list is a ChangeSet.  If it is a ChangeSet, then the results for the various operations inside a ChangeSet are enumerated and the HTTP status code is displayed to the end user.

If it’s a Query operation


If it’s a Query operation, simply check the status of the response code…

else if (item is IODataResponseSingle)

{

   var response = (ODataResponseSingle)item;

   this.EntityList.Add((SAP.Data.OData.Online.ODataEntity)(response).Payload);

                   

   BatchRequestItems.Add(“ODataResponseSingle Status Code: “ + response.Headers[“http.code”].ElementAt(0));

}

The batch response is parsed.  The second check is to determine if the item in the response list is a Query operation. If it is a Query operation, then the payload of the READ operation is added to the EntityList.  Also, the HTTP status code is displayed to the end user.

Ok, that concludes the blog on batch processing.  See you at the next blog.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply