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); }
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"); }