Kinetica   C#   API  Version 7.2.3.0
UnionSchema.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 using Newtonsoft.Json.Linq;
22 using Newtonsoft.Json;
23 
24 namespace Avro
25 {
29  public class UnionSchema : UnnamedSchema
30  {
34  public IList<Schema> Schemas { get; private set; }
35 
39  public int Count { get { return Schemas.Count; } }
40 
48  internal static UnionSchema NewInstance(JArray jarr, PropertyMap props, SchemaNames names, string encspace)
49  {
50  List<Schema> schemas = new List<Schema>();
51  IDictionary<string, string> uniqueSchemas = new Dictionary<string, string>();
52 
53  foreach (JToken jvalue in jarr)
54  {
55  Schema unionType = Schema.ParseJson(jvalue, names, encspace);
56  if (null == unionType)
57  throw new SchemaParseException("Invalid JSON in union" + jvalue.ToString());
58 
59  string name = unionType.Name;
60  if (uniqueSchemas.ContainsKey(name))
61  throw new SchemaParseException("Duplicate type in union: " + name);
62 
63  uniqueSchemas.Add(name, name);
64  schemas.Add(unionType);
65  }
66 
67  return new UnionSchema(schemas, props);
68  }
69 
74  private UnionSchema(List<Schema> schemas, PropertyMap props) : base(Type.Union, props)
75  {
76  if (schemas == null)
77  throw new ArgumentNullException("schemas");
78  this.Schemas = schemas;
79  }
80 
86  public Schema this[int index]
87  {
88  get
89  {
90  return Schemas[index];
91  }
92  }
93 
100  protected internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
101  {
102  writer.WriteStartArray();
103  foreach (Schema schema in this.Schemas)
104  schema.WriteJson(writer, names, encspace);
105  writer.WriteEndArray();
106  }
107 
113  public int MatchingBranch(Schema s)
114  {
115  if (s is UnionSchema) throw new AvroException("Cannot find a match against union schema");
116  // Try exact match.
117  //for (int i = 0; i < Count; i++) if (Schemas[i].Equals(s)) return i; // removed this for performance's sake
118  for (int i = 0; i < Count; i++) if (Schemas[i].CanRead(s)) return i;
119  return -1;
120  }
121 
127  public override bool CanRead(Schema writerSchema)
128  {
129  return writerSchema.Tag == Schema.Type.Union || MatchingBranch(writerSchema) >= 0;
130  }
131 
137  public override bool Equals(object obj)
138  {
139  if (obj == this) return true;
140  if (obj != null && obj is UnionSchema)
141  {
142  UnionSchema that = obj as UnionSchema;
143  if (that.Count == Count)
144  {
145  for (int i = 0; i < Count; i++) if (!that[i].Equals(this[i])) return false;
146  return areEqual(that.Props, this.Props);
147  }
148  }
149  return false;
150  }
151 
156  public override int GetHashCode()
157  {
158  int result = 53;
159  foreach (Schema schema in Schemas) result += 89 * schema.GetHashCode();
160  result += getHashCode(Props);
161  return result;
162  }
163  }
164 }
static int getHashCode(object obj)
Hash code helper function
Definition: Schema.cs:301
override bool CanRead(Schema writerSchema)
Checks if this schema can read data written by the given schema.
Definition: UnionSchema.cs:127
override int GetHashCode()
Hash code function
Definition: Schema.cs:272
Base class for all unnamed schemas
Base class for all schema types
Definition: Schema.cs:29
Class for union schemas
Definition: UnionSchema.cs:29
IList< Schema > Schemas
List of schemas in the union
Definition: UnionSchema.cs:34
internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes union schema in JSON format
Definition: UnionSchema.cs:100
Type
Enum for schema types
Definition: Schema.cs:34
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
Definition: Schema.cs:248
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
override bool Equals(object obj)
Compares two union schema objects
Definition: UnionSchema.cs:137
int MatchingBranch(Schema s)
Returns the index of a branch that can read the data written by the given schema s.
Definition: UnionSchema.cs:113
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
Schema(Type type, PropertyMap props)
Constructor for schema class
Definition: Schema.cs:67
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
Definition: Schema.cs:291
override int GetHashCode()
Hash code function
Definition: UnionSchema.cs:156
abstract string Name
The name of this schema.
Definition: Schema.cs:77
int Count
Count of schemas in the union
Definition: UnionSchema.cs:39
Type Tag
Schema type property
Definition: Schema.cs:56