Пример #1
0
        /// <summary>
        /// Insert user topic
        /// </summary>
        /// <param name="storageConsistencyMode">Storage consistency mode</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="topicHandle">Topic handle</param>
        /// <returns>Insert user topic task</returns>
        public async Task InsertUserTopic(
            StorageConsistencyMode storageConsistencyMode,
            string userHandle,
            string appHandle,
            string topicHandle)
        {
            TopicFeedEntity topicFeedEntity = new TopicFeedEntity()
            {
                AppHandle   = appHandle,
                TopicHandle = topicHandle,
                UserHandle  = userHandle
            };

            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.UserTopics);

            FeedTable  feedTable  = this.tableStoreManager.GetTable(ContainerIdentifier.UserTopics, TableIdentifier.UserTopicsFeed) as FeedTable;
            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.UserTopics, TableIdentifier.UserTopicsCount) as CountTable;

            Transaction transaction = new Transaction();

            transaction.Add(Operation.Insert(feedTable, userHandle, appHandle, topicHandle, topicFeedEntity));
            transaction.Add(Operation.Insert(feedTable, userHandle, MasterApp.AppHandle, topicHandle, topicFeedEntity));
            transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, appHandle));
            transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, MasterApp.AppHandle));
            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #2
0
        public override T Compute <T>(T val)
        {
            lock (Table)
            {
                if (Table[_flag] == null)
                {
                    Table.Add(_flag, val);
                    CountTable.Add(_flag, 1);
                }
                else
                {
                    List <object> objects    = (List <object>)Table[_flag];
                    List <object> newObjects = (List <object>)Convert.ChangeType(val, typeof(List <object>));
                    objects.AddRange(newObjects);
                    List <object> distinctObjects = new List <object>(objects.Distinct());
                    Table[_flag] = distinctObjects;

                    int count = int.Parse(CountTable[_flag].ToString());
                    CountTable[_flag] = count + 1;
                }
            }
            if (Waite(_flag))
            {
                T t = (T)Convert.ChangeType(Table[_flag], typeof(T));
                return(t);
            }
            throw new TimeoutException();
        }
Пример #3
0
        /// <summary>
        /// Insert a new user-generated report of another user into the store
        /// </summary>
        /// <param name="storageConsistencyMode">consistency to use</param>
        /// <param name="reportHandle">uniquely identifies this report</param>
        /// <param name="reportedUserHandle">uniquely identifies the user who is being reported</param>
        /// <param name="reportingUserHandle">uniquely identifies the user doing the reporting</param>
        /// <param name="appHandle">uniquely identifies the app that the user is in</param>
        /// <param name="reason">the complaint against the content</param>
        /// <param name="lastUpdatedTime">when the report was received</param>
        /// <param name="hasComplainedBefore">has the reporting user complained about this user before?</param>
        /// <returns>a task that inserts the report into the store</returns>
        public async Task InsertUserReport(
            StorageConsistencyMode storageConsistencyMode,
            string reportHandle,
            string reportedUserHandle,
            string reportingUserHandle,
            string appHandle,
            ReportReason reason,
            DateTime lastUpdatedTime,
            bool hasComplainedBefore)
        {
            // get all the table interfaces
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.UserReports);

            ObjectTable lookupTable               = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsLookup) as ObjectTable;
            ObjectTable lookupUniquenessTable     = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsLookupUniquenessByReportingUser) as ObjectTable;
            FeedTable   feedByAppTable            = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsRecentFeedByApp) as FeedTable;
            FeedTable   feedByReportedUserTable   = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsRecentFeedByReportedUser) as FeedTable;
            FeedTable   feedByReportingUserTable  = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsRecentFeedByReportingUser) as FeedTable;
            CountTable  countByReportedUserTable  = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsCountByReportedUser) as CountTable;
            CountTable  countByReportingUserTable = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsCountByReportingUser) as CountTable;

            // create the two entities that will be inserted into the tables
            UserReportEntity userReportEntity = new UserReportEntity()
            {
                ReportedUserHandle  = reportedUserHandle,
                ReportingUserHandle = reportingUserHandle,
                AppHandle           = appHandle,
                Reason      = reason,
                CreatedTime = lastUpdatedTime
            };

            UserReportFeedEntity userReportFeedEntity = new UserReportFeedEntity()
            {
                ReportHandle        = reportHandle,
                ReportedUserHandle  = reportedUserHandle,
                ReportingUserHandle = reportingUserHandle,
                AppHandle           = appHandle
            };

            // do the inserts and increments as a transaction
            Transaction transaction = new Transaction();

            // the partition key is app handle for all tables so that a transaction can be achieved
            transaction.Add(Operation.Insert(lookupTable, appHandle, reportHandle, userReportEntity));
            transaction.Add(Operation.Insert(feedByAppTable, appHandle, appHandle, reportHandle, userReportFeedEntity));
            transaction.Add(Operation.Insert(feedByReportedUserTable, appHandle, reportedUserHandle, reportHandle, userReportFeedEntity));
            transaction.Add(Operation.Insert(feedByReportingUserTable, appHandle, reportingUserHandle, reportHandle, userReportFeedEntity));

            // if the reporting user has not previously reported this user, then increment counts
            if (!hasComplainedBefore)
            {
                string uniquenessKey = UniquenessObjectKey(reportedUserHandle, reportingUserHandle);
                transaction.Add(Operation.Insert(lookupUniquenessTable, appHandle, uniquenessKey, new ObjectEntity()));
                transaction.Add(Operation.InsertOrIncrement(countByReportedUserTable, appHandle, reportedUserHandle));
                transaction.Add(Operation.InsertOrIncrement(countByReportingUserTable, appHandle, reportingUserHandle));
            }

            // execute the transaction
            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #4
