Пример #1
0
        private async Task <int> SaveChangesBatch(IDbConnection connection, IList <IReplicaInfo> localKnowledge,
                                                  int startItem)
        {
            var request = new JObject
            {
                { "sessionID", _remoteSessionId },
                { "startItem", startItem },
                { "maxBatchCount", PullMaxBatchCount },
                { "maxBatchSize", PullMaxBatchSize }
            };

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

            var batch = (JArray)response["batch"];

            foreach (var item in batch)
            {
                ISyncableItemInfo remoteSyncableItemInfo = SyncUtil.SyncableItemInfoFromJson(item["item"]);
                var itemData = new JObject {
                    { "item", item["item"] }
                };

                var status = SyncStatus.MayBeNeeded;
                if (!SyncUtil.KnowledgeContains(localKnowledge, remoteSyncableItemInfo.Modified))
                {
                    ISyncableItemInfo localSyncableItemInfo = _store.LocateCurrentItemInfo(remoteSyncableItemInfo);
                    status = SyncUtil.CalculateSyncStatus(remoteSyncableItemInfo, localSyncableItemInfo,
                                                          _remoteKnowledge);
                }
                SessionDbHelper.SaveItemData(connection, remoteSyncableItemInfo, status, itemData);
                var itemRefs = (JArray)itemData["item"]["itemRefs"];
                foreach (var itemRef in itemRefs)
                {
                    ISyncableItemInfo itemRefInfo      = SyncUtil.SyncableItemInfoFromJson(itemRef);
                    ISyncableItemInfo localItemRefInfo = _store.LocateCurrentItemInfo(itemRefInfo);
                    if (localItemRefInfo != null && localItemRefInfo.Deleted)
                    {
                        await SaveSyncData(connection, itemRefInfo, SyncStatus.MayBeNeeded);
                    }
                }
            }
            return(batch.Count);
        }
Пример #2
0
        private static void UpdateItem(IDbConnection connection, ISyncableStore store, ISyncableItemInfo itemInfo,
                                       JObject itemData)
        {
            // Make sure all the item refs for this item have already been handled
            foreach (var itemRefJson in itemData["item"]["itemRefs"])
            {
                var referencedItemInfo      = SyncableItemInfoFromJson(itemRefJson);
                var localReferencedItemInfo = store.LocateCurrentItemInfo(referencedItemInfo);
                if (localReferencedItemInfo == null || localReferencedItemInfo.Deleted)
                {
                    var getReferencedItemCommand = connection.CreateCommand();
                    getReferencedItemCommand.CommandText =
                        String.Format(
                            "SELECT SyncStatus, GlobalCreatedReplica, CreatedTickCount, GlobalModifiedReplica, ModifiedTickCount, ItemData  FROM SyncItems WHERE  GlobalCreatedReplica=@GlobalCreatedReplica AND CreatedTickCount=@CreatedTickCount AND ItemType=@ItemType");
                    getReferencedItemCommand.AddParameter("@GlobalCreatedReplica", referencedItemInfo.Created.ReplicaId);
                    getReferencedItemCommand.AddParameter("@CreatedTickCount",
                                                          referencedItemInfo.Created.ReplicaTickCount);
                    getReferencedItemCommand.AddParameter("@ItemType", referencedItemInfo.ItemType);

                    using (var reader = getReferencedItemCommand.ExecuteReader())
                    {
                        while (reader != null && reader.Read())
                        {
                            var createdReplicaInfo         = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                            var modifiedReplicaInfo        = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                            var refItemData                = JObject.Parse((string)reader["ItemData"]);
                            var possiblyUpdatedRefItemInfo = new SyncableItemInfo
                            {
                                ItemType = referencedItemInfo.ItemType,
                                Created  = createdReplicaInfo,
                                Modified = modifiedReplicaInfo,
                                Deleted  = false
                            };
                            UpdateItem(connection, store, possiblyUpdatedRefItemInfo, refItemData);
                        }
                    }
                }
            }
            store.SaveItemData(itemInfo, itemData);
        }
Пример #3
0
        public JObject PutItemDataBatch(JObject request)
        {
            IDbCommand command = _connection.CreateCommand();

            command.CommandText = "BEGIN";
            command.ExecuteNonQuery();
            try
            {
                var batch = (JArray)request["batch"];
                foreach (JToken item in batch)
                {
                    var remoteSyncableItemInfo = SyncUtil.SyncableItemInfoFromJson(item["item"]);
                    var itemData = new JObject {
                        { "item", item["item"] }
                    };
                    var remoteKnowledge = SessionDbHelper.LoadRemoteKnowledge(_connection);

                    var localSyncableItemInfo = _store.LocateCurrentItemInfo(remoteSyncableItemInfo);

                    var status = SyncUtil.CalculateSyncStatus(remoteSyncableItemInfo, localSyncableItemInfo, remoteKnowledge);

                    SessionDbHelper.SaveItemData(_connection, remoteSyncableItemInfo, status, itemData);
                }
                command.CommandText = "COMMIT";
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                command.CommandText = "ROLLBACK";
                command.ExecuteNonQuery();
                throw;
            }

            var json = new JObject();

            return(json);
        }