summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/designercore/projectstorage/projectstoragepathwatcher.h
blob: 0d3bc730f813e83ceb4d891cf29b304a6ab0bb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "directorypathcompressor.h"
#include "filesystem.h"
#include "projectstoragepathwatcherinterface.h"
#include "projectstoragepathwatchernotifierinterface.h"
#include "projectstoragepathwatchertypes.h"
#include "storagecache.h"

#include <utils/algorithm.h>

#include <QTimer>

namespace QmlDesigner {

template<class InputIt1, class InputIt2, class Callable>
void set_greedy_intersection_call(
    InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Callable callable)
{
    while (first1 != last1 && first2 != last2) {
        if (*first1 < *first2) {
            ++first1;
        } else {
            if (*first2 < *first1)
                ++first2;
            else
                callable(*first1++);
        }
    }
}

template<typename FileSystemWatcher, typename Timer, class SourcePathCache>
class ProjectStoragePathWatcher : public ProjectStoragePathWatcherInterface
{
public:
    ProjectStoragePathWatcher(SourcePathCache &pathCache,
                              FileSystemInterface &fileSystem,
                              ProjectStoragePathWatcherNotifierInterface *notifier = nullptr)
        : m_fileStatusCache(fileSystem)
        , m_fileSystem(fileSystem)
        , m_pathCache(pathCache)
        , m_notifier(notifier)
    {
        QObject::connect(&m_fileSystemWatcher,
                         &FileSystemWatcher::directoryChanged,
                         [&](const QString &path) { compressChangedDirectoryPath(path); });

        m_directoryPathCompressor.setCallback([&](QmlDesigner::SourceContextIds &&sourceContextIds) {
            addChangedPathForFilePath(std::move(sourceContextIds));
        });
    }

    ~ProjectStoragePathWatcher()
    {
        m_directoryPathCompressor.setCallback([&](SourceContextIds &&) {});
    }

    void updateIdPaths(const std::vector<IdPaths> &idPaths) override
    {
        const auto &[entires, ids] = convertIdPathsToWatcherEntriesAndIds(idPaths);

        addEntries(entires);

        auto notContainsdId = [&, &ids = ids](WatcherEntry entry) {
            return !std::binary_search(ids.begin(), ids.end(), entry.id);
        };
        removeUnusedEntries(entires, notContainsdId);
    }

    void updateContextIdPaths(const std::vector<IdPaths> &idPaths,
                              const SourceContextIds &sourceContextIds)
    {
        const auto &[entires, ids] = convertIdPathsToWatcherEntriesAndIds(idPaths);

        addEntries(entires);

        auto notContainsdId = [&, &ids = ids](WatcherEntry entry) {
            return !std::binary_search(ids.begin(), ids.end(), entry.id)
                   || !std::binary_search(sourceContextIds.begin(),
                                          sourceContextIds.end(),
                                          entry.sourceContextId);
        };

        removeUnusedEntries(entires, notContainsdId);
    }

    void removeIds(const ProjectPartIds &ids) override
    {
        auto removedEntries = removeIdsFromWatchedEntries(ids);

        auto filteredPaths = filterNotWatchedPaths(removedEntries);

        if (!filteredPaths.empty())
            m_fileSystemWatcher.removePaths(convertWatcherEntriesToDirectoryPathList(filteredPaths));
    }

    void setNotifier(ProjectStoragePathWatcherNotifierInterface *notifier) override
    {
        m_notifier = notifier;
    }

    std::size_t sizeOfIdPaths(const std::vector<IdPaths> &idPaths)
    {
        auto sumSize = [](std::size_t size, const IdPaths &idPath) {
            return size + idPath.sourceIds.size();
        };

        return std::accumulate(idPaths.begin(), idPaths.end(), std::size_t(0), sumSize);
    }

    std::pair<WatcherEntries, ProjectChunkIds> convertIdPathsToWatcherEntriesAndIds(
        const std::vector<IdPaths> &idPaths)
    {
        WatcherEntries entries;
        entries.reserve(sizeOfIdPaths(idPaths));
        ProjectChunkIds ids;
        ids.reserve(ids.size());

        auto outputIterator = std::back_inserter(entries);

        for (const IdPaths &idPath : idPaths)
        {
            ProjectChunkId id = idPath.id;

            ids.push_back(id);

            outputIterator = std::transform(
                idPath.sourceIds.begin(), idPath.sourceIds.end(), outputIterator, [&](SourceId sourceId) {
                    return WatcherEntry{id,
                                        m_pathCache.sourceContextId(sourceId),
                                        sourceId,
                                        m_fileStatusCache.lastModifiedTime(sourceId)};
                });
        }

        std::sort(entries.begin(), entries.end());
        std::sort(ids.begin(), ids.end());

        return {entries, ids};
    }

