I common problem I encounter when maintaining Notes applications is that documents are being saved multiple times as the result of a single event/action.
In some cases there may not have been any changes made to the actual data. Reducing the number of times a document is saved can generate a number of benefits to the application.
- Application Performance: The save operation is an expensive one. The fewer the number of saves, the faster the code runs.
- Server Performance: Each and every time a document is saved it creates the potential need for the server to update every view index to establish if the change has an effect on the view. The server may also need to update the full text index. If the data did not change this work results in no change (but the server doesn't know that!)
- Reduced Conflicts: A repeated number of saves can result in a save conflict being generated. It also increases the chances of replication conflicts if the document is being edited on another server.
- Accurate Edit History: The document history is updated each time the document is saved. The date/time of the last change and the identity of the author is somehwat invalid if the actual data did not change. The number of entries in the edit history can also become much larger than it needs to be if additional saves have been performed.
One of the main reasons for multiple saves is the difficulty of keeping track of when changes had been made. e.g. each section of code does a save just in case the next code block doesn't do a save. One of the way to minimize the number of changes is to keep track of whenever any item in a document has been changed and then only save the document if a change has been made (to one ore more items). The .Domino Framework provides one potential solution. Being Object Oriented, the document-based classes maintain an internal iHasChanged variable that is initially set to false. Each of the Property Set code blocks sets the iHasChanged variable to True. The .Domino Framework Property Generator Tool provides the option to include this logic as part of the p[roperty code it generates.
e.g.
Property Get CompanyName As String
If iDocument Is Nothing Then Exit Property
If iDocument.HasItem("CompanyName" ) Then CompanyName$ = iDocument.GetItemValue("CompanyName" )(0)
End Property
Property Set CompanyName As String
If iDocument Is Nothing Then Exit Property
Call iDocument.ReplaceItemValue("CompanyName",CompanyName$)
iHasChanged = True
End Property
It is then possible at the very end of a code block (e.g. event or action) to test to see if any changes have been made and only invoke a save when the variable has been set.
Note: The framework's DominoDocument class has a Save event which supports the above by only saving the document if a change has been made (iHasChanges = true). It also resets the flag after the save.
To further reduce the number of changes it is also suggested that comparisons be made with the existing value before updating the object's property.
e.g.
If Employee.CompanyName$ <> CompanyName$ Then Employee.CompanyName$ = CompanyName$
If Employee.Postcode$ <> Postcode$ Then Employee.Postcode$ = Postcode$
Using the above it is often possible to develop code that only needs to do a single save at the very end if, and only the values of at least one field have been changed.