Kinetica   C#   API  Version 7.2.3.1
ShardKeyValue.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Runtime.CompilerServices;
5 
6 namespace kinetica.Records;
7 
12  public enum ShardKeyValueType
13  {
15  Null,
17  Int,
19  Long,
21  Float,
23  Double,
25  String,
27  Date,
29  DateTime,
31  Time,
33  Ipv4,
35  Decimal,
37  Boolean,
39  Int8,
41  Int16,
43  Timestamp,
45  Uuid,
47  Ulong
48  }
49 
57  public readonly struct ShardKeyValue : IEquatable<ShardKeyValue>
58  {
59  private readonly ShardKeyValueType _type;
60  private readonly long _intValue;
61  private readonly double _floatValue;
62  private readonly string? _stringValue;
63 
64  private ShardKeyValue(ShardKeyValueType type, long intValue = 0, double floatValue = 0, string? stringValue = null)
65  {
66  _type = type;
67  _intValue = intValue;
68  _floatValue = floatValue;
69  _stringValue = stringValue;
70  }
71 
73  public ShardKeyValueType Type => _type;
74 
76  public bool IsNull => _type == ShardKeyValueType.Null;
77 
78  #region Factory Methods (Equivalent to Rust enum variants)
79 
81  public static ShardKeyValue Null() => new(ShardKeyValueType.Null);
82 
84  public static ShardKeyValue Int(int value) => new(ShardKeyValueType.Int, value);
85 
87  public static ShardKeyValue Long(long value) => new(ShardKeyValueType.Long, value);
88 
90  public static ShardKeyValue Float(float value) => new(ShardKeyValueType.Float, floatValue: value);
91 
93  public static ShardKeyValue Double(double value) => new(ShardKeyValueType.Double, floatValue: value);
94 
96  public static ShardKeyValue String(string value) => new(ShardKeyValueType.String, stringValue: value);
97 
99  public static ShardKeyValue Date(string value) => new(ShardKeyValueType.Date, stringValue: value);
100 
102  public static ShardKeyValue DateTime(string value) => new(ShardKeyValueType.DateTime, stringValue: value);
103 
105  public static ShardKeyValue Time(string value) => new(ShardKeyValueType.Time, stringValue: value);
106 
108  public static ShardKeyValue Ipv4(string value) => new(ShardKeyValueType.Ipv4, stringValue: value);
109 
111  public static ShardKeyValue Decimal(string value) => new(ShardKeyValueType.Decimal, stringValue: value);
112 
114  public static ShardKeyValue Boolean(bool value) => new(ShardKeyValueType.Boolean, value ? 1 : 0);
115 
117  public static ShardKeyValue Int8(sbyte value) => new(ShardKeyValueType.Int8, value);
118 
120  public static ShardKeyValue Int16(short value) => new(ShardKeyValueType.Int16, value);
121 
123  public static ShardKeyValue Timestamp(long value) => new(ShardKeyValueType.Timestamp, value);
124 
126  public static ShardKeyValue Uuid(string value) => new(ShardKeyValueType.Uuid, stringValue: value);
127 
129  public static ShardKeyValue Ulong(string value) => new(ShardKeyValueType.Ulong, stringValue: value);
130 
131  #endregion
132 
133  #region Value Accessors
134 
137  [MethodImpl(MethodImplOptions.AggressiveInlining)]
138  public int AsInt()
139  {
140  return _type switch
141  {
143  => (int)_intValue,
144  _ => throw new InvalidOperationException($"Cannot convert {_type} to Int")
145  };
146  }
147 
150  [MethodImpl(MethodImplOptions.AggressiveInlining)]
151  public long AsLong()
152  {
153  return _type switch
154  {
155  ShardKeyValueType.Long or ShardKeyValueType.Timestamp or
157  => _intValue,
158  _ => throw new InvalidOperationException($"Cannot convert {_type} to Long")
159  };
160  }
161 
164  [MethodImpl(MethodImplOptions.AggressiveInlining)]
165  public float AsFloat()
166  {
167  return _type switch
168  {
169  ShardKeyValueType.Float => (float)_floatValue,
170  _ => throw new InvalidOperationException($"Cannot convert {_type} to Float")
171  };
172  }
173 
176  [MethodImpl(MethodImplOptions.AggressiveInlining)]
177  public double AsDouble()
178  {
179  return _type switch
180  {
181  ShardKeyValueType.Double or ShardKeyValueType.Float => _floatValue,
182  _ => throw new InvalidOperationException($"Cannot convert {_type} to Double")
183  };
184  }
185 
188  [MethodImpl(MethodImplOptions.AggressiveInlining)]
189  public string? AsString()
190  {
191  return _type switch
192  {
193  ShardKeyValueType.String or ShardKeyValueType.Date or ShardKeyValueType.DateTime or
194  ShardKeyValueType.Time or ShardKeyValueType.Ipv4 or ShardKeyValueType.Decimal or
196  => _stringValue,
197  ShardKeyValueType.Null => null,
198  _ => throw new InvalidOperationException($"Cannot convert {_type} to String")
199  };
200  }
201 
204  [MethodImpl(MethodImplOptions.AggressiveInlining)]
205  public bool AsBool()
206  {
207  return _type switch
208  {
209  ShardKeyValueType.Boolean => _intValue != 0,
210  _ => throw new InvalidOperationException($"Cannot convert {_type} to Boolean")
211  };
212  }
213 
215  public bool TryGetInt(out int value)
216  {
217  if (_type is ShardKeyValueType.Int or ShardKeyValueType.Int8 or ShardKeyValueType.Int16 or ShardKeyValueType.Boolean)
218  {
219  value = (int)_intValue;
220  return true;
221  }
222  value = default;
223  return false;
224  }
225 
227  public bool TryGetLong(out long value)
228  {
229  if (_type is ShardKeyValueType.Long or ShardKeyValueType.Timestamp or
231  {
232  value = _intValue;
233  return true;
234  }
235  value = default;
236  return false;
237  }
238 
240  public bool TryGetString(out string? value)
241  {
242  if (_type is ShardKeyValueType.String or ShardKeyValueType.Date or ShardKeyValueType.DateTime or
243  ShardKeyValueType.Time or ShardKeyValueType.Ipv4 or ShardKeyValueType.Decimal or
245  {
246  value = _stringValue;
247  return true;
248  }
249  value = default;
250  return false;
251  }
252 
253  #endregion
254 
255  #region Equality and Hashing
256 
257  public bool Equals(ShardKeyValue other)
258  {
259  if (_type != other._type) return false;
260 
261  return _type switch
262  {
263  ShardKeyValueType.Null => true,
265  ShardKeyValueType.Int16 or ShardKeyValueType.Boolean or ShardKeyValueType.Timestamp
266  => _intValue == other._intValue,
267  ShardKeyValueType.Float or ShardKeyValueType.Double
268  => _floatValue == other._floatValue,
269  _ => string.Equals(_stringValue, other._stringValue, StringComparison.Ordinal)
270  };
271  }
272 
273  public override bool Equals(object? obj) => obj is ShardKeyValue other && Equals(other);
274 
275  public override int GetHashCode()
276  {
277  return _type switch
278  {
279  ShardKeyValueType.Null => 0,
281  ShardKeyValueType.Int16 or ShardKeyValueType.Boolean or ShardKeyValueType.Timestamp
282  => HashCode.Combine(_type, _intValue),
283  ShardKeyValueType.Float or ShardKeyValueType.Double
284  => HashCode.Combine(_type, _floatValue),
285  _ => HashCode.Combine(_type, _stringValue)
286  };
287  }
288 
289  public static bool operator ==(ShardKeyValue left, ShardKeyValue right) => left.Equals(right);
290  public static bool operator !=(ShardKeyValue left, ShardKeyValue right) => !left.Equals(right);
291 
292  #endregion
293 
294  public override string ToString()
295  {
296  return _type switch
297  {
298  ShardKeyValueType.Null => "null",
300  ShardKeyValueType.Int16 or ShardKeyValueType.Timestamp
301  => _intValue.ToString(),
302  ShardKeyValueType.Boolean => (_intValue != 0).ToString(),
303  ShardKeyValueType.Float or ShardKeyValueType.Double
304  => _floatValue.ToString(),
305  _ => _stringValue ?? "null"
306  };
307  }
308  }
309 
317  public readonly struct ShardKeyValues : IReadOnlyList<(string Name, ShardKeyValue Value)>
318  {
319  private readonly (string Name, ShardKeyValue Value)[]? _values;
320 
322  public static ShardKeyValues Empty => new(Array.Empty<(string, ShardKeyValue)>());
323 
325  public ShardKeyValues(params (string Name, ShardKeyValue Value)[] values)
326  {
327  _values = values;
328  }
329 
331  public static ShardKeyValues Single(string name, ShardKeyValue value)
332  => new((name, value));
333 
335  public static ShardKeyValues Two(string name1, ShardKeyValue value1, string name2, ShardKeyValue value2)
336  => new((name1, value1), (name2, value2));
337 
339  public static ShardKeyValues Three(
340  string name1, ShardKeyValue value1,
341  string name2, ShardKeyValue value2,
342  string name3, ShardKeyValue value3)
343  => new((name1, value1), (name2, value2), (name3, value3));
344 
346  public static ShardKeyValues Four(
347  string name1, ShardKeyValue value1,
348  string name2, ShardKeyValue value2,
349  string name3, ShardKeyValue value3,
350  string name4, ShardKeyValue value4)
351  => new((name1, value1), (name2, value2), (name3, value3), (name4, value4));
352 
354  public int Count => _values?.Length ?? 0;
355 
357  public (string Name, ShardKeyValue Value) this[int index]
358  {
359  get
360  {
361  if (_values == null || index < 0 || index >= _values.Length)
362  throw new IndexOutOfRangeException();
363  return _values[index];
364  }
365  }
366 
368  public IEnumerator<(string Name, ShardKeyValue Value)> GetEnumerator()
369  {
370  if (_values == null)
371  yield break;
372 
373  foreach (var value in _values)
374  yield return value;
375  }
376 
377  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
378  }
16-bit signed integer
ShardKeyValueType
Represents the type of a shard key value.
static ShardKeyValue Int(int value)
Creates a 32-bit integer shard key value.
static ShardKeyValue Float(float value)
Creates a 32-bit float shard key value.
bool TryGetLong(out long value)
Tries to get the value as a long.
UUID in standard format
string? AsString()
Gets the value as a string.
64-bit signed integer
override string ToString()
8-bit signed integer
int Count
Gets the number of shard key values.
static bool operator !=(ShardKeyValue left, ShardKeyValue right)
override int GetHashCode()
static ShardKeyValues Three(string name1, ShardKeyValue value1, string name2, ShardKeyValue value2, string name3, ShardKeyValue value3)
Creates a ShardKeyValues collection with three values.
static ShardKeyValue Int16(short value)
Creates a 16-bit integer shard key value.
Date in YYYY-MM-DD format
string Name
Gets the shard key value at the specified index.
static ShardKeyValues Two(string name1, ShardKeyValue value1, string name2, ShardKeyValue value2)
Creates a ShardKeyValues collection with two values.
static ShardKeyValue Uuid(string value)
Creates a UUID shard key value (standard format).
static ShardKeyValue Time(string value)
Creates a time shard key value (HH:MM:SS.mmm format).
Time in HH:MM:SS.mmm format
32-bit IEEE 754 float
float AsFloat()
Gets the value as a 32-bit float.
int AsInt()
Gets the value as a 32-bit integer.
static ShardKeyValue Double(double value)
Creates a 64-bit double shard key value.
IPv4 address in dotted-quad format
Unsigned long as string (0 to 18446744073709551615)
static ShardKeyValues Single(string name, ShardKeyValue value)
Creates a ShardKeyValues collection with a single value.
static ShardKeyValue Ipv4(string value)
Creates an IPv4 shard key value (dotted-quad format).
static ShardKeyValue Null()
Creates a null shard key value.
long AsLong()
Gets the value as a 64-bit integer.
IEnumerator<(string Name, ShardKeyValue Value)> GetEnumerator()
Gets an enumerator for the shard key values.
Collection of shard key column names and values.
static ShardKeyValue Timestamp(long value)
Creates a timestamp shard key value (milliseconds since Unix epoch).
bool AsBool()
Gets the value as a boolean.
static ShardKeyValue Decimal(string value)
Creates a decimal shard key value (string representation).
bool Equals(ShardKeyValue other)
double AsDouble()
Gets the value as a 64-bit double.
static ShardKeyValues Empty
Creates an empty ShardKeyValues collection.
Decimal number as string
static ShardKeyValue Int8(sbyte value)
Creates an 8-bit integer shard key value.
static ShardKeyValue DateTime(string value)
Creates a datetime shard key value (YYYY-MM-DD HH:MM:SS.mmm format).
static ShardKeyValue Long(long value)
Creates a 64-bit integer shard key value.
64-bit IEEE 754 double
static ShardKeyValue Date(string value)
Creates a date shard key value (YYYY-MM-DD format).
Immutable collection of metadata about a Kinetica type.
Definition: Type.cs:36
static ShardKeyValue String(string value)
Creates a string shard key value.
A typed value for shard key computation.
Timestamp as milliseconds since Unix epoch
ShardKeyValues(params(string Name, ShardKeyValue Value)[] values)
Creates a ShardKeyValues collection from the specified values.
static bool operator==(ShardKeyValue left, ShardKeyValue right)
32-bit signed integer (also used for boolean, int8, int16)
bool TryGetString(out string? value)
Tries to get the value as a string.
bool IsNull
Returns true if this value is null.
DateTime in YYYY-MM-DD HH:MM:SS.mmm format
static ShardKeyValue Boolean(bool value)
Creates a boolean shard key value.
static ShardKeyValue Ulong(string value)
Creates an unsigned long shard key value (string representation).
bool TryGetInt(out int value)
Tries to get the value as an integer.
static ShardKeyValues Four(string name1, ShardKeyValue value1, string name2, ShardKeyValue value2, string name3, ShardKeyValue value3, string name4, ShardKeyValue value4)
Creates a ShardKeyValues collection with four values.
Boolean stored as integer (0 or 1)