0
        //public override T Compute<T>(T val)
        //{
        //    return default(T);
        //}

        public T[] ArrayCompute <T>(T[] val)
        {
            lock (Table)
            {
                if (Table[_flag] == null)
                {
                    Table.Add(_flag, ChangeType(val));
                    CountTable.Add(_flag, 1);
                }
                else
                {
                    double[]             list    = (double[])Table[_flag];
                    CombineSort <double> combine = new CombineSort <double>();
                    list         = combine.GetResult(list, ChangeType(val));
                    Table[_flag] = list;
                    int count = int.Parse(CountTable[_flag].ToString());
                    CountTable[_flag] = count + 1;
                }
            }
            if (Waite(_flag))
            {
                return(Array.ConvertAll((double[])Table[_flag],
                                        n => (T)Convert.ChangeType(n, typeof(T))));
            }
            throw new TimeoutException();
        }
Пример #5
0
        /// <summary>
        /// Update following relationship.
        /// Following user : someone who i am following.
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="relationshipHandle">Relationship handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="relationshipUserHandle">Relationship user handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="userRelationshipStatus">User relationship status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readUserRelationshipLookupEntity">Read user relationship lookup entity</param>
        /// <returns>Update following relationship task</returns>
        public async Task UpdateFollowingRelationship(
            StorageConsistencyMode storageConsistencyMode,
            string relationshipHandle,
            string userHandle,
            string relationshipUserHandle,
            string appHandle,
            UserRelationshipStatus userRelationshipStatus,
            DateTime lastUpdatedTime,
            IUserRelationshipLookupEntity readUserRelationshipLookupEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.UserFollowing);

            ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.UserFollowing, TableIdentifier.FollowingLookup) as ObjectTable;
            FeedTable   feedTable   = this.tableStoreManager.GetTable(ContainerIdentifier.UserFollowing, TableIdentifier.FollowingFeed) as FeedTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.UserFollowing, TableIdentifier.FollowingCount) as CountTable;

            await this.UpdateUserRelationship(
                storageConsistencyMode,
                relationshipHandle,
                userHandle,
                relationshipUserHandle,
                appHandle,
                userRelationshipStatus,
                lastUpdatedTime,
                readUserRelationshipLookupEntity,
                store,
                lookupTable,
                feedTable,
                countTable);
        }
        /// <summary>
        /// Update notifications status
        /// </summary>
        /// <param name="storageConsistencyMode">Storage consistency mode</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="readActivityHandle">Read activity handle</param>
        /// <param name="readNotificationsStatusEntity">Read notifications status entity</param>
        /// <returns>Update notifications status task</returns>
        public async Task UpdateNotificationsStatus(
            StorageConsistencyMode storageConsistencyMode,
            string userHandle,
            string appHandle,
            string readActivityHandle,
            INotificationsStatusEntity readNotificationsStatusEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Notifications);

            ObjectTable statusTable = this.tableStoreManager.GetTable(ContainerIdentifier.Notifications, TableIdentifier.NotificationsStatus) as ObjectTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Notifications, TableIdentifier.NotificationsCount) as CountTable;

            Transaction transaction = new Transaction();

            if (readNotificationsStatusEntity == null)
            {
                NotificationsStatusEntity notificationsStatusEntity = new NotificationsStatusEntity()
                {
                    ReadActivityHandle = readActivityHandle
                };

                transaction.Add(Operation.Insert(statusTable, userHandle, appHandle, notificationsStatusEntity));
            }
            else
            {
                readNotificationsStatusEntity.ReadActivityHandle = readActivityHandle;
                transaction.Add(Operation.Replace(statusTable, userHandle, appHandle, readNotificationsStatusEntity as NotificationsStatusEntity));
            }

            transaction.Add(Operation.InsertOrReplace(countTable, userHandle, appHandle, 0));
            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #7
