Kinetica   C#   API  Version 7.2.3.0
GenericDatumWriter.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
21 
22 namespace Avro.Generic
23 {
29  {
30  public GenericDatumWriter( Schema schema ) : base(schema, new GenericArrayAccess(), new DictionaryMapAccess())
31  {
32  }
33 
34  protected override void WriteRecordFields(object recordObj, RecordFieldWriter[] writers, Encoder encoder)
35  {
36  var record = (GenericRecord) recordObj;
37  foreach (var writer in writers)
38  {
39  writer.WriteField(record[writer.Field.Name], encoder);
40  }
41  }
42 
43  protected override void EnsureRecordObject( RecordSchema recordSchema, object value )
44  {
45  if( value == null || !( value is GenericRecord ) || !( ( value as GenericRecord ).Schema.Equals( recordSchema ) ) )
46  {
47  throw TypeMismatch( value, "record", "GenericRecord" );
48  }
49  }
50 
51  protected override void WriteField(object record, string fieldName, int fieldPos, WriteItem writer, Encoder encoder)
52  {
53  writer(((GenericRecord)record)[fieldName], encoder);
54  }
55 
56  protected override WriteItem ResolveEnum(EnumSchema es)
57  {
58  return (v,e) =>
59  {
60  if( v == null || !(v is GenericEnum) || !((v as GenericEnum).Schema.Equals(es)))
61  throw TypeMismatch(v, "enum", "GenericEnum");
62  e.WriteEnum(es.Ordinal((v as GenericEnum ).Value));
63  };
64  }
65 
66  protected override void WriteFixed( FixedSchema es, object value, Encoder encoder )
67  {
68  if (value == null || !(value is GenericFixed) || !(value as GenericFixed).Schema.Equals(es))
69  {
70  throw TypeMismatch(value, "fixed", "GenericFixed");
71  }
72  GenericFixed ba = (GenericFixed)value;
73  encoder.WriteFixed(ba.Value);
74  }
75 
76  /*
77  * FIXME: This method of determining the Union branch has problems. If the data is IDictionary<string, object>
78  * if there are two branches one with record schema and the other with map, it choose the first one. Similarly if
79  * the data is byte[] and there are fixed and bytes schemas as branches, it choose the first one that matches.
80  * Also it does not recognize the arrays of primitive types.
81  */
82  protected override bool UnionBranchMatches(Schema sc, object obj)
83  {
84  if (obj == null && sc.Tag != Avro.Schema.Type.Null) return false;
85  switch (sc.Tag)
86  {
87  case Schema.Type.Null:
88  return obj == null;
89  case Schema.Type.Boolean:
90  return obj is bool;
91  case Schema.Type.Int:
92  return obj is int;
93  case Schema.Type.Long:
94  return obj is long;
95  case Schema.Type.Float:
96  return obj is float;
97  case Schema.Type.Double:
98  return obj is double;
99  case Schema.Type.Bytes:
100  return obj is byte[];
101  case Schema.Type.String:
102  return obj is string;
103  case Schema.Type.Error:
104  case Schema.Type.Record:
105  //return obj is GenericRecord && (obj as GenericRecord).Schema.Equals(s);
106  return obj is GenericRecord && (obj as GenericRecord).Schema.SchemaName.Equals((sc as RecordSchema).SchemaName);
107  case Schema.Type.Enumeration:
108  //return obj is GenericEnum && (obj as GenericEnum).Schema.Equals(s);
109  return obj is GenericEnum && (obj as GenericEnum).Schema.SchemaName.Equals((sc as EnumSchema).SchemaName);
110  case Schema.Type.Array:
111  return obj is Array && !(obj is byte[]);
112  case Schema.Type.Map:
113  return obj is IDictionary<string, object>;
114  case Schema.Type.Union:
115  return false; // Union directly within another union not allowed!
116  case Schema.Type.Fixed:
117  //return obj is GenericFixed && (obj as GenericFixed).Schema.Equals(s);
118  return obj is GenericFixed && (obj as GenericFixed).Schema.SchemaName.Equals((sc as FixedSchema).SchemaName);
119  default:
120  throw new AvroException("Unknown schema type: " + sc.Tag);
121  }
122  }
123 
124  private class GenericArrayAccess : ArrayAccess
125  {
126  public void EnsureArrayObject( object value )
127  {
128  if( value == null || !( value is Array ) ) throw TypeMismatch( value, "array", "Array" );
129  }
130 
131  public long GetArrayLength( object value )
132  {
133  return ( (Array) value ).Length;
134  }
135 
136  public void WriteArrayValues(object array, WriteItem valueWriter, Encoder encoder)
137  {
138  var arrayInstance = (Array) array;
139  for(int i = 0; i < arrayInstance.Length; i++)
140  {
141  encoder.StartItem();
142  valueWriter(arrayInstance.GetValue(i), encoder);
143  }
144  }
145  }
146  }
147 }
override WriteItem ResolveEnum(EnumSchema es)
Serializes an enumeration.
Class for record schemas
Definition: RecordSchema.cs:31
The defualt class to hold values for enum schema in GenericReader and GenericWriter.
Definition: GenericEnum.cs:27
Class for enum type schemas
Definition: EnumSchema.cs:28
override void WriteRecordFields(object recordObj, RecordFieldWriter[] writers, Encoder encoder)
Base class for all schema types
Definition: Schema.cs:29
Avro.IO.Encoder Encoder
Class for fixed schemas
Definition: FixedSchema.cs:28
override void EnsureRecordObject(RecordSchema recordSchema, object value)
Type
Enum for schema types
Definition: Schema.cs:34
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
int Ordinal(string symbol)
Returns the position of the given symbol within this enum.
Definition: EnumSchema.cs:116
The default type used by GenericReader and GenericWriter for RecordSchema.
PreresolvingDatumWriter for writing data from GenericRecords or primitive types.
override void WriteField(object record, string fieldName, int fieldPos, WriteItem writer, Encoder encoder)
Extracts the field value from the given object.
override void WriteFixed(FixedSchema es, object value, Encoder encoder)
Serialized a fixed object.
A general purpose writer of data from avro streams.
Type Tag
Schema type property
Definition: Schema.cs:56
static AvroException TypeMismatch(object obj, string schemaType, string type)
delegate void WriteItem(Object value, Encoder encoder)
The default type used by GenericReader and GenericWriter for objects for FixedSchema
Definition: GenericFixed.cs:28
override bool UnionBranchMatches(Schema sc, object obj)