Loading TOC...

infodev:transaction

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()

Summary

[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.

Parameters
document-map A map containing a batch of documents to be ingested in a single transaction. Map keys are used to construct the URI used to locate the document to be ingested, such as the $source-location in the function referenced by the $function parameter, below.
ticket-id The ID of the created ticket.
function The ingestion function to apply to each file. This function should call infodev:ingest to insert the document into the database.

The function signature of $function must be of the pattern:
 
    declare function my:function(
        $document as node(),
        $source-location as xs:string,
        $ticket-id as xs:string,
        $policy-deltas as element(info:options)?,
        $context as item()?)
     as xs:string+
      
policy-deltas One or more deltas to be applied to the policy.
transaction-index An integer representing this transaction's index in a sequence of transactions. This is used in error reporting, to distinguish between transactions.
context User-supplied context that is passed to the process function.
error-log-level The error log level. The possible levels are emergency, alert, critcal, error, warning, notice, info, config, debug, fine, finer, or finest. The default level is info.

Example

   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 iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.