Beispiel #1
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);
        }
Beispiel #2
0
        public JObject GetItemData(JObject request)
        {
            ISyncableItemInfo requestedItemInfo = SyncUtil.SyncableItemInfoFromJson(request["item"]);

            var json = new JObject();

            JObject builder = SyncUtil.JsonItemFromSyncableItemInfo(requestedItemInfo);

            _store.BuildItemData(requestedItemInfo, builder);
            json.Add(new JProperty("item", builder));

            return(json);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public static void ReplaceAllItemRefs(IDbConnection connection, ISyncableStore store,
                                              ISyncableItemInfo remoteItemInfo, ISyncableItemInfo changedItemInfo)
        {
            // update *all* the itemData.item refs in all rows from remoteItemInfo to changedItemInfo
            var selectItemDataCommand = connection.CreateCommand();

            selectItemDataCommand.CommandText = String.Format("SELECT ItemID, ItemData FROM SyncItems");

            using (var reader = selectItemDataCommand.ExecuteReader())
            {
                while (reader != null && reader.Read())
                {
                    JObject itemData     = JObject.Parse((string)reader["ItemData"]);
                    bool    needToUpdate = false;
                    foreach (var itemRefJson in itemData["item"]["itemRefs"])
                    {
                        ISyncableItemInfo referencedItemInfo = SyncUtil.SyncableItemInfoFromJson(itemRefJson);
                        if (referencedItemInfo.ItemType == remoteItemInfo.ItemType &&
                            referencedItemInfo.Created.ReplicaId == remoteItemInfo.Created.ReplicaId &&
                            referencedItemInfo.Created.ReplicaTickCount == remoteItemInfo.Created.ReplicaTickCount &&
                            referencedItemInfo.Modified.ReplicaId == remoteItemInfo.Modified.ReplicaId &&
                            referencedItemInfo.Modified.ReplicaTickCount == remoteItemInfo.Modified.ReplicaTickCount
                            )
                        {
                            itemRefJson["creationReplicaID"]            = changedItemInfo.Created.ReplicaId;
                            itemRefJson["creationReplicaTickCount"]     = changedItemInfo.Created.ReplicaTickCount;
                            itemRefJson["modificationReplicaID"]        = changedItemInfo.Modified.ReplicaId;
                            itemRefJson["modificationReplicaTickCount"] = changedItemInfo.Modified.ReplicaTickCount;
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        IDbCommand updateCommand = connection.CreateCommand();
                        updateCommand.CommandText =
                            "UPDATE SyncItems SET ItemData=@ItemData, GlobalModifiedReplica=@ModifiedReplica, ModifiedTickCount=@TickCount WHERE ItemID=@ItemID";
                        updateCommand.AddParameter("@ItemID", reader["ItemID"]);
                        updateCommand.AddParameter("@ItemData", itemData.ToString());
                        updateCommand.AddParameter("@TickCount", store.IncrementLocalRepilcaTickCount());
                        updateCommand.AddParameter("@ModifiedReplica", store.GetLocalReplicaId());
                        updateCommand.ExecuteNonQuery();
                    }
                }
            }
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public JObject ApplyChanges(JObject request)
        {
            SyncUtil.ApplyChangesAndUpdateKnowledge(_connection, _store, SessionDbHelper.LoadRemoteKnowledge(_connection));

            return(new JObject());
        }