summaryrefslogtreecommitdiff
path: root/src/dbus/server/pim/filtered-view.cpp
blob: cd354dc55a6d6ee6ae964d33af22fac520fc59a7 (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
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "filtered-view.h"
#include <syncevo/lcs.h>
#include <iterator>
#include <syncevo/BoostHelper.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

FilteredView::FilteredView(const boost::shared_ptr<IndividualView> &parent,
                           const boost::shared_ptr<IndividualFilter> &filter) :
    m_parent(parent),
    m_filter(filter)
{
    setName("filtered view");
}

void FilteredView::init(const boost::shared_ptr<FilteredView> &self)
{
    m_self = self;
    m_parent->m_quiescenceSignal.connect(boost::bind(&FilteredView::parentQuiescent, m_self));
}

void FilteredView::parentQuiescent()
{
    // State of the parent is stable again. Check if we queued a "fill view"
    // operation and do it now, before forwarding the quiescent signal.
    // This gives us the chance to add a contact before a previous remove
    // signal is sent, which then enables the combination of two signals
    // into one.
    if (m_fillViewOnIdle) {
        fillViewCb();
        m_fillViewOnIdle.deactivate();
    }
    m_quiescenceSignal();
}

boost::shared_ptr<FilteredView> FilteredView::create(const boost::shared_ptr<IndividualView> &parent,
                                                     const boost::shared_ptr<IndividualFilter> &filter)
{
    boost::shared_ptr<FilteredView> view(new FilteredView(parent, filter));
    view->init(view);
    return view;
}

void FilteredView::doStart()
{
    m_parent->start();

    // Add initial content. Our processing of the new contact must not
    // cause changes to the parent view, otherwise the result will not
    // be inconsistent.
    for (int index = 0; !isFull() && index < m_parent->size(); index++) {
        addIndividual(index, *m_parent->getContact(index));
    }

    // Start listening to signals.
    m_parent->m_addedSignal.connect(ChangeSignal_t::slot_type(boost::bind(&FilteredView::addIndividual, this, _1, _2)).track(m_self));
    m_parent->m_modifiedSignal.connect(ChangeSignal_t::slot_type(boost::bind(&FilteredView::modifyIndividual, this, _1, _2)).track(m_self));
    m_parent->m_removedSignal.connect(ChangeSignal_t::slot_type(boost::bind(&FilteredView::removeIndividual, this, _1, _2)).track(m_self));
}

bool FilteredView::isFull(const Entries_t local2parent,
                          const boost::shared_ptr<IndividualFilter> &filter)
{
    size_t newEndIndex = local2parent.end() - local2parent.begin();
    return !filter->isIncluded(newEndIndex);
}

void FilteredView::fillViewCb()
{
    // Can we add back contacts which were excluded because of the
    // maximum number of results?
    SE_LOG_DEBUG(NULL, NULL, "filtered view %s: fill view on idle", getName());
    int candidate = m_local2parent.empty() ? 0 : m_local2parent.back() + 1;
    while (!isFull() &&
           candidate < m_parent->size()) {
        const IndividualData *data = m_parent->getContact(candidate);
        addIndividual(candidate, *data);
        candidate++;
    }
}

void FilteredView::fillView()
{
    if (!m_fillViewOnIdle) {
        m_fillViewOnIdle.runOnce(-1,
                                 boost::bind(&FilteredView::fillViewCb, this));
    }
}

void FilteredView::replaceFilter(const boost::shared_ptr<IndividualFilter> &individualFilter,
                                 bool refine)
{
    // Keep number of results the same, to avoid additional corner
    // cases.
    if (individualFilter->getMaxResults() != -1 &&
        individualFilter->getMaxResults() != m_filter->getMaxResults()) {
        SE_THROW("refining the search must not change the maximum number of results");
    }
    individualFilter->setMaxResults(m_filter->getMaxResults());

    if (refine) {
        // Take advantage of the hint that the search is more strict:
        // we know that we can limit searching to the contacts which
        // already matched the previous search.
        bool removed = false;
        size_t index = 0;
        while (index < m_local2parent.size()) {
            const IndividualData *data = m_parent->getContact(m_local2parent[index]);
            if (individualFilter->matches(*data)) {
                // Still matched, just skip it.
                ++index;
            } else {
                // No longer matched, remove it.
                m_local2parent.erase(m_local2parent.begin() + index);
                m_removedSignal(index, *data);
                removed = true;
            }
        }
        m_filter = individualFilter;

        if (removed) {
            fillView();
        }
    } else {
        // Brute-force approach.
        //
        // Here is an example of old and new mapping:
        // index into local2parent old value     new value
        //    0                      10              10
        //    1                      20              30
        //    2                      30              40
        //    3                      50              50
        //    4                      70              60
        //    5                       -              70
        //    6                       -              80
        //
        // The LCS (see below) is:
        // (0, 0, 10) (2, 1, 30) (3, 3, 50) (4, 5, 70)
        //
        // The expected change signals for this transition are:
        // "removed", 1
        // "added", 2
        // "added", 4
        // "added", 6
        //
        // Note that this example does not include all corner cases.
        // Also relevant is adding or removing multiple entries at the
        // same index.
        //
        // One could also emit a "modified" signal for each index if
        // it is different, but then a single insertion or deletion
        // would led to invalidating the entire view.
        //
        // 1. build new result list.
        Entries_t local2parent;
        int candidate = 0;
        while (!isFull(local2parent, individualFilter) &&
               candidate < m_parent->size()) {
            const IndividualData *data = m_parent->getContact(candidate);
            if (individualFilter->matches(*data)) {
                local2parent.push_back(candidate);
            }
            candidate++;
        }

        // 2. morph existing one into new one.
        //
        // Uses the SyncEvolution longest-common-subsequence
        // algorithm.  Because all entries are different, there can be
        // only one solution and thus there is no need for a cost
        // function to find "better" solutions.
        std::vector< LCS::Entry<int> > common;
        common.reserve(std::min(m_local2parent.size(), local2parent.size()));
        LCS::lcs(m_local2parent, local2parent, std::back_inserter(common), LCS::accessor_sequence<Entries_t>());

        // To emit the discovered changes as "added" and "removed"
        // signals, we need to look at identical entries.

        // The "delta" here always represents the value "new = b" -
        // "old = a" which needs to be added to the old index to get
        // the new one. It summarizes the change signals emitted so
        // far. For each common entry, we need to check if the delta
        // is different from what we have told the agent so far.
        int delta = 0;

        // The "shift" is the "old modified" - "old original" that
        // tells us how many entries were added (positive value) or
        // removed (negative value) via signals.
        int shift = 0;

        // Old and new index represent the indices of the previous
        // common element plus 1; in other words, the expected next
        // common element.
        size_t oldIndex = 0,
            newIndex = 0;

        BOOST_FOREACH (const LCS::Entry<int> &entry, common) {
            int new_delta = (ssize_t)entry.index_b - (ssize_t)entry.index_a;
            if (delta != new_delta) {
                // When emitting "added" or "removed" signals,
                // be careful about getting the original index
                // in the old set right. It needs to be adjusted
                // to include the already sent changes.
                if (delta < new_delta) {
                    size_t change = new_delta - delta;
                    for (size_t i = 0; i < change; i++) {
                        const IndividualData *data = m_parent->getContact(local2parent[newIndex + i]);
                        // Keep adding at new indices, one new element
                        // after the other.
                        m_addedSignal(oldIndex - shift + i, *data);
                    }
                    shift -= change;
                } else if (delta > new_delta) {
                    size_t change = delta - new_delta;
                    shift += change;
                    for (size_t i = 0; i < change; i++) {
                        size_t index = oldIndex - shift;
                        const IndividualData *data = m_parent->getContact(m_local2parent[index + i]);
                        // Keep removing at the same index, because
                        // that's how it'll look to the recipient.
                        m_removedSignal(index, *data);
                    }
                }
                new_delta = delta;
            }
            oldIndex = entry.index_a + 1;
            newIndex = entry.index_b + 1;
        }

        // Now deal with entries after the latest common entry, in
        // both arrays.
        for (size_t index = oldIndex; index < m_local2parent.size(); index++) {
            const IndividualData *data = m_parent->getContact(m_local2parent[index]);
            m_removedSignal(oldIndex - shift, *data);
        }
        for (size_t index = newIndex; index < local2parent.size(); index++) {
            const IndividualData *data = m_parent->getContact(local2parent[index]);
            m_addedSignal(index, *data);
        }

        // - swap
        std::swap(m_local2parent, local2parent);
    }

    // If the parent is currently busy, then we can delay sending the
    // signal until it is no longer busy.
    if (isQuiescent()) {
        parentQuiescent();
    }
}

void FilteredView::addIndividual(int parentIndex, const IndividualData &data)
{
    // We can use binary search to find the insertion point.
    // Check last entry first, because that is going to be
    // very common when adding via doStart().
    Entries_t::iterator it;
    if (!m_local2parent.empty() &&
        m_local2parent.back() < parentIndex) {
        it = m_local2parent.end();
    } else {
        it =
            std::lower_bound(m_local2parent.begin(),
                             m_local2parent.end(),
                             parentIndex);
    }

    // Adding a contact in the parent changes values in our
    // mapping array, regardless whether the new contact also
    // gets and entry in it. Shift all following indices.
    for (Entries_t::iterator it2 = it;
         it2 != m_local2parent.end();
         ++it2) {
        (*it2)++;
    }

    if (m_filter->matches(data)) {
        size_t index = it - m_local2parent.begin();
        if (m_filter->isIncluded(index)) {
            // Remove first if necessary, to ensure that recipient
            // never has more entries in its view than requested.
            size_t newEndIndex = m_local2parent.end() - m_local2parent.begin();
            if (newEndIndex > index &&
                !m_filter->isIncluded(newEndIndex)) {
                const IndividualData *data = m_parent->getContact(m_local2parent.back());
                SE_LOG_DEBUG(NULL, NULL, "%s: removed at #%ld/%ld to make room for new entry", getName(), (long)(newEndIndex - 1), (long)m_local2parent.size());
                m_local2parent.pop_back();
                m_removedSignal(newEndIndex - 1, *data);
                // Iterator might have pointed to removed entry, which may have
                // invalidated it. Get fresh iterator based on index.
                it = m_local2parent.begin() + index;
            }

            m_local2parent.insert(it, parentIndex);
            SE_LOG_DEBUG(NULL, NULL, "%s: added at #%ld/%ld", getName(), (long)index, (long)m_local2parent.size());
            m_addedSignal(index, data);
        } else {
            SE_LOG_DEBUG(NULL, NULL, "%s: not added at #%ld/%ld because outside of result range", getName(), (long)index, (long)m_local2parent.size());
        }
    }
}

void FilteredView::removeIndividual(int parentIndex, const IndividualData &data)
{
    // The entries are sorted. Therefore we can use a binary search
    // to find the parentIndex or the first entry after it.
    Entries_t::iterator it =
        std::lower_bound(m_local2parent.begin(),
                         m_local2parent.end(),
                         parentIndex);
    // Removing a contact in the parent changes values in our mapping
    // array, regardless whether the removed contact is part of our
    // view. Shift all following indices, including the removed entry
    // if it is part of the view.
    bool found = it != m_local2parent.end() && *it == parentIndex;
    for (Entries_t::iterator it2 = it;
         it2 != m_local2parent.end();
         ++it2) {
        (*it2)--;
    }

    if (found) {
        size_t index = it - m_local2parent.begin();
        SE_LOG_DEBUG(NULL, NULL, "%s: removed at #%ld/%ld", getName(), (long)index, (long)m_local2parent.size());
        m_local2parent.erase(it);
        m_removedSignal(index, data);
        // Try adding more contacts from the parent once the parent
        // is done with sending us changes - in other words, wait until
        // the process is idle.
        fillView();
    }
}

void FilteredView::modifyIndividual(int parentIndex, const IndividualData &data)
{
    Entries_t::iterator it =
        std::lower_bound(m_local2parent.begin(),
                         m_local2parent.end(),
                         parentIndex);
    bool matches = m_filter->matches(data);
    if (it != m_local2parent.end() && *it == parentIndex) {
        // Was matched before the change.
        size_t index = it - m_local2parent.begin();
        if (matches) {
            // Still matched, merely pass on modification signal.
            SE_LOG_DEBUG(NULL, NULL, "%s: modified at #%ld/%ld", getName(), (long)index, (long)m_local2parent.size());
            m_modifiedSignal(index, data);
        } else {
            // Removed.
            SE_LOG_DEBUG(NULL, NULL, "%s: removed at #%ld/%ld due to modification", getName(), (long)index, (long)m_local2parent.size());
            m_local2parent.erase(it);
            m_removedSignal(index, data);
            fillView();
        }
    } else if (matches) {
        // Was not matched before and is matched now => add it.
        size_t index = it - m_local2parent.begin();
        if (m_filter->isIncluded(index)) {
            m_local2parent.insert(it, parentIndex);
            SE_LOG_DEBUG(NULL, NULL, "%s: added at #%ld/%ld due to modification", getName(), (long)index, (long)m_local2parent.size());
            m_addedSignal(index, data);
        }
    } else {
        // Neither matched before nor now => nothing changed.
    }
}

SE_END_CXX