summaryrefslogtreecommitdiff
path: root/src/syncevo/TrackingSyncSource.cpp
blob: ca8c2951860521a8c42da61e28b5101e0934e59f (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
/*
 * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
 * Copyright (C) 2009 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 <syncevo/TrackingSyncSource.h>
#include <syncevo/SafeConfigNode.h>
#include <syncevo/PrefixConfigNode.h>

#include <boost/bind.hpp>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

TrackingSyncSource::TrackingSyncSource(const SyncSourceParams &params,
                                       int granularitySeconds) :
    TestingSyncSource(params)
{
    boost::shared_ptr<ConfigNode> safeNode(new SafeConfigNode(params.m_nodes.getTrackingNode()));
    m_trackingNode.reset(new PrefixConfigNode("item-", safeNode));
    m_metaNode = safeNode;
    m_operations.m_checkStatus = boost::bind(&TrackingSyncSource::checkStatus, this, _1);
    m_operations.m_isEmpty = boost::bind(&TrackingSyncSource::isEmpty, this);
    SyncSourceRevisions::init(this, this, granularitySeconds, m_operations);
}

void TrackingSyncSource::checkStatus(SyncSourceReport &changes)
{
    // use the most reliable (and most expensive) method by default
    ChangeMode mode = CHANGES_FULL;

    // assume that we do a regular sync, with reusing stored information
    // if possible
    string oldRevision = m_metaNode->readProperty("databaseRevision");
    if (!oldRevision.empty()) {
        string newRevision = databaseRevision();
        SE_LOG_DEBUG(NULL, getDisplayName(), "old database revision '%s', new revision '%s'",
                     oldRevision.c_str(),
                     newRevision.c_str());
        if (newRevision == oldRevision) {
            SE_LOG_DEBUG(NULL, getDisplayName(), "revisions match, no item changes");
            mode = CHANGES_NONE;
        }
    }
    if (mode == CHANGES_FULL) {
        SE_LOG_DEBUG(NULL, getDisplayName(), "using full item scan to detect changes");
    }

    detectChanges(*m_trackingNode, mode);

    // copy our item counts into the report
    changes.setItemStat(ITEM_LOCAL, ITEM_ADDED, ITEM_TOTAL, getNewItems().size());
    changes.setItemStat(ITEM_LOCAL, ITEM_UPDATED, ITEM_TOTAL, getUpdatedItems().size());
    changes.setItemStat(ITEM_LOCAL, ITEM_REMOVED, ITEM_TOTAL, getDeletedItems().size());
    changes.setItemStat(ITEM_LOCAL, ITEM_ANY, ITEM_TOTAL, getAllItems().size());
}

void TrackingSyncSource::beginSync(const std::string &lastToken, const std::string &resumeToken)
{
    // use the most reliable (and most expensive) method by default
    ChangeMode mode = CHANGES_FULL;

    // resume token overrides the normal token; safe to ignore in most
    // cases and this detectChanges() is done independently of the
    // token, but let's do it right here anyway
    string token;
    if (!resumeToken.empty()) {
        token = resumeToken;
    } else {
        token = lastToken;
    }
    // slow sync if token is empty
    if (token.empty()) {
        SE_LOG_DEBUG(NULL, getDisplayName(), "slow sync or testing, do full item scan to detect changes");
        mode = CHANGES_SLOW;
    } else {
        string oldRevision = m_metaNode->readProperty("databaseRevision");
        if (!oldRevision.empty()) {
            string newRevision = databaseRevision();
            SE_LOG_DEBUG(NULL, getDisplayName(), "old database revision '%s', new revision '%s'",
                         oldRevision.c_str(),
                         newRevision.c_str());
            if (newRevision == oldRevision) {
                SE_LOG_DEBUG(NULL, getDisplayName(), "revisions match, no item changes");
                mode = CHANGES_NONE;
            }

            // Reset old revision. If anything goes wrong, then we
            // don't want to rely on a possibly incorrect optimization.
            m_metaNode->setProperty("databaseRevision", "");
            m_metaNode->flush();
        }
    }
    if (mode == CHANGES_FULL) {
        SE_LOG_DEBUG(NULL, getDisplayName(), "using full item scan to detect changes");
    }

    bool forceSlowSync = detectChanges(*m_trackingNode, mode);
    if (forceSlowSync) {
        // tell engine that we need a slow sync
        SE_THROW_EXCEPTION_STATUS(StatusException,
                                  "change detection incomplete, must do slow sync",
                                  STATUS_SLOW_SYNC_508);
    }
}

std::string TrackingSyncSource::endSync(bool success)
{
    // store changes persistently
    flush();

    if (success) {
        string updatedRevision = databaseRevision();
        m_metaNode->setProperty("databaseRevision", updatedRevision);
        // flush both nodes, just in case; in practice, the properties
        // end up in the same file and only get flushed once
        m_trackingNode->flush();
        m_metaNode->flush();
    } else {
        // The Synthesis docs say that we should rollback in case of
        // failure. Cannot do that for data, so lets at least keep
        // the revision map unchanged.
    }

    // no token handling at the moment (not needed for clients):
    // return a non-empty token to distinguish an incremental
    // sync from a slow sync in beginSync()
    return "1";
}

TrackingSyncSource::InsertItemResult TrackingSyncSource::insertItem(const std::string &luid, const std::string &item)
{
    InsertItemResult res = insertItem(luid, item, false);
    if (res.m_state != ITEM_NEEDS_MERGE) {
        updateRevision(*m_trackingNode, luid, res.m_luid, res.m_revision);
    }
    return res;
}

TrackingSyncSource::InsertItemResult TrackingSyncSource::insertItemRaw(const std::string &luid, const std::string &item)
{
    InsertItemResult res = insertItem(luid, item, true);
    if (res.m_state != ITEM_NEEDS_MERGE) {
        updateRevision(*m_trackingNode, luid, res.m_luid, res.m_revision);
    }
    return res;
}

void TrackingSyncSource::readItem(const std::string &luid, std::string &item)
{
    readItem(luid, item, false);
}

void TrackingSyncSource::readItemRaw(const std::string &luid, std::string &item)
{
    readItem(luid, item, true);
}

void TrackingSyncSource::deleteItem(const std::string &luid)
{
    removeItem(luid);
    deleteRevision(*m_trackingNode, luid);
}

void TrackingSyncSource::enableServerMode()
{
    SyncSourceAdmin::init(m_operations, this);
    SyncSourceBlob::init(m_operations, getCacheDir());
}

bool TrackingSyncSource::serverModeEnabled() const
{
    return m_operations.m_loadAdminData;
}

std::string TrackingSyncSource::getPeerMimeType() const
{
    return getMimeType();
}

SE_END_CXX