Exemplo n.º 1
0
        public async Task <(string url, bool permanent, bool preserveMethod)> TryGetUrlAsync(string idcode)
        {
            if (idcode.Length >= 3 && Int64.TryParse(idcode, out long discard))
            {
                var partkey = "";
                var rowkey  = "";
                var chars   = idcode.ToCharArray();
                for (int i = 0; i < idcode.Length; i++)
                {
                    if (i <= 1)
                    {
                        partkey += idcode[i];
                    }
                    else
                    {
                        rowkey += idcode[i];
                    }
                }
                CloudTable table = await TableExecutor <TableEntity> .CreateTableAsync("shorturls", _cloudStorage);

                var result = await TableExecutor <ShorterData> .PointQueryAsync(table, partkey, rowkey);

                if (result != null)
                {
                    return(result.Url, result.Permanent, result.PreserveMethod);
                }
            }
            return(String.Empty, false, true);
        }
Exemplo n.º 2
0
        public async Task <bool> Init()
        {
            var table = await TableExecutor <TableEntity> .CreateTableAsync("shorturls", _cloudStorage);

            for (int i = 0; i < 99; i++)
            {
                var partkey = i.ToString("00");
                var index   = await TableExecutor <IndexEntity> .PointQueryAsync(table, partkey, "index");

                if (index == null)
                {
                    index = new IndexEntity(partkey, "index", 0L);
                    await TableExecutor <IndexEntity> .InsertOrMergeEntityAsync(table, index);
                }
                TableIndex.AddOrUpdate(partkey, index.Index, (k, v) => v = index.Index);
            }
            return(true);
        }
Exemplo n.º 3
0
        public async Task <string> TryAddNewUrlAsync(string url, bool permanent = false, bool preserve = true, bool statsCount = false)
        {
            CloudTable table = await TableExecutor <TableEntity> .CreateTableAsync("shorturls", _cloudStorage);

            var id     = GetNext();
            var result = await TableExecutor <ShorterData> .InsertOnlyEntityAsync(table, new ShorterData()
            {
                Url = url, PartitionKey = id.table, RowKey = id.id, Permanent = permanent, PreserveMethod = preserve, Count = 0, StatCount = statsCount
            });

            if (result != null)
            {
                if (result.RowKey != "conflict")
                {
                    //fnf
                    Task.Factory.StartNew(() => {
                        if (TableIndex.TryGetValue(result.PartitionKey, out long indexvalue))
                        {
                            TableExecutor <IndexEntity> .InsertOrMergeEntityAsync(table, new IndexEntity(result.PartitionKey, "index", indexvalue));
                        }
                    });
                    return(result.PartitionKey + result.RowKey);
                }
                else
                {
                    //deal with conflict, retry 3 times then return fail (empty)
                    retryCount++;
                    if (retryCount <= 3)
                    {
                        return(await TryAddNewUrlAsync(url, permanent, preserve));
                    }
                    else
                    {
                        return(string.Empty);
                    }
                }
            }
            return("error");
        }