Kinetica   C#   API  Version 7.2.3.0
SpecificWriter.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using Avro;
22 using Avro.IO;
23 using Avro.Generic;
24 
25 namespace Avro.Specific
26 {
31  public class SpecificWriter<T> : GenericWriter<T>
32  {
33  public SpecificWriter(Schema schema) : base(new SpecificDefaultWriter(schema)) { }
34  public SpecificWriter(SpecificDefaultWriter writer) : base(writer) { }
35  }
36 
41  {
46  public SpecificDefaultWriter(Schema schema) : base(schema) { }
47 
55 
56  protected override void WriteRecord(RecordSchema schema, object value, Encoder encoder)
57  {
58  var rec = value as ISpecificRecord;
59  if (rec == null)
60  throw new AvroTypeException("Record object is not derived from ISpecificRecord");
61 
62  foreach (Field field in schema)
63  {
64  try
65  {
66  Write(field.Schema, rec.Get(field.Pos), encoder);
67  }
68  catch (Exception ex)
69  {
70  throw new AvroException(ex.Message + " in field " + field.Name);
71  }
72  }
73  }
74 
82  protected override void WriteFixed(FixedSchema schema, object value, Encoder encoder)
83  {
84  var fixedrec = value as SpecificFixed;
85  if (fixedrec == null)
86  throw new AvroTypeException("Fixed object is not derived from SpecificFixed");
87 
88  encoder.WriteFixed(fixedrec.Value);
89  }
90 
97  protected override void WriteEnum(EnumSchema schema, object value, Encoder encoder)
98  {
99  if (value == null)
100  throw new AvroTypeException("value is null in SpecificDefaultWriter.WriteEnum");
101 
102  encoder.WriteEnum(schema.Ordinal(value.ToString()));
103  }
104 
113  protected override void WriteArray(ArraySchema schema, object value, Encoder encoder)
114  {
115  var arr = value as System.Collections.IList;
116  if (arr == null)
117  throw new AvroTypeException("Array does not implement non-generic IList");
118 
119  long l = arr.Count;
120  encoder.WriteArrayStart();
121  encoder.SetItemCount(l);
122  for (int i = 0; i < l; i++)
123  {
124  encoder.StartItem();
125  Write(schema.ItemSchema, arr[i], encoder);
126  }
127  encoder.WriteArrayEnd();
128  }
129 
136  protected override void WriteMap(MapSchema schema, object value, Encoder encoder)
137  {
138  var map = value as System.Collections.IDictionary;
139  if (map == null)
140  throw new AvroTypeException("Map does not implement non-generic IDictionary");
141 
142  encoder.WriteArrayStart();
143  encoder.SetItemCount(map.Count);
144  foreach (System.Collections.DictionaryEntry de in map)
145  {
146  encoder.StartItem();
147  encoder.WriteString(de.Key as string);
148  Write(schema.ValueSchema, de.Value, encoder);
149  }
150  encoder.WriteMapEnd();
151  }
152 
161  protected override void WriteUnion(UnionSchema us, object value, Encoder encoder)
162  {
163  for (int i = 0; i < us.Count; i++)
164  {
165  if (Matches(us[i], value))
166  {
167  encoder.WriteUnionIndex(i);
168  Write(us[i], value, encoder);
169  return;
170  }
171  }
172  throw new AvroException("Cannot find a match for " + value.GetType() + " in " + us);
173  }
174 
175  protected override bool Matches(Schema sc, object obj)
176  {
177  if (obj == null && sc.Tag != Avro.Schema.Type.Null) return false;
178  switch (sc.Tag)
179  {
180  case Schema.Type.Null:
181  return obj == null;
182  case Schema.Type.Boolean:
183  return obj is bool;
184  case Schema.Type.Int:
185  return obj is int;
186  case Schema.Type.Long:
187  return obj is long;
188  case Schema.Type.Float:
189  return obj is float;
190  case Schema.Type.Double:
191  return obj is double;
192  case Schema.Type.Bytes:
193  return obj is byte[];
194  case Schema.Type.String:
195  return obj is string;
196  case Schema.Type.Error:
197  case Schema.Type.Record:
198  return obj is ISpecificRecord &&
199  (((obj as ISpecificRecord).Schema) as RecordSchema).SchemaName.Equals((sc as RecordSchema).SchemaName);
200  case Schema.Type.Enumeration:
201  return obj.GetType().IsEnum && (sc as EnumSchema).Symbols.Contains(obj.ToString());
202  case Schema.Type.Array:
203  return obj is System.Collections.IList;
204  case Schema.Type.Map:
205  return obj is System.Collections.IDictionary;
206  case Schema.Type.Union:
207  return false; // Union directly within another union not allowed!
208  case Schema.Type.Fixed:
209  return obj is SpecificFixed &&
210  (((obj as SpecificFixed).Schema) as FixedSchema).SchemaName.Equals((sc as FixedSchema).SchemaName);
211  default:
212  throw new AvroException("Unknown schema type: " + sc.Tag);
213  }
214  }
215  }
216 }
Class for record schemas
Definition: RecordSchema.cs:31
Schema Schema
Field type's schema
Definition: Field.cs:75
Class for fields defined in a record
Definition: Field.cs:30
override void WriteUnion(UnionSchema us, object value, Encoder encoder)
Resolves the given value against the given UnionSchema and serializes the object against the resolved...
Generic wrapper class for writing data from specific objects
Class for enum type schemas
Definition: EnumSchema.cs:28
Schema ValueSchema
Schema for map values type
Definition: MapSchema.cs:33
void WriteArrayStart()
int Pos
Position of the field within its record.
Definition: Field.cs:55
Base class for all schema types
Definition: Schema.cs:29
override void WriteRecord(RecordSchema schema, object value, Encoder encoder)
Serialized a record using the given RecordSchema.
void SetItemCount(long value)
Class for union schemas
Definition: UnionSchema.cs:29
override void WriteEnum(EnumSchema schema, object value, Encoder encoder)
Writes the given enum value into the given encoder.
readonly string Name
Name of the field.
Definition: Field.cs:45
void WriteArrayEnd()
Class for fixed schemas
Definition: FixedSchema.cs:28
A General purpose writer for serializing objects into a Stream using Avro.
Interface class for generated classes
Type
Enum for schema types
Definition: Schema.cs:34
override void WriteMap(MapSchema schema, object value, Encoder encoder)
Writes the given map into the given encoder.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
void WriteMapEnd()
override bool Matches(Schema sc, object obj)
int Ordinal(string symbol)
Returns the position of the given symbol within this enum.
Definition: EnumSchema.cs:116
Class to store schema name, namespace and enclosing namespace
Definition: SchemaName.cs:27
SpecificDefaultWriter(Schema schema)
Constructor
virtual void Write(Schema schema, object value, Encoder encoder)
Examines the schema and dispatches the actual work to one of the other methods of this class.
void WriteEnum(int value)
void WriteFixed(byte[] data)
Base class for all generated classes
Class for array type schemas
Definition: ArraySchema.cs:27
override void WriteArray(ArraySchema schema, object value, Encoder encoder)
Serialized an array.
override bool Equals(Object obj)
Compares two schema names
Definition: SchemaName.cs:114
Class for map schemas
Definition: MapSchema.cs:28
SpecificWriter(SpecificDefaultWriter writer)
A typesafe wrapper around DefaultWriter.
void WriteUnionIndex(int value)
int Count
Count of schemas in the union
Definition: UnionSchema.cs:39
void WriteString(string value)
Schema ItemSchema
Schema for the array 'type' attribute
Definition: ArraySchema.cs:32
Class for writing data from any specific objects
Type Tag
Schema type property
Definition: Schema.cs:56
override void WriteFixed(FixedSchema schema, object value, Encoder encoder)
Validates that the record is a fixed record object and that the schema in the object is the same as t...