/// <summary> /// Loads all tag definitions for the specified historian. /// </summary> /// <param name="historian">The historian.</param> /// <param name="callback">A callback function that is invoked every time a tag definition is loaded from Redis.</param> /// <param name="cancellationToken">The cancellation token for the request.</param> /// <returns> /// A task that will load all tags into the historian. /// </returns> internal static async Task LoadAll(RedisHistorian historian, Action <RedisTagDefinition> callback, CancellationToken cancellationToken) { var key = historian.GetKeyForTagIdsList(); const int pageSize = 100; var page = 0; bool @continue; // Load tag definitions 100 at a time. do { @continue = false; ++page; long start = (page - 1) * pageSize; long end = start + pageSize - 1; // -1 because right-most item is included when getting the list range. var tagIds = await historian.Connection.GetDatabase().ListRangeAsync(key, start, end).ConfigureAwait(false); @continue = tagIds.Length == pageSize; if (tagIds.Length == 0) { continue; } var tasks = tagIds.Select(x => Task.Run(async() => { var tag = await Load(historian, x, cancellationToken).ConfigureAwait(false); callback(tag); })).ToArray(); await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(-1, cancellationToken)).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); } while (@continue); }
/// <summary> /// Deletes the tag definition and raises the <see cref="TagDefinition.Deleted"/> event. /// </summary> /// <param name="cancellationToken">The cancellation token for the request.</param> /// <returns> /// A task that will delete the tag. /// </returns> internal async Task Delete(CancellationToken cancellationToken) { var tasks = new List <Task>(); var db = _historian.Connection.GetDatabase(); // Delete the tag definition. tasks.Add(db.KeyDeleteAsync(_tagDefinitionKey)); // Delete the snapshot value. tasks.Add(db.KeyDeleteAsync(_snapshotKey)); // Delete the raw values. tasks.Add(db.KeyDeleteAsync(_archiveKey)); // Delete the archive candidate value. tasks.Add(db.KeyDeleteAsync(_archiveCandidateKey)); // Remove the tag ID from the list of defined tag IDs. var tagIdListKey = _historian.GetKeyForTagIdsList(); tasks.Add(_historian.Connection.GetDatabase().ListRemoveAsync(tagIdListKey, Id)); try { await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(-1, cancellationToken)).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); } finally { Updated -= TagUpdated; OnDeleted(); } }
/// <summary> /// Creates a new <see cref="RedisTagDefinition"/>. /// </summary> /// <param name="historian">The historian for the tag.</param> /// <param name="settings">The tag settings.</param> /// <param name="creator">The tag's creator.</param> /// <param name="cancellationToken">The cancellation token for the request.</param> /// <returns> /// A task that will create a new <see cref="RedisTagDefinition"/> and save it to the /// historian's Redis server. /// </returns> internal static async Task <RedisTagDefinition> Create(RedisHistorian historian, TagSettings settings, ClaimsPrincipal creator, CancellationToken cancellationToken) { var now = DateTime.UtcNow; var result = new RedisTagDefinition(historian, null, settings, new TagMetadata(DateTime.UtcNow, creator?.Identity.Name), null, new[] { TagChangeHistoryEntry.Created(creator) }); var key = historian.GetKeyForTagIdsList(); await Task.WhenAny(Task.WhenAll(result.Save(cancellationToken), historian.Connection.GetDatabase().ListRightPushAsync(key, result.Id)), Task.Delay(-1, cancellationToken)).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); return(result); }