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);
}
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;
}
Aggregations