Update functions - Error handling and counting

Friday, 24 August 2018 · 6 messages · last reply 26 Aug 2018
BB
Bogdan Bogucki24 Aug 2018 23:13 #

Hello,

I am playing with XQUF in BaseX ann I encountered following problems.

1) XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?

Example:

declare %private %updating function cfv:upsertVehicles ($vehicles as
node()*) as empty-sequence() {

    let $db := db:open("db")/cars

    for $v in $vehicles

    let $dbVehicle :=  $db/car[vin = $v/vin]

    return if (not(exists($dbVehicle))) then (insert node $v as last into
$db)

else

               if (exists($dbVehicle)) then (replace node $dbVehicle with
$v)

else ()

};

I have similar problem with error handling. How can I catch and return any error.

Regards Bogdan Bogucki

CG
Christian Grün24 Aug 2018 23:41 #

Hi Bogdan,

Quoted text · Bogdan Bogucki · 1 line
> 1) XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?

BaseX provides the update:output function for that purpose [1]. A solution could look as follows:

  declare %updating function cfv:upsertVehicles(
    $vehicles  as element(car)*
  ) as empty-sequence() {
    let $db := db:open("db")/cars
    let $existing := $vehicles[$db/car/vin = vin]
    return (
      (: replace existing vehicles :)
      for $updated in $existing
      let $old := $db/car[vin = $updated/vin]
      return replace node $old with $updated,
      (: insert new vehicles :)
      insert node ($vehicles except $existing) into $db,
      (: return count :)
      update:output(count($vehicles))
    )
  };
Quoted text · Bogdan Bogucki · 1 line
> I have similar problem with error handling. How can I catch and return any error.

Due to the semantics of XQuery Update, logical errors will be raised before your updates are eventually executed [2]. No updates will be performed if the execution is expected to fail.

Best, Christian

[1] http://docs.basex.org/wiki/Update_Module#update:output [2] http://docs.basex.org/wiki/XQuery_Update#Error_Messages

BB
Bogdan Bogucki25 Aug 2018 17:57 #

Hi Christian,

Thank you it helps me a lot.

I found that update:output-cache() is in module db. Wiki describe output-cache() in update module.

Best Regards Bogdan Bogucki

-----Wiadomość oryginalna----- Od: Christian Grün [mailto:christian.gruen at gmail.com] Wysłano: 24 sierpnia 2018 23:41 Do: Bogdan Bogucki DW: BaseX Temat: Re: [basex-talk] Update functions - Error handling and counting

Hi Bogdan,

Quoted text · Christian Grün · 1 line
> 1) XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?

BaseX provides the update:output function for that purpose [1]. A solution could look as follows:

  declare %updating function cfv:upsertVehicles(
    $vehicles  as element(car)*
  ) as empty-sequence() {
    let $db := db:open("db")/cars
    let $existing := $vehicles[$db/car/vin = vin]
    return (
      (: replace existing vehicles :)
      for $updated in $existing
      let $old := $db/car[vin = $updated/vin]
      return replace node $old with $updated,
      (: insert new vehicles :)
      insert node ($vehicles except $existing) into $db,
      (: return count :)
      update:output(count($vehicles))
    )
  };
Quoted text · Christian Grün · 1 line
> I have similar problem with error handling. How can I catch and return any error.

Due to the semantics of XQuery Update, logical errors will be raised before your updates are eventually executed [2]. No updates will be performed if the execution is expected to fail.

Best, Christian

[1] http://docs.basex.org/wiki/Update_Module#update:output [2] http://docs.basex.org/wiki/XQuery_Update#Error_Messages

CG
Christian Grün25 Aug 2018 20:08 #
Quoted text · Bogdan Bogucki · 1 line
> I found that update:output-cache() is in module db. Wiki describe output-cache() in update module.

Thanks for the info. I have updated our documentation. The function is now called update:cache(), and the legacy function db:output-cache() will be removed in a future version of BaseX.

