Skip to content

Instantly share code, notes, and snippets.

View dmcassel's full-sized avatar

David Cassel dmcassel

View GitHub Profile
declare function local:fib($n)
{
if ($n = 1 or $n = 2) then
1
else
local:fib($n - 1) + local:fib($n - 2)
};
declare function local:fib($n, $map)
{
if ($n = 1 or $n = 2) then
1
else
let $n1 := fn:string($n - 1)
let $n2 := fn:string($n - 2)
let $_ :=
if (map:get($map, $n1)) then ()
else
xquery version "1.0-ml";
for $i in (1 to 10000)
return
xdmp:document-insert(
"/" || $i || ".xml",
<doc>
{
let $lo := xdmp:random(10)
let $hi := $lo + xdmp:random(10)
declare function bucket:parse(
$query-elem as element(),
$options as element(search:options))
as schema-element(cts:query)
{
<root>
{
let $bucket := fn:tokenize($query-elem/search:text, "-")
return
cts:and-query((
@dmcassel
dmcassel / startfinish.xqy
Created December 12, 2014 17:42
Two-function implementation of the ranged-bucket facet problem.
declare function bucket:start(
$constraint as element(search:constraint),
$query as cts:query?,
$facet-options as xs:string*,
$quality-weight as xs:double?,
$forests as xs:unsignedLong*)
as item()*
{
let $buckets := $constraint/search:custom/search:bucket
for $bucket in $buckets
{
"com.marklogic.samplestack.domain.Contributor": {
"displayName": "JoeUser",
"userName": "joe@example.com",
"id": "cf99542d-f024-4478-a6dc-7e723a51b040",
"location": "San Francisco",
"reputation": 50,
"voteCount": 3,
}
}
{
"originalId": "3587258",
"title": "Blocking until event",
"text": "In a web worker, I'm firing off a postMessage() and need to wait for the result before continuing execution. Is there anyway of blocking until the onMessage event occurs, short of busy waiting plus something like Peterson's Algorithm?",
"owner": {
"displayName": "Michael",
},
"accepted": false,
"acceptedAnswerId": null,
"answerCount": 1,
Transaction transaction = startTransaction(SAMPLESTACK_CONTRIBUTOR);
//QnA Document: Mark the Question as having an accepted answer, note
//Answer Id for the accepted answer, and update the last activity date.
patchBuilder.replaceValue("/acceptedAnswerId", answerId);
patchBuilder.replaceFragment("/accepted", true);
patchBuilder.replaceValue("/lastActivityDate", ISO8601Formatter.format(new Date()));
//Reputation handling: Loop through all answers to find the currently
//and previously accepted answers. Increase reputation by 1 for the
//user with the current accepted answer. Decrease reputation by 1 for
//the user who previously had the accepted answer (if applicable).
// all or nothing – consistency ftw!
try {
// Transactional updates
transaction.commit();
transaction = null;
return acceptedDocument;
} finally {
if (transaction != null) { transaction.rollback(); };
}