
infodev:transaction( $document-map as map:map, $ticket-id as xs:string, $function as xdmp:function, $policy-deltas as element(info:options)?, $transaction-index as xs:integer, $context as item()?, [$error-log-level as xs:string?] ) as empty-sequence()
[DEPRECATED] This function ingests a batch of documents in a single invoked transaction.
The batch of documents is contained in $document-map.
Ingestion is done using a function named by the $function parameter.
xquery version "1.0-ml";
import module namespace info = "http://marklogic.com/appservices/infostudio"
at "/MarkLogic/appservices/infostudio/info.xqy";
import module namespace infodev = "http://marklogic.com/appservices/infostudio/dev"
at "/MarkLogic/appservices/infostudio/infodev.xqy";
(: Define a process-file function. :)
declare function local:process-file(
$document as node()?,
$source-location as xs:string,
$ticket-id as xs:string,
$policy-deltas as element(info:options)?,
$context as item()?)
{
(: You can modify the file here :)
infodev:ingest($document, $source-location, $ticket-id)
};
let $function := xdmp:function(xs:QName("local:process-file"))
let $annotation := <info:annotation>Loading XML Shakespeare docs</info:annotation>
(: Create a ticket to load contents into the myDB database. :)
let $ticket := infodev:ticket-create($annotation, "myDB", (), ())
(: Gather the documents to be loaded into $entries. :)
let $playdir := "C:\Shakespeare\bill_xml"
let $entries := for $x in xdmp:filesystem-directory($playdir)//dir:pathname/text()
[ends-with(lower-case(.), ".xml")]
return xdmp:document-get($x)
(: Determine the number of documents; the number of documents to be loaded in each
transaction, and the total number of transactions needed to load all of the documents. :)
let $entry-count := fn:count($entries)
let $transaction-size := 5 (: Typically, this value would be extracted from the policy. :)
let $total-transactions := ceiling($entry-count div $transaction-size)
(: Create a map containing the documents to be loaded. :)
let $transactions :=
for $i at $index in 1 to $total-transactions
let $map := map:map()
let $start := (($i -1) *$transaction-size) + 1
let $finish := min((($start - 1 + $transaction-size),$entry-count))
let $put := for $entry in ($entries)[$start to $finish]
(: Create an ID for each document that consists of the date, a number, and a
filename contructed from the title of the play, minus spaces. :)
let $title := fn:replace($entry/PLAY/TITLE, " ", "")
let $id := fn:concat("/", fn:format-dateTime(fn:current-dateTime(),
"[Y01]-[M01]-[D01]_[H01]-[m01]-[s01]-[f01]") ,
"/", $index ,"/", $title, ".xml")
return map:put($map,$id,$entry)
return $map
(: Create the transactions to load the documents into the database. :)
let $ingestion :=
for $transaction at $index in $transactions
return
try {
infodev:transaction($transaction, $ticket, $function, (), $index,(),())
} catch($e) {
infodev:handle-error($ticket, concat("transaction ",$index), $e)
}
(: When the load transactions have completed, set the ticket status to "completed". :)
let $_ := infodev:ticket-set-status($ticket, "completed")
return ()
(: This query creates multiple transactions to load the XML files located in the
"C:\Shakespeare\bill_xml" directory into the myDB database. Five documents are
loaded in each transaction. :)
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.