    void addEntries(const WatcherEntries &entries)
    {
        auto newEntries = notWatchedEntries(entries);

        auto filteredPaths = filterNotWatchedPaths(newEntries);

        mergeToWatchedEntries(newEntries);

        if (!filteredPaths.empty())
            m_fileSystemWatcher.addPaths(convertWatcherEntriesToDirectoryPathList(filteredPaths));
    }

    template<typename Filter>
    void removeUnusedEntries(const WatcherEntries &entries, Filter filter)
    {
        auto oldEntries = notAnymoreWatchedEntriesWithIds(entries, filter);

        removeFromWatchedEntries(oldEntries);

        auto filteredPaths = filterNotWatchedPaths(oldEntries);

        if (!filteredPaths.empty())
            m_fileSystemWatcher.removePaths(convertWatcherEntriesToDirectoryPathList(filteredPaths));
    }

    FileSystemWatcher &fileSystemWatcher() { return m_fileSystemWatcher; }

    QStringList convertWatcherEntriesToDirectoryPathList(const SourceContextIds &sourceContextIds) const
    {
        return Utils::transform<QStringList>(sourceContextIds, [&](SourceContextId id) {
            return QString(m_pathCache.sourceContextPath(id));
        });
    }

    QStringList convertWatcherEntriesToDirectoryPathList(const WatcherEntries &watcherEntries) const
    {
        SourceContextIds sourceContextIds = Utils::transform<SourceContextIds>(
            watcherEntries, [&](WatcherEntry entry) { return entry.sourceContextId; });

        std::sort(sourceContextIds.begin(), sourceContextIds.end());
        sourceContextIds.erase(std::unique(sourceContextIds.begin(), sourceContextIds.end()),
                               sourceContextIds.end());

        return convertWatcherEntriesToDirectoryPathList(sourceContextIds);
    }

    WatcherEntries notWatchedEntries(const WatcherEntries &entries) const
    {
        WatcherEntries notWatchedEntries;
        notWatchedEntries.reserve(entries.size());

        std::set_difference(entries.begin(),
                            entries.end(),
                            m_watchedEntries.cbegin(),
                            m_watchedEntries.cend(),
                            std::back_inserter(notWatchedEntries));

        return notWatchedEntries;
    }

    SourceContextIds notWatchedPaths(const SourceContextIds &ids) const
    {
        SourceContextIds notWatchedDirectoryIds;
        notWatchedDirectoryIds.reserve(ids.size());

        std::set_difference(ids.begin(),
                            ids.end(),
                            m_watchedEntries.cbegin(),
                            m_watchedEntries.cend(),
                            std::back_inserter(notWatchedDirectoryIds));

        return notWatchedDirectoryIds;
    }

    template<typename Compare>
    WatcherEntries notAnymoreWatchedEntries(const WatcherEntries &newEntries, Compare compare) const
    {
        WatcherEntries notAnymoreWatchedEntries;
        notAnymoreWatchedEntries.reserve(m_watchedEntries.size());

        std::set_difference(m_watchedEntries.cbegin(),
                            m_watchedEntries.cend(),
                            newEntries.begin(),
                            newEntries.end(),
                            std::back_inserter(notAnymoreWatchedEntries),
                            compare);

        return notAnymoreWatchedEntries;
    }

    template<typename Filter>
    WatcherEntries notAnymoreWatchedEntriesWithIds(const WatcherEntries &newEntries, Filter filter) const
    {
        auto oldEntries = notAnymoreWatchedEntries(newEntries, std::less<WatcherEntry>());

        auto newEnd = std::remove_if(oldEntries.begin(), oldEntries.end(), filter);

        oldEntries.erase(newEnd, oldEntries.end());

        return oldEntries;
    }

    void mergeToWatchedEntries(const WatcherEntries &newEntries)
    {
        WatcherEntries newWatchedEntries;
        newWatchedEntries.reserve(m_watchedEntries.size() + newEntries.size());

        std::merge(m_watchedEntries.cbegin(),
                   m_watchedEntries.cend(),
                   newEntries.begin(),
                   newEntries.end(),
                   std::back_inserter(newWatchedEntries));

        m_watchedEntries = std::move(newWatchedEntries);
    }

    static SourceContextIds uniquePaths(const WatcherEntries &pathEntries)
    {
        SourceContextIds uniqueDirectoryIds;
        uniqueDirectoryIds.reserve(pathEntries.size());

        auto compare = [](WatcherEntry first, WatcherEntry second) {
            return first.sourceContextId == second.sourceContextId;
        };

        std::unique_copy(pathEntries.begin(),
                         pathEntries.end(),
                         std::back_inserter(uniqueDirectoryIds),
                         compare);

        return uniqueDirectoryIds;
    }

