Exemple #1
0
        /// <summary>
        /// Gets the number of operations matching the provided operation kind executed within this batch.
        /// </summary>
        /// <param name="operationKind">The kind of operation.</param>
        /// <returns>The number of operations matching the provided count.</returns>
        public int GetOperationCountByKind(LocalStoreOperationKind operationKind)
        {
            if (this.operationsCountByType.ContainsKey(operationKind))
            {
                return(this.operationsCountByType[operationKind]);
            }

            return(0);
        }
        /// <summary>
        /// Gets the number of operations matching the provided operation kind executed within this batch.
        /// </summary>
        /// <param name="operationKind">The kind of operation.</param>
        /// <returns>The number of operations matching the provided count.</returns>
        public int GetOperationCountByKind(LocalStoreOperationKind operationKind)
        {
            if (this.operationsCountByType.ContainsKey(operationKind))
            {
                return this.operationsCountByType[operationKind];
            }

            return 0;
        }
        public async Task UpsertAsync(string tableName, IEnumerable <JObject> items, bool ignoreMissingColumns)
        {
            Arguments.IsNotNull(tableName, nameof(tableName));
            Arguments.IsNotNull(items, nameof(items));

            if (!tableName.StartsWith(MobileServiceLocalSystemTables.Prefix))
            {
                IDictionary <string, string> existingRecords = null;
                bool analyzeUpserts  = this.trackingContext.TrackingOptions.HasFlag(StoreTrackingOptions.DetectInsertsAndUpdates);
                bool supportsVersion = false;

                if (analyzeUpserts)
                {
                    MobileServiceSystemProperties systemProperties = await this.settings.GetSystemPropertiesAsync(tableName);

                    supportsVersion = systemProperties.HasFlag(MobileServiceSystemProperties.Version);

                    existingRecords = await GetItemsAsync(tableName, items.Select(i => this.objectReader.GetId(i)), supportsVersion);
                }

                await this.store.UpsertAsync(tableName, items, ignoreMissingColumns);

                foreach (var item in items)
                {
                    string itemId = this.objectReader.GetId(item);
                    LocalStoreOperationKind operationKind = LocalStoreOperationKind.Upsert;

                    if (analyzeUpserts)
                    {
                        if (existingRecords.ContainsKey(itemId))
                        {
                            operationKind = LocalStoreOperationKind.Update;

                            // If the update isn't a result of a local operation, check if the item exposes a version property
                            // and if we truly have a new version (an actual change) before tracking the change.
                            // This avoids update notifications for records that haven't changed, which would usually happen as a result of a pull
                            // operation, because of the logic used to pull changes.
                            if (this.trackingContext.Source != StoreOperationSource.Local && supportsVersion &&
                                string.Compare(existingRecords[itemId], item[MobileServiceSystemColumns.Version].ToString()) == 0)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            operationKind = LocalStoreOperationKind.Insert;
                        }
                    }

                    TrackStoreOperation(tableName, itemId, operationKind);
                }
            }
            else
            {
                await this.store.UpsertAsync(tableName, items, ignoreMissingColumns);
            }
        }
Exemple #4
0
        /// <summary>
        /// Creates an instance of <see cref="StoreOperation"/> class.
        /// </summary>
        /// <param name="tableName">The name of the table that is the target of this operation.</param>
        /// <param name="recordId">The target record identifier.</param>
        /// <param name="kind">The kind of operation.</param>
        /// <param name="source">The source that triggered this operation.</param>
        /// <param name="batchId">The id of the batch this operation belongs to.</param>
        public StoreOperation(string tableName, string recordId, LocalStoreOperationKind kind, StoreOperationSource source, string batchId)
        {
            Arguments.IsNotNull(tableName, nameof(tableName));
            Arguments.IsNotNull(recordId, nameof(recordId));
            Arguments.IsNotNull(batchId, nameof(batchId));

            this.TableName = tableName;
            this.RecordId  = recordId;
            this.Kind      = kind;
            this.Source    = source;
            this.BatchId   = batchId;
        }
