public GetChangesParameters GetChanges(uint batchSize, SyncKnowledge destinationKnowledge) { Log("GetChangeBatch: {0}", sqlProvider.Connection.ConnectionString); var changesWrapper = new GetChangesParameters(); changesWrapper.ChangeBatch = sqlProvider.GetChangeBatch(batchSize, destinationKnowledge, out changesWrapper.DataRetriever); var context = changesWrapper.DataRetriever as DbSyncContext; //Check to see if data is batched if (context != null && context.IsDataBatched) { Log("GetChangeBatch: Data Batched. Current Batch #:{0}", ++batchCount); //Dont send the file location info. Just send the file name var fileName = new FileInfo(context.BatchFileName).Name; batchIdToFileMapper[fileName] = context.BatchFileName; context.BatchFileName = fileName; } return(changesWrapper); }
/// <summary> /// Get changes for a client using the knowledge that is passed in. /// </summary> /// <param name="serverBlob">Client knowledge as byte[]</param> /// <returns>Response containing the new knowledge and the list of changes.</returns> public GetChangesResponse GetChanges(byte[] serverBlob) { bool isNewClient = false; var response = new GetChangesResponse(); var syncBlob = new SyncBlob(); byte[] clientKnowledgeBlob = null; // If the incoming knowledge blob is null, then we need to initialize a new scope // for this request. if (null == serverBlob || 0 == serverBlob.Length) { // Create a new Guid and use that as the client Id. Guid clientId = Guid.NewGuid(); _clientScopeName = String.Format(CultureInfo.InvariantCulture, "{0}_{1}", _scopeName, clientId); _clientSyncId = new SyncId(clientId); CreateNewScopeForClient(); isNewClient = true; syncBlob.ClientScopeName = clientId.ToString(); } else { SyncBlob incomingBlob = SyncBlob.DeSerialize(serverBlob); PopulateClientScopeNameAndSyncId(incomingBlob); syncBlob.ClientScopeName = incomingBlob.ClientScopeName; clientKnowledgeBlob = incomingBlob.ClientKnowledge; if (null != incomingBlob.BatchCode && null != incomingBlob.NextBatch) { // This is a batched request, so handle it separately. return GetChanges(incomingBlob.ClientKnowledge, incomingBlob.BatchCode.Value, incomingBlob.NextBatch.Value); } } // Intialize a SqlSyncProvider object. _sqlSyncProvider = CreateSqlSyncProviderInstance(_clientScopeName, _serverConnectionString, _configuration.SyncObjectSchema); var sessionContext = new SyncSessionContext(_sqlSyncProvider.IdFormats, new SyncCallbacks()); _sqlSyncProvider.BeginSession(SyncProviderPosition.Remote, sessionContext); try { // Get the SyncKnowledge from the blob. If the blob is null, initialize a default SyncKnowledge object. SyncKnowledge clientKnowledge = GetSyncKnowledgeFromBlob(clientKnowledgeBlob); DbSyncContext dbSyncContext; uint changeBatchSize = (_configuration.IsBatchingEnabled) ? (uint)_configuration.DownloadBatchSizeInKB : 0; RowSorter rowSorter = null; do { object changeDataRetriever; // Get the next batch. _sqlSyncProvider.GetChangeBatch(changeBatchSize, clientKnowledge, out changeDataRetriever); dbSyncContext = (DbSyncContext)changeDataRetriever; // Only initialize the RowSorter, if the data is batched. if (null == rowSorter && _configuration.IsBatchingEnabled) { // Clone the client knowledge. var clonedClientKnowledge = clientKnowledge.Clone(); // Combine with the MadeWithKnowledge of the server. clonedClientKnowledge.Combine(dbSyncContext.MadeWithKnowledge); // Use the new knowledge and get and instance of the RowSorter class. rowSorter = GetRowSorter(clonedClientKnowledge); } // Remove version information from the result dataset. RemoveSyncVersionColumns(dbSyncContext.DataSet); // For a new client, we don't want to send tombstones. This will reduce amount of data // transferred and the client doesn't care about tombstones anyways. if (isNewClient) { RemoveTombstoneRowsFromDataSet(dbSyncContext.DataSet); } // Add the dataset to the row sorter. Only use this if batching is enabled. if (_configuration.IsBatchingEnabled) { rowSorter.AddUnsortedDataSet(dbSyncContext.DataSet); // Delete the batch file generated by the provider, since we have read it. // Otherwise we will keep accumulating files which are not needed. if (!String.IsNullOrEmpty(dbSyncContext.BatchFileName) && File.Exists(dbSyncContext.BatchFileName)) { File.Delete(dbSyncContext.BatchFileName); } } } while (!dbSyncContext.IsLastBatch && dbSyncContext.IsDataBatched); List<IOfflineEntity> entities; if (_configuration.IsBatchingEnabled) { // If batching is enabled. Batch batch = SaveBatchesAndReturnFirstBatch(rowSorter); if (null == batch) { entities = new List<IOfflineEntity>(); } else { // Conver to to entities. entities = _converter.ConvertDataSetToEntities(batch.Data); //Only combine the knowledge of this batch. clientKnowledge.Combine(SyncKnowledge.Deserialize(_sqlSyncProvider.IdFormats, batch.LearnedKnowledge)); response.IsLastBatch = batch.IsLastBatch; syncBlob.IsLastBatch = batch.IsLastBatch; if (batch.IsLastBatch) { syncBlob.NextBatch = null; syncBlob.BatchCode = null; } else { syncBlob.NextBatch = batch.NextBatch; syncBlob.BatchCode = batch.BatchCode; } } } else { // No batching. response.IsLastBatch = true; entities = _converter.ConvertDataSetToEntities(dbSyncContext.DataSet); // combine the client and the server knowledge. // the server may have an updated knowledge from the last time the client sync'd. clientKnowledge.Combine(dbSyncContext.MadeWithKnowledge); } // Save data in the response object. syncBlob.ClientKnowledge = clientKnowledge.Serialize(); response.ServerBlob = syncBlob.Serialize(); response.EntityList = entities; } finally { _sqlSyncProvider.EndSession(sessionContext); } return response; }