0
        public override T Compute <T>(T val)
        {
            lock (Table)
            {
                if (Table[_flag] == null)
                {
                    Table.Add(_flag, val);
                    CountTable.Add(_flag, 1);
                }
                else
                {
                    List <object> list = (List <object>)Table[_flag];
                    list.AddRange((List <object>)(object) val);

                    Table[_flag] = list;
                    int count = int.Parse(CountTable[_flag].ToString());
                    CountTable[_flag] = count + 1;
                }
            }
            if (Waite(_flag))
            {
                T t = (T)Convert.ChangeType(Table[_flag], typeof(T));
                return(t);
            }
            throw new TimeoutException();
        }
        /// <summary>
        /// Insert notification
        /// </summary>
        /// <param name="storageConsistencyMode">Storage consistency mode</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="activityHandle">Activity handle</param>
        /// <param name="activityType">Activity type</param>
        /// <param name="actorUserHandle">Actor user handle</param>
        /// <param name="actedOnUserHandle">Acted on user handle</param>
        /// <param name="actedOnContentType">Acted on content type</param>
        /// <param name="actedOnContentHandle">Acted on content handle</param>
        /// <param name="createdTime">Created time</param>
        /// <returns>Insert notification task</returns>
        public async Task InsertNotification(
            StorageConsistencyMode storageConsistencyMode,
            string userHandle,
            string appHandle,
            string activityHandle,
            ActivityType activityType,
            string actorUserHandle,
            string actedOnUserHandle,
            ContentType actedOnContentType,
            string actedOnContentHandle,
            DateTime createdTime)
        {
            ActivityFeedEntity activityFeedEntity = new ActivityFeedEntity()
            {
                ActivityHandle       = activityHandle,
                AppHandle            = appHandle,
                ActivityType         = activityType,
                ActorUserHandle      = actorUserHandle,
                ActedOnUserHandle    = actedOnUserHandle,
                ActedOnContentType   = actedOnContentType,
                ActedOnContentHandle = actedOnContentHandle,
                CreatedTime          = createdTime
            };

            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Notifications);

            FeedTable  feedTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Notifications, TableIdentifier.NotificationsFeed) as FeedTable;
            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.Notifications, TableIdentifier.NotificationsCount) as CountTable;

            // do an insert & increment in a transaction.
            // if a queue message for inserting a notification gets processed twice, then this transaction will generate
            // a storage exception on the second attempt (because the insert will fail with a conflict (409) http status)
            try
            {
                Transaction transaction = new Transaction();
                transaction.Add(Operation.Insert(feedTable, userHandle, appHandle, activityHandle, activityFeedEntity));
                transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, appHandle));
                await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
            }
            catch (StorageException e)
            {
                // ignore this exception only if item exists (error code 409 conflict)
                if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict)
                {
                    this.log.LogInformation("NotificationsStore.InsertNotification received a conflict on insert" + e.Message);
                }
                else
                {
                    throw e;
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Query comment replies count
        /// </summary>
        /// <param name="commentHandle">Comment handle</param>
        /// <returns>Comment replies count</returns>
        public async Task <long?> QueryCommentRepliesCount(string commentHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.CommentReplies);

            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.CommentReplies, TableIdentifier.CommentRepliesCount) as CountTable;
            var        result     = await store.QueryCountAsync(countTable, commentHandle, this.tableStoreManager.DefaultCountKey);

            if (result == null)
            {
                return(null);
            }

            return((long)result.Count);
        }
Пример #10
0
        /// <summary>
        /// Query count of pins for a user in an app
        /// </summary>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <returns>Pin count for a user handle and app handle</returns>
        public async Task <long?> QueryPinsCount(string userHandle, string appHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Pins);

            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Pins, TableIdentifier.PinsCount) as CountTable;
            CountEntity countEntity = await store.QueryCountAsync(countTable, userHandle, appHandle);

            if (countEntity == null)
            {
                return(null);
            }

            return((long)countEntity.Count);
        }
        /// <summary>
        /// Query notifications count
        /// </summary>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <returns>Notifications count</returns>
        public async Task <long?> QueryNotificationsCount(string userHandle, string appHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Notifications);

            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.Notifications, TableIdentifier.NotificationsCount) as CountTable;
            var        result     = await store.QueryCountAsync(countTable, userHandle, appHandle);

            if (result == null)
            {
                return(null);
            }

            return((long)result.Count);
        }
