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); } } } }
public SyncSession(ISyncableStore store, ISyncSessionDbConnectionProvider syncSessionDbConnectionProvider, ISyncTransport transport) { if (store == null) { throw new ArgumentNullException("store"); } if (syncSessionDbConnectionProvider == null) { throw new ArgumentNullException("syncSessionDbConnectionProvider"); } if (transport == null) { throw new ArgumentNullException("transport"); } PushMaxBatchCount = 500; PushMaxBatchSize = 1024 * 1024; PullMaxBatchCount = 5000; PullMaxBatchSize = 1024 * 1024 * 10; _store = store; _syncSessionDbConnectionProvider = syncSessionDbConnectionProvider; _transport = transport; _localSessionId = Guid.NewGuid().ToString(); _syncSessionDbConnectionProvider.SessionStart(_localSessionId); using (var connection = _syncSessionDbConnectionProvider.GetSyncSessionDbConnection(_localSessionId)) { SessionDbHelper.CreateSessionDbTables(connection); } }
public HttpResponseMessage BeginSession(JObject request) { var resp = Request.CreateResponse(HttpStatusCode.OK, string.Empty); ValidateRequest(request, false); if (_userService.GetSessionId((string)request["username"]) != null) { ThrowSafeException("Session In Progress", 1, HttpStatusCode.ServiceUnavailable); } string sessionId = Guid.NewGuid().ToString(); _syncSessionDbConnectionProvider.SessionStart(sessionId); using (ISyncableStore store = _userService.GetSyncableStore(_username)) using (IDbConnection connection = _syncSessionDbConnectionProvider.GetSyncSessionDbConnection(sessionId)) { var json = new SyncServerSession(store, connection).BeginSession(request); json.Add(new JProperty("sessionID", sessionId)); _userService.SetSessionId(_username, sessionId); resp.Content = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); } return(resp); }
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); } } } }
public HttpResponseMessage PutItemDataBatch(JObject request) { var resp = Request.CreateResponse(HttpStatusCode.OK, string.Empty); ValidateRequest(request); using (ISyncableStore store = _userService.GetSyncableStore(_username)) using (IDbConnection connection = _syncSessionDbConnectionProvider.GetSyncSessionDbConnection(_userService.GetSessionId(_username))) { JObject json = new SyncServerSession(store, connection).PutItemDataBatch(request); resp.Content = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); } return(resp); }
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(); } } } }
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); }
public static void ApplyChangesAndUpdateKnowledge(IDbConnection connection, ISyncableStore store, IEnumerable <IReplicaInfo> remoteKnowledge) { if (ConflictsExist(connection) || MissingData(connection)) { throw new InvalidOperationException(); } store.BeginChanges(); try { UpdateItems(connection, store); DeleteItems(connection, store); store.UpdateLocalKnowledge(remoteKnowledge); store.AcceptChanges(); } catch (Exception) { store.RejectChanges(); throw; } }
public DirectSyncTransport(ISyncableStore store, IDbConnection connection) { _serverSession = new SyncServerSession(store, connection); }
public SyncServerSession(ISyncableStore store, IDbConnection connection) { _store = store; _connection = connection; }