Skip to content

Instantly share code, notes, and snippets.

View ableasdale's full-sized avatar

Alex Bleasdale ableasdale

  • Cyera
  • Exeter
View GitHub Profile
@ableasdale
ableasdale / group-level-caches.xqy
Created December 29, 2015 23:40
Group Level Cache settings template
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare variable $SIXTEENTH as xs:unsignedInt := 1024;
declare variable $EIGHTH as xs:unsignedInt := 2048;
declare variable $PARTITIONS as xs:unsignedInt := 1;
let $config := admin:get-configuration()
let $groupid := admin:group-get-id($config, "Default")
@ableasdale
ableasdale / term-keys.xqy
Created December 30, 2015 09:13
Get unique term keys from xdmp:plan
xquery version "1.0-ml";
declare namespace qry="http://marklogic.com/cts/query";
declare variable $QUERY as cts:query := cts:word-query( ("one", "two") );
declare function local:get-keys($query as cts:query) as xs:unsignedLong* {
fn:distinct-values(fn:data(xdmp:plan( cts:search(doc(), $QUERY ) )//qry:key))
};
@ableasdale
ableasdale / load-trusted-pem-certificate.xqy
Created January 28, 2016 12:17
Using Query Console to load a trusted certificate into MarkLogic Server
xquery version "1.0-ml";
import module namespace pki = "http://marklogic.com/xdmp/pki" at "/MarkLogic/pki.xqy";
pki:insert-trusted-certificates(
xdmp:document-get("/OurCertificateLocation/DemoLabCA.pem",
<options xmlns="xdmp:document-get">
<format>text</format>
</options>)
)
@ableasdale
ableasdale / configure-ssl-client-certificate.xqy
Created January 28, 2016 12:25
Associate an installed certificate with a MarkLogic Application Server
xquery version "1.0-ml";
import module namespace pki = "http://marklogic.com/xdmp/pki" at "/MarkLogic/pki.xqy";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare namespace x509 = "http://marklogic.com/xdmp/x509";
let $config := admin:get-configuration()
let $groupid := admin:group-get-id($config, "Default")
let $appservid := admin:appserver-get-id($config, $groupid, "DemoAppServer")
@ableasdale
ableasdale / validate-ssl-ca-certificate.xqy
Created January 28, 2016 12:34
Get SSL Client Certificate Authorities by Application Server name
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $appServer := admin:appserver-get-id($config, admin:group-get-id($config, "Default"), "DemoAppServer")
return admin:appserver-get-ssl-client-certificate-authorities($config, $appServer)
@ableasdale
ableasdale / rebalancer-preview.xqy
Created January 28, 2016 22:54
Rebalancer Preview
element table { attribute border {1},
element tr { element th {"Source"}, element th {"Active"}, element th {"Deleted"}, element th {"Rebalancing"}, element th {"Total to be moved"}},
for $f in xdmp:document-get("/path/to/result.xml")/result/*:forest-counts
return
element tr { element td {$f/*:forest-name/fn:string(.)} , element td {fn:sum ($f//*:active-fragment-count)},
element td {fn:sum ($f//*:deleted-fragment-count)},
element td {
fn:string-join(
for $c in $f//*:rebalance-fragment-count[*:fragment-count != 0] return ($c/*:rebalance-destination || ": " || $c/*:fragment-count), " | ")},
element td {fn:sum($f//*:rebalance-fragment-count[*:fragment-count != 0]/*:fragment-count)}}
@ableasdale
ableasdale / rebalancer-preview-aggregates.xqy
Created January 29, 2016 14:09
MarkLogic: Aggregated Rebalancer Preview Fragment Counts
declare function local:update($map, $key, $num) {
let $key := fn:tokenize($key, "-")[3]
let $e := ( map:get($map, $key), 0) [1]
return map:put($map, $key, $e + $num)
};
let $map:= map:map()
let $_ :=
@ableasdale
ableasdale / batch-change-perms.xqy
Created January 29, 2016 20:30
MarkLogic - Batch Change Perms
xquery version "1.0-ml";
declare function local:doWork( $cnt as xs:integer, $start as xs:integer, $end as xs:integer, $uris as xs:string* ){
xdmp:log(fn:concat("permission-change database:" , " total-cnt:", $cnt, " start:", $start, " end:", $end, " start:", xdmp:eval("fn:current-time()"))),
for $i in $uris return xdmp:document-set-permissions($i,xdmp:permission("read-only","read")),
xdmp:commit(),
xdmp:log(fn:concat("permission-change database: ", " total-cnt:", $cnt, " start:", $start, " end:", $end, " finished:", xdmp:eval("fn:current-time()"), " Elapsed:", xdmp:elapsed-time() ))
};
@ableasdale
ableasdale / batch-change-perms-v2.xqy
Created February 4, 2016 13:28
Batch Change Permissions - Second Cut...
xquery version "1.0-ml";
declare variable $chunk as xs:integer := 1000;
declare variable $count as xs:unsignedLong := xdmp:eager(fn:count( cts:uris( (), (), (), (), () )));
declare variable $loop as xs:unsignedLong := xs:integer(fn:ceiling ( $count ) div $chunk) + 1;
declare variable $option as element() :=
<options xmlns="xdmp:eval">
<transaction-mode>update-auto-commit</transaction-mode>
</options>;
@ableasdale
ableasdale / ml-rebalancer-preview.xqy
Last active July 28, 2016 09:21
MarkLogic Forest Counts with Rebalancer preview
xquery version "1.0-ml";
declare namespace f = "http://marklogic.com/xdmp/status/forest";
declare variable $DATABASE as xs:string := xdmp:get-request-field("db", "Documents");
declare function local:create-bootstrap-page($title as xs:string, $content as element()){
element html { attribute lang {"en"},
element head {
element meta { attribute charset {"utf-8"}},