Пример #12
0
        /// <summary>
        /// Query count of likes for a content
        /// </summary>
        /// <param name="contentHandle">Content handle</param>
        /// <returns>Likes count for a content</returns>
        public async Task <long?> QueryLikesCount(string contentHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Likes);

            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesCount) as CountTable;
            CountEntity countEntity = await store.QueryCountAsync(countTable, contentHandle, this.tableStoreManager.DefaultCountKey);

            if (countEntity == null)
            {
                return(null);
            }

            return((long)countEntity.Count);
        }
Пример #13
0
        /// <summary>
        /// Query count of blocked users for a user in an app
        /// </summary>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <returns>Blocked users count for a user handle and app handle</returns>
        public async Task <long?> QueryBlockedUsersCount(string userHandle, string appHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.UserFollowers);

            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.UserFollowers, TableIdentifier.FollowersCount) as CountTable;
            string      countKey    = this.GetCountKey(appHandle, UserRelationshipStatus.Blocked);
            CountEntity countEntity = await store.QueryCountAsync(countTable, userHandle, countKey);

            if (countEntity == null)
            {
                return(null);
            }

            return((long)countEntity.Count);
        }
Пример #14
0
        /// <summary>
        /// Delete comment reply
        /// </summary>
        /// <param name="storageConsistencyMode">Storage consistency mode</param>
        /// <param name="commentHandle">Comment handle</param>
        /// <param name="replyHandle">Reply handle</param>
        /// <returns>Delete comment reply task</returns>
        public async Task DeleteCommentReply(
            StorageConsistencyMode storageConsistencyMode,
            string commentHandle,
            string replyHandle)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.CommentReplies);

            FeedTable  feedTable  = this.tableStoreManager.GetTable(ContainerIdentifier.CommentReplies, TableIdentifier.CommentRepliesFeed) as FeedTable;
            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.CommentReplies, TableIdentifier.CommentRepliesCount) as CountTable;

            Transaction transaction = new Transaction();

            transaction.Add(Operation.Delete(feedTable, commentHandle, this.tableStoreManager.DefaultFeedKey, replyHandle));
            transaction.Add(Operation.Increment(countTable, commentHandle, this.tableStoreManager.DefaultCountKey, -1.0));
            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #15
0
        /// <summary>
        /// Look up the number of unique users reporting against a particular user
        /// </summary>
        /// <param name="appHandle">uniquely identifies the app that the user is in</param>
        /// <param name="reportedUserHandle">uniquely identifies the user who is being reported</param>
        /// <returns>a task that returns the number of reports</returns>
        public async Task <long> QueryUserReportCount(string appHandle, string reportedUserHandle)
        {
            // get the table interface
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.UserReports);

            CountTable countByReportedUserTable = this.tableStoreManager.GetTable(ContainerIdentifier.UserReports, TableIdentifier.UserReportsCountByReportedUser) as CountTable;

            // query the count
            CountEntity countEntity = await store.QueryCountAsync(countByReportedUserTable, appHandle, reportedUserHandle);

            if (countEntity == null || countEntity.PartitionKey != appHandle || countEntity.CountKey != reportedUserHandle)
            {
                return(0);
            }

            return(Convert.ToInt64(countEntity.Count));
        }
