Exemple #1
0
        public async Task <LittleUrlAzure> CheckDupe(string longUrl)
        {
            LittleUrlAzure itemRet = null;

            try
            {
                // Construct the query operation
                TableQuery <LittleUrlAzure> query = new TableQuery <LittleUrlAzure>()
                                                    .Where(TableQuery.GenerateFilterCondition("LongUrl", QueryComparisons.Equal, longUrl));

                // Collect the urls in the list
                TableContinuationToken             token         = null;
                TableQuerySegment <LittleUrlAzure> resultSegment =
                    await _littleUrlTable.ExecuteQuerySegmentedAsync <LittleUrlAzure>(query, token);

                itemRet = resultSegment.Results.FirstOrDefault <LittleUrlAzure>();

                // If this URL is deleted; undelete and return
                if (itemRet.IsDeleted)
                {
                    itemRet = await ToggleDelete(itemRet.ShortUrl, false);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("CheckDupe() | Exception caught:" + ex.Message);
                itemRet = null;
            }

            return(itemRet);
        }
Exemple #2
0
        // Retention/Undelete
        public async Task <LittleUrlAzure> ToggleDelete(string shortUrl, bool bDeleteThis)
        {
            // Move url to 'True' or 'False' partitions based on current state
            if (!string.IsNullOrEmpty(shortUrl))
            {
                // Setup Table op from appropriate partition
                TableOperation retrieveOp = TableOperation.Retrieve <LittleUrlAzure>((bDeleteThis).ToString(), shortUrl);
                TableResult    result     = await _littleUrlTable.ExecuteAsync(retrieveOp);

                LittleUrlAzure item = (LittleUrlAzure)result.Result;

                if (item != null)
                {
                    // Found
                    //  Now delete it from this partition and recreate in the other
                    TableOperation deleteOp = TableOperation.Delete(item);
                    await _littleUrlTable.ExecuteAsync(deleteOp);

                    // If we are deleting this entity
                    if (bDeleteThis)
                    {
                        // Set deleted time
                        item.DeletedTime = DateTime.UtcNow;

                        // Set retention limit (midnight 90 days from now)
                        DateTime purgeDate = DateTime.UtcNow.AddDays(_retentionDays);
                        item.PurgeDate = new DateTime(purgeDate.Year, purgeDate.Month, purgeDate.Day,
                                                      23, 59, 59, 999, DateTimeKind.Utc);
                    }
                    else
                    {
                        // We are undeleting; reset retention dates
                        item.DeletedTime = DateTime.MinValue;
                        item.PurgeDate   = DateTime.MinValue;
                    }

                    // Insert into the other partition
                    item.PartitionKey = (!bDeleteThis).ToString();
                    TableOperation insertOp = TableOperation.Insert(item);
                    await _littleUrlTable.ExecuteAsync(insertOp);

                    // Return
                    return(item);
                }
            }
            return(null);
        }
Exemple #3
0
        public async Task <TableResult> GetUrl(string sKey, bool bIsUrlDeleted = false)
        {
            // Call purge operation whenever a Get is called
            //  The op runs only once a day
            if (_lastPurgeDate.AddDays(1) < DateTime.UtcNow)
            {
                PurgeUrls();
            }

            if (!string.IsNullOrEmpty(sKey))
            {
                // Setup Table op to retrieve from the appropriate partition
                string         partitionKey = (!bIsUrlDeleted).ToString();
                TableOperation retrieveOp   = TableOperation.Retrieve <LittleUrlAzure>(partitionKey, sKey);

                bool bExists = await _littleUrlTable.ExistsAsync();

                if (bExists)
                {
                    TableResult result = await _littleUrlTable.ExecuteAsync(retrieveOp);

                    // If found, update last accessed time
                    LittleUrlAzure item = (LittleUrlAzure)result.Result;
                    if (item != null)
                    {
                        item.LastAccessedTime = DateTime.UtcNow;
                        TableOperation updateOp = TableOperation.InsertOrReplace(item);
                        await _littleUrlTable.ExecuteAsync(updateOp);
                    }

                    // Perform retention management once a day
                    if (_lastRetentionDate.AddDays(1) < DateTime.UtcNow)
                    {
                        RemoveStaleUrls();
                    }

                    return(result);
                }
            }
            return(null);
        }
Exemple #4
0
        public async Task <TableResult> InsertUrl(int urlId, string urlToInsert)
        {
            TableResult result = null;

            try
            {
                string         sKey      = GetNewKey();
                LittleUrlAzure littleUrl = new LittleUrlAzure(_partitionKey, sKey, urlId, urlToInsert);

                // Create the insert op (use upsert to prevent key duplication)
                TableOperation insertOp = TableOperation.InsertOrMerge(littleUrl);

                // Execute
                result = await _littleUrlTable.ExecuteAsync(insertOp);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("InsertUrl() | Exception caught:" + ex.Message);
                result = null;
            }
            return(result);
        }