|
| 1 | +/* |
| 2 | +Copyright 2024 The Spice.ai OSS Authors |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +using System.Globalization; |
| 24 | +using System.Text.Json; |
| 25 | + |
| 26 | +namespace Spice.Datasets; |
| 27 | + |
| 28 | +/// <summary> |
| 29 | +/// The refresh mode to use for a single on-demand dataset refresh. |
| 30 | +/// </summary> |
| 31 | +/// <remarks> |
| 32 | +/// On-demand refreshes apply to the <c>full</c> and <c>append</c> refresh modes only. |
| 33 | +/// Datasets accelerated with <c>changes</c> mode are kept up to date by change data |
| 34 | +/// capture and are not refreshed through this API. |
| 35 | +/// </remarks> |
| 36 | +public enum RefreshMode |
| 37 | +{ |
| 38 | + /// <summary> |
| 39 | + /// Replace the accelerated data with the full result of the refresh query. |
| 40 | + /// </summary> |
| 41 | + Full, |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Append newly returned rows to the accelerated data. |
| 45 | + /// </summary> |
| 46 | + Append, |
| 47 | +} |
| 48 | + |
| 49 | +/// <summary> |
| 50 | +/// Optional overrides for a single on-demand dataset acceleration refresh. |
| 51 | +/// </summary> |
| 52 | +/// <remarks> |
| 53 | +/// Every option is optional. Any option left unset falls back to the value configured |
| 54 | +/// for the dataset in the Spicepod. An instance with no options set requests a refresh |
| 55 | +/// using the dataset's existing configuration. |
| 56 | +/// </remarks> |
| 57 | +/// <example> |
| 58 | +/// <code> |
| 59 | +/// var options = new RefreshOptions() |
| 60 | +/// .WithRefreshSql("SELECT * FROM taxi_trips WHERE tip_amount > 10.0") |
| 61 | +/// .WithRefreshMode(RefreshMode.Append) |
| 62 | +/// .WithMaxJitter(TimeSpan.FromSeconds(10)); |
| 63 | +/// |
| 64 | +/// await client.RefreshDatasetAsync("taxi_trips", options); |
| 65 | +/// </code> |
| 66 | +/// </example> |
| 67 | +public sealed class RefreshOptions |
| 68 | +{ |
| 69 | + /// <summary> |
| 70 | + /// The SQL statement used for this refresh. Defaults to the <c>refresh_sql</c> |
| 71 | + /// configured for the dataset, if any. |
| 72 | + /// </summary> |
| 73 | + public string? RefreshSql { get; set; } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// The refresh mode to use for this refresh. Defaults to the <c>refresh_mode</c> |
| 77 | + /// configured for the dataset, or <see cref="Datasets.RefreshMode.Full"/>. |
| 78 | + /// </summary> |
| 79 | + public RefreshMode? RefreshMode { get; set; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// The maximum amount of jitter to add before starting this refresh. Defaults to the |
| 83 | + /// <c>refresh_jitter_max</c> configured for the dataset, or 10% of the refresh check interval. |
| 84 | + /// </summary> |
| 85 | + public TimeSpan? MaxJitter { get; set; } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Sets the SQL statement used for this refresh. |
| 89 | + /// </summary> |
| 90 | + /// <param name="refreshSql">The refresh SQL statement.</param> |
| 91 | + /// <returns>The current instance of <see cref="RefreshOptions"/> for method chaining.</returns> |
| 92 | + /// <exception cref="System.ArgumentNullException">Thrown when refreshSql is null.</exception> |
| 93 | + /// <exception cref="System.ArgumentException">Thrown when refreshSql is empty or whitespace.</exception> |
| 94 | + public RefreshOptions WithRefreshSql(string refreshSql) |
| 95 | + { |
| 96 | +#if NET8_0_OR_GREATER |
| 97 | + ArgumentException.ThrowIfNullOrWhiteSpace(refreshSql); |
| 98 | +#else |
| 99 | + ThrowHelper.ThrowIfNullOrWhiteSpace(refreshSql, nameof(refreshSql)); |
| 100 | +#endif |
| 101 | + RefreshSql = refreshSql; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Sets the refresh mode to use for this refresh. |
| 107 | + /// </summary> |
| 108 | + /// <param name="refreshMode">The refresh mode.</param> |
| 109 | + /// <returns>The current instance of <see cref="RefreshOptions"/> for method chaining.</returns> |
| 110 | + public RefreshOptions WithRefreshMode(RefreshMode refreshMode) |
| 111 | + { |
| 112 | + RefreshMode = refreshMode; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Sets the maximum amount of jitter to add before starting this refresh. |
| 118 | + /// </summary> |
| 119 | + /// <param name="maxJitter">The maximum jitter. Must not be negative.</param> |
| 120 | + /// <returns>The current instance of <see cref="RefreshOptions"/> for method chaining.</returns> |
| 121 | + /// <exception cref="System.ArgumentOutOfRangeException">Thrown when maxJitter is negative.</exception> |
| 122 | + public RefreshOptions WithMaxJitter(TimeSpan maxJitter) |
| 123 | + { |
| 124 | + if (maxJitter < TimeSpan.Zero) |
| 125 | + { |
| 126 | + throw new ArgumentOutOfRangeException(nameof(maxJitter), maxJitter, "maxJitter must not be negative."); |
| 127 | + } |
| 128 | + |
| 129 | + MaxJitter = maxJitter; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Serializes the options to the JSON request body expected by the Spice runtime. |
| 135 | + /// Unset options are omitted so the runtime falls back to the dataset configuration. |
| 136 | + /// </summary> |
| 137 | + internal string ToJson() |
| 138 | + { |
| 139 | + var body = new Dictionary<string, string>(StringComparer.Ordinal); |
| 140 | + |
| 141 | + if (!string.IsNullOrWhiteSpace(RefreshSql)) |
| 142 | + { |
| 143 | + body["refresh_sql"] = RefreshSql!; |
| 144 | + } |
| 145 | + |
| 146 | + if (RefreshMode.HasValue) |
| 147 | + { |
| 148 | + body["refresh_mode"] = ToWireValue(RefreshMode.Value); |
| 149 | + } |
| 150 | + |
| 151 | + if (MaxJitter.HasValue) |
| 152 | + { |
| 153 | + if (MaxJitter.Value < TimeSpan.Zero) |
| 154 | + { |
| 155 | + throw new ArgumentOutOfRangeException(nameof(MaxJitter), MaxJitter.Value, "MaxJitter must not be negative."); |
| 156 | + } |
| 157 | + |
| 158 | + body["refresh_jitter_max"] = FormatDuration(MaxJitter.Value); |
| 159 | + } |
| 160 | + |
| 161 | + return JsonSerializer.Serialize(body); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Maps a <see cref="Datasets.RefreshMode"/> to the value understood by the runtime. |
| 166 | + /// </summary> |
| 167 | + internal static string ToWireValue(RefreshMode refreshMode) => refreshMode switch |
| 168 | + { |
| 169 | + Datasets.RefreshMode.Full => "full", |
| 170 | + Datasets.RefreshMode.Append => "append", |
| 171 | + _ => throw new ArgumentOutOfRangeException(nameof(refreshMode), refreshMode, "Unsupported refresh mode."), |
| 172 | + }; |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Formats a <see cref="TimeSpan"/> as a duration string the runtime can parse (for example "10s" or "1500ms"). |
| 176 | + /// </summary> |
| 177 | + internal static string FormatDuration(TimeSpan value) |
| 178 | + { |
| 179 | + var totalMilliseconds = (long)value.TotalMilliseconds; |
| 180 | + |
| 181 | + return totalMilliseconds % 1000 == 0 |
| 182 | + ? string.Concat((totalMilliseconds / 1000).ToString(CultureInfo.InvariantCulture), "s") |
| 183 | + : string.Concat(totalMilliseconds.ToString(CultureInfo.InvariantCulture), "ms"); |
| 184 | + } |
| 185 | +} |
0 commit comments