Пример #16
0
        public override T Compute <T>(T val)
        {
            lock (Table)
            {
                if (Table[_flag] == null)
                {
                    Table.Add(_flag, val);
                    CountTable.Add(_flag, 1);
                }
                else
                {
                    Hashtable oldTable     = (Hashtable)Table[_flag];
                    Hashtable currentTable = (Hashtable)(object)val;
                    foreach (DictionaryEntry dictionaryEntry in currentTable)
                    {
                        if (oldTable.ContainsKey(dictionaryEntry.Key))
                        {
                            double oldValue = double.Parse(oldTable[dictionaryEntry.Key].ToString());
                            oldTable[dictionaryEntry.Key] = oldValue + double.Parse(dictionaryEntry.Value.ToString());
                        }
                        else
                        {
                            oldTable.Add(dictionaryEntry.Key, dictionaryEntry.Value);
                        }
                    }

                    Table[_flag] = oldTable;
                    int count = int.Parse(CountTable[_flag].ToString());
                    CountTable[_flag] = count + 1;
                }
            }
            if (Waite(_flag))
            {
                T t = (T)Convert.ChangeType(Table[_flag], typeof(T));
                return(t);
            }
            throw new TimeoutException();
        }
        /// <summary>
        /// Insert topic comment
        /// </summary>
        /// <param name="storageConsistencyMode">Storage consistency mode</param>
        /// <param name="topicHandle">Topic handle</param>
        /// <param name="commentHandle">Comment handle</param>
        /// <param name="commentUserHandle">Comment user handle</param>
        /// <returns>Insert topic comment task</returns>
        public async Task InsertTopicComment(
            StorageConsistencyMode storageConsistencyMode,
            string topicHandle,
            string commentHandle,
            string commentUserHandle)
        {
            CommentFeedEntity commentFeedEntity = new CommentFeedEntity()
            {
                CommentHandle = commentHandle,
                UserHandle    = commentUserHandle
            };

            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.TopicComments);

            FeedTable  feedTable  = this.tableStoreManager.GetTable(ContainerIdentifier.TopicComments, TableIdentifier.TopicCommentsFeed) as FeedTable;
            CountTable countTable = this.tableStoreManager.GetTable(ContainerIdentifier.TopicComments, TableIdentifier.TopicCommentsCount) as CountTable;

            Transaction transaction = new Transaction();

            transaction.Add(Operation.Insert(feedTable, topicHandle, this.tableStoreManager.DefaultFeedKey, commentHandle, commentFeedEntity));
            transaction.Add(Operation.InsertOrIncrement(countTable, topicHandle, this.tableStoreManager.DefaultCountKey));
            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #18
0
 public override T Compute <T>(T val)
 {
     lock (Table)
     {
         if (Table[_flag] == null)
         {
             Table.Add(_flag, val);
             CountTable.Add(_flag, 1);
         }
         else
         {
             double average = (double.Parse(Table[_flag].ToString()) + double.Parse(val.ToString())) / 2.0;
             Table[_flag] = average;
             int count = int.Parse(CountTable[_flag].ToString());
             CountTable[_flag] = count + 1;
         }
     }
     if (Waite(_flag))
     {
         T t = (T)Convert.ChangeType(Table[_flag], typeof(T));
         return(t);
     }
     throw new TimeoutException();
 }
Пример #19
0
 public CorrelationIdContext(string correlationId)
 {
     CreatedAt     = DateTimeOffset.Now;
     CorrelationId = correlationId;
     Table         = new CountTable();
 }
Пример #20
0
        /// <summary>
        /// Update pin
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="pinHandle">Pin handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="topicHandle">Topic handle</param>
        /// <param name="topicUserHandle">User handle of the topic owner</param>
        /// <param name="pinned">Pin status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readPinLookupEntity">Read pin lookup entity</param>
        /// <returns>Update pin task</returns>
        public async Task UpdatePin(
            StorageConsistencyMode storageConsistencyMode,
            string pinHandle,
            string userHandle,
            string appHandle,
            string topicHandle,
            string topicUserHandle,
            bool pinned,
            DateTime lastUpdatedTime,
            IPinLookupEntity readPinLookupEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Pins);

            ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.Pins, TableIdentifier.PinsLookup) as ObjectTable;
            FeedTable   feedTable   = this.tableStoreManager.GetTable(ContainerIdentifier.Pins, TableIdentifier.PinsFeed) as FeedTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Pins, TableIdentifier.PinsCount) as CountTable;
            Transaction transaction = new Transaction();

            PinFeedEntity pinFeedEntity = new PinFeedEntity()
            {
                PinHandle       = pinHandle,
                TopicHandle     = topicHandle,
                TopicUserHandle = topicUserHandle,
                AppHandle       = appHandle
            };

            if (readPinLookupEntity == null)
            {
                PinLookupEntity newPinLookupEntity = new PinLookupEntity()
                {
                    PinHandle       = pinHandle,
                    LastUpdatedTime = lastUpdatedTime,
                    Pinned          = pinned
                };

                transaction.Add(Operation.Insert(lookupTable, userHandle, topicHandle, newPinLookupEntity));

                if (pinned)
                {
                    transaction.Add(Operation.Insert(feedTable, userHandle, appHandle, pinHandle, pinFeedEntity));
                    transaction.Add(Operation.Insert(feedTable, userHandle, MasterApp.AppHandle, pinHandle, pinFeedEntity));
                    transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, appHandle));
                    transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, MasterApp.AppHandle));
                }
            }
            else
            {
                bool   oldPinned    = readPinLookupEntity.Pinned;
                string oldPinHandle = readPinLookupEntity.PinHandle;

                readPinLookupEntity.PinHandle       = pinHandle;
                readPinLookupEntity.Pinned          = pinned;
                readPinLookupEntity.LastUpdatedTime = lastUpdatedTime;

                transaction.Add(Operation.Replace(lookupTable, userHandle, topicHandle, readPinLookupEntity as PinLookupEntity));

                if (pinned == oldPinned)
                {
                    if (pinned && pinHandle != oldPinHandle)
                    {
                        transaction.Add(Operation.Delete(feedTable, userHandle, appHandle, oldPinHandle));
                        transaction.Add(Operation.Delete(feedTable, userHandle, MasterApp.AppHandle, oldPinHandle));
                        transaction.Add(Operation.Insert(feedTable, userHandle, appHandle, pinHandle, pinFeedEntity));
                        transaction.Add(Operation.Insert(feedTable, userHandle, MasterApp.AppHandle, pinHandle, pinFeedEntity));
                    }
                }
                else
                {
                    if (pinned)
                    {
                        transaction.Add(Operation.Insert(feedTable, userHandle, appHandle, pinHandle, pinFeedEntity));
                        transaction.Add(Operation.Insert(feedTable, userHandle, MasterApp.AppHandle, pinHandle, pinFeedEntity));
                        transaction.Add(Operation.Increment(countTable, userHandle, appHandle, 1));
                        transaction.Add(Operation.Increment(countTable, userHandle, MasterApp.AppHandle, 1));
                    }
                    else
                    {
                        transaction.Add(Operation.Delete(feedTable, userHandle, appHandle, oldPinHandle));
                        transaction.Add(Operation.Delete(feedTable, userHandle, MasterApp.AppHandle, oldPinHandle));
                        transaction.Add(Operation.Increment(countTable, userHandle, appHandle, -1.0));
                        transaction.Add(Operation.Increment(countTable, userHandle, MasterApp.AppHandle, -1.0));
                    }
                }
            }

            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #21
