Last active
July 16, 2019 13:44
-
-
Save derms/c723ddb2c2ddeed4f400bd7c58bd07a1 to your computer and use it in GitHub Desktop.
Get forest in memory size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare namespace forest="http://marklogic.com/xdmp/status/forest"; | |
fn:concat("Database Name,Memory (Mb),In Memory Stands (Mb),Index etc (Mb),Disk Size (Gb),Fragments"), | |
for $database in xdmp:databases() | |
let $db-mem-size := fn:sum(for $forest in xdmp:database-forests($database) return fn:sum(xs:int(xdmp:forest-status($forest)/forest:stands/forest:stand/forest:memory-size/text()))) | |
let $db-mem-size-2 := fn:sum(for $forest in xdmp:database-forests($database) return fn:sum(xs:int(xdmp:forest-status($forest)/forest:stands/forest:stand[forest:disk-size lt forest:memory-size]/forest:memory-size/text()))) | |
let $db-mem-size-3 := fn:sum(for $forest in xdmp:database-forests($database) return fn:sum(xs:int(xdmp:forest-status($forest)/forest:stands/forest:stand[forest:disk-size gt forest:memory-size]/forest:memory-size/text()))) | |
let $db-disk-size := fn:sum(for $forest in xdmp:database-forests($database) return fn:sum(xs:int(xdmp:forest-status($forest)/forest:stands/forest:stand/forest:disk-size/text()))) | |
let $db-fragment-count := fn:sum(for $forest in xdmp:database-forests($database) return fn:sum(xs:int(xdmp:forest-counts($forest)/forest:stands-counts/forest:stand-counts/forest:active-fragment-count/text()))) | |
order by $db-mem-size descending | |
return | |
fn:concat(xdmp:database-name($database),",",$db-mem-size,",",$db-mem-size-2,",",$db-mem-size-3,",",$db-disk-size div 1000,",",$db-fragment-count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
for $host in xdmp:group-hosts(xdmp:group("Default")) | |
let $mem := for $forest in xdmp:host-forests($host ) | |
return xs:unsignedLong(xdmp:forest-status($forest)//*:memory-size/text()) | |
return xdmp:host-name($host) || " forests using " || fn:sum($mem) || " MB of memory" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; | |
declare namespace d="http://marklogic.com/xdmp/database"; | |
declare variable $IS-LINUX := fn:true(); | |
declare variable $IS-MAC := fn:false(); | |
declare variable $db-name := "Documents"; | |
declare function local:qname-key($uri,$nam) | |
{ | |
xdmp:add64( | |
xdmp:mul64( | |
xdmp:add64( | |
xdmp:mul64(xdmp:hash64($uri),5), | |
xdmp:hash64($nam)), | |
5), | |
xdmp:hash64("qname()")) | |
}; | |
declare function local:attr-key($euri,$enam,$auri,$anam) | |
{ | |
xdmp:add64( | |
xdmp:mul64( | |
xdmp:add64( | |
xdmp:mul64(local:qname-key($euri,$enam),5), | |
xdmp:hash64("/@")), | |
5), | |
local:qname-key($auri,$anam)) | |
}; | |
declare function local:element-range-index($uri, $nam) | |
{ | |
xdmp:integer-to-hex(local:qname-key($uri,$nam)) | |
}; | |
declare function local:element-attribute-range-index($euri,$enam,$auri,$anam) | |
{ | |
xdmp:integer-to-hex(local:attr-key($euri,$enam,$auri,$anam)) | |
}; | |
declare function local:get-element-range-index-keys($map as map:map,$db-name as xs:string) | |
{ | |
let $ranges := admin:database-get-range-element-indexes(admin:get-configuration(), xdmp:database($db-name)) | |
for $range in $ranges | |
let $uri := fn:string($range/d:namespace-uri) | |
let $coll := fn:string($range/d:collation) | |
let $ckey := | |
if ( | |
$coll eq "http://marklogic.com/collation/codepoint" | |
or $range/d:scalar-type ne "string" | |
) | |
then "-" | |
else fn:concat("-", xdmp:integer-to-hex(xdmp:hash64($coll)), "+") | |
for $name in fn:tokenize(fn:string($range/d:localname), " ") | |
let $nam := fn:string($name) | |
let $key := local:element-range-index($uri, $nam) | |
for $pos in ( | |
"", | |
if (data($range/d:range-value-positions)) then "=" else () | |
) | |
let $fkey := fn:concat($key, $ckey, $range/d:scalar-type, $pos) | |
let $uri := if($uri) then $uri else "empty" | |
return map:put($map,$fkey,fn:concat($uri,":",$nam)) | |
}; | |
declare function local:get-element-attribute-range-index-keys($map as map:map,$db-name as xs:string) | |
{ | |
let $ranges := admin:database-get-range-element-attribute-indexes(admin:get-configuration(),xdmp:database($db-name)) | |
for $range in $ranges | |
let $euri := fn:string($range/d:parent-namespace-uri) | |
let $auri := fn:string($range/d:namespace-uri) | |
let $coll := fn:string($range/d:collation) | |
let $ckey := | |
if ( | |
$coll eq "http://marklogic.com/collation/codepoint" | |
or $range/d:scalar-type ne "string" | |
) | |
then "-" | |
else fn:concat("-", xdmp:integer-to-hex(xdmp:hash64($coll)), "+") | |
for $ename in fn:tokenize(fn:string($range/d:parent-localname), " ") | |
let $enam := fn:string($ename) | |
for $aname in fn:tokenize(fn:string($range/d:localname), " ") | |
let $anam := fn:string($aname) | |
for $pos in ( | |
"", | |
if (data($range/d:range-value-positions)) then "=" else () | |
) | |
let $key := local:element-attribute-range-index($euri, $enam, $auri, $anam) | |
let $fkey := fn:concat($key, $ckey, $range/d:scalar-type, $pos) | |
return map:put($map,$fkey,fn:concat($euri,":",$enam,"@",$auri,":",$anam)) | |
}; | |
declare function local:default-data-directory() as xs:string{ | |
if($IS-LINUX) then | |
"/var/opt/MarkLogic" | |
else if($IS-MAC) then | |
"/Users/dsmyth/Library/Application Support/MarkLogic/Data/" | |
else | |
"C:\Program Files\MarkLogic\Data" | |
}; | |
declare function local:get-slash(){ | |
if($IS-LINUX or $IS-MAC) then "/" else "\" | |
}; | |
declare function local:get-forest-data-directory($forest-id) as xs:string{ | |
let $dir := admin:forest-get-data-directory(admin:get-configuration(),$forest-id) | |
let $dir := if($dir) then $dir else local:default-data-directory() | |
let $dir := if(fn:ends-with($dir,local:get-slash())) then $dir else fn:concat($dir,local:get-slash()) | |
return | |
fn:concat($dir,"Forests",local:get-slash(),xdmp:forest-name($forest-id)) | |
}; | |
declare function local:get-stand-directories($dir as xs:string){ | |
if (xdmp:filesystem-file-exists($dir)) then | |
for $entry in xdmp:filesystem-directory($dir)//dir:entry | |
return | |
if(($entry/dir:type = "directory") and fn:not($entry/dir:filename/text() = ("Journals","Large"))) then | |
$entry//dir:pathname/text() | |
else() | |
else() | |
}; | |
declare function local:add-to-memory-map($map as map:map,$entry as element(dir:entry)){ | |
let $filename := fn:replace($entry/dir:filename/text(),"-$","") | |
let $null := if(map:get($map,$filename)) then () else map:put($map,$filename,0) | |
return | |
map:put($map,$filename,map:get($map,$filename) + xs:long($entry/dir:content-length/text())) | |
}; | |
declare function local:format-number($number){ | |
xs:string($number) | |
}; | |
let $map := map:map() | |
let $name-map := map:map() | |
let $null := (local:get-element-range-index-keys($name-map,$db-name),local:get-element-attribute-range-index-keys($name-map,$db-name)) | |
let $null := | |
for $forest in admin:database-get-attached-forests(admin:get-configuration(),xdmp:database($db-name)) | |
return | |
for $dir in local:get-stand-directories(local:get-forest-data-directory($forest)) | |
return | |
for $entry in xdmp:filesystem-directory($dir)//dir:entry | |
return | |
local:add-to-memory-map($map,$entry) | |
return | |
( | |
"Range Index + Lexicons", | |
"======================", | |
"", | |
for $key in map:keys($map) | |
let $range-index := map:get($name-map,$key) | |
let $range-index := if(fn:lower-case($key) = "4c8c228348a3b60d-string") then "URI Lexicon" else $range-index | |
let $range-index := if(fn:lower-case($key) = "702c7a5fff541f5e-string") then "Collection Lexicon" else $range-index | |
let $range-index := if($range-index) then $range-index else "Mapping not known" | |
where fn:not(fn:matches($key,"^[^\d]*$")) | |
order by map:get($map,$key) descending | |
return | |
fn:concat(xdmp:host-name(),$key,",",$range-index,",",local:format-number(map:get($map,$key))) | |
, | |
"", | |
"Memory Mapped, but not Range Index", | |
"==================================", | |
"", | |
for $key in map:keys($map) | |
where (fn:matches($key,"^[^\d]*$") and fn:not($key = ("ListData","TreeData"))) | |
order by map:get($map,$key) descending | |
return | |
fn:concat(xdmp:host-name(),$key,",",local:format-number(map:get($map,$key)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment