Kinetica   C#   API  Version 7.2.3.1
GenericRecord.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 
6 namespace kinetica.Records;
7 
35  public sealed class GenericRecord : IBulkInsertable
36  {
37  private readonly Type _type;
38  private readonly RecordValue[] _values;
39 
45  public GenericRecord(Type type)
46  {
47  _type = type ?? throw new ArgumentNullException(nameof(type));
48  _values = new RecordValue[type.ColumnCount];
49 
50  // Initialize all values to Null
51  for (int i = 0; i < _values.Length; i++)
52  _values[i] = RecordValue.Null();
53  }
54 
56  public Type RecordType => _type;
57 
58  #region Put Methods (by index)
59 
61  public bool Put(int index, int value)
62  {
63  if (index < 0 || index >= _values.Length) return false;
64  _values[index] = RecordValue.Int(value);
65  return true;
66  }
67 
69  public bool Put(int index, long value)
70  {
71  if (index < 0 || index >= _values.Length) return false;
72  _values[index] = RecordValue.Long(value);
73  return true;
74  }
75 
77  public bool Put(int index, float value)
78  {
79  if (index < 0 || index >= _values.Length) return false;
80  _values[index] = RecordValue.Float(value);
81  return true;
82  }
83 
85  public bool Put(int index, double value)
86  {
87  if (index < 0 || index >= _values.Length) return false;
88  _values[index] = RecordValue.Double(value);
89  return true;
90  }
91 
93  public bool Put(int index, string? value)
94  {
95  if (index < 0 || index >= _values.Length) return false;
96  _values[index] = value != null ? RecordValue.String(value) : RecordValue.Null();
97  return true;
98  }
99 
101  public bool Put(int index, byte[]? value)
102  {
103  if (index < 0 || index >= _values.Length) return false;
104  _values[index] = value != null ? RecordValue.Bytes(value) : RecordValue.Null();
105  return true;
106  }
107 
109  public bool Put(int index, bool value)
110  {
111  if (index < 0 || index >= _values.Length) return false;
112  _values[index] = RecordValue.Boolean(value);
113  return true;
114  }
115 
117  public bool PutNull(int index)
118  {
119  if (index < 0 || index >= _values.Length) return false;
120  _values[index] = RecordValue.Null();
121  return true;
122  }
123 
125  public bool Put(int index, RecordValue value)
126  {
127  if (index < 0 || index >= _values.Length) return false;
128  _values[index] = value;
129  return true;
130  }
131 
132  #endregion
133 
134  #region Put Methods (by name)
135 
137  public bool Put(string name, int value)
138  {
139  var index = _type.GetColumnIndex(name);
140  return index.HasValue && Put(index.Value, value);
141  }
142 
144  public bool Put(string name, long value)
145  {
146  var index = _type.GetColumnIndex(name);
147  return index.HasValue && Put(index.Value, value);
148  }
149 
151  public bool Put(string name, float value)
152  {
153  var index = _type.GetColumnIndex(name);
154  return index.HasValue && Put(index.Value, value);
155  }
156 
158  public bool Put(string name, double value)
159  {
160  var index = _type.GetColumnIndex(name);
161  return index.HasValue && Put(index.Value, value);
162  }
163 
165  public bool Put(string name, string? value)
166  {
167  var index = _type.GetColumnIndex(name);
168  return index.HasValue && Put(index.Value, value);
169  }
170 
172  public bool Put(string name, byte[]? value)
173  {
174  var index = _type.GetColumnIndex(name);
175  return index.HasValue && Put(index.Value, value);
176  }
177 
179  public bool Put(string name, bool value)
180  {
181  var index = _type.GetColumnIndex(name);
182  return index.HasValue && Put(index.Value, value);
183  }
184 
186  public bool PutNull(string name)
187  {
188  var index = _type.GetColumnIndex(name);
189  return index.HasValue && PutNull(index.Value);
190  }
191 
193  public bool Put(string name, RecordValue value)
194  {
195  var index = _type.GetColumnIndex(name);
196  return index.HasValue && Put(index.Value, value);
197  }
198 
199  #endregion
200 
201  #region Get Methods (by index)
202 
204  public RecordValue? Get(int index)
205  {
206  if (index < 0 || index >= _values.Length) return null;
207  return _values[index];
208  }
209 
211  public int? GetInt(int index)
212  {
213  if (index < 0 || index >= _values.Length) return null;
214  return _values[index].AsInt();
215  }
216 
218  public long? GetLong(int index)
219  {
220  if (index < 0 || index >= _values.Length) return null;
221  return _values[index].AsLong();
222  }
223 
225  public float? GetFloat(int index)
226  {
227  if (index < 0 || index >= _values.Length) return null;
228  return _values[index].AsFloat();
229  }
230 
232  public double? GetDouble(int index)
233  {
234  if (index < 0 || index >= _values.Length) return null;
235  return _values[index].AsDouble();
236  }
237 
239  public string? GetString(int index)
240  {
241  if (index < 0 || index >= _values.Length) return null;
242  return _values[index].AsString();
243  }
244 
246  public byte[]? GetBytes(int index)
247  {
248  if (index < 0 || index >= _values.Length) return null;
249  return _values[index].AsBytes();
250  }
251 
253  public bool? GetBool(int index)
254  {
255  if (index < 0 || index >= _values.Length) return null;
256  return _values[index].AsBool();
257  }
258 
259  #endregion
260 
261  #region Get Methods (by name)
262 
264  public RecordValue? Get(string name)
265  {
266  var index = _type.GetColumnIndex(name);
267  return index.HasValue ? Get(index.Value) : null;
268  }
269 
271  public int? GetInt(string name)
272  {
273  var index = _type.GetColumnIndex(name);
274  return index.HasValue ? GetInt(index.Value) : null;
275  }
276 
278  public long? GetLong(string name)
279  {
280  var index = _type.GetColumnIndex(name);
281  return index.HasValue ? GetLong(index.Value) : null;
282  }
283 
285  public float? GetFloat(string name)
286  {
287  var index = _type.GetColumnIndex(name);
288  return index.HasValue ? GetFloat(index.Value) : null;
289  }
290 
292  public double? GetDouble(string name)
293  {
294  var index = _type.GetColumnIndex(name);
295  return index.HasValue ? GetDouble(index.Value) : null;
296  }
297 
299  public string? GetString(string name)
300  {
301  var index = _type.GetColumnIndex(name);
302  return index.HasValue ? GetString(index.Value) : null;
303  }
304 
306  public byte[]? GetBytes(string name)
307  {
308  var index = _type.GetColumnIndex(name);
309  return index.HasValue ? GetBytes(index.Value) : null;
310  }
311 
313  public bool? GetBool(string name)
314  {
315  var index = _type.GetColumnIndex(name);
316  return index.HasValue ? GetBool(index.Value) : null;
317  }
318 
319  #endregion
320 
321  #region IKineticaRecord Implementation
322 
326  public string GetAvroSchema() => _type.SchemaString;
327 
331  public static IReadOnlyDictionary<string, IList<string>> GetTypeProperties(Type type)
332  {
333  var result = new Dictionary<string, IList<string>>();
334  foreach (var col in type.Columns)
335  {
336  if (col.Properties.Count > 0)
337  result[col.Name] = col.Properties.ToList();
338  }
339  return result;
340  }
341 
342  #endregion
343 
344  #region IShardKeyExtractor Implementation
345 
350  {
351  var shardKeyIndices = _type.ShardKeyIndices;
352  if (shardKeyIndices.Count == 0)
353  return ShardKeyValues.Empty;
354 
355  var values = new (string Name, ShardKeyValue Value)[shardKeyIndices.Count];
356 
357  for (int i = 0; i < shardKeyIndices.Count; i++)
358  {
359  var idx = shardKeyIndices[i];
360  var col = _type.GetColumn(idx)!;
361  var recordValue = _values[idx];
362 
363  var shardValue = ConvertToShardKeyValue(recordValue, col.ColumnType);
364  values[i] = (col.Name, shardValue);
365  }
366 
367  return new ShardKeyValues(values);
368  }
369 
370  private static ShardKeyValue ConvertToShardKeyValue(RecordValue value, ColumnType columnType)
371  {
372  if (value.IsNull)
373  return ShardKeyValue.Null();
374 
375  return columnType switch
376  {
377  ColumnType.Integer or ColumnType.Int8 or ColumnType.Int16 or ColumnType.Boolean
378  => ShardKeyValue.Int(value.AsInt() ?? 0),
379 
380  ColumnType.Long
381  => ShardKeyValue.Long(value.AsLong() ?? 0),
382 
383  ColumnType.Timestamp
384  => ShardKeyValue.Timestamp(value.AsLong() ?? 0),
385 
386  ColumnType.Float
387  => ShardKeyValue.Float(value.AsFloat() ?? 0),
388 
389  ColumnType.Double
390  => ShardKeyValue.Double(value.AsDouble() ?? 0),
391 
392  ColumnType.Date
393  => ShardKeyValue.Date(value.AsString() ?? ""),
394 
395  ColumnType.DateTime
396  => ShardKeyValue.DateTime(value.AsString() ?? ""),
397 
398  ColumnType.Time
399  => ShardKeyValue.Time(value.AsString() ?? ""),
400 
401  ColumnType.Ipv4
402  => ShardKeyValue.Ipv4(value.AsString() ?? ""),
403 
404  ColumnType.Decimal
405  => ShardKeyValue.Decimal(value.AsString() ?? ""),
406 
407  ColumnType.Uuid
408  => ShardKeyValue.Uuid(value.AsString() ?? ""),
409 
410  _ => ShardKeyValue.String(value.AsString() ?? "")
411  };
412  }
413 
414  #endregion
415 
416  #region Utility Methods
417 
422  {
423  var copy = new GenericRecord(_type);
424  Array.Copy(_values, copy._values, _values.Length);
425  return copy;
426  }
427 
431  public void Clear()
432  {
433  for (int i = 0; i < _values.Length; i++)
434  _values[i] = RecordValue.Null();
435  }
436 
440  internal RecordValue[] GetValues() => _values;
441 
445  public object?[] ToObjectArray()
446  {
447  var result = new object?[_values.Length];
448  for (int i = 0; i < _values.Length; i++)
449  {
450  var value = _values[i];
451  result[i] = value.Type switch
452  {
453  RecordValueType.Null => null,
454  RecordValueType.Int => value.AsInt(),
455  RecordValueType.Long => value.AsLong(),
456  RecordValueType.Float => value.AsFloat(),
457  RecordValueType.Double => value.AsDouble(),
458  RecordValueType.String => value.AsString(),
459  RecordValueType.Bytes => value.AsBytes(),
460  _ => null
461  };
462  }
463  return result;
464  }
465 
466  #endregion
467 
468  public override string ToString()
469  {
470  var sb = new StringBuilder();
471  sb.Append(_type.Label);
472  sb.Append(" { ");
473 
474  for (int i = 0; i < _type.ColumnCount; i++)
475  {
476  if (i > 0) sb.Append(", ");
477  var col = _type.GetColumn(i)!;
478  sb.Append(col.Name);
479  sb.Append(": ");
480  sb.Append(_values[i]);
481  }
482 
483  sb.Append(" }");
484  return sb.ToString();
485  }
486  }
string? AsString()
Gets the value as a string.
Definition: RecordValue.cs:156
static ShardKeyValue Int(int value)
Creates a 32-bit integer shard key value.
int ColumnCount
Gets the number of columns.
Definition: Type.cs:286
static ShardKeyValue Float(float value)
Creates a 32-bit float shard key value.
bool Put(string name, float value)
Sets a float value by column name.
bool PutNull(string name)
Sets a null value by column name.
int? GetInt(string name)
Gets an integer value by column name.
byte? [] GetBytes(string name)
Gets a bytes value by column name.
string? GetString(int index)
Gets a string value by column index.
float? GetFloat(int index)
Gets a float value by column index.
override string ToString()
static RecordValue Null()
Creates a null record value.
string? GetString(string name)
Gets a string value by column name.
long? GetLong(string name)
Gets a long value by column name.
bool Put(string name, string? value)
Sets a string value by column name.
static RecordValue Long(long value)
Creates a 64-bit integer record value.
Combined interface for records that support bulk insertion with shard-aware routing.
bool PutNull(int index)
Sets a null value by column index.
static ShardKeyValue Int16(short value)
Creates a 16-bit integer shard key value.
ShardKeyValues GetShardKeyValues()
Returns shard key column names and their typed values.
bool Put(int index, int value)
Sets an integer value by column index.
bool Put(string name, double value)
Sets a double value by column name.
GenericRecord(Type type)
Creates a new GenericRecord for the given type.
long? AsLong()
Gets the value as a 64-bit integer.
Definition: RecordValue.cs:118
static ShardKeyValue Uuid(string value)
Creates a UUID shard key value (standard format).
bool Put(int index, double value)
Sets a double value by column index.
static ShardKeyValue Time(string value)
Creates a time shard key value (HH:MM:SS.mmm format).
static RecordValue Boolean(bool value)
Creates a boolean record value (stored as int).
bool IsNull
Returns true if this value is null.
Definition: RecordValue.cs:53
int? AsInt()
Gets the value as a 32-bit integer.
Definition: RecordValue.cs:106
int? GetInt(int index)
Gets an integer value by column index.
RecordValue? Get(string name)
Gets the raw RecordValue by column name.
static ShardKeyValue Double(double value)
Creates a 64-bit double shard key value.
bool? GetBool(string name)
Gets a boolean value by column name.
bool Put(int index, string? value)
Sets a string value by column index.
static RecordValue Float(float value)
Creates a 32-bit float record value.
static ShardKeyValue Ipv4(string value)
Creates an IPv4 shard key value (dotted-quad format).
static ShardKeyValue Null()
Creates a null shard key value.
Collection of shard key column names and values.
static ShardKeyValue Timestamp(long value)
Creates a timestamp shard key value (milliseconds since Unix epoch).
bool Put(int index, float value)
Sets a float value by column index.
static ShardKeyValue Decimal(string value)
Creates a decimal shard key value (string representation).
bool? GetBool(int index)
Gets a boolean value by column index.
static RecordValue String(string value)
Creates a string record value.
static ShardKeyValues Empty
Creates an empty ShardKeyValues collection.
bool Put(string name, RecordValue value)
Sets a RecordValue by column name.
static ShardKeyValue Int8(sbyte value)
Creates an 8-bit integer shard key value.
bool Put(int index, byte[]? value)
Sets a bytes value by column index.
void Clear()
Resets all values to null.
static ShardKeyValue DateTime(string value)
Creates a datetime shard key value (YYYY-MM-DD HH:MM:SS.mmm format).
string GetAvroSchema()
Returns the Avro schema as a JSON string for this record type.
object? [] ToObjectArray()
Gets all values as objects (for Avro serialization).
RecordValue? Get(int index)
Gets the raw RecordValue at the specified index.
static ShardKeyValue Long(long value)
Creates a 64-bit integer shard key value.
bool Put(int index, bool value)
Sets a boolean value by column index.
static RecordValue Bytes(byte[] value)
Creates a bytes record value.
double? GetDouble(string name)
Gets a double value by column name.
bool Put(int index, RecordValue value)
Sets a RecordValue by column index.
static ShardKeyValue Date(string value)
Creates a date shard key value (YYYY-MM-DD format).
Type RecordType
Gets the Type definition for this record.
long? GetLong(int index)
Gets a long value by column index.
double? AsDouble()
Gets the value as a 64-bit double.
Definition: RecordValue.cs:143
Immutable collection of metadata about a Kinetica type.
Definition: Type.cs:36
byte? [] GetBytes(int index)
Gets a bytes value by column index.
static ShardKeyValue String(string value)
Creates a string shard key value.
bool Put(int index, long value)
Sets a long value by column index.
A typed value for shard key computation.
A generic record that can hold values for any Kinetica type.
GenericRecord Clone()
Creates a copy of this record.
bool Put(string name, bool value)
Sets a boolean value by column name.
bool Put(string name, byte[]? value)
Sets a bytes value by column name.
RecordValueType Type
Gets the type of this record value.
Definition: RecordValue.cs:50
static RecordValue Int(int value)
Creates a 32-bit integer record value.
float? GetFloat(string name)
Gets a float value by column name.
RecordValueType
Represents the type of a record value.
Definition: RecordValue.cs:10
bool Put(string name, long value)
Sets a long value by column name.
float? AsFloat()
Gets the value as a 32-bit float.
Definition: RecordValue.cs:131
static RecordValue Double(double value)
Creates a 64-bit double record value.
double? GetDouble(int index)
Gets a double value by column index.
static ShardKeyValue Boolean(bool value)
Creates a boolean shard key value.
A typed value that can be stored in a GenericRecord.
Definition: RecordValue.cs:34
bool Put(string name, int value)
Sets an integer value by column name.
static IReadOnlyDictionary< string, IList< string > > GetTypeProperties(Type type)
Returns type properties for this record type.