0
        /// <summary>
        /// Update user relationship
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="relationshipHandle">Relationship handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="relationshipUserHandle">Relationship user handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="userRelationshipStatus">Relationship status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readUserRelationshipLookupEntity">Read user relationship lookup entity</param>
        /// <param name="store">store object</param>
        /// <param name="lookupTable">lookup table</param>
        /// <param name="feedTable">feed table</param>
        /// <param name="countTable">count table</param>
        /// <returns>Update follower relationship task</returns>
        private async Task UpdateUserRelationship(
            StorageConsistencyMode storageConsistencyMode,
            string relationshipHandle,
            string userHandle,
            string relationshipUserHandle,
            string appHandle,
            UserRelationshipStatus userRelationshipStatus,
            DateTime lastUpdatedTime,
            IUserRelationshipLookupEntity readUserRelationshipLookupEntity,
            CTStore store,
            ObjectTable lookupTable,
            FeedTable feedTable,
            CountTable countTable)
        {
            string objectKey = this.GetObjectKey(appHandle, relationshipUserHandle);
            string feedKey   = this.GetFeedKey(appHandle, userRelationshipStatus);
            string countKey  = this.GetCountKey(appHandle, userRelationshipStatus);

            Transaction transaction = new Transaction();

            UserRelationshipFeedEntity relationshipFeedEntity = new UserRelationshipFeedEntity()
            {
                RelationshipHandle = relationshipHandle,
                UserHandle         = relationshipUserHandle
            };

            if (readUserRelationshipLookupEntity == null)
            {
                UserRelationshipLookupEntity newRelationshipLookupEntity = new UserRelationshipLookupEntity()
                {
                    RelationshipHandle     = relationshipHandle,
                    LastUpdatedTime        = lastUpdatedTime,
                    UserRelationshipStatus = userRelationshipStatus,
                };

                transaction.Add(Operation.Insert(lookupTable, userHandle, objectKey, newRelationshipLookupEntity));

                if (userRelationshipStatus != UserRelationshipStatus.None)
                {
                    transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                    transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, countKey));
                }
            }
            else
            {
                UserRelationshipStatus oldUserRelationshipStatus = readUserRelationshipLookupEntity.UserRelationshipStatus;
                string oldRelationshipHandle = readUserRelationshipLookupEntity.RelationshipHandle;

                readUserRelationshipLookupEntity.RelationshipHandle     = relationshipHandle;
                readUserRelationshipLookupEntity.UserRelationshipStatus = userRelationshipStatus;
                readUserRelationshipLookupEntity.LastUpdatedTime        = lastUpdatedTime;

                string oldFeedKey  = this.GetFeedKey(appHandle, oldUserRelationshipStatus);
                string oldCountKey = this.GetCountKey(appHandle, oldUserRelationshipStatus);

                transaction.Add(Operation.Replace(lookupTable, userHandle, objectKey, readUserRelationshipLookupEntity as UserRelationshipLookupEntity));

                if (userRelationshipStatus == oldUserRelationshipStatus)
                {
                    if (userRelationshipStatus != UserRelationshipStatus.None && relationshipHandle != oldRelationshipHandle)
                    {
                        transaction.Add(Operation.Delete(feedTable, userHandle, oldFeedKey, oldRelationshipHandle));
                        transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                    }
                }
                else
                {
                    if (userRelationshipStatus != UserRelationshipStatus.None)
                    {
                        transaction.Add(Operation.Insert(feedTable, userHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                        transaction.Add(Operation.InsertOrIncrement(countTable, userHandle, countKey));
                    }

                    if (oldUserRelationshipStatus != UserRelationshipStatus.None)
                    {
                        transaction.Add(Operation.Delete(feedTable, userHandle, oldFeedKey, oldRelationshipHandle));
                        transaction.Add(Operation.Increment(countTable, userHandle, oldCountKey, -1.0));
                    }
                }
            }

            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
        /// <summary>
        /// Update topic follower relationship.
        /// Follower user : someone who follows the topic.
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="relationshipHandle">Relationship handle</param>
        /// <param name="topicHandle">topic handle</param>
        /// <param name="relationshipUserHandle">Relationship user handle</param>
        /// <param name="appHandle">App handle</param>
        /// <param name="topicRelationshipStatus">Topic relationship status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readTopicRelationshipLookupEntity">Read topic relationship lookup entity</param>
        /// <returns>Update follower relationship task</returns>
        public async Task UpdateTopicFollowerRelationship(
            StorageConsistencyMode storageConsistencyMode,
            string relationshipHandle,
            string topicHandle,
            string relationshipUserHandle,
            string appHandle,
            TopicRelationshipStatus topicRelationshipStatus,
            DateTime lastUpdatedTime,
            ITopicRelationshipLookupEntity readTopicRelationshipLookupEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.TopicFollowers);

            ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersLookup) as ObjectTable;
            FeedTable   feedTable   = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersFeed) as FeedTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.TopicFollowers, TableIdentifier.TopicFollowersCount) as CountTable;

            string objectKey = this.GetObjectKey(appHandle, relationshipUserHandle);
            string feedKey   = this.GetFeedKey(appHandle, topicRelationshipStatus);
            string countKey  = this.GetCountKey(appHandle, topicRelationshipStatus);

            Transaction transaction = new Transaction();

            UserRelationshipFeedEntity relationshipFeedEntity = new UserRelationshipFeedEntity()
            {
                RelationshipHandle = relationshipHandle,
                UserHandle         = relationshipUserHandle
            };

            if (readTopicRelationshipLookupEntity == null)
            {
                // if readTopicRelationshipLookupEntity is null, then we are inserting a new relationship
                TopicRelationshipLookupEntity newRelationshipLookupEntity = new TopicRelationshipLookupEntity()
                {
                    RelationshipHandle      = relationshipHandle,
                    LastUpdatedTime         = lastUpdatedTime,
                    TopicRelationshipStatus = topicRelationshipStatus,
                };

                transaction.Add(Operation.Insert(lookupTable, topicHandle, objectKey, newRelationshipLookupEntity));

                if (topicRelationshipStatus != TopicRelationshipStatus.None)
                {
                    transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                    transaction.Add(Operation.InsertOrIncrement(countTable, topicHandle, countKey));
                }
            }
            else
            {
                // otherwise, we are updating an existing relationship
                TopicRelationshipStatus oldTopicRelationshipStatus = readTopicRelationshipLookupEntity.TopicRelationshipStatus;
                string oldRelationshipHandle = readTopicRelationshipLookupEntity.RelationshipHandle;
                string oldFeedKey            = this.GetFeedKey(appHandle, oldTopicRelationshipStatus);
                string oldCountKey           = this.GetCountKey(appHandle, oldTopicRelationshipStatus);

                readTopicRelationshipLookupEntity.RelationshipHandle      = relationshipHandle;
                readTopicRelationshipLookupEntity.TopicRelationshipStatus = topicRelationshipStatus;
                readTopicRelationshipLookupEntity.LastUpdatedTime         = lastUpdatedTime;

                transaction.Add(Operation.Replace(lookupTable, topicHandle, objectKey, readTopicRelationshipLookupEntity as TopicRelationshipLookupEntity));

                if (topicRelationshipStatus == oldTopicRelationshipStatus)
                {
                    if (topicRelationshipStatus != TopicRelationshipStatus.None && relationshipHandle != oldRelationshipHandle)
                    {
                        transaction.Add(Operation.Delete(feedTable, topicHandle, oldFeedKey, oldRelationshipHandle));
                        transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                    }
                }
                else
                {
                    if (topicRelationshipStatus != TopicRelationshipStatus.None)
                    {
                        transaction.Add(Operation.Insert(feedTable, topicHandle, feedKey, relationshipHandle, relationshipFeedEntity));
                        transaction.Add(Operation.Increment(countTable, topicHandle, countKey));
                    }

                    if (oldTopicRelationshipStatus != TopicRelationshipStatus.None)
                    {
                        transaction.Add(Operation.Delete(feedTable, topicHandle, oldFeedKey, oldRelationshipHandle));
                        transaction.Add(Operation.Increment(countTable, topicHandle, oldCountKey, -1.0));
                    }
                }
            }

            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }
