Aller au contenu principal
Version: v20 R4

Using the Global Stamp

Vue d’ensemble

4D automatically manages an internal global modification stamp, useful to handle data change tracking implementations, for example to monitor activity, backup, run incremental synchronization, etc.

The global modification stamp is a number, always maintained by 4D, even in case of database restoration, import, etc. Note however that the stamp can be modified using the .setGlobalStamp() function.

Once the data change tracking is configured and enabled, the following actions are automatically executed by 4D at each record modification (add, modify, delete):

  1. The current global modification stamp value is saved in the special "GlobalStamp" attribute of the involved entity. In case of a deletion, a new entity is also added to the `DeletedRecords` table with information about the deleted entity and the current global modification stamp value is saved in the "__Stamp" attribute.

  2. The global modification stamp value is incremented.

This mechanism allows you to identify entities that have been modified, added, or deleted since a point in time, and to implement any appropriate action (see example).

info

Do not confuse the global modification stamp with the internal entity stamp, used for the optimistic locking feature.

Configuring data change tracking

By default, the global modification stamp is not created (the .getGlobalStamp() function returns 0. To enable data change tracking, you need to add special fields and a table to your structure. You can use the contextual menu of the Structure Editor to create automatically all necessary elements.

Structure requirements

To enable data change tracking, the application structure must contain at least one table with a __GlobalStamp field.

In addition, to ensure proper operation of the feature, the following conditions are required:

  • The __GlobalStamp field must must be of type Integer 64 bits, with automatic index, Expose as REST resource, and Invisible properties selected.
  • A __DeletedRecords table must be added, with the following fields:
ChampTypeDescription
__PrimaryKeyTextPrimary key of the deleted entity
__StampInteger 64 bitsGlobal stamp just before the deletion
__TableNameTextName of the deleted entity table
__TableNumberLongintNumber of the deleted entity table

You can only track changes for data in tables having the __GlobalStamp field.

note

In the 4D language, the __GlobalStamp field value should be handled through a Real type variable.

Using the Structure Editor

The 4D Structure Editor allows you to enable or disable data change tracking using a single menu item.

To enable data change tracking:

  1. Select the table(s) for which you want to enable data change tracking.
  2. Right-click on a selected table and select Enable data change tracking in the contextual menu.
  3. A confirmation dialog box is displayed. Cliquez sur OK.

4D then makes the following changes:

  • A preconfigured __GlobalStamp field is added to the table(s).
  • If not already existing, a __DeletedRecords table is added to the structure.

To disable data change tracking:

  1. Select the table(s) for which you want to remove data change tracking.
  2. Right-click on a selected table and select Disable data change tracking in the contextual menu.
  3. A confirmation dialog box is displayed. Cliquez sur OK.

4D then removes the __GlobalStamp field from the table(s). Note that if you want to remove the __DeletedRecords table, you need to do it manually.

Exemple


$tableName:="Employee" $oldStamp:=... //load the previous stamp value
//from which you want to compare the current stamp

If ($oldStamp # ds.getGlobalStamp()) //get all new or modified entities $modifiedEmps:=ds[$tableName].query("__GlobalStamp > :1"; $oldStamp) //get all deleted entities $deletedEmpsInfo:=ds.__DeletedRecords.query("__Stamp > :1 and __TableName = :2";\
$oldStamp; $tableName) End if