Article
· Jul 20, 2023 4m read

How to find a specific global variable in the journal file with a command

InterSystems FAQ rubric

You can search for a specific global variable in the journal file using the ByTimeReverseOrder query of the %SYS.Journal.File class and the List query of the %SYS.Journal.Record class.

The role of each query is as follows.

A) %SYS.Journal.File query of the ByTimeReverseOrder class

You can get the journal file name. Results are returned in descending order of journal file name. 

USER>set stmt=##class(%SQL.Statement).%New()

USER>set status=stmt.%PrepareClassQuery("%SYS.Journal.File","ByTimeReverseOrder")

USER>set rs=stmt.%Execute()

USER>while rs.%Next() { write rs.%Get("Name"),! }
c:\intersystems\irishcom\mgr\journal\20230725.002
c:\intersystems\irishcom\mgr\journal\20230725.001
C:\InterSystems\IRISHCom\mgr\journal\20230707.002

 

B) %SYS.Journal.Record query of the List class

You can get journal records for a specific journal file.

If you use 2022.2.0+ , you can run the query with %SQL.Statement.

USER>set status=stmt.%PrepareClassQuery("%SYS.Journal.Record","List")
 
USER>set rs2=stmt.%Execute("c:\intersystems\irishcom\mgr\journal\20230725.002")
 
USER>while rs2.%Next() {if rs2.%Get("GlobalReference")["TEST" write rs2.%Get("GlobalReference"),!}
^["^^c:\intersystems\irishcom\mgr\user\"]TEST(1)
     ・・・ omit ・・・
^["^^c:\intersystems\irishcom\mgr\user\"]TEST(4)
^["^^c:\intersystems\irishcom\mgr\user\"]TEST(5)

If you use pre 2022.2.0 , you can run it with %ResutSet.

USER>set rs2=##class(%ResultSet).%New("%SYS.Journal.Record:List")

USER>do rs2.Execute("c:\intersystems\irishcom\mgr\journal\20230725.002")

USER>while rs2.Next() { if rs2.Get("GlobalReference")["TEST" write rs2.Get("GlobalReference"),!}
^["^^c:\intersystems\cache\mgr\user\"]TEST(1)
     ・・・ omit ・・・
^["^^c:\intersystems\cache\mgr\user\"]TEST(9)
^["^^c:\intersystems\cache\mgr\user\"]TEST(10)

Note:  Specify the journal file name obtained in A) as an argument when executing the query in B).

Please refer to the class reference for the contents that can be obtained by the query in B).
An example method that combines A) and B) is as follows:

/// 1st argument: specify the name of the global(without ^)
/// 2nd argument: specify the name of the folder(without full path)
ClassMethod SearchJournal(name As %String, filename As %String = "")
{
	if $get(name)="" {
		write "Please specify a global name",!
		quit
	}
	//Find the specified global in the currently active journal file
	set stmt=##class(%SQL.Statement).%New()
	set status=stmt.%PrepareClassQuery("%SYS.Journal.File","ByTimeReverseOrder")
	set rs1=stmt.%Execute()
	while rs1.%Next() {
		set jrnfile=rs1.%Get("Name")
		set size=rs1.%Get("Size")
		write "Journal file:",jrnfile,!," file size:",size/1024/1024," MB",!
		do ..SearchGlo(name,jrnfile)
	}
}

ClassMethod SearchGlo(name As %String, jrnfile As %String)
{
	set stmt=##class(%SQL.Statement).%New()
	set status=stmt.%PrepareClassQuery("%SYS.Journal.Record","List")
	write status,!

	//Open journal file from here and look for the specified global
	set rs1=stmt.%Execute(jrnfile,"GlobalReference,NewValue",,,$lb("GlobalReference","[",name))
	
	while rs1.%Next() {
		set glo=rs1.%Get("GlobalReference")
		write " ",glo," = ",rs1.%Get("NewValue"),!
	}
}

 

If you need to use %ResultSet, please refere below:

/// 1st argument: specify the name of the global(without ^)
/// 2nd argument: specify the name of the folder(without full path)
ClassMethod SearchJournal(name As %String, filename As %String = "")
{
	if $get(name)="" {
		write "Please specify a global name",!
		quit
	}
	//Find the specified global in the currently active journal file
	set rs1=##class(%ResultSet).%New()
	set rs1.ClassName="%SYS.Journal.File"
	set rs1.QueryName="ByTimeReverseOrder"
	do rs1.%Execute()
	while rs1.%Next() {
		set jrnfile=rs1.%Get("Name")
		set size=rs1.%Get("Size")
		write "Journal file:",jrnfile,!," file size:",size/1024/1024," MB",!
		do ..SearchGlo(name,jrnfile)
	}
	do rs1.Close()
}

ClassMethod SearchGlo(name As %String, jrnfile As %String)
{
	set rs1=##class(%ResultSet).%New()
	set rs1.ClassName="%SYS.Journal.Record"
	set rs1.QueryName="List"

	//Open journal file from here and look for the specified global
	do rs1.%Execute(jrnfile,"GlobalReference,NewValue",,,$lb("GlobalReference","[",name))
	
	while rs1.%Next() {
		set glo=rs1.%Get("GlobalReference")
		write " ",glo," = ",rs1.%Get("NewValue"),!
	}
	do rs1.Close()
}
Discussion (1)1
Log in or sign up to continue