Skip to content

Instantly share code, notes, and snippets.

View ableasdale's full-sized avatar

Alex Bleasdale ableasdale

View GitHub Profile
@ableasdale
ableasdale / insert-test-data.xqy
Created November 29, 2012 19:40
Inserting content using xdmp:eval for isolated (separate) transctions
xquery version "1.0-ml";
declare namespace prop="http://marklogic.com/xdmp/property";
declare variable $doc-count as xs:int := 10;
for $item in (1 to $doc-count)
let $eval := "xquery version '1.0-ml';
declare variable $count as xs:string external;
xdmp:document-insert(fn:concat('/test/', $count, '.xml'), element root{ $count })"
return
@ableasdale
ableasdale / list-docs-by-insert-time.xqy
Created November 29, 2012 19:46
A List of all documents ordered by the time they were inserted into the database
xquery version "1.0-ml";
for $doc in doc()
let $sort-key := xs:dateTime(xdmp:document-properties(fn:base-uri($doc))/prop:properties/prop:last-modified)
order by $sort-key
return
fn:concat("Doc: ", fn:base-uri($doc), " was inserted at: ", $sort-key)
@ableasdale
ableasdale / forest-rollback.xqy
Last active December 14, 2015 08:38
Using xdmp:forest-rollback to avert a crisis
xquery version "1.0-ml";
xdmp:forest-rollback(
xdmp:database-forests(xdmp:database("YourDatabase")),
xdmp:wallclock-to-timestamp(
xs:dateTime("2013-02-28T17:55:07Z")))
@ableasdale
ableasdale / prod-disaster.xqy
Created February 28, 2013 18:02
Creating a disaster scenario to recover from
xquery version "1.0-ml";
for $d in doc()
return xdmp:document-delete(xdmp:node-uri($d))
@ableasdale
ableasdale / list-groups.xqy
Last active December 14, 2015 08:39
Lists out all the groups and their associated hosts and application servers
xquery version "1.0-ml";
element groups {
for $grp in xdmp:groups()
return
(element group {attribute id {$grp}, element group-name {xdmp:group-name($grp)},
for $host in xdmp:group-hosts($grp)
return element host {attribute id {$host}, xdmp:host-name($host)},
for $server in xdmp:group-servers($grp)
return element server {attribute id {$server},xdmp:server-name($server)}
@ableasdale
ableasdale / rebalance-setup-databases.xqy
Last active December 15, 2015 06:09
Create two test databases
(:::::::::::::: Step one - set up two test databases :::::::::::::::)
xquery version "1.0-ml";
import module namespace info = "http://marklogic.com/appservices/infostudio"
at "/MarkLogic/appservices/infostudio/info.xqy";
(: Setup test databases :)
(info:database-create("source-db", 1), info:database-create("rebalance-me", 8))
(: after completion - confirm that the source-db1 forest contains 200K docs :)
@ableasdale
ableasdale / rebalance-create-sample-data.xqy
Created March 21, 2013 16:49
Fill the source database with sample content to test the process
(:::::::::::::: Step two - create content in source-db :::::::::::::::)
xquery version "1.0-ml";
(: run against source-db to fill the source forest with 200K docs - may take several minutes to complete :)
for $i in 1 to 200000
return xdmp:document-insert(concat($i,".xml"), element node {$i})
@ableasdale
ableasdale / rebalance-reattach-forest.xqy
Created March 21, 2013 17:00
Reattach the source-db-1 forest to the "rebalance-me" database
(:::::::::::::: Step four - re-attach source-db-1 forest to rebalance-me database :::::::::::::::)
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin"
at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $config := admin:database-detach-forest($config, xdmp:database("source-db"), xdmp:forest("source-db-1") )
let $config := admin:database-attach-forest($config, xdmp:database("rebalance-me"), xdmp:forest("source-db-1") )
return
@ableasdale
ableasdale / rebalance-evacuate-forest.xqy
Last active December 15, 2015 06:18
Evacuate the source-db-1 forest
(:::::::::::::: Step five - evacuate source-db-1 forest :::::::::::::::)
xquery version "1.0-ml";
xdmp:spawn(
"forest-uris-evacuate.xqy",
(xs:QName('FOREST'), xdmp:forest('source-db-1'),
xs:QName('INDEX'), -1,
xs:QName('LIMIT'), 0,
xs:QName('RESPAWN'), true()),
<options xmlns="xdmp:eval">
@ableasdale
ableasdale / rebalance-template.xqy
Created March 21, 2013 18:15
Example usage for the task rebalancer
xdmp:spawn(
"rebalance-evacuate-forest.xqy",
(
xs:QName('FOREST'), xdmp:forest('%SOURCE FOREST%'),
xs:QName('INDEX'), -1,
xs:QName('LIMIT'), 0,
xs:QName('RESPAWN'), true()
),
<options xmlns="xdmp:eval">
<database>{ xdmp:database("%DATABASE NAME%") }</database>