/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json; namespace Avro { /// /// Class for union schemas /// public class UnionSchema : UnnamedSchema { /// /// List of schemas in the union /// public IList Schemas { get; private set; } /// /// Count of schemas in the union /// public int Count { get { return Schemas.Count; } } /// /// Static function to return instance of the union schema /// /// JSON object for the union schema /// dictionary that provides access to custom properties /// list of named schemas already read /// enclosing namespace of the schema /// new UnionSchema object internal static UnionSchema NewInstance(JArray jarr, PropertyMap props, SchemaNames names, string encspace) { List schemas = new List(); IDictionary uniqueSchemas = new Dictionary(); foreach (JToken jvalue in jarr) { Schema unionType = Schema.ParseJson(jvalue, names, encspace); if (null == unionType) throw new SchemaParseException($"Invalid JSON in union {jvalue.ToString()} at '{jvalue.Path}'"); string name = unionType.Fullname; if (uniqueSchemas.ContainsKey(name)) throw new SchemaParseException($"Duplicate type in union: {name} at '{jvalue.Path}'"); uniqueSchemas.Add(name, name); schemas.Add(unionType); } return new UnionSchema(schemas, props); } /// /// Contructor for union schema /// /// /// dictionary that provides access to custom properties private UnionSchema(List schemas, PropertyMap props) : base(Type.Union, props) { if (schemas == null) throw new ArgumentNullException(nameof(schemas)); this.Schemas = schemas; } /// /// Returns the schema at the given branch. /// /// Index to the branch, starting with 0. /// The branch corresponding to the given index. public Schema this[int index] { get { return Schemas[index]; } } /// /// Writes union schema in JSON format /// /// JSON writer /// list of named schemas already written /// enclosing namespace of the schema protected internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace) { writer.WriteStartArray(); foreach (Schema schema in this.Schemas) schema.WriteJson(writer, names, encspace); writer.WriteEndArray(); } /// /// Returns the index of a branch that can read the data written by the given schema s. /// /// The schema to match the branches against. /// The index of the matching branch. If non matches a -1 is returned. public int MatchingBranch(Schema s) { if (s is UnionSchema) throw new AvroException("Cannot find a match against union schema"); // Try exact match. //for (int i = 0; i < Count; i++) if (Schemas[i].Equals(s)) return i; // removed this for performance's sake for (int i = 0; i < Count; i++) if (Schemas[i].CanRead(s)) return i; return -1; } /// /// Checks if this schema can read data written by the given schema. Used for decoding data. /// /// writer schema /// true if this and writer schema are compatible based on the AVRO specification, false otherwise public override bool CanRead(Schema writerSchema) { return writerSchema.Tag == Schema.Type.Union || MatchingBranch(writerSchema) >= 0; } /// /// Compares two union schema objects /// /// union schema object to compare against this schema /// true if objects are equal, false otherwise public override bool Equals(object obj) { if (obj == this) return true; if (obj != null && obj is UnionSchema) { UnionSchema that = obj as UnionSchema; if (that.Count == Count) { for (int i = 0; i < Count; i++) if (!that[i].Equals(this[i])) return false; return areEqual(that.Props, this.Props); } } return false; } /// /// Hash code function /// /// public override int GetHashCode() { int result = 53; foreach (Schema schema in Schemas) result += 89 * schema.GetHashCode(); result += getHashCode(Props); return result; } } }