summaryrefslogtreecommitdiff
path: root/src/backends/webdav/CardDAVSource.cpp
blob: 0e6c1b42c52d31b6132e023501b10af9da7ecf8f (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
/*
 * Copyright (C) 2010 Intel Corporation
 */

#include "CardDAVSource.h"

#ifdef ENABLE_DAV

#include <syncevo/declarations.h>
SE_BEGIN_CXX

// TODO: use EDS backend icalstrdup.c
#define ical_strdup(_x) (_x)

typedef std::shared_ptr<TransportStatusException> BatchReadFailure;

class CardDAVCache : public std::map< std::string, boost::variant<std::string, BatchReadFailure> >
{
};


CardDAVSource::CardDAVSource(const SyncSourceParams &params,
                             const std::shared_ptr<Neon::Settings> &settings) :
    WebDAVSource(params, settings),
    m_readAheadOrder(READ_NONE),
    m_cacheMisses(0),
    m_contactReads(0),
    m_contactsFromDB(0),
    m_contactQueries(0)
{
    SyncSourceLogging::init(InitList<std::string>("N_FIRST") + "N_MIDDLE" + "N_LAST",
                            " ",
                            m_operations);
}

void CardDAVSource::logCacheStats(Logger::Level level)
{
    SE_LOG(getDisplayName(), level,
           "requested %d, retrieved %d from server in %d queries, misses %d/%d (%d%%)",
           m_contactReads,
           m_contactsFromDB,
           m_contactQueries,
           m_cacheMisses, m_contactReads,
           m_contactReads ? m_cacheMisses * 100 / m_contactReads : 0);
}

std::string CardDAVSource::getDescription(const string &luid)
{
    // TODO
    return "";
}

void CardDAVSource::readItemInternal(const std::string &luid, std::string &item, bool raw)
{
    if (m_cardDAVCache) {
        auto it = m_cardDAVCache->find(luid);
        if (it != m_cardDAVCache->end()) {
            const std::string *data = boost::get<const std::string>(&it->second);
            if (data) {
                SE_LOG_DEBUG(getDisplayName(), "reading %s from cache", luid.c_str());
                item = *data;
                return;
            }
            const BatchReadFailure *failure = boost::get<BatchReadFailure>(&it->second);
            if (failure) {
                SE_LOG_DEBUG(getDisplayName(), "reading %s into cache had failed: %s",
                             luid.c_str(), (*failure)->what());
                throw **failure;
            }
            SE_THROW(StringPrintf("internal error, empty cache entry for %s", luid.c_str()));
        }
    }

    if (m_readAheadOrder != READ_NONE) {
        m_cardDAVCache = readBatch(luid);
        // Try again above.
        readItemInternal(luid, item, raw);
        return;
    }

    // Fallback: get inidividual item.
    m_contactsFromDB++;
    m_contactQueries++;
    WebDAVSource::readItem(luid, item, raw);
}

void CardDAVSource::readItem(const std::string &luid, std::string &item, bool raw)
{
    m_contactReads++;
    readItemInternal(luid, item, raw);
    logCacheStats(Logger::DEBUG);
}

static size_t MaxBatchSize()
{
    int maxBatchSize = atoi(getEnv("SYNCEVOLUTION_CARDDAV_BATCH_SIZE", "50"));
    if (maxBatchSize < 1) {
        maxBatchSize = 1;
    }
    return maxBatchSize;
}

std::shared_ptr<CardDAVCache> CardDAVSource::readBatch(const std::string &luid)
{
    static size_t maxBatchSize = MaxBatchSize();
    BatchLUIDs luids;
    luids.reserve(maxBatchSize);
    bool found = false;

    switch (m_readAheadOrder) {
    case READ_ALL_ITEMS:
    case READ_CHANGED_ITEMS: {
        const Items_t &items = getAllItems();
        const Items_t &newItems = getNewItems();
        const Items_t &updatedItems = getUpdatedItems();
        auto it = items.find(luid);

        // Always read the requested item, even if not found in item list.
        luids.push_back(&luid);

        if (it != items.end()) {
            // Check that it is a valid candidate for caching, else
            // we have a cache miss prediction.
            if (m_readAheadOrder == READ_ALL_ITEMS ||
                newItems.find(luid) != newItems.end() ||
                updatedItems.find(luid) != updatedItems.end()) {
                found = true;
            }
            ++it;
        }
        while (luids.size() < maxBatchSize &&
               it != items.end()) {
            const std::string &luid = *it;
            if (m_readAheadOrder == READ_ALL_ITEMS ||
                newItems.find(luid) != newItems.end() ||
                updatedItems.find(luid) != updatedItems.end()) {
                luids.push_back(&luid);
            }
            ++it;
        }
        break;
    }
    case READ_SELECTED_ITEMS: {
        ReadAheadItems::const_iterator it; // boost::find(std::make_pair(, m_nextLUIDs.end()), luid) - not available on Ubuntu Lucid
        for (it = m_nextLUIDs.begin();
             it != m_nextLUIDs.end() && *it != luid;
             ++it)
            {}

        // Always read the requested item, even if not found in item list.
        luids.push_back(&luid);
        if (it != m_nextLUIDs.end()) {
            found = true;
            ++it;
        }
        while (luids.size() < maxBatchSize &&
               it != m_nextLUIDs.end()) {
            luids.push_back(&*it);
            ++it;
        }
        break;
    }
    case READ_NONE:
        // May be reached when read-ahead was turned off while
        // preparing for it.
        luids.push_back(&luid);
        break;
    }

    std::shared_ptr<CardDAVCache> cache;
    if (m_readAheadOrder != READ_NONE &&
        !found) {
        // The requested contact was not on our list. Consider this
        // a cache miss (or rather, cache prediction failure) and turn
        // off the read-ahead.
        m_cacheMisses++;
        SE_LOG_DEBUG(getDisplayName(), "reading: disable read-ahead due to cache miss");
        m_readAheadOrder = READ_NONE;
        return cache;
    }

    cache.reset(new CardDAVCache);
    if (!luids.empty()) {
        Timespec deadline = createDeadline();
        m_contactQueries++;
        m_contactsFromDB += luids.size();
        getSession()->startOperation("MULTIGET", deadline);
        while (!luids.empty()) {
            std::stringstream query;

            query <<
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
                "<C:addressbook-multiget xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
                "<D:prop>\n"
                "<D:getetag/>\n"
                "<C:address-data/>\n"
                "</D:prop>\n"
                ;
            for (const std::string *luid: luids) {
                query << "<D:href>" << luid2path(*luid) << "</D:href>\n";
            }
            query << "</C:addressbook-multiget>";

            string data;
            Neon::XMLParser parser;
            // This removes all items for which we get data from luids.
            // The purpose of that is two-fold: don't request data again that
            // we already got when resending, and detect missing 404 status errors
            // with Google.
            auto process = [this, &luids, &data, &cache] (const std::string &href, const std::string &etag, const std::string &status) {
                std::string luid = path2luid(href);

                // TODO: error checking
                CardDAVCache::mapped_type result;
                if (!data.empty()) {
                    result = data;
                    SE_LOG_DEBUG(getDisplayName(), "batch response: got %ld bytes of data for %s",
                                 (long)data.size(), luid.c_str());
                } else {
                    SE_LOG_DEBUG(getDisplayName(), "batch response: unknown failure for %s",
                                 luid.c_str());
                }

                (*cache)[luid] = result;
                bool found = false;
                for (auto it = luids.begin();
                     it != luids.end();
                     ++it) {
                    if (**it == luid) {
                        luids.erase(it);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    SE_LOG_DEBUG(getDisplayName(), "batch response: unexpected item: %s = %s",
                                 href.c_str(), luid.c_str());
                }

                // reset data for next item
                data.clear();
            };
            parser.initReportParser(process);
            parser.pushHandler(Neon::XMLParser::accept("urn:ietf:params:xml:ns:carddav", "address-data"),
                               Neon::XMLParser::append(data));
            std::string request = query.str();
            Neon::Request req(*getSession(), "REPORT", getCalendar().m_path,
                              request, parser);
            req.addHeader("Depth", "0");
            req.addHeader("Content-Type", "application/xml; charset=\"utf-8\"");
            if (req.run()) {
                // CardDAV servers must include a response for each requested item.
                // Google CardDAV doesn't due that at the time of implementing the
                // batched read. As a workaround assume that any remaining item
                // isn't available.
                for (const std::string *luid: luids) {
                    auto failure = std::make_shared<TransportStatusException>(__FILE__,
                                                                              __LINE__,
                                                                              StringPrintf("%s: not contained in multiget response", luid->c_str()),
                                                                              STATUS_NOT_FOUND);
                    (*cache)[*luid] = failure;
                }
                break;
            }
        }
    }
    return cache;
}

CardDAVSource::InsertItemResult CardDAVSource::insertItem(const string &luid, const std::string &item, bool raw)
{
    invalidateCachedItem(luid);
    return WebDAVSource::insertItem(luid, item, raw);
}

void CardDAVSource::removeItem(const string &luid)
{
    invalidateCachedItem(luid);
    WebDAVSource::removeItem(luid);
}

void CardDAVSource::setReadAheadOrder(ReadAheadOrder order,
                                      const ReadAheadItems &luids)
{
    SE_LOG_DEBUG(getDisplayName(), "reading: set order '%s', %ld luids",
                 order == READ_NONE ? "none" :
                 order == READ_ALL_ITEMS ? "all" :
                 order == READ_CHANGED_ITEMS ? "changed" :
                 order == READ_SELECTED_ITEMS ? "selected" :
                 "???",
                 (long)luids.size());
    m_readAheadOrder = order;
    m_nextLUIDs = luids;

    // Be conservative and throw away all cached data. Not doing so
    // can confuse our "cache miss" counting, for example when it uses
    // a cache where some entries have been removed in
    // invalidateCachedContact() and then mistakes the gaps for cache
    // misses.
    //
    // Another reason is that we want to use fairly recent data (in
    // case of concurrent changes in the DB, which currently is not
    // detected by the cache).
    m_cardDAVCache.reset();
}

void CardDAVSource::getReadAheadOrder(ReadAheadOrder &order,
                                      ReadAheadItems &luids)
{
    order = m_readAheadOrder;
    luids = m_nextLUIDs;
}

void CardDAVSource::invalidateCachedItem(const std::string &luid)
{
    if (m_cardDAVCache) {
        auto it = m_cardDAVCache->find(luid);
        if (it != m_cardDAVCache->end()) {
            SE_LOG_DEBUG(getDisplayName(), "reading: remove contact %s from cache because of remove or update", luid.c_str());
            // If we happen to read that contact (unlikely), it'll be
            // considered a cache miss. That's okay. Together with
            // counting cache misses it'll help us avoid using
            // read-ahead when the Synthesis engine is randomly
            // accessing contacts.
            m_cardDAVCache->erase(it);
        }
    }
}

bool CardDAVSource::typeMatches(const StringMap &props) const
{
    auto it = props.find("DAV::resourcetype");
    if (it != props.end()) {
        const std::string &type = it->second;
        // allow parameters (no closing bracket)
        // and allow also "carddavaddressbook" (caused by invalid Neon 
        // string concatenation?!)
        if (type.find("<urn:ietf:params:xml:ns:carddav:addressbook") != type.npos ||
            type.find("<urn:ietf:params:xml:ns:carddavaddressbook") != type.npos) {
            return true;
        }
    }
    return false;
}

SE_END_CXX

#endif // ENABLE_DAV