Search code examples
xquerymarklogic

MarkLogic : How to get the count of all documents in a database with rest-reader, rest-writer permission?


We had recently set the permissions(rest-reader, read and rest-writer, update) to all the documents in a collection in the database using corb. This activity was done on 10 million documents which are inside the collection.

xquery version "1.0-ml";
declare variable $URI as xs:string external; 
xdmp:document-add-permissions($URI,
(xdmp:permission("rest-reader", "read"),
xdmp:permission("rest-writer", "update")))

How can I get the total count of document with permissions ( rest-reader, read and rest-writer, update) inside a database?


Solution

  • I would not iterate docs and check permissions directly, but leverage security for this. As admin you first run some estimates:

    xdmp:estimate(collection()), (: total count :)
    xdmp:estimate(collection('mycollection')) (: count of all in that collection :)
    

    Then repeat the same estimates with a test user that has rest-reader role only:

    xdmp:invoke-function(function() {
      xdmp:estimate(collection()), (: total count visible to rest-reader user :)
      xdmp:estimate(collection('mycollection')) (: count of all in that collection visible to rest-reader user:)
    }, (), map:entry("user-id", xdmp:user('myrestreaderuser')))
    

    By substracting the admin counts with those from rest-reader user, you can derive how many have not gotten the permissions you tried to apply.

    HTH!