Un-Typical persistence
During my search for a snapshot of a persistent object,
I met a feature that I would like to share as it could be useful in some special situations.
My trigger was to have a before- and an after-image during unit testing. A typical persistent class may have a storge definition like this
Storage Default
{
<Data name="kDefaultData">
+<Value name="1">
</Value>
</Data>
<DataLocation>^rcc.kD</DataLocation>
<DefaultData>kDefaultData</DefaultData>
<IdLocation>^rcc.kD</IdLocation>
<IndexLocation>^rcc.kI</IndexLocation>
<StreamLocation>^rcc.kS</StreamLocation>
<Type>%Storage.Persistent</Type>
}
Now I applied this change:
Parameter MANAGEDEXTENT = 0; ;extent manager dislikes this change
Storage Default
{
<Data name="kDefaultData">
+<Value name="1">
</Value>
</Data>
<DataLocation>@(%storage_"D")</DataLocation>
<DefaultData>kDefaultData</DefaultData>
<IdLocation>@(%storage_"D")</IdLocation>
<IndexLocation>@(%storage_"I")</IndexLocation>
<StreamLocation>@(%storage_"S")</StreamLocation>
<Type>%Storage.Persistent</Type>
}
All you have to do now to use it:
set %storage="^myGlobal" ;; normal use with ROLLBACK
or set %storage="%myLocalVariable" ;; no ROLLBACK
orset %storage="^||myPPG" ;; no ROLLBACK
and it works as you are used to it.
Except for ROLLBACK as there is, of course, no Journal behind PPG or local variables
A typical use sequence to prepare a check for changes could look similar:
set %storage="^rcc.k"
set obj=##class(rcc.k).%OpenId(id) ;; get original
do obj.%SetModified(1) ;; prepare for %Save
set %storage="^||rcc" ;; location of copy
set sc=obj.%Save() ;; write copy to temp storage
//// carry on with testing and changes and find what happened
I think it's worth to share it.