Kinetica   C#   API  Version 7.2.3.0
GenericRecord.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using Avro;
23 
24 namespace Avro.Generic
25 {
29  public class GenericRecord
30  {
31  public RecordSchema Schema { get; private set; }
32 
33  private IDictionary<string, object> contents = new Dictionary<string, object>();
34  public GenericRecord(RecordSchema schema)
35  {
36  this.Schema = schema;
37  }
38 
39  public object this[string fieldName]
40  {
41  get { return contents[fieldName]; }
42  }
43 
44  public void Add(string fieldName, object fieldValue)
45  {
46  if (Schema.Contains(fieldName))
47  {
48  // TODO: Use a matcher to verify that object has the right type for the field.
49  //contents.Add(fieldName, fieldValue);
50  contents[fieldName] = fieldValue;
51  return;
52  }
53  throw new AvroException("No such field: " + fieldName);
54  }
55 
56  public bool TryGetValue(string fieldName, out object result)
57  {
58  return contents.TryGetValue(fieldName, out result);
59  }
60 
61  public override bool Equals(object obj)
62  {
63  if (this == obj) return true;
64  if (obj != null && obj is GenericRecord)
65  {
66  GenericRecord other = obj as GenericRecord;
67  return Schema.Equals(other.Schema) && areEqual(contents, other.contents);
68  }
69  return false;
70  }
71 
72  private static bool areEqual(IDictionary<string, object> d1, IDictionary<string, object> d2)
73  {
74  if (d1.Count == d2.Count)
75  {
76  foreach (KeyValuePair<string, object> kv in d1)
77  {
78  object o;
79  if (!d2.TryGetValue(kv.Key, out o)) return false;
80  if (!areEqual(o, kv.Value)) return false;
81  }
82  return true;
83  }
84  return false;
85  }
86 
87  private static bool areEqual(object o1, object o2)
88  {
89  if (o1 == null) return o2 == null;
90  if (o2 == null) return false;
91  if (o1 is Array)
92  {
93  if (!(o2 is Array)) return false;
94  return areEqual(o1 as Array, o1 as Array);
95  }
96  else if (o1 is IDictionary<string, object>)
97  {
98  if (!(o2 is IDictionary<string, object>)) return false;
99  return areEqual(o1 as IDictionary<string, object>, o1 as IDictionary<string, object>);
100  }
101  return o1.Equals(o2);
102  }
103 
104  private static bool areEqual(Array a1, Array a2)
105  {
106  if (a1.Length != a2.Length) return false;
107  for (int i = 0; i < a1.Length; i++)
108  {
109  if (!areEqual(a1.GetValue(i), a2.GetValue(i))) return false;
110  }
111  return true;
112  }
113 
114  public override int GetHashCode()
115  {
116  return 31 * contents.GetHashCode()/* + 29 * Schema.GetHashCode()*/;
117  }
118 
119  public override string ToString()
120  {
121  StringBuilder sb = new StringBuilder();
122  sb.Append("Schema: ");
123  sb.Append(Schema);
124  sb.Append(", contents: ");
125  sb.Append("{ ");
126  foreach (KeyValuePair<string, object> kv in contents)
127  {
128  sb.Append(kv.Key);
129  sb.Append(": ");
130  sb.Append(kv.Value);
131  sb.Append(", ");
132  }
133  sb.Append("}");
134  return sb.ToString();
135  }
136  }
137 }
Class for record schemas
Definition: RecordSchema.cs:31
void Add(string fieldName, object fieldValue)
override bool Equals(object obj)
Base class for all schema types
Definition: Schema.cs:29
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
GenericRecord(RecordSchema schema)
The default type used by GenericReader and GenericWriter for RecordSchema.
override string ToString()
bool TryGetValue(string fieldName, out object result)