Skip to content

Commit 158eb8d

Browse files
committed
Add reindex method
1 parent 3c3aaa6 commit 158eb8d

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/TableStorage.Abstractions.POCO.SecondaryIndexes/PocoStoreIndexer.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Azure.Documents;
56
using TableStorage.Abstractions.Models;
@@ -436,5 +437,70 @@ public static IPocoTableStore<T, TIndexPartitionKey, TIndexRowKey> Index<T, TPar
436437
}
437438

438439

440+
/// <summary>
441+
/// Reindexes a table. You will need to call this for existing tables that have never had an index, or if things get out of sync. This can take a while.
442+
/// </summary>
443+
/// <typeparam name="T"></typeparam>
444+
/// <typeparam name="TPartitionKey">The type of the t partition key.</typeparam>
445+
/// <typeparam name="TRowKey">The type of the t row key.</typeparam>
446+
/// <param name="tableStore">The table store.</param>
447+
/// <param name="indexName">Name of the index.</param>
448+
/// <param name="maxDegreeOfParallelism">The maximum degree of parallelism.</param>
449+
/// <param name="recordsIndexedCallback">The records indexed callback.</param>
450+
/// <param name="failedIndexCallback">The failed index callback.</param>
451+
/// <exception cref="ArgumentException"></exception>
452+
public static async Task ReindexAsync<T, TPartitionKey, TRowKey>(this IPocoTableStore<T, TPartitionKey, TRowKey> tableStore, string indexName, int? maxDegreeOfParallelism = null, Action<int> recordsIndexedCallback = null, Action<T, Exception> failedIndexCallback = null)
453+
{
454+
maxDegreeOfParallelism = maxDegreeOfParallelism ?? Environment.ProcessorCount * 20;
455+
456+
string pageToken = null;
457+
int count = 0;
458+
using (var semaphore = new SemaphoreSlim(maxDegreeOfParallelism.Value, maxDegreeOfParallelism.Value))
459+
{
460+
try
461+
{
462+
dynamic indexStore = _indexes[indexName];
463+
do
464+
{
465+
var result = await indexStore.GetAllRecordsPagedAsync(1000, pageToken);
466+
pageToken = result.ContinuationToken;
467+
468+
if (result.Items.Count > 0)
469+
{
470+
foreach (var record in result.Items)
471+
{
472+
await semaphore.WaitAsync(TimeSpan.FromSeconds(20));
473+
Task task = indexStore.InsertOrReplaceAsync(record);
474+
task.ContinueWith(r =>
475+
{
476+
if (r.IsFaulted)
477+
{
478+
failedIndexCallback?.Invoke(record, r.Exception);
479+
}
480+
Interlocked.Increment(ref count);
481+
semaphore.Release(1);
482+
});
483+
484+
}
485+
}
486+
487+
recordsIndexedCallback?.Invoke(count);
488+
} while (pageToken != null);
489+
490+
while (semaphore.CurrentCount < maxDegreeOfParallelism)
491+
{
492+
493+
await Task.Delay(5);
494+
}
495+
recordsIndexedCallback?.Invoke(count);
496+
}
497+
catch (KeyNotFoundException e)
498+
{
499+
throw new ArgumentException($"{indexName} is not a defined secondary index");
500+
}
501+
}
502+
}
503+
504+
439505
}
440506
}

tests/TableStorage.Abstractions.POCO.SecondaryIndexes.Tests/IndexTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,32 @@ public async Task get_by_index_partition_key_string_paged_async()
405405
Assert.IsNull(p.ContinuationToken);
406406

407407
}
408+
409+
[TestMethod]
410+
public async Task reindex()
411+
{
412+
var employee = new Employee
413+
{
414+
Name = "Test",
415+
CompanyId = 99,
416+
Id = 99,
417+
Department = new Department { Id = 5, Name = "Test" }
418+
};
419+
var employee2 = new Employee
420+
{
421+
Name = "Test2",
422+
CompanyId = 99,
423+
Id = 100,
424+
Department = new Department { Id = 5, Name = "Test" }
425+
};
426+
await TableStore.InsertAsync(employee);
427+
await TableStore.InsertAsync(employee2);
428+
429+
int count = 0;
430+
431+
await TableStore.ReindexAsync("Name", maxDegreeOfParallelism: 20, recordsIndexedCallback: i=>count = i);
432+
433+
Assert.AreEqual(2, count);
434+
}
408435
}
409436
}

0 commit comments

Comments
 (0)