/* * 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; namespace Avro { /// /// Class for fixed schemas /// public class FixedSchema : NamedSchema { /// /// Fixed size for the bytes /// public int Size { get; set; } /// /// Static function to return new instance of the fixed schema class /// /// JSON object for the fixed schema /// dictionary that provides access to custom properties /// list of named schema already parsed in /// enclosing namespace of the fixed schema /// internal static FixedSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace) { SchemaName name = NamedSchema.GetName(jtok, encspace); var aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace); try { return new FixedSchema(name, aliases, JsonHelper.GetRequiredInteger(jtok, "size"), props, names, JsonHelper.GetOptionalString(jtok, "doc")); } catch (Exception e) { throw new SchemaParseException($"{e.Message} at '{jtok.Path}'", e); } } /// /// Constructor /// /// name of the fixed schema /// list of aliases for the name /// fixed size /// custom properties on this schema /// list of named schema already parsed in /// documentation for this named schema private FixedSchema(SchemaName name, IList aliases, int size, PropertyMap props, SchemaNames names, string doc) : base(Type.Fixed, name, aliases, props, names, doc) { if (null == name.Name) throw new SchemaParseException("name cannot be null for fixed schema."); if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size), "size must be greater than zero."); this.Size = size; } /// /// Writes the fixed schema class in JSON format /// /// JSON writer /// list of named schema already written /// enclosing namespace for the fixed schema protected internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace) { base.WriteJsonFields(writer, names, encspace); writer.WritePropertyName("size"); writer.WriteValue(this.Size); } /// /// Compares two fixed schemas /// /// fixed schema to compare against this schema /// true if two schemas are the same, false otherwise public override bool Equals(object obj) { if (obj == this) return true; if (obj != null && obj is FixedSchema) { FixedSchema that = obj as FixedSchema; return SchemaName.Equals(that.SchemaName) && Size == that.Size && areEqual(that.Props, this.Props); } return false; } /// /// Hash code function /// /// public override int GetHashCode() { return 53 * SchemaName.GetHashCode() + 47 * Size + getHashCode(Props); } /// /// 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) { if (writerSchema.Tag != Tag) return false; FixedSchema that = writerSchema as FixedSchema; if (that.Size != Size) return false; if (that.SchemaName.Equals(SchemaName)) return true; else return InAliases(that.SchemaName); } } }