19 using System.Collections.Generic;
30 public static long Empty64 = -4513414715797952619;
39 IDictionary<string, string> env =
new Dictionary<string, string>();
40 return Build(env, s,
new StringBuilder()).ToString();
77 long fp = Fingerprint64(data);
78 byte[] result =
new byte[8];
79 for (
int i = 0; i < 8; i++)
81 result[i] = (byte) fp;
86 var md5 = System.Security.Cryptography.MD5.Create();
87 return md5.ComputeHash(data);
89 var sha256 = System.Security.Cryptography.SHA256.Create();
90 return sha256.ComputeHash(data);
92 throw new ArgumentException(
string.Format(
"Unsupported fingerprint computation algorithm ({0})", fpName));
114 return Fingerprint64(Encoding.UTF8.GetBytes(
ToParsingForm(s)));
122 private static long Fingerprint64(
byte[] data)
125 foreach (var b
in data)
127 result = ((long)(((ulong)result) >> 8)) ^ Fp64.FpTable[(int) (result ^ b) & 0xff];
132 private static StringBuilder Build(IDictionary<string, string> env, Schema s, StringBuilder o)
134 bool firstTime =
true;
135 Schema.Type st = s.Tag;
138 case Schema.Type.Union:
139 UnionSchema us = s as UnionSchema;
141 foreach(Schema b
in us.Schemas)
153 return o.Append(
']');
155 case Schema.Type.Array:
156 case Schema.Type.Map:
157 o.Append(
"{\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append(
"\"");
158 if (st == Schema.Type.Array)
160 ArraySchema arraySchema = s as ArraySchema;
161 Build(env, arraySchema.ItemSchema, o.Append(
",\"items\":"));
165 MapSchema mapSchema = s as MapSchema;
166 Build(env, mapSchema.ValueSchema, o.Append(
",\"values\":"));
168 return o.Append(
"}");
170 case Schema.Type.Enumeration:
171 case Schema.Type.Fixed:
172 case Schema.Type.Record:
173 NamedSchema namedSchema = s as NamedSchema;
174 var name = namedSchema.Fullname;
175 if (env.ContainsKey(name))
177 return o.Append(env[name]);
179 var qname =
"\"" + name +
"\"";
180 env.Add(name, qname);
181 o.Append(
"{\"name\":").Append(qname);
182 o.Append(
",\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append(
"\"");
183 if (st == Schema.Type.Enumeration)
185 EnumSchema enumSchema = s as EnumSchema;
186 o.Append(
",\"symbols\":[");
187 foreach (var enumSymbol
in enumSchema.Symbols)
197 o.Append(
"\"").Append(enumSymbol).Append(
"\"");
201 else if (st == Schema.Type.Fixed)
203 FixedSchema fixedSchema = s as FixedSchema;
204 o.Append(
",\"size\":").Append(fixedSchema.Size.ToString());
208 RecordSchema recordSchema = s as RecordSchema;
209 o.Append(
",\"fields\":[");
210 foreach (var field
in recordSchema.Fields)
220 o.Append(
"{\"name\":\"").Append(field.Name).Append(
"\"");
221 Build(env, field.Schema, o.Append(
",\"type\":")).Append(
"}");
225 return o.Append(
"}");
228 return o.Append(
"\"").Append(s.Name).Append(
"\"");
232 private static class Fp64
234 private static readonly
long[] fpTable =
new long[256];
236 public static long[] FpTable
238 get {
return fpTable; }
243 for (
int i = 0; i < 256; i++)
246 for (
int j = 0; j < 8; j++)
248 long mask = -(fp & 1L);
249 fp = ((long) (((ulong) fp) >> 1)) ^ (
Empty64 & mask);
static long ParsingFingerprint64(Schema s)
Returns Fingerprint64(byte[]) applied to the parsing canonical form of the supplied schema.
Base class for all schema types
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
static string ToParsingForm(Schema s)
Parses a schema into the canonical form as defined by Avro spec.
Collection of static methods for generating the cannonical form of schemas.
static byte [] ParsingFingerprint(string fpName, Schema s)
Returns Fingerprint(string, byte[]) applied to the parsing canonical form of the supplied schema.
static byte [] Fingerprint(string fpName, byte[] data)