/wp-content/uploads/2015/04/softstate_685624.png

Prerequisites


SAP Gateway SP9+ is required.


You should have done the following steps:

#2 – How To Implement $skiptoken

#3 – How To Implement Lightening Fast OData Services with Exchange Table


How It Works


This H2G will explain how to implement both Server Side Paging and Delta Query altogether in SAP Gateway.

/wp-content/uploads/2015/04/useboth_685646.png

Why do we need to use both? – because, for Offline Store, the Server Side Paging is for avoiding the HTTP timeout with the large volume of entities, while Delta Query is for optimizing the payload volume after the Offline Store is initialized and created. Both technique complements each other.


Here’s a quick idea how to do it:

/wp-content/uploads/2015/04/steps_685647.png

One important remark – you have to be aware of the fact that the Table data (= entity set) being “paged” by Server Side Paging can be out of date immediately. This means during the Server Side Paging calculation, the pages downloaded into an Offline Store is not the latest data anymore. So it is very important that (1) you obtain both a Table data AND a deltatoken as a “snapshot”. And a next fetch via Delta Query can provide you the correct delta data since you took that snapshot.


In order to take a correct snapshot data, (2) we need to keep both Table data and deltatoken in a stateful session – until the delta token is sent to the Offline Store. This ensures the data consistency so that the Offline Store can do the Delta Query for the right delta data after the snapshot is taken.


Once you keep a snapshot in the stateful session, (3) you can split the Table data by means of Server Side Paging. And the last page should contain the deltatoken for the Offline Store. The Offline Store will use it in the next Delta Query for the delta data.


So we need to learn how to use a Stateful Session in OData services. “Soft State” feature introduced in SAP Gateway SP9 can do the job.

Step-by-Step Procedure


– Obtaining Table and Skiptoken Value


1. Your Query operation should have the code as written here. So the current code already obtains both the TravelAgency table and a delta token string.

/wp-content/uploads/2015/04/obtain_685648.png

– Activating Soft State

1. Let’s activate Soft State. First off go to SEGW and find the “…_MPC_EXT” artifact and choose Go to ABAP Workbench.

/wp-content/uploads/2015/04/1_688804.png

2. Here you’ll add one method implementation. Find the “DEFINE” method press Redefine icon.

/wp-content/uploads/2015/04/2_688934.png

3. You’ll have following implementation – this enables your MPC class to handle Soft State.

/wp-content/uploads/2015/04/3_688935.png

01 method DEFINE.
02   super->define( ).
03   model->set_soft_state_enabled( abap_true ).
04 endmethod.

Activate it.


4. Go back SEGW and select GetEntitySet.

/wp-content/uploads/2015/04/4_688936.png

5. Open it in ABAP Workbench – and find the “/IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_START” method and press Redefine icon.

/wp-content/uploads/2015/04/5_688937.png

6. You’ll have an empty implementation. This method will be called back when a soft state session starts. You can insert any code whenever necessary however for this use case, we can keep it empty. Just save it.

/wp-content/uploads/2015/04/6_688938.png

7. Do the same redefinition for the “/IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_END” method. This is another call back method when the soft state session ends. Leave it empty and save it. Both Start and End methods are required for the Soft State activation later.

/wp-content/uploads/2015/04/7_688939.png

8. Go back to SEGW and choose your service maintenance item and select “Maintain”.

/wp-content/uploads/2015/04/8_688940.png

9. Now you have the current Sesssion Timeout value as “00:00:00”. We need to change it.

/wp-content/uploads/2015/04/9_688941.png

10. Select ICF Node button and Configure (SICF).

/wp-content/uploads/2015/04/10_688942.png

11. Find the node and double click on it.

/wp-content/uploads/2015/04/11_688946.png

12. Here you can change the Session Timeout value – enter the value of how long you want to hold a stateful session.

/wp-content/uploads/2015/04/12_688947.png

13. Save it and you’ll find the new Session Timeout value.

/wp-content/uploads/2015/04/13_688948.png

14. Find “Load Metadata” button and press it. You would find the “Soft-state status” column turns from “Not Supported” to “Inactive”.

/wp-content/uploads/2015/04/14_688949.png

Tip: If you don’t see the status change, clear the cache by the two transactions “/IWFND/CACHE_CLEANUP” and “/IWBEP/CACHE_CLEANUP”.