Exemple #5
0
        private void TrackStoreOperation(string tableName, string itemId, LocalStoreOperationKind operationKind)
        {
            var operation = new StoreOperation(tableName, itemId, operationKind, this.trackingContext.Source, this.trackingContext.BatchId);

            if (this.trackBatches)
            {
                this.operationsBatch.IncrementOperationCount(operationKind)
                .ContinueWith(t => t.Exception.Handle(e => true), TaskContinuationOptions.OnlyOnFaulted);
            }

            if (this.trackRecordOperations)
            {
                this.eventManager.BackgroundPublish(new StoreOperationCompletedEvent(operation));
            }
        }
Exemple #6
0
        internal async Task IncrementOperationCount(LocalStoreOperationKind operationKind)
        {
            try
            {
                await this.operationsCountSemaphore.WaitAsync();

                if (!this.operationsCountByType.ContainsKey(operationKind))
                {
                    this.operationsCountByType.Add(operationKind, 1);
                }
                else
                {
                    this.operationsCountByType[operationKind]++;
                }
            }
            finally
            {
                this.operationsCountSemaphore.Release();
            }
        }
        internal async Task IncrementOperationCount(LocalStoreOperationKind operationKind)
        {
            try
            {
                await this.operationsCountSemaphore.WaitAsync();

                if (!this.operationsCountByType.ContainsKey(operationKind))
                {
                    this.operationsCountByType.Add(operationKind, 1);
                }
                else
                {
                    this.operationsCountByType[operationKind]++;
                }
            }
            finally
            {
                this.operationsCountSemaphore.Release();
            }
        }
        /// <summary>
        /// Creates an instance of <see cref="StoreOperation"/> class.
        /// </summary>
        /// <param name="tableName">The name of the table that is the target of this operation.</param>
        /// <param name="recordId">The target record identifier.</param>
        /// <param name="kind">The kind of operation.</param>
        /// <param name="source">The source that triggered this operation.</param>
        /// <param name="batchId">The id of the batch this operation belongs to.</param>
        public StoreOperation(string tableName, string recordId, LocalStoreOperationKind kind, StoreOperationSource source, string batchId)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            if (recordId == null)
            {
                throw new ArgumentNullException("recordId");
            }
            if (batchId == null)
            {
                throw new ArgumentNullException("batchId");
            }

            this.TableName = tableName;
            this.RecordId  = recordId;
            this.Kind      = kind;
            this.Source    = source;
            this.BatchId   = batchId;
        }
 /// <summary>
 /// Creates an instance of <see cref="StoreOperation"/> class.
 /// </summary>
 /// <param name="tableName">The name of the table that is the target of this operation.</param>
 /// <param name="recordId">The target record identifier.</param>
 /// <param name="kind">The kind of operation.</param>
 /// <param name="source">The source that triggered this operation.</param>
 /// <param name="batchId">The id of the batch this operation belongs to.</param>
 public StoreOperation(string tableName, string recordId, LocalStoreOperationKind kind, StoreOperationSource source, string batchId)
 {
     if (tableName == null)
     {
         throw new ArgumentNullException("tableName");
     }
     if (recordId == null)
     {
         throw new ArgumentNullException("recordId");
     }
     if(batchId == null)
     {
         throw new ArgumentNullException("batchId");
     }
     
     this.TableName = tableName;
     this.RecordId = recordId;
     this.Kind = kind;
     this.Source = source;
     this.BatchId = batchId;
 }
Exemple #10
0
 /// <summary>
 /// Gets the number of operations matching the provided operation kind executed within this batch.
 /// </summary>
 /// <param name="operationKind">The kind of operation.</param>
 /// <returns>The number of operations matching the provided count.</returns>
 public int GetOperationCountByKind(LocalStoreOperationKind operationKind)
 => operationsCountByType.ContainsKey(operationKind) ? operationsCountByType[operationKind] : 0;
        private void TrackStoreOperation(string tableName, string itemId, LocalStoreOperationKind operationKind)
        {
            var operation = new StoreOperation(tableName, itemId, operationKind, this.trackingContext.Source, this.trackingContext.BatchId);

            if (this.trackBatches)
            {
               this.operationsBatch.IncrementOperationCount(operationKind)
                   .ContinueWith(t => t.Exception.Handle(e => true), TaskContinuationOptions.OnlyOnFaulted);
            }

            if (this.trackRecordOperations)
            {
                this.eventManager.BackgroundPublish(new StoreOperationCompletedEvent(operation));
            }
        }