001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (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 processDocumentCheckedOut(CoreSession session, DocumentModel doc) {
084        // NOP
085    }
086
087    @Override
088    protected void processDocumentUpdated(CoreSession session, DocumentModel doc) {
089    }
090
091    @Override
092    protected void processDocumentMoved(CoreSession session, DocumentModel doc, DocumentModel sourceParent) {
093        List<DocumentModel> ancestors = getAncestors(session, doc);
094        List<DocumentModel> sourceAncestors = getAncestors(session, sourceParent);
095        sourceAncestors.add(0, sourceParent);
096        long docCount = getCount(doc);
097        updateCountStatistics(session, doc, ancestors, docCount);
098        updateCountStatistics(session, doc, sourceAncestors, -docCount);
099    }
100
101    @Override
102    protected void processDocumentAboutToBeRemoved(CoreSession session, DocumentModel doc) {
103        List<DocumentModel> ancestors = getAncestors(session, doc);
104        long docCount = getCount(doc);
105        updateCountStatistics(session, doc, ancestors, -docCount);
106    }
107
108    @Override
109    protected void handleQuotaExceeded(QuotaExceededException e, Event event) {
110        // never rollback on Exceptions
111    }
112
113    @Override
114    protected boolean needToProcessEventOnDocument(Event event, DocumentModel doc) {
115        return true;
116    }
117
118    @Override
119    protected void processDocumentBeforeUpdate(CoreSession session, DocumentModel doc) {
120        // NOP
121    }
122
123    protected void updateCountStatistics(CoreSession session, DocumentModel doc, List<DocumentModel> ancestors,
124            long count) {
125        if (ancestors == null || ancestors.isEmpty()) {
126            return;
127        }
128        if (count == 0) {
129            return;
130        }
131
132        if (!doc.hasFacet(FOLDERISH)) {
133            DocumentModel parent = ancestors.get(0);
134            updateCount(session, parent, DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, count);
135        }
136
137        ancestors.forEach(ancestor -> updateCount(session, ancestor,
138                                                  DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY, count));
139        session.save();
140    }
141
142    protected void updateCount(CoreSession session, DocumentModel parent, String xpath, long count) {
143        Number previous;
144        if (parent.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
145            previous = (Number) parent.getPropertyValue(xpath);
146        } else {
147            parent.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
148            previous = null;
149        }
150        DeltaLong childrenCount = DeltaLong.valueOf(previous, count);
151        parent.setPropertyValue(xpath, childrenCount);
152        // do not send notifications
153        QuotaUtils.disableListeners(parent);
154        DocumentModel origParent = parent;
155        parent = session.saveDocument(parent);
156        QuotaUtils.clearContextData(origParent);
157    }
158
159    protected long getCount(DocumentModel doc) {
160        if (doc.hasFacet(FOLDERISH)) {
161            if (doc.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
162                Number count = (Number) doc.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY);
163                return count == null ? 0 : count.longValue();
164            } else {
165                return 0;
166            }
167        } else {
168            return 1;
169        }
170    }
171
172    @Override
173    public void computeInitialStatistics(CoreSession session, QuotaStatsInitialWork currentWorker, String path) {
174        // TODO path != null
175        Map<String, String> folders = getFolders(session);
176        Map<String, Count> documentsCountByFolder = computeDocumentsCountByFolder(session, folders);
177        saveDocumentsCount(session, documentsCountByFolder);
178    }
179
180    protected Map<String, String> getFolders(CoreSession session) {
181        IterableQueryResult res = session.queryAndFetch(
182                "SELECT ecm:uuid, ecm:parentId FROM Document WHERE ecm:mixinType = 'Folderish'", "NXQL");
183        try {
184            Map<String, String> folders = new HashMap<>();
185
186            for (Map<String, Serializable> r : res) {
187                folders.put((String) r.get("ecm:uuid"), (String) r.get("ecm:parentId"));
188            }
189            return folders;
190        } finally {
191            if (res != null) {
192                res.close();
193            }
194        }
195    }
196
197    protected Map<String, Count> computeDocumentsCountByFolder(CoreSession session, Map<String, String> folders) {
198        IterableQueryResult res = session.queryAndFetch("SELECT ecm:uuid, ecm:parentId FROM Document", "NXQL");
199        try {
200            Map<String, Count> foldersCount = new HashMap<>();
201            for (Map<String, Serializable> r : res) {
202                String uuid = (String) r.get("ecm:uuid");
203                if (folders.containsKey(uuid)) {
204                    // a folder
205                    continue;
206                }
207
208                String folderId = (String) r.get("ecm:parentId");
209                if (!foldersCount.containsKey(folderId)) {
210                    foldersCount.put(folderId, new Count());
211                }
212                Count count = foldersCount.get(folderId);
213                count.childrenCount++;
214                count.descendantsCount++;
215
216                updateParentsDocumentsCount(folders, foldersCount, folderId);
217            }
218            return foldersCount;
219        } finally {
220            if (res != null) {
221                res.close();
222            }
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        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}