001/*
002 * (C) Copyright 2006-2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.quota.count;
021
022import static org.nuxeo.ecm.core.schema.FacetNames.FOLDERISH;
023import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY;
024import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY;
025import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_FACET;
026
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentNotFoundException;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.core.api.IterableQueryResult;
039import org.nuxeo.ecm.core.api.model.DeltaLong;
040import org.nuxeo.ecm.core.event.Event;
041import org.nuxeo.ecm.quota.AbstractQuotaStatsUpdater;
042import org.nuxeo.ecm.quota.QuotaStatsInitialWork;
043import org.nuxeo.ecm.quota.QuotaUtils;
044import org.nuxeo.ecm.quota.size.QuotaExceededException;
045import org.nuxeo.runtime.transaction.TransactionHelper;
046
047/**
048 * {@link org.nuxeo.ecm.quota.QuotaStatsUpdater} counting the non folderish documents.
049 * <p>
050 * Store the descendant and children count on {@code Folderish} documents.
051 *
052 * @since 5.5
053 */
054public class DocumentsCountUpdater extends AbstractQuotaStatsUpdater {
055
056    private static final Log log = LogFactory.getLog(DocumentsCountUpdater.class);
057
058    public static final int BATCH_SIZE = 50;
059
060    @Override
061    protected void processDocumentCreated(CoreSession session, DocumentModel doc) {
062        if (doc.isVersion()) {
063            return;
064        }
065        List<DocumentModel> ancestors = getAncestors(session, doc);
066        long docCount = getCount(doc);
067        updateCountStatistics(session, doc, ancestors, docCount);
068    }
069
070    @Override
071    protected void processDocumentCopied(CoreSession session, DocumentModel doc) {
072        List<DocumentModel> ancestors = getAncestors(session, doc);
073        long docCount = getCount(doc);
074        updateCountStatistics(session, doc, ancestors, docCount);
075    }
076
077    @Override
078    protected void processDocumentCheckedIn(CoreSession session, DocumentModel doc) {
079        // NOP
080    }
081
082    @Override
083    protected void processDocumentBeforeCheckedIn(CoreSession session, DocumentModel doc) {
084        // NOP
085    }
086
087    @Override
088    protected void processDocumentCheckedOut(CoreSession session, DocumentModel doc) {
089        // NOP
090    }
091
092    @Override
093    protected void processDocumentBeforeCheckedOut(CoreSession session, DocumentModel doc) {
094        // NOP
095    }
096
097    @Override
098    protected void processDocumentUpdated(CoreSession session, DocumentModel doc) {
099    }
100
101    @Override
102    protected void processDocumentMoved(CoreSession session, DocumentModel doc, DocumentModel sourceParent) {
103        List<DocumentModel> ancestors = getAncestors(session, doc);
104        List<DocumentModel> sourceAncestors = getAncestors(session, sourceParent);
105        sourceAncestors.add(0, sourceParent);
106        long docCount = getCount(doc);
107        updateCountStatistics(session, doc, ancestors, docCount);
108        updateCountStatistics(session, doc, sourceAncestors, -docCount);
109    }
110
111    @Override
112    protected void processDocumentAboutToBeRemoved(CoreSession session, DocumentModel doc) {
113        List<DocumentModel> ancestors = getAncestors(session, doc);
114        long docCount = getCount(doc);
115        updateCountStatistics(session, doc, ancestors, -docCount);
116    }
117
118    @Override
119    protected void handleQuotaExceeded(QuotaExceededException e, Event event) {
120        // never rollback on Exceptions
121    }
122
123    @Override
124    protected boolean needToProcessEventOnDocument(Event event, DocumentModel doc) {
125        return true;
126    }
127
128    @Override
129    protected void processDocumentBeforeUpdate(CoreSession session, DocumentModel doc) {
130        // NOP
131    }
132
133    protected void updateCountStatistics(CoreSession session, DocumentModel doc, List<DocumentModel> ancestors,
134            long count) {
135        if (ancestors == null || ancestors.isEmpty()) {
136            return;
137        }
138        if (count == 0) {
139            return;
140        }
141
142        if (!doc.hasFacet(FOLDERISH)) {
143            DocumentModel parent = ancestors.get(0);
144            updateCount(session, parent, DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, count);
145        }
146
147        ancestors.forEach(ancestor -> updateCount(session, ancestor,
148                DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY, count));
149        session.save();
150    }
151
152    protected void updateCount(CoreSession session, DocumentModel parent, String xpath, long count) {
153        Number previous;
154        if (parent.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
155            previous = (Number) parent.getPropertyValue(xpath);
156        } else {
157            parent.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
158            previous = null;
159        }
160        DeltaLong childrenCount = DeltaLong.valueOf(previous, count);
161        parent.setPropertyValue(xpath, childrenCount);
162        // do not send notifications
163        QuotaUtils.disableListeners(parent);
164        DocumentModel origParent = parent;
165        parent = session.saveDocument(parent);
166        QuotaUtils.clearContextData(origParent);
167    }
168
169    protected long getCount(DocumentModel doc) {
170        if (doc.hasFacet(FOLDERISH)) {
171            if (doc.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
172                Number count = (Number) doc.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY);
173                return count == null ? 0 : count.longValue();
174            } else {
175                return 0;
176            }
177        } else {
178            return 1;
179        }
180    }
181
182    @Override
183    public void computeInitialStatistics(CoreSession session, QuotaStatsInitialWork currentWorker, String path) {
184        // TODO path != null
185        Map<String, String> folders = getFolders(session);
186        Map<String, Count> documentsCountByFolder = computeDocumentsCountByFolder(session, folders);
187        saveDocumentsCount(session, documentsCountByFolder);
188    }
189
190    protected Map<String, String> getFolders(CoreSession session) {
191        try (IterableQueryResult res = session.queryAndFetch(
192                "SELECT ecm:uuid, ecm:parentId FROM Document WHERE ecm:mixinType = 'Folderish'", "NXQL")) {
193            Map<String, String> folders = new HashMap<>();
194
195            for (Map<String, Serializable> r : res) {
196                folders.put((String) r.get("ecm:uuid"), (String) r.get("ecm:parentId"));
197            }
198            return folders;
199        }
200    }
201
202    protected Map<String, Count> computeDocumentsCountByFolder(CoreSession session, Map<String, String> folders) {
203        try (IterableQueryResult res = session.queryAndFetch("SELECT ecm:uuid, ecm:parentId FROM Document", "NXQL")) {
204            Map<String, Count> foldersCount = new HashMap<>();
205            for (Map<String, Serializable> r : res) {
206                String uuid = (String) r.get("ecm:uuid");
207                if (folders.containsKey(uuid)) {
208                    // a folder
209                    continue;
210                }
211
212                String folderId = (String) r.get("ecm:parentId");
213                if (!foldersCount.containsKey(folderId)) {
214                    foldersCount.put(folderId, new Count());
215                }
216                Count count = foldersCount.get(folderId);
217                count.childrenCount++;
218                count.descendantsCount++;
219
220                updateParentsDocumentsCount(folders, foldersCount, folderId);
221            }
222            return foldersCount;
223        }
224    }
225
226    protected void updateParentsDocumentsCount(Map<String, String> folders, Map<String, Count> foldersCount,
227            String folderId) {
228        String parent = folders.get(folderId);
229        while (parent != null) {
230            if (!foldersCount.containsKey(parent)) {
231                foldersCount.put(parent, new Count());
232            }
233            Count c = foldersCount.get(parent);
234            c.descendantsCount++;
235            parent = folders.get(parent);
236        }
237    }
238
239    protected void saveDocumentsCount(CoreSession session, Map<String, Count> foldersCount) {
240        long docsCount = 0;
241        for (Map.Entry<String, Count> entry : foldersCount.entrySet()) {
242            String folderId = entry.getKey();
243            if (folderId == null) {
244                continue;
245            }
246            DocumentModel folder;
247            try {
248                folder = session.getDocument(new IdRef(folderId));
249            } catch (DocumentNotFoundException e) {
250                log.warn(e);
251                log.debug(e, e);
252                continue;
253            }
254            if (folder.getPath().isRoot()) {
255                // Root document
256                continue;
257            }
258            saveDocumentsCount(session, folder, entry.getValue());
259            docsCount++;
260            if (docsCount % BATCH_SIZE == 0) {
261                session.save();
262                if (TransactionHelper.isTransactionActive()) {
263                    TransactionHelper.commitOrRollbackTransaction();
264                    TransactionHelper.startTransaction();
265                }
266            }
267        }
268        session.save();
269    }
270
271    protected void saveDocumentsCount(CoreSession session, DocumentModel folder, Count count) {
272        if (!folder.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
273            folder.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
274        }
275        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, Long.valueOf(count.childrenCount));
276        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY,
277                Long.valueOf(count.descendantsCount));
278        // do not send notifications
279        QuotaUtils.disableListeners(folder);
280        DocumentModel origFolder = folder;
281        folder = session.saveDocument(folder);
282        QuotaUtils.clearContextData(origFolder);
283    }
284
285    /**
286     * Object to store documents count for a folder
287     */
288    private static class Count {
289
290        public long childrenCount = 0;
291
292        public long descendantsCount = 0;
293    }
294
295    @Override
296    protected void processDocumentTrashOp(CoreSession session, DocumentModel doc, boolean isTrashed) {
297        // do nothing for count
298    }
299
300    @Override
301    protected void processDocumentRestored(CoreSession session, DocumentModel doc) {
302        // do nothing
303    }
304
305    @Override
306    protected void processDocumentBeforeRestore(CoreSession session, DocumentModel doc) {
307        // do nothing
308    }
309}