Пример #1
0
        public JObject GetChanges(JObject request)
        {
            var remoteKnowledge = SyncUtil.KnowledgeFromJson(request["knowledge"]);
            var changedItems    = _store.LocateChangedItems(remoteKnowledge).ToList();

            SessionDbHelper.SaveChangedItems(_connection, changedItems);
            var json = new JObject
            {
                { "knowledge", SyncUtil.KnowledgeToJson(_store.GenerateLocalKnowledge()) },
                { "totalChanges", changedItems.Count() }
            };

            return(json);
        }
Пример #2
0
        private async Task <IEnumerable <SyncConflict> > PullChanges()
        {
            ReportProgressAndCheckCacellation(new SyncProgress
            {
                Stage           = SyncStage.FindingRemoteChanges,
                PercentComplete = 0,
                Message         = "Looking for remote changes"
            });

            var request = new JObject {
                { "sessionID", _remoteSessionId }
            };

            var localKnowledge = _store.GenerateLocalKnowledge().ToList();

            request.Add(new JProperty("knowledge", SyncUtil.KnowledgeToJson(localKnowledge)));

            JObject response = await _transport.TransportAsync(SyncEndpoint.GetChanges, request);

            _remoteKnowledge = SyncUtil.KnowledgeFromJson(response["knowledge"]);
            var totalChanges = (int)response["totalChanges"];

            ReportProgressAndCheckCacellation(new SyncProgress
            {
                Stage           = SyncStage.FindingRemoteChanges,
                PercentComplete = 100,
                Message         = String.Format("Found {0} remote changes", totalChanges)
            });

            ReportProgressAndCheckCacellation(new SyncProgress
            {
                Stage           = SyncStage.DownloadingRemoteChanges,
                PercentComplete = 0,
                Message         = String.Format("Downloading {0} remote changes", totalChanges)
            });
            using (var connection = _syncSessionDbConnectionProvider.GetSyncSessionDbConnection(_localSessionId))
            {
                connection.ExecuteNonQuery("BEGIN");
                SessionDbHelper.ClearSyncItems(connection);

                long startTick = Environment.TickCount;
                int  previousPercentComplete = -1;
                for (int i = 1; i <= totalChanges;)
                {
                    i += await SaveChangesBatch(connection, localKnowledge, i);

                    int percentComplete = ((i * 100) / totalChanges);
                    if (percentComplete != previousPercentComplete)
                    {
                        ReportProgressAndCheckCacellation(new SyncProgress
                        {
                            Stage           = SyncStage.DownloadingRemoteChanges,
                            PercentComplete = percentComplete,
                            Message         =
                                String.Format("Downloading remote changes, {0}% complete ({1})", percentComplete,
                                              String.Format("Averaging {0}ms/item over {1} items",
                                                            (Environment.TickCount - startTick) / i, i))
                        });
                    }
                    previousPercentComplete = percentComplete;
                }
                connection.ExecuteNonQuery("COMMIT");
                ReportProgressAndCheckCacellation(new SyncProgress
                {
                    Stage           = SyncStage.DownloadingRemoteChanges,
                    PercentComplete = 100,
                    Message         = String.Format("Downloaded all {0} remote changes", totalChanges)
                });

                ReportProgressAndCheckCacellation(new SyncProgress
                {
                    Stage           = SyncStage.CheckingForConflicts,
                    PercentComplete = 0,
                    Message         = "Looking for conflicts"
                });
                var conflicts = new List <SyncConflict>();
                conflicts.AddRange(CheckForDuplicates(connection));
                conflicts.AddRange(LoadConflicts(connection));
                ReportProgressAndCheckCacellation(new SyncProgress
                {
                    Stage           = SyncStage.CheckingForConflicts,
                    PercentComplete = 100,
                    Message         = String.Format("Found {0} conflicts", conflicts.Count)
                });
                return(conflicts);
            }
        }