6

I am trying to get total size of all the forests which are attached to a particular database.

Using below code I have got the size of all individual forests, but stuck on how to achieve the solution:

for $db-id in xdmp:databases()
let $db-name := xdmp:database-name($db-id)
for $forests in xdmp:forest-status(xdmp:database-forests(xdmp:database($db-name)))
let $space := $forests//forest:device-space
let $f_name := $forests//forest:forest-name
for $stand in $forests//forest:stands
let $f_size := fn:sum($stand/forest:stand/forest:disk-size)
grtjn
  • 20,254
  • 1
  • 24
  • 35
Saahil Gupta
  • 209
  • 2
  • 6

2 Answers2

6

I think you are looking for something like:

xquery version "1.0-ml";

declare namespace forest = "http://marklogic.com/xdmp/status/forest";

for $db-id in xdmp:databases()
let $db-name := xdmp:database-name($db-id)
let $db-size :=
  fn:sum(
    for $f-id in xdmp:database-forests($db-id)
    let $f-status := xdmp:forest-status($f-id)
    let $space := $f-status/forest:device-space
    let $f-name := $f-status/forest:forest-name
    let $f-size :=
      fn:sum(
        for $stand in $f-status/forest:stands/forest:stand
        let $stand-size := $stand/forest:disk-size/fn:data(.)
        return $stand-size
      )
    return $f-size
  )
order by $db-size descending
return $db-name || " = " || $db-size

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
6

It's best to call xdmp:forest-status() with a sequence of forest IDs, rather than make a bunch of individual calls so the work is done in parallel.

xquery version "1.0-ml";

declare namespace fs = "http://marklogic.com/xdmp/status/forest";

let $include-replicas := fn:true()
let $db := xdmp:database("MyDatabase")
for $fs in xdmp:forest-status(xdmp:database-forests($db, $include-replicas))
return
  fn:string-join(
    ( $fs/fs:forest-name, fn:sum($fs/fs:stands/fs:stand/fs:disk-size) ) ! fn:string(.),
    " ")
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Wayne Feick
  • 555
  • 2
  • 4