示例#1
0
        private static void UpdateItems(IDbConnection connection, ISyncableStore store)
        {
            // loop through types and apply updates or inserts
            foreach (var itemType in store.GetItemTypes())
            {
                System.Diagnostics.Debug.WriteLine("Updating itemType = " + itemType);

                IDbCommand getUpdatedItemsCommand = connection.CreateCommand();
                getUpdatedItemsCommand.CommandText =
                    String.Format(
                        "SELECT SyncStatus, GlobalCreatedReplica, CreatedTickCount, GlobalModifiedReplica, ModifiedTickCount, ItemData  FROM SyncItems WHERE SyncStatus IN ({0},{1}) AND ItemType=@ItemType",
                        (int)SyncStatus.Insert, (int)SyncStatus.Update);
                getUpdatedItemsCommand.AddParameter("@ItemType", itemType);

                using (var reader = getUpdatedItemsCommand.ExecuteReader())
                {
                    while (reader != null && reader.Read())
                    {
                        var createdReplicaInfo  = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                        var modifiedReplicaInfo = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                        var itemData            = JObject.Parse((string)reader["ItemData"]);
                        var itemInfo            = new SyncableItemInfo
                        {
                            ItemType = itemType,
                            Created  = createdReplicaInfo,
                            Modified = modifiedReplicaInfo,
                            Deleted  = false
                        };
                        UpdateItem(connection, store, itemInfo, itemData);
                    }
                }
            }
        }
示例#2
0
        private static void DeleteItems(IDbConnection connection, ISyncableStore store)
        {
            // loop backwards through types and apply deletions
            foreach (var itemType in store.GetItemTypes().Reverse())
            {
                IDbCommand getDeletedItemsCommand = connection.CreateCommand();
                getDeletedItemsCommand.CommandText =
                    String.Format("SELECT * FROM SyncItems WHERE SyncStatus IN ({0},{1}) AND ItemType=@ItemType",
                                  (int)SyncStatus.Delete, (int)SyncStatus.DeleteNonExisting);
                getDeletedItemsCommand.AddParameter("@ItemType", itemType);

                using (var reader = getDeletedItemsCommand.ExecuteReader())
                {
                    while (reader != null && reader.Read())
                    {
                        var createdReplicaInfo  = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Created");
                        var modifiedReplicaInfo = SessionDbHelper.ReplicaInfoFromDataReader(reader, "Modified");
                        var itemInfo            = new SyncableItemInfo
                        {
                            ItemType = itemType,
                            Created  = createdReplicaInfo,
                            Modified = modifiedReplicaInfo,
                            Deleted  = true
                        };
                        store.DeleteItem(itemInfo);
                    }
                }
            }
        }
示例#3
0
        public JObject PutChanges(JObject request)
        {
            SessionDbHelper.ClearSyncItems(_connection);

            var remoteKnowledge = SyncUtil.KnowledgeFromJson(request["knowledge"]);

            SessionDbHelper.SaveRemoteKnowledge(_connection, remoteKnowledge);

            var json = new JObject();

            return(json);
        }
示例#4
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);
        }
示例#5
0
        public JObject BeginSession(JObject request)
        {
            var remoteItemTypes = request["itemTypes"].Select(itemType => (string)itemType).ToList();

            if (!remoteItemTypes.SequenceEqual(_store.GetItemTypes()))
            {
                throw new Exception("Mismatched item types");
            }

            SessionDbHelper.CreateSessionDbTables(_connection);

            var json = new JObject {
                { "maxBatchCount", 50 }, { "maxBatchSize", 1024 * 1024 }, { "useGzip", true }
            };

            return(json);
        }
示例#6
0
        public JObject GetItemDataBatch(JObject request)
        {
            var startItem     = (int)request["startItem"];
            var maxBatchCount = (int)request["maxBatchCount"];
            var maxBatchSize  = (int)request["maxBatchSize"];

            var batchArray = new JArray();

            var getChangedItemsCommand = _connection.CreateCommand();

            getChangedItemsCommand.CommandText = String.Format("SELECT ItemID, SyncStatus, ItemType, GlobalCreatedReplica, CreatedTickCount, GlobalModifiedReplica, ModifiedTickCount FROM SyncItems WHERE ItemID >= {0} ORDER BY ItemID", startItem);
            using (var reader = getChangedItemsCommand.ExecuteReader())
            {
                var batchSize = 0;
                while (reader != null && reader.Read())
                {
                    ISyncableItemInfo requestedItemInfo = SessionDbHelper.SyncableItemInfoFromDataReader(reader);

                    var     singleItem = new JObject();
                    JObject builder    = SyncUtil.JsonItemFromSyncableItemInfo(requestedItemInfo);
                    if (!requestedItemInfo.Deleted)
                    {
                        _store.BuildItemData(requestedItemInfo, builder);
                    }
                    singleItem.Add(new JProperty("item", builder));

                    batchSize += singleItem.ToString().Length;
                    batchArray.Add(singleItem);

                    if (batchArray.Count >= maxBatchCount || batchSize >= maxBatchSize)
                    {
                        break;
                    }
                }
            }

            var json = new JObject {
                { "batch", batchArray }
            };

            return(json);
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
0
        public JObject ApplyChanges(JObject request)
        {
            SyncUtil.ApplyChangesAndUpdateKnowledge(_connection, _store, SessionDbHelper.LoadRemoteKnowledge(_connection));

            return(new JObject());
        }