Search in sources :

Example 1 with IterableQueryResult

use of org.nuxeo.ecm.core.api.IterableQueryResult in project nuxeo-drive-server by nuxeo.

the class DocumentBackedFolderItem method getScrollBatch.

@SuppressWarnings("unchecked")
protected ScrollDocumentModelList getScrollBatch(String scrollId, int batchSize, CoreSession session, long keepAlive) {
    Cache scrollingCache = Framework.getService(CacheService.class).getCache(DESCENDANTS_SCROLL_CACHE);
    if (scrollingCache == null) {
        throw new NuxeoException("Cache not found: " + DESCENDANTS_SCROLL_CACHE);
    }
    String newScrollId;
    List<String> descendantIds;
    if (StringUtils.isEmpty(scrollId)) {
        // Perform initial query to fetch ids of all the descendant documents and put the result list in a
        // cache, aka "search context"
        descendantIds = new ArrayList<>();
        StringBuilder sb = new StringBuilder(String.format("SELECT ecm:uuid FROM Document WHERE ecm:ancestorId = '%s'", docId));
        sb.append(" AND ecm:isTrashed = 0");
        sb.append(" AND ecm:mixinType != 'HiddenInNavigation'");
        // Don't need to add ecm:isVersion = 0 because versions are already excluded by the
        // ecm:ancestorId clause since they have no path
        String query = sb.toString();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Executing initial query to scroll through the descendants of %s: %s", docPath, query));
        }
        try (IterableQueryResult res = session.queryAndFetch(sb.toString(), NXQL.NXQL)) {
            Iterator<Map<String, Serializable>> it = res.iterator();
            while (it.hasNext()) {
                descendantIds.add((String) it.next().get(NXQL.ECM_UUID));
            }
        }
        // Generate a scroll id
        newScrollId = UUID.randomUUID().toString();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Put initial query result list (search context) in the %s cache at key (scrollId) %s", DESCENDANTS_SCROLL_CACHE, newScrollId));
        }
        scrollingCache.put(newScrollId, (Serializable) descendantIds);
    } else {
        // Get the descendant ids from the cache
        descendantIds = (List<String>) scrollingCache.get(scrollId);
        if (descendantIds == null) {
            throw new NuxeoException(String.format("No search context found in the %s cache for scrollId [%s]", DESCENDANTS_SCROLL_CACHE, scrollId));
        }
        newScrollId = scrollId;
    }
    if (descendantIds.isEmpty()) {
        return new ScrollDocumentModelList(newScrollId, 0);
    }
    // Extract a batch of descendant ids
    List<String> descendantIdsBatch = getBatch(descendantIds, batchSize);
    // Update descendant ids in the cache
    scrollingCache.put(newScrollId, (Serializable) descendantIds);
    // Fetch documents from VCS
    DocumentModelList descendantDocsBatch = fetchFromVCS(descendantIdsBatch, session);
    return new ScrollDocumentModelList(newScrollId, descendantDocsBatch);
}
Also used : DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) IterableQueryResult(org.nuxeo.ecm.core.api.IterableQueryResult) HashMap(java.util.HashMap) Map(java.util.Map) Cache(org.nuxeo.ecm.core.cache.Cache) CacheService(org.nuxeo.ecm.core.cache.CacheService)

Example 2 with IterableQueryResult

use of org.nuxeo.ecm.core.api.IterableQueryResult in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method queryAndFetchSynchronizationRoots.

protected Map<String, SynchronizationRoots> queryAndFetchSynchronizationRoots(CoreSession session, String query) {
    Map<String, SynchronizationRoots> syncRoots = new HashMap<String, SynchronizationRoots>();
    Set<IdRef> references = new LinkedHashSet<IdRef>();
    Set<String> paths = new LinkedHashSet<String>();
    IterableQueryResult results = session.queryAndFetch(query, NXQL.NXQL);
    try {
        for (Map<String, Serializable> result : results) {
            IdRef docRef = new IdRef(result.get("ecm:uuid").toString());
            try {
                DocumentModel doc = session.getDocument(docRef);
                references.add(docRef);
                paths.add(doc.getPathAsString());
            } catch (DocumentNotFoundException e) {
                log.warn(String.format("Document %s not found, not adding it to the list of synchronization roots for user %s.", docRef, session.getPrincipal().getName()));
            } catch (DocumentSecurityException e) {
                log.warn(String.format("User %s cannot access document %s, not adding it to the list of synchronization roots.", session.getPrincipal().getName(), docRef));
            }
        }
    } finally {
        results.close();
    }
    SynchronizationRoots repoSyncRoots = new SynchronizationRoots(session.getRepositoryName(), paths, references);
    syncRoots.put(session.getRepositoryName(), repoSyncRoots);
    return syncRoots;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Serializable(java.io.Serializable) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) HashMap(java.util.HashMap) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) IterableQueryResult(org.nuxeo.ecm.core.api.IterableQueryResult)

Aggregations

HashMap (java.util.HashMap)2 IterableQueryResult (org.nuxeo.ecm.core.api.IterableQueryResult)2 Serializable (java.io.Serializable)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)1 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)1 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)1 DocumentNotFoundException (org.nuxeo.ecm.core.api.DocumentNotFoundException)1 DocumentSecurityException (org.nuxeo.ecm.core.api.DocumentSecurityException)1 IdRef (org.nuxeo.ecm.core.api.IdRef)1 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)1 Cache (org.nuxeo.ecm.core.cache.Cache)1 CacheService (org.nuxeo.ecm.core.cache.CacheService)1