/wp-content/uploads/2015/04/15_688950.png

/wp-content/uploads/2015/04/16_688951.png

16. Additionally – make sure if your Security Session Status is activated via the transaction “SICF_SESSIONS”. This is a must checkpoint even though you have successfully activated the Soft State.

/wp-content/uploads/2015/04/17_688952.png

17. Now find the “Soft State” button and press it.

/wp-content/uploads/2015/04/18_688953.png

/wp-content/uploads/2015/04/19_688954.png

18. You should see the Soft-state status “Active”. Now your stateful session is activated!

/wp-content/uploads/2015/04/20_688964.png


– Keeping Values in a Stateful OData Session by Soft State

/wp-content/uploads/2015/04/keep_685649.png

1. How do we keep the Table and deltatoken in a stateful session? Declare them as Static Atttributes of OData class – we can keep it in the session via Soft State. The static values in Soft State persist data in a user specific session. 


Go to SEGW and open ABAP Workbench.

/wp-content/uploads/2015/04/4_688936.png

2. Select the “Attributes” tab. Declare a class attribute “MT_ENTITYSET”. This class member variable will hold the table data during the session.


Enter the following values:

Attribute Name: MT_ENTITYSET

Level: Static Attribute

Visibility: Protected

Typing: Type

Associated Type: STRAVELAG

/wp-content/uploads/2015/04/21_688994.png

3. The entered values in the previous step declared it as a structure. We need to declare it as table structure – here’s how to do it. Click on the arrow icon next to the Associated Type column. You’ll go to the code declaration – and add “table of” statement as highlighted in blue in the capture.

/wp-content/uploads/2015/04/22_688996.png

Save it – now you have MT_ENTITYSET attribute in STRAVELAG table structure.


4. Add another attribute for keeping a delta token value.


Attribute Name: MV_DELTA_TOKEN

Level: Static Attribute

Visibility: Protected

Typing: Type

Associated Type: TIMESTAMP

/wp-content/uploads/2015/04/23_688997.png

Now you can keep both Table and Deltatoken in a stateful session!


5. Let’s enhance your current Query operation code so that it keeps both Table and Deltatoken as class attributes. Your Query operation should have the code as written here. Replace the lines of #05 to #10 with following codes:

...
05 * load the entityset into the session variables
06   IF me->mt_entityset IS INITIAL.
07     SELECT * FROM stravelag INTO TABLE lt_entityset.
08     me->mt_entityset = lt_entityset.
09 * delta token value during the session
10     GET TIME STAMP FIELD me->mv_delta_token.
11   ENDIF.
...

This code keeps the variables in a stateful session. And now that we declared these variables as class attributes, you can delete the existing declaration of the lv_delta_token now.


You can activate it. We’ll tweak the current code so that Server Side Paging and deltatoken logic can live together in the next step.

– Splitting a Table into Pages


1. Essentially you can simply put the Server Side Paging logic as you learned. However you should replace the “lt_entityset” with “me->mt_entityset”, as we want to remember the fact that it is a “snapshot”. The code in Appendix has the entire GET_ENTITYSET implementation. The lines between “Skiptoken starts” and “Skiptoken ends” shows you how the “me->entityset” variable is being used.

/wp-content/uploads/2015/04/split_685650.png

The thing we should note here is the way to clean up the Table and Deltatoken in the last page. 

01 * if the skiptoken is empty, it is the end of chunk
02   IF es_response_context-skiptoken IS INITIAL.
03     es_response_context-deltatoken = me->mv_delta_token.
04 * the table content & delta token should be valid til the delta token sent
05     CLEAR: me->mt_entityset,
06            me->mv_delta_token.
07   ENDIF.

#02 checks if “es_response_context-skiptoken” is empty or not – if it is empty, that indicates the page you’re cropping is the last one. You need to send the deltatoken instead of skiptoken.

#03 set the delta token value, which has been in the stateful session (as a snapshot).

#05 and #06 does a very important job to clean up the snapshot data in the stateful session – so the entire Table is being sent to the Offline Store!

– Testing Server Side Paging and Delta Token

