/// <summary>
        ///     Create sync state in database for a sync down and return corresponding SyncState
        /// </summary>
        /// <param name="store"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        /// <param name="soupName"></param>
        /// <returns></returns>
        public static SyncState CreateSyncDown(SmartStore.Store.SmartStore store, SyncDownTarget target, string soupName, SyncOptions options = null)
        {
            var sync = new JObject
            {
                { Constants.SyncType, SyncTypes.SyncDown.ToString() },
                { Constants.SyncTarget, target.AsJson() },
                { Constants.SyncSoupName, soupName },
                { Constants.SyncStatus, SyncStatusTypes.New.ToString() },
                { Constants.SyncProgress, 0 },
                { Constants.SyncTotalSize, -1 },
                { Constants.SyncMaxTimeStamp, -1 },
            };

            if (options != null)
            {
                sync[Constants.SyncOptions] = options.AsJson();
            }
            JObject upserted = store.Upsert(Constants.SyncsSoup, sync);

            if (upserted != null)
            {
                return(FromJson(upserted));
            }
            sync[Constants.SyncStatus] = SyncStatusTypes.Failed.ToString();
            return(FromJson(sync));
        }
Exemplo n.º 2
0
        private void SaveRecordsToSmartStore(string soupName, IEnumerable <JToken> records,
                                             SyncState.MergeModeOptions mergeMode)
        {
            _smartStore.Database.BeginTransaction();
            HashSet <string> idsToSkip = null;

            if (SyncState.MergeModeOptions.LeaveIfChanged == mergeMode)
            {
                idsToSkip = GetDirtyRecordIds(soupName, Constants.Id);
            }

            foreach (JObject record in records.Select(t => t.ToObject <JObject>()))
            {
                // Skip if LeaveIfChanged and id is in dirty list
                if (idsToSkip != null && SyncState.MergeModeOptions.LeaveIfChanged == mergeMode)
                {
                    var id = record.ExtractValue <string>(Constants.Id);
                    if (!String.IsNullOrWhiteSpace(id) && idsToSkip.Contains(id))
                    {
                        continue; // don't write over dirty record
                    }
                }

                // Save
                record[Local]          = false;
                record[LocallyCreated] = false;
                record[LocallyUpdated] = false;
                record[LocallyUpdated] = false;
                _smartStore.Upsert(soupName, record, Constants.Id, false);
            }
            _smartStore.Database.CommitTransaction();
        }
 /// <summary>
 ///     Helper method that inserts/updates a record in the cache.
 /// </summary>
 /// <param name="soupName"></param>
 /// <param name="jObject"></param>
 /// <param name="cacheKey"></param>
 private void UpsertData(string soupName, JObject jObject, string cacheKey)
 {
     if (jObject == null || String.IsNullOrWhiteSpace(soupName))
     {
         return;
     }
     RegisterSoup(soupName, cacheKey);
     try
     {
         _smartStore.Upsert(soupName, jObject, cacheKey);
         AddSoupNameToMasterSoup(soupName);
     }
     catch (SmartStoreException)
     {
         Debug.WriteLine("SmartStoreException occurred while attempting to cache data");
     }
 }