Thursday, February 27, 2014

Marklogic: Point-in-Time queries


Enabling Point-in-time queries in Admin Interface

  • In order to use point-in-time queries in a database, you must set up merges to preserve old versions of fragments.
  • To access the Merge Policy Configuration page, click the Databases > db_name > Merge Policy link from the tree menu of the Admin Interface.
  • To set the merge timestamp parameter to the current timestamp, click the get current timestamp button on the Merge Control Configuration page and then Click OK.

Fig1: Enabling the point-in-time queries in Training DB.


Example: Querying Deleted Documents

Step 1: Execute the below query to insert a sample XML into Marklogic DB by using query console(http://localhost:8000/qconsole/). Copy the below code and made few changes and execute the query in query console.

(: defining the version of the xquery used for ML-DB :)
xquery version "1.0-ml";

(: delimited input data for XML :)
let $record := "778171;srinivasan;SE,557474;raman;ITA,663344;hachiko;ITC"

(: prepare the XML from input data :)
let $recordToXML := element employeeXML { for $each in fn:tokenize($record,",")
return
element employee {
let $eachrec := fn:tokenize($each,";")
return
(element empid {$eachrec[1]},element empname {$eachrec[2]},element empdesignation {$eachrec[3]})
}
}

(: provide the name for your sample XML name :)
let $sampleXMLfilename := "/examples/empsmall.xml"

(: insert this XML into Marklogic DB :)
return xdmp:document-insert($sampleXMLfilename,$recordToXML)

Note: You can replace the coloured details with your own details.

Step-2: Execute the below code in query console to view the document from Marklogic DB

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

(: specify the document URI - unique resource identifier:)
let $docURI := "/examples/empsmall.xml"

(: retrieve the document by using "doc" function:)
let $viewDoc := fn:doc($docURI)

return $viewDoc


Step-3: execute the below query to delete the document from DB

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

(: specify the document URI - unique resource identifier:)
let $docURI := "/examples/empsmall.xml"

(: delete the document by using XDMP function:)
let $deleteDoc := xdmp:document-delete($docURI)

return ()

Step-4: Run the Step 2 again for view the document. It returns the empty sequence because document was just deleted.

In Marklogic it is possible to travel back in TIME and view the deleted document”

Step-5: Run a point-in-time query, specifying the current timestamp (this is semantically the same as querying the document without specifying a timestamp):

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

(: eval function usefull when want to run the query for specific transaction :)
xdmp:eval("doc('/examples/empsmall.xml')", (),
<options xmlns="xdmp:eval">
  <timestamp>{xdmp:request-timestamp()}</timestamp>
</options>)

(: returns the empty sequence because the document has been deleted :)

Step-6: Run the point-in-time query at one less than the current timestamp, which is the old timestamp in this case because only one change has happened to the database. The following query statement returns the old document.

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

(: Below eval function using the older timestamp to fetch the deleted document version. Here I have copied this time stamp from Fig-1: field merge-timestamp  :)
xdmp:eval("doc('/examples/empsmall.xml')", (),
<options xmlns="xdmp:eval">
  <timestamp>13934835606579742</timestamp>
</options>)
(: returns the deleted version of the document :)

No comments:

Post a Comment