Just like you had tested the $skiptoken before, try running the Query operation from either the browser or REST Client. Have a look at the <link> data at the bottom of the OData payload – there should be a $skiptoken we have implemented. You can try copy this generated $skiptoken URL as the OData URL and see how the next $skiptoken data looks like – for this example it continues to show the next chunk starting from the 51st entity.

/wp-content/uploads/2015/04/token_689796.png

…and in the last page, you’ll see the deltatoken value instead of $skiptoken. Offline Store will understand the sequence of $skiptoken and deltatoken and loads the entityset in the store for offline usage.

/wp-content/uploads/2015/04/1_688804.png

…congratulations, now your Query operation has Server Side Paging and Delta Query!


Note: during the EFI implementation, we had embedded a few lines of code for Soft State as commented. These codes are required in order to properly commit record data in Exchange Table with Soft State.

– Further Reading

How to use Soft-State support for OData services – Introductory H2G of Soft State. It also talks about the response time benefit too.

List of H2Gs


Blog – SMP3 OData SDK – Performance Tuning with Offline Store


Appendix – Query Operation with Server Side Paging and Delta Token

method TRAVELAGENCYSET_GET_ENTITYSET.

  DATA: lt_entityset   TYPE TABLE OF stravelag,
* variables for $skiptoken
        ls_entity           TYPE stravelag,
        iv_paging_max_chunk TYPE i VALUE 50,
        lv_index_beg        TYPE i,
        lv_index_end        TYPE i,
        lv_skiptoken        TYPE string,
        lv_token_len        TYPE i,
        lv_token_beg        TYPE c,
        lv_token_end        TYPE c,
        lv_table_size       TYPE i.

* load the entityset into the session variables
  IF me->mt_entityset IS INITIAL.
    SELECT * FROM stravelag INTO TABLE lt_entityset.
    me->mt_entityset = lt_entityset.
* delta token value during the session
    GET TIME STAMP FIELD me->mv_delta_token.
  ENDIF.

********************
* Skiptoken starts *
********************
  lv_skiptoken = io_tech_request_context->get_skiptoken( ).

* Get number of entries
  DESCRIBE TABLE me->mt_entityset LINES lv_table_size.
  IF lv_table_size IS INITIAL.
    CLEAR es_response_context-skiptoken.
    EXIT.
  ENDIF.

* Set Skiptoken only if more entries than Paging max number
  IF lv_table_size < iv_paging_max_chunk.
    lv_index_beg = 1.
    lv_index_end = lv_table_size + 1.
  ELSE.

    IF lv_skiptoken IS NOT INITIAL.
      lv_token_len = STRLEN( lv_skiptoken ) - 1.
      IF lv_token_len > 0.
        lv_token_beg = lv_skiptoken(1).
        lv_token_end = lv_skiptoken+lv_token_len(1).
        IF lv_token_beg = '''' AND lv_token_end = ''''.
          lv_token_len = lv_token_len - 1.
          lv_skiptoken = lv_skiptoken+1(lv_token_len).
        ENDIF.
      ENDIF.

* Determine indices for BEG and END
      lv_token_len = STRLEN( lv_skiptoken ).
      IF lv_token_len > 0   AND
         lv_token_len < 10  AND
         lv_skiptoken CO '0123456789'.
        lv_index_beg = lv_skiptoken.
      ELSE.
        lv_index_beg = 1.
      ENDIF.

    ELSE.
      lv_index_beg = 1.
    ENDIF.
    lv_index_end = lv_index_beg + iv_paging_max_chunk.
  ENDIF.

  IF lv_index_end <= lv_table_size.
    es_response_context-skiptoken = lv_index_end.
  ENDIF.
  CONDENSE es_response_context-skiptoken.

  LOOP AT me->mt_entityset INTO ls_entity.
    IF sy-tabix <  lv_index_beg. CONTINUE. ENDIF.
    IF sy-tabix >= lv_index_end. EXIT. ENDIF.
    INSERT ls_entity INTO TABLE et_entityset.
  ENDLOOP.
********************
* Skiptoken ends   *
********************

* if the skiptoken is empty, it is the end of chunk
  IF es_response_context-skiptoken IS INITIAL.
    es_response_context-deltatoken = me->mv_delta_token.
* the table content & delta token should be valid til the delta token sent
    CLEAR: me->mt_entityset,
           me->mv_delta_token.
  ENDIF.

endmethod.
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