/// <summary> /// Get the data associated with the specified key. /// If the key is not present or the data value is null then the /// return value will be <code>default(T)</code>. /// </summary> /// <typeparam name="T"> /// The type of the data to return. /// </typeparam> /// <param name="key"> /// The key used to access the data. /// </param> /// <returns> /// The data. /// </returns> /// <exception cref="InvalidCastException"> /// Thrown if the data object stored under the name of the key /// cannot be cast to the expected type T. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if the key argument is null /// </exception> /// <exception cref="ArgumentException"> /// Thrown if the Name property of the key argument is null /// </exception> /// <exception cref="KeyNotFoundException"> /// Thrown if the map does not contain an entry for the key /// </exception> public T Get <T>(ITypedKey <T> key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (key.Name == null) { throw new ArgumentException(Messages.ExceptionKeyNameNull, nameof(key)); } T result = default(T); if (_data.ContainsKey(key.Name)) { object obj = _data[key.Name]; if (obj != null) { result = (T)obj; } } else { throw new KeyNotFoundException(GetKeyMissingMessage(key.Name)); } return(result); }
private Func <Task> CreateDeleteFunction(ITypedKey <TKey, TMeasureType> key, ContinuationToken token, List <TEntry> entries) { if (entries.Count == 0) { return(() => Task.FromResult(0)); } else { DateTime from; DateTime to; var ts1 = ((IEntry)entries[0]).GetTimestamp(); var ts2 = ((IEntry)entries[entries.Count - 1]).GetTimestamp(); if (ts1 >= ts2) { to = ts1; from = ts2; } else { to = ts2; from = ts1; } to = to.AddTicks(1); // TODO: Implement DeleteInternalAsync return(() => this.DeleteInternalAsync(key, from, to)); } }
/// <summary> /// Add the specified element data to the internal map. /// </summary> /// <typeparam name="T"> /// The type of the data being stored. /// </typeparam> /// <param name="key"> /// The key to use when storing the data. /// </param> /// <param name="dataFactory"> /// The method to use to create a new data to store if one does not /// already exist. /// </param> /// <returns> /// Existing data matching the key, or newly added data. /// </returns> /// <exception cref="ArgumentNullException"> /// Thrown if the supplied data factory is null /// </exception> public T GetOrAdd <T>(ITypedKey <T> key, Func <IPipeline, T> dataFactory) where T : IElementData { if (dataFactory == null) { throw new ArgumentNullException(nameof(dataFactory)); } T data = default(T); if (_data.TryGetValue(key, out data) == false) { if (Pipeline.IsConcurrent == true) { lock (_dataLock) { if (_data.TryGetValue(key, out data) == false) { data = dataFactory(Pipeline); _data.Add(key, data); } } } else { data = dataFactory(Pipeline); _data.Add(key, data); } } return(data); }
private async Task DeleteInternalAsync(ITypedKey <TKey, TMeasureType> id, DateTime from, DateTime to) { using (await _cc.WriteAsync().ConfigureAwait(false)) { await CreateDatabase().ConfigureAwait(false); var resultSet = await _client.ReadAsync <TEntry>(_database, CreateDeleteQuery( new[] { id }, from, to )).ConfigureAwait(false); } }
/// <summary> /// Add the specified data to the collection using the specified key. /// </summary> /// <typeparam name="T"> /// The type of the data being stored. /// </typeparam> /// <param name="key"> /// The key used to identify the data. /// </param> /// <param name="data"> /// The data to store. /// </param> public void Add <T>(ITypedKey <T> key, T data) { if (_data.ContainsKey(key.Name) == false) { _data.Add(key.Name, data); } else { _data[key.Name] = data; } }
/// <summary> /// Get the <see cref="IElementData"/> instance containing data /// populated by the specified element. /// </summary> /// <typeparam name="T"> /// The expected type of the data to be returned. /// </typeparam> /// <param name="key"> /// An <see cref="ITypedKey{T}"/> indicating the element /// to get data from. /// </param> /// <returns> /// An instance of type T containing the data. /// </returns> /// <exception cref="ArgumentNullException"> /// Thrown if the supplied key is null /// </exception> /// <exception cref="PipelineException"> /// Thrown if this FlowData instance has not been processed yet. /// </exception> public T Get <T>(ITypedKey <T> key) where T : IElementData { if (_processed == false) { throw new PipelineException(Messages.ExceptionFlowDataNotYetProcessed); } if (key == null) { throw new ArgumentNullException(nameof(key)); } return(_data.Get(key)); }
private SegmentedReadResult <TKey, TEntry> Convert(ITypedKey <TKey, TMeasureType> id, InfluxResultSet <TEntry> resultSet, int segmentSize) { var list = resultSet.Results.FirstOrDefault()?.Series.FirstOrDefault()?.Rows; var entries = list; DateTime?to = null; if (entries.Count > 0) { to = ((IEntry)entries[entries.Count - 1]).GetTimestamp(); } var continuationToken = new ContinuationToken(entries.Count == segmentSize, to); return(new SegmentedReadResult <TKey, TEntry>(id.Key, Sort.Descending, continuationToken, entries, CreateDeleteFunction(id, continuationToken, entries))); }
private string CreateSegmentedSelectQuery(ITypedKey <TKey, TMeasureType> id, DateTime?from, DateTime?to, int take, bool reverse, bool isFirstSegment) { var key = id.Key; var measureType = id.GetMeasureType(); if (!reverse || isFirstSegment) { if (from.HasValue && to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND '{from.Value.ToIso8601()}' <= time AND time < '{to.Value.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else if (!from.HasValue && to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND time < '{to.Value.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else if (from.HasValue && !to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND '{from.Value.ToIso8601()}' <= time AND time < '{_maxTo.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND time < '{_maxTo.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } } else { if (from.HasValue && to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND '{from.Value.ToIso8601()}' < time AND time < '{to.Value.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else if (!from.HasValue && to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND time < '{to.Value.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else if (from.HasValue && !to.HasValue) { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND '{from.Value.ToIso8601()}' < time AND time < '{_maxTo.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } else { return($"SELECT {CreateDefaultFieldQuery( measureType.GetFields() )} FROM \"{measureType.GetName()}\" WHERE {CreateDefaultTagFilter( id.Key )} AND time < '{_maxTo.ToIso8601()}' ORDER BY time {( reverse ? "ASC" : "DESC" )} LIMIT {take}"); } } }
/// <summary> /// Check if the map contains an item with the specified /// key name and type. If it does exist, retrieve it. /// </summary> /// <param name="key"> /// The key to check for. /// </param> /// <param name="value"> /// The value associated with the key. /// </param> /// <returns> /// True if an entry matching the key exists in the map. /// False otherwise. /// </returns> public bool TryGetValue <T>(ITypedKey <T> key, out T value) { bool result = false; value = default(T); object valueObj = null; if (_data.TryGetValue(key.Name, out valueObj)) { if (typeof(T).IsAssignableFrom(valueObj.GetType())) { value = (T)valueObj; result = true; } } return(result); }
/// <summary> /// Check if the flow data contains an item with the specified /// key name and type. If it does exist, retrieve it. /// </summary> /// <param name="key"> /// The key to check for. /// </param> /// <param name="value"> /// The value associated with the key. /// </param> /// <returns> /// True if an entry matching the key exists, false otherwise. /// </returns> public bool TryGetValue <T>(ITypedKey <T> key, out T value) where T : IElementData { return(_data.TryGetValue(key, out value)); }
internal TypedReadResult <TKey, TEntry, TMeasureType> AsTaggedResult <TMeasureType>(ITypedKey <TKey, TMeasureType> typedKey) where TMeasureType : IMeasureType { return(new TypedReadResult <TKey, TEntry, TMeasureType>(typedKey, Sort, Entries)); }
internal TypedReadResult(ITypedKey <TKey, TMeasureType> typedKey, Sort sort, List <TEntry> entries) { TypedKey = typedKey; Sort = sort; Entries = entries; }