BB
Bogdan Boguckiin reply to Bogdan Bogucki, 25 Aug 2018, 17:5726 Aug 2018 00:00 #

Hi Christian,

I have question about updates.

I use updating function upsertVehicles in loop. Function downloadVehicles download from url and pass to upsertVehicles function.

declare %updating function cfv:downloadData () as empty-sequence() {
    for $URL in doc("config.xml")/urls
    return (cfv:downloadVehicles($URL) => cfv:upsertVehicles(), admin:write-log("Inserted: " || serialize(update:cache())))
};

When does cache is purged ? In my case cache contains information from previous iterations. If upsertVehicles fail only updates from this function are reverted. What is the scope of the PUL ? Is this query, function or all invocation chain of updating functions ?

I have problem with update:cache(). What is the return type ? Sequence of items or sequence of sequence [....] of items ? When I try get one item like this: update:cache()[1] I receive error: Item expected, sequence found: (0, 0).

Best Regards Bogdan Bogucki

-----Wiadomość oryginalna----- Od: BaseX-Talk [mailto:basex-talk-bounces at mailman.uni-konstanz.de] W imieniu Bogdan Bogucki Wysłano: 25 sierpnia 2018 17:58 Do: 'Christian Grün' DW: 'BaseX' Temat: [basex-talk] ODP: Update functions - Error handling and counting

Hi Christian,

Thank you it helps me a lot.

I found that update:output-cache() is in module db. Wiki describe output-cache() in update module.

Best Regards Bogdan Bogucki

-----Wiadomość oryginalna----- Od: Christian Grün [mailto:christian.gruen at gmail.com] Wysłano: 24 sierpnia 2018 23:41 Do: Bogdan Bogucki DW: BaseX Temat: Re: [basex-talk] Update functions - Error handling and counting

Hi Bogdan,

Quoted text · Bogdan Bogucki · 1 line
> 1) XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?

BaseX provides the update:output function for that purpose [1]. A solution could look as follows:

  declare %updating function cfv:upsertVehicles(
    $vehicles  as element(car)*
  ) as empty-sequence() {
    let $db := db:open("db")/cars
    let $existing := $vehicles[$db/car/vin = vin]
    return (
      (: replace existing vehicles :)
      for $updated in $existing
      let $old := $db/car[vin = $updated/vin]
      return replace node $old with $updated,
      (: insert new vehicles :)
      insert node ($vehicles except $existing) into $db,
      (: return count :)
      update:output(count($vehicles))
    )
  };
Quoted text · Bogdan Bogucki · 1 line
> I have similar problem with error handling. How can I catch and return any error.

Due to the semantics of XQuery Update, logical errors will be raised before your updates are eventually executed [2]. No updates will be performed if the execution is expected to fail.

Best, Christian

[1] http://docs.basex.org/wiki/Update_Module#update:output [2] http://docs.basex.org/wiki/XQuery_Update#Error_Messages

CG
Christian Grün26 Aug 2018 15:13 #

Hi Bogdan,

Quoted text · Bogdan Bogucki · 1 line
> When does cache is purged ? In my case cache contains information from previous iterations.

The update:cache function is non-deterministic: It will return different results before and after items have been cached [1].

I have only used this function for a few tests and in some XQUnit test cases. I don’t think there’ll be a reasonable use for it in productive code.

Quoted text · Bogdan Bogucki · 1 line
> What is the scope of the PUL ?

Please have a look at other available online resources first (e.g. [2,3,4]).

Quoted text · Bogdan Bogucki · 2 lines
> When I try get one item like this: update:cache()[1] I receive error: > Item expected, sequence found: (0, 0).

Thanks for the hint, the static return type for this function in our code base will be fixed in our next stable snapshot.

Cheers Christian

[1] http://docs.basex.org/wiki/Update_Module#update:cache [2] http://docs.basex.org/wiki/XQuery_Update#Concepts [3] https://www.w3.org/TR/xquery-update-10/#dt-pending-update-list [4] http://www.xmlmind.com/tutorials/XQueryUpdate/index.html


⚡Generated with XQuery