Skip to content

Instantly share code, notes, and snippets.

View ableasdale's full-sized avatar

Alex Bleasdale ableasdale

View GitHub Profile
@ableasdale
ableasdale / database-reorder-forests.xqy
Created February 15, 2016 08:58
Reordering forests for the rebalancer after adding more hosts to a cluster.
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare variable $DATABASE as xs:string := "example-db";
let $config := admin:get-configuration()
let $dbid := admin:database-get-id($config, $DATABASE)
return
@ableasdale
ableasdale / rest-backup-forest.xqy
Last active April 26, 2016 18:09
MarkLogic: Create a backup using a ReST API call
xquery version "1.0-ml";
(: See http://docs.marklogic.com/REST/POST/manage/v2/databases/%5Bid-or-name%5D :)
declare variable $payload-status := '{"operation": "backup-database", "forest": ["Documents"], "backup-dir": "/tmp" }';
xdmp:http-post("http://localhost:8002/manage/v2/databases/Documents?format=json",
<options xmlns="xdmp:http">
<authentication method="digest">
<username>username</username>
@ableasdale
ableasdale / backup-status.xqy
Last active April 26, 2016 18:09
MarkLogic: Get backup (job) status via ReST call given an id
xquery version "1.0-ml";
(: See http://docs.marklogic.com/REST/POST/manage/v2/databases/%5Bid-or-name%5D :)
declare variable $payload-status := '{"operation": "backup-status", "job-id" : "' || "12979282468857286095" || '","host-name": "' || xdmp:host-name() || '"}';
xdmp:http-post("http://localhost:8002/manage/v2/databases/Documents?format=json",
<options xmlns="xdmp:http">
<authentication method="digest">
<username>username</username>
@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"}},
@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 / 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 / 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 / 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 / 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 / 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")