Example #1
0
        public async Task BuildIndexes()
        {
            TableQuery <TableUser>        query        = new TableQuery <TableUser>();
            TableQuerySegment <TableUser> querySegment = null;
            List <Task> insertOperation = new List <Task>();

            while (querySegment == null || querySegment.ContinuationToken != null)
            {
                querySegment = await _userTable.ExecuteQuerySegmentedAsync(query, querySegment != null?querySegment.ContinuationToken : null);

                foreach (TableUser tableUser in querySegment.Results)
                {
                    TableUserIdIndex indexItem = new TableUserIdIndex(tableUser.UserName.Base64Encode(), tableUser.Id);
                    insertOperation.Add(_userIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(indexItem)));
                    if (insertOperation.Count > 100)
                    {
                        await Task.WhenAll(insertOperation);

                        insertOperation.Clear();
                    }
                }
                if (insertOperation.Count > 0)
                {
                    await Task.WhenAll(insertOperation);

                    insertOperation.Clear();
                }
            }
        }
        public async Task BuildIndexes()
        {
            TableQuery<TableUser> query = new TableQuery<TableUser>();
            TableQuerySegment<TableUser> querySegment = null;
            List<Task> insertOperation = new List<Task>();

            while (querySegment == null || querySegment.ContinuationToken != null)
            {
                querySegment = await _userTable.ExecuteQuerySegmentedAsync(query, querySegment != null ? querySegment.ContinuationToken : null);
                foreach (TableUser tableUser in querySegment.Results)
                {
                    TableUserIdIndex indexItem = new TableUserIdIndex(tableUser.UserName, tableUser.Id);
                    insertOperation.Add(_userIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(indexItem)));
                    if (insertOperation.Count > 100)
                    {
                        await Task.WhenAll(insertOperation);
                        insertOperation.Clear();
                    }
                }
                if (insertOperation.Count > 0)
                {
                    await Task.WhenAll(insertOperation);
                    insertOperation.Clear();
                }
            }
        }
Example #3
0
        public Task <T> FindByNameAsync(string userName)
        {
            if (String.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException("userName");
            }
            return(Task.Factory.StartNew(() =>
            {
                TableQuery <TableUserIdIndex> indexQuery = new TableQuery <TableUserIdIndex>().Where(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, userName.Base64Encode())).Take(1);
                IEnumerable <TableUserIdIndex> indexResults = _userIndexTable.ExecuteQuery(indexQuery);
                TableUserIdIndex indexItem = indexResults.SingleOrDefault();

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

                TableQuery <T> query =
                    new TableQuery <T>().Where(
                        TableQuery.GenerateFilterCondition("PartitionKey",
                                                           QueryComparisons.Equal, indexItem.UserId)).Take(1);
                IEnumerable <T> results = _userTable.ExecuteQuery(query);
                return results.SingleOrDefault();
            }));
        }
Example #4
0
        private async Task RemoveIndices(T user)
        {
            TableUserIdIndex UserIdIndex = new TableUserIdIndex(user.UserName.Base64Encode(), user.Id);

            UserIdIndex.ETag = "*";
            TableUserEmailIndex EmailIndex = new TableUserEmailIndex(user.Email.Base64Encode(), user.Id);

            EmailIndex.ETag = "*";

            Task t1 = _userIndexTable.ExecuteAsync(TableOperation.Delete(UserIdIndex));
            Task t2 = _userEmailIndexTable.ExecuteAsync(TableOperation.Delete(EmailIndex));

            await Task.WhenAll(t1, t2);
        }
Example #5
0
        public async Task CreateAsync(T user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            user.SetPartitionAndRowKey();
            TableUserIdIndex indexItem      = new TableUserIdIndex(user.UserName.Base64Encode(), user.Id);
            TableOperation   indexOperation = TableOperation.Insert(indexItem);

            try
            {
                await _userIndexTable.ExecuteAsync(indexOperation);
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 409)
                {
                    throw new DuplicateUsernameException();
                }
                throw;
            }

            if (!String.IsNullOrWhiteSpace(user.Email))
            {
                TableUserEmailIndex emailIndexItem      = new TableUserEmailIndex(user.Email.Base64Encode(), user.Id);
                TableOperation      emailIndexOperation = TableOperation.Insert(emailIndexItem);
                try
                {
                    await _userEmailIndexTable.ExecuteAsync(emailIndexOperation);
                }
                catch (StorageException ex)
                {
                    try
                    {
                        indexItem.ETag = "*";
                        TableOperation deleteOperation = TableOperation.Delete(indexItem);
                        _userIndexTable.ExecuteAsync(deleteOperation).Wait();
                    }
                    catch (Exception)
                    {
                        // if we can't delete the index item throw out the exception below
                    }


                    if (ex.RequestInformation.HttpStatusCode == 409)
                    {
                        throw new DuplicateEmailException();
                    }
                    throw;
                }
            }

            try
            {
                if (user.LockoutEndDate < _minTableStoreDate)
                {
                    user.LockoutEndDate = _minTableStoreDate;
                }
                TableOperation operation = TableOperation.InsertOrReplace(user);
                await _userTable.ExecuteAsync(operation);

                if (user.Logins.Any())
                {
                    TableBatchOperation batch = new TableBatchOperation();
                    List <TableUserLoginProviderKeyIndex> loginIndexItems = new List <TableUserLoginProviderKeyIndex>();
                    foreach (TableUserLogin login in user.Logins)
                    {
                        login.UserId = user.Id;
                        login.SetPartitionAndRowKey();
                        batch.InsertOrReplace(login);

                        TableUserLoginProviderKeyIndex loginIndexItem = new TableUserLoginProviderKeyIndex(user.Id, login.ProviderKey, login.LoginProvider);
                        loginIndexItems.Add(loginIndexItem);
                    }
                    await _loginTable.ExecuteBatchAsync(batch);

                    // can't batch the index items as different primary keys
                    foreach (TableUserLoginProviderKeyIndex loginIndexItem in loginIndexItems)
                    {
                        await _loginProviderKeyIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(loginIndexItem));
                    }
                }
            }
            catch (Exception)
            {
                // attempt to delete the index item - needs work
                indexItem.ETag = "*";
                TableOperation deleteOperation = TableOperation.Delete(indexItem);
                _userIndexTable.ExecuteAsync(deleteOperation).Wait();
                throw;
            }
        }