    SourceContextIds filterNotWatchedPaths(const WatcherEntries &entries) const
    {
        return notWatchedPaths(uniquePaths(entries));
    }

    const WatcherEntries &watchedEntries() const
    {
        return m_watchedEntries;
    }

    WatcherEntries removeIdsFromWatchedEntries(const ProjectPartIds &ids)
    {
        auto keep = [&](WatcherEntry entry) {
            return !std::binary_search(ids.begin(), ids.end(), entry.id);
        };

        auto found = std::stable_partition(m_watchedEntries.begin(), m_watchedEntries.end(), keep);

        WatcherEntries removedEntries(found, m_watchedEntries.end());

        m_watchedEntries.erase(found, m_watchedEntries.end());

        return removedEntries;
    }

    void removeFromWatchedEntries(const WatcherEntries &oldEntries)
    {
        WatcherEntries newWatchedEntries;
        newWatchedEntries.reserve(m_watchedEntries.size() - oldEntries.size());

        std::set_difference(m_watchedEntries.cbegin(),
                            m_watchedEntries.cend(),
                            oldEntries.begin(),
                            oldEntries.end(),
                            std::back_inserter(newWatchedEntries));

        m_watchedEntries = std::move(newWatchedEntries);
    }

    void compressChangedDirectoryPath(const QString &path)
    {
        m_directoryPathCompressor.addSourceContextId(
            m_pathCache.sourceContextId(Utils::PathString{path}));
    }

    WatcherEntries watchedEntriesForPaths(QmlDesigner::SourceContextIds &&sourceContextIds)
    {
        WatcherEntries foundEntries;
        foundEntries.reserve(m_watchedEntries.size());

        set_greedy_intersection_call(m_watchedEntries.begin(),
                                     m_watchedEntries.end(),
                                     sourceContextIds.begin(),
                                     sourceContextIds.end(),
                                     [&](WatcherEntry &entry) {
                                         m_fileStatusCache.update(entry.sourceId);
                                         auto currentLastModified = m_fileStatusCache.lastModifiedTime(
                                             entry.sourceId);
                                         if (entry.lastModified < currentLastModified) {
                                             foundEntries.push_back(entry);
                                             entry.lastModified = currentLastModified;
                                         }
                                     });

        return foundEntries;
    }

    SourceIds watchedPaths(const WatcherEntries &entries) const
    {
        auto sourceIds = Utils::transform<SourceIds>(entries, &WatcherEntry::sourceId);

        std::sort(sourceIds.begin(), sourceIds.end());

        sourceIds.erase(std::unique(sourceIds.begin(), sourceIds.end()), sourceIds.end());

        return sourceIds;
    }

    std::vector<IdPaths> idPathsForWatcherEntries(WatcherEntries &&foundEntries)
    {
        std::sort(foundEntries.begin(), foundEntries.end(), [](WatcherEntry first, WatcherEntry second) {
            return std::tie(first.id, first.sourceId) < std::tie(second.id, second.sourceId);
        });

        std::vector<IdPaths> idPaths;
        idPaths.reserve(foundEntries.size());

        for (WatcherEntry entry : foundEntries) {
            if (idPaths.empty() || idPaths.back().id != entry.id)
                idPaths.emplace_back(entry.id, SourceIds{});
            idPaths.back().sourceIds.push_back(entry.sourceId);
        }

        return idPaths;
    }

    void addChangedPathForFilePath(SourceContextIds &&sourceContextIds)
    {
        if (m_notifier) {
            WatcherEntries foundEntries = watchedEntriesForPaths(std::move(sourceContextIds));

            SourceIds watchedSourceIds = watchedPaths(foundEntries);

            std::vector<IdPaths> changedIdPaths = idPathsForWatcherEntries(std::move(foundEntries));

            m_notifier->pathsChanged(watchedSourceIds);
            m_notifier->pathsWithIdsChanged(changedIdPaths);
        }
    }

    SourcePathCache &pathCache() { return m_pathCache; }

private:
    WatcherEntries m_watchedEntries;
    FileSystemWatcher m_fileSystemWatcher;
    FileStatusCache m_fileStatusCache;
    FileSystemInterface &m_fileSystem;
    SourcePathCache &m_pathCache;
    ProjectStoragePathWatcherNotifierInterface *m_notifier;
    DirectoryPathCompressor<Timer> m_directoryPathCompressor;
};

} // namespace QmlDesigner