Пример #23
0
        /// <summary>
        /// Update like
        /// </summary>
        /// <param name="storageConsistencyMode">Consistency mode</param>
        /// <param name="likeHandle">Like handle</param>
        /// <param name="contentHandle">Content handle</param>
        /// <param name="userHandle">User handle</param>
        /// <param name="liked">Like status</param>
        /// <param name="lastUpdatedTime">Last updated time</param>
        /// <param name="readLikeLookupEntity">Read like lookup entity</param>
        /// <returns>Update like task</returns>
        public async Task UpdateLike(
            StorageConsistencyMode storageConsistencyMode,
            string likeHandle,
            string contentHandle,
            string userHandle,
            bool liked,
            DateTime lastUpdatedTime,
            ILikeLookupEntity readLikeLookupEntity)
        {
            CTStore store = await this.tableStoreManager.GetStore(ContainerIdentifier.Likes);

            ObjectTable lookupTable = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesLookup) as ObjectTable;
            FeedTable   feedTable   = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesFeed) as FeedTable;
            CountTable  countTable  = this.tableStoreManager.GetTable(ContainerIdentifier.Likes, TableIdentifier.LikesCount) as CountTable;
            Transaction transaction = new Transaction();

            LikeFeedEntity likeFeedEntity = new LikeFeedEntity()
            {
                LikeHandle = likeHandle,
                UserHandle = userHandle
            };

            if (readLikeLookupEntity == null)
            {
                LikeLookupEntity newLikeLookupEntity = new LikeLookupEntity()
                {
                    LikeHandle      = likeHandle,
                    LastUpdatedTime = lastUpdatedTime,
                    Liked           = liked
                };

                transaction.Add(Operation.Insert(lookupTable, contentHandle, userHandle, newLikeLookupEntity));

                if (liked)
                {
                    transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                    transaction.Add(Operation.InsertOrIncrement(countTable, contentHandle, this.tableStoreManager.DefaultCountKey));
                }
            }
            else
            {
                bool   oldLiked      = readLikeLookupEntity.Liked;
                string oldLikeHandle = readLikeLookupEntity.LikeHandle;

                readLikeLookupEntity.LikeHandle      = likeHandle;
                readLikeLookupEntity.Liked           = liked;
                readLikeLookupEntity.LastUpdatedTime = lastUpdatedTime;

                transaction.Add(Operation.Replace(lookupTable, contentHandle, userHandle, readLikeLookupEntity as LikeLookupEntity));

                if (liked == oldLiked)
                {
                    if (liked && likeHandle != oldLikeHandle)
                    {
                        transaction.Add(Operation.Delete(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, oldLikeHandle));
                        transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                    }
                }
                else
                {
                    if (liked)
                    {
                        transaction.Add(Operation.Insert(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, likeHandle, likeFeedEntity));
                        transaction.Add(Operation.Increment(countTable, contentHandle, this.tableStoreManager.DefaultCountKey, 1));
                    }
                    else
                    {
                        transaction.Add(Operation.Delete(feedTable, contentHandle, this.tableStoreManager.DefaultFeedKey, oldLikeHandle));
                        transaction.Add(Operation.Increment(countTable, contentHandle, this.tableStoreManager.DefaultCountKey, -1.0));
                    }
                }
            }

            await store.ExecuteTransactionAsync(transaction, storageConsistencyMode.ToConsistencyMode());
        }