[MarkLogic Dev General] REST API call results in XDMP-UPDATEFUNCTIONFROMQUERY

Danny Sinang d.sinang at gmail.com
Wed Mar 11 20:03:07 PDT 2015


Thanks Joe.

I decided to not use xdmp:spawn anymore.

Here's the working code :

==========================================================

*xquery version *"1.0-ml";

*module namespace **local* = "http://marklogic.com/rest-api/resource/resize"
;

*declare namespace **rapi* = "http://marklogic.com/rest-api";

*declare* %rapi:transaction-mode("update") *function*
*local:get*(*$context* *as
**map:map*, *$params*  *as **map:map*) *as **document-node*()* {
 *let **$image_server_root*  := "
http://solrdev1.mycompany.com:7000/resize_image"
 *let* *$uri*                := *map:get*(*$params*,"uri")
 *let* *$width*              := *map:get*(*$params*,"width")
 *let* *$height*             := *map:get*(*$params*,"height")
 *let* *$resized_uri*        := *fn:concat*(*fn:substring-before*(*$uri*,
".jpg"),"_",*$width*,"_",*$height*,".jpg")
 *let* *$resized_image*      :=
  *if* (*xdmp:uri-is-file*(*$resized_uri*)) *then
            *
   *fn:doc*(*$resized_uri*)
  *else                                      *
   *let **$image*      := *fn:doc*(*$uri*)/*node*()

   *let* *$image_name* := *fn:tokenize*(*$uri*,"/")[*last*()]

   *let* *$temp_image* := *xdmp:http-put*(*fn:concat*(*$image_server_root*,
"/",*$width*,"/",*$height*,"/",*$image_name*,"?resized_uri=",*$resized_uri*),
(), *$image*)[2]
   *let* *$_* := *xdmp:document-insert*("/2014/11/20/danny.jpg",
*$temp_image*, *xdmp:default-permissions*())
   *return *
   *$temp_image*

 *let* *$mime_type* := *map:put*(*$context*,"output-types","image/jpeg")

 *return document*{*$resized_image*}
};

On Wed, Mar 11, 2015 at 8:57 PM, Joe Bryan <Joe.Bryan at marklogic.com> wrote:

>  Hi Danny,
>
>  You don't have to make the entire transaction a query to spawn an
> update; you can explicitly set the transaction-mode in the
> xdmp:spawn-function expression. (note: you will have to explicitly commit
> the update transaction)
>
>  Ex:
>
>  xdmp:spawn-function(function() {
>     xdmp:document-insert("/2014/11/20/danny.jpg", $temp_image,
> xdmp:default-permissions()),
>     xdmp:commit()
>   },
>   <options xmlns="xdmp:eval">
>     <transaction-mode>update</transaction-mode>
>   </options>)
>
>  Thanks.
>
>  -jb
>
>   From: Danny Sinang <d.sinang at gmail.com>
> Reply-To: MarkLogic Developer Discussion <general at developer.marklogic.com>
> Date: Wednesday, March 11, 2015 at 2:34 PM
> To: general <General at developer.marklogic.com>
> Subject: Re: [MarkLogic Dev General] REST API call results in
> XDMP-UPDATEFUNCTIONFROMQUERY
>
>   I think it's working now.
>
>  Just added this line :
>
> *declare namespace **rapi*= "http://marklogic.com/rest-api";
>
> On Wed, Mar 11, 2015 at 3:27 PM, Danny Sinang <d.sinang at gmail.com> wrote:
>
>> I tried prefixing my function declaration like this :
>>
>>   *declare* %rapi:transaction-mode("update")
>>
>>  as suggested in
>> http://docs.marklogic.com/guide/rest-dev/extensions#id_17130 .
>>
>>  But I'm now getting this error :
>>
>>   XDMP-UNBPRFX: (err:XPST0081) Prefix rapi has no namespace binding.
>>
>>
>>   Regards,
>> Danny
>>
>>
>> On Wed, Mar 11, 2015 at 3:00 PM, Danny Sinang <d.sinang at gmail.com> wrote:
>>
>>>  Hello,
>>>
>>>  Whenever I make call to
>>>
>>>
>>> http://marklogic.mycompany.com:9042/v1/resources/resize?rs:width=300&rs:height=300&rs:uri=/2014/11/20/IC20141120.jpg
>>>
>>>  I get this error message :
>>>
>>>  <rapi:error xmlns:rapi="http://marklogic.com/rest-api"><
>>> rapi:status-code>500</rapi:status-code><rapi:status>INTERNAL ERROR</
>>> rapi:status><rapi:message-code>XDMP-UPDATEFUNCTIONFROMQUERY</
>>> rapi:message-code><rapi:message>XDMP-UPDATEFUNCTIONFROMQUERY: Q{
>>> http://marklogic.com/rest-api/resource/resize}get#2($context,
>>> $service-params) -- Cannot apply an update function from a query. See the
>>> MarkLogic server error log for further detail.</rapi:message></
>>> rapi:error>
>>>
>>>  I know this happens because of the xdmp:document-insert() call inside
>>> the code for that API call.
>>>
>>>  My question is, is there a workaround for this ?
>>>
>>>  I already tried wrapping the document-insert call with
>>> xdmp:spawn-function, but that didn't work either.
>>>
>>>  Below is the code.
>>>
>>>  Regards,
>>> Danny
>>>
>>>
>>> =======================================================================================
>>>
>>> *xquery version *"1.0-ml";
>>>
>>> *module namespace **local* = "
>>> http://marklogic.com/rest-api/resource/resize";
>>>
>>> *declare option **xdmp:update* "true";
>>>
>>> *declare function **local:get*(*$context**as **map:map*, *$params*  *as
>>> * *map:map*) *as **document-node*()* {
>>> *let **$image_server_root*  := "
>>> http://solrdev1.mycompany.com:7000/resize_image"
>>> *let* *$uri*                := *map:get*(*$params*,"uri")
>>> *let* *$width*              := *map:get*(*$params*,"width")
>>> *let* *$height*             := *map:get*(*$params*,"height")
>>> *let* *$resized_uri*        := *fn:concat*(*fn:substring-before*(*$uri*,
>>> ".jpg"),"_",*$width*,"_",*$height*,".jpg")
>>> *let* *$resized_image*      :=
>>> *if* (*xdmp:uri-is-file*(*$resized_uri*)) *then
>>>               *
>>> *fn:doc*(*$resized_uri*)
>>> *else                                      *
>>> *let * *$image*      := *fn:doc*(*$uri*)/*node*()
>>>
>>> *let**$image_name* := *fn:tokenize*(*$uri*,"/")[*last*()]
>>>
>>> *let**$temp_image* := *xdmp:http-put*(*fn:concat*(*$image_server_root*,
>>> "/",*$width*,"/",*$height*,"/",*$image_name*,"?resized_uri=",
>>> *$resized_uri*), (), *$image*)[2]
>>> *let**$_* := *xdmp:spawn-function*(*function*() { *xdmp:document-insert*
>>> ("/2014/11/20/danny.jpg", *$temp_image*, *xdmp:default-permissions*())
>>> })
>>> *return *
>>> *$temp_image*
>>>
>>> *let* *$mime_type* := *map:put*(*$context*,"output-types","image/jpeg")
>>>
>>> *return document*{*$resized_image*}
>>> };
>>>
>>
>>
>
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20150311/c647010d/attachment-0001.html 


More information about the General mailing list