You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dk...@apache.org on 2020/05/22 12:37:02 UTC
[avro] branch master updated: AVRO-2782:Add indexed access to C#
GenericRecord
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 005a391 AVRO-2782:Add indexed access to C# GenericRecord
005a391 is described below
commit 005a39123a67b8fa81d985a63239c4f6a5096c34
Author: Yaniv ru <Ya...@gmail.com>
AuthorDate: Sat Mar 28 15:59:02 2020 +0300
AVRO-2782:Add indexed access to C# GenericRecord
---
.../src/apache/main/Generic/GenericDatumReader.cs | 2 +-
.../src/apache/main/Generic/GenericDatumWriter.cs | 4 +-
.../src/apache/main/Generic/GenericReader.cs | 4 +-
.../src/apache/main/Generic/GenericRecord.cs | 87 ++++++++++++++++++----
.../src/apache/main/Generic/GenericWriter.cs | 2 +-
.../csharp/src/apache/test/Generic/GenericTests.cs | 2 +-
6 files changed, 81 insertions(+), 20 deletions(-)
diff --git a/lang/csharp/src/apache/main/Generic/GenericDatumReader.cs b/lang/csharp/src/apache/main/Generic/GenericDatumReader.cs
index da33afd..76a95b9 100644
--- a/lang/csharp/src/apache/main/Generic/GenericDatumReader.cs
+++ b/lang/csharp/src/apache/main/Generic/GenericDatumReader.cs
@@ -131,7 +131,7 @@ namespace Avro.Generic
public object GetField(object record, string fieldName, int fieldPos)
{
object result;
- if(!((GenericRecord)record).TryGetValue(fieldName, out result))
+ if(!((GenericRecord)record).TryGetValue(fieldPos, out result))
{
return null;
}
diff --git a/lang/csharp/src/apache/main/Generic/GenericDatumWriter.cs b/lang/csharp/src/apache/main/Generic/GenericDatumWriter.cs
index eb8c81f..9e1ccd5 100644
--- a/lang/csharp/src/apache/main/Generic/GenericDatumWriter.cs
+++ b/lang/csharp/src/apache/main/Generic/GenericDatumWriter.cs
@@ -42,7 +42,7 @@ namespace Avro.Generic
var record = (GenericRecord) recordObj;
foreach (var writer in writers)
{
- writer.WriteField(record[writer.Field.Name], encoder);
+ writer.WriteField(record.GetValue(writer.Field.Pos), encoder);
}
}
@@ -58,7 +58,7 @@ namespace Avro.Generic
/// <inheritdoc/>
protected override void WriteField(object record, string fieldName, int fieldPos, WriteItem writer, Encoder encoder)
{
- writer(((GenericRecord)record)[fieldName], encoder);
+ writer(((GenericRecord)record).GetValue(fieldPos), encoder);
}
/// <inheritdoc/>
diff --git a/lang/csharp/src/apache/main/Generic/GenericReader.cs b/lang/csharp/src/apache/main/Generic/GenericReader.cs
index 74a1b67..f42e572 100644
--- a/lang/csharp/src/apache/main/Generic/GenericReader.cs
+++ b/lang/csharp/src/apache/main/Generic/GenericReader.cs
@@ -337,7 +337,7 @@ namespace Avro.Generic
/// <returns>True if and only if a field with the given name is found.</returns>
protected virtual bool TryGetField(object record, string fieldName, int fieldPos, out object value)
{
- return (record as GenericRecord).TryGetValue(fieldName, out value);
+ return (record as GenericRecord).TryGetValue(fieldPos, out value);
}
/// <summary>
@@ -351,7 +351,7 @@ namespace Avro.Generic
/// <param name="fieldValue">The value to be added for the field</param>
protected virtual void AddField(object record, string fieldName, int fieldPos, object fieldValue)
{
- (record as GenericRecord).Add(fieldName, fieldValue);
+ (record as GenericRecord).Add(fieldPos, fieldValue);
}
/// <summary>
diff --git a/lang/csharp/src/apache/main/Generic/GenericRecord.cs b/lang/csharp/src/apache/main/Generic/GenericRecord.cs
index a96280f..f43484e 100644
--- a/lang/csharp/src/apache/main/Generic/GenericRecord.cs
+++ b/lang/csharp/src/apache/main/Generic/GenericRecord.cs
@@ -32,7 +32,7 @@ namespace Avro.Generic
/// </summary>
public RecordSchema Schema { get; private set; }
- private readonly Dictionary<string, object> contents = new Dictionary<string, object>();
+ private readonly object[] contents;
/// <summary>
/// Initializes a new instance of the <see cref="GenericRecord"/> class.
@@ -41,6 +41,7 @@ namespace Avro.Generic
public GenericRecord(RecordSchema schema)
{
this.Schema = schema;
+ contents = new object[schema.Fields.Count];
}
/// <summary>
@@ -56,13 +57,19 @@ namespace Avro.Generic
/// </exception>
public object this[string fieldName]
{
- get { return contents[fieldName]; }
+ get
+ {
+ if (Schema.TryGetField(fieldName, out Field field))
+ {
+ return contents[field.Pos];
+ }
+ throw new KeyNotFoundException("Key name: " + fieldName);
+ }
}
/// <summary>
/// Sets the value for a field. You may call this method multiple times with the same
- /// field name to change its value. The given field name must exist in the schema. This
- /// method does not ensure that the given field value is compatible with the field's schema.
+ /// field name to change its value. The given field name must exist in the schema.
/// </summary>
/// <param name="fieldName">Name of the field.</param>
/// <param name="fieldValue">Value of the field.</param>
@@ -71,13 +78,14 @@ namespace Avro.Generic
/// </exception>
public void Add(string fieldName, object fieldValue)
{
- if (Schema.Contains(fieldName))
+ if (Schema.TryGetField(fieldName, out Field field))
{
// TODO: Use a matcher to verify that object has the right type for the field.
//contents.Add(fieldName, fieldValue);
- contents[fieldName] = fieldValue;
+ contents[field.Pos] = fieldValue;
return;
}
+
throw new AvroException("No such field: " + fieldName);
}
@@ -91,12 +99,65 @@ namespace Avro.Generic
/// </param>
/// <returns>
/// True if the field was found in the record. This method will only return true if
- /// <see cref="Add(string, object)"/> has been called for the given field name. This method
- /// cannot be used to determine whether or not the schema has a field with a given name.
+ /// <see cref="Add(string, object)"/> has been called for the given field name.
/// </returns>
public bool TryGetValue(string fieldName, out object result)
{
- return contents.TryGetValue(fieldName, out result);
+ if (!Schema.TryGetField(fieldName, out Field field))
+ {
+ result = null;
+ return false;
+ }
+
+ result = contents[field.Pos];
+ return true;
+ }
+
+ /// <summary>
+ /// Returns the value of the field with the given position.
+ /// </summary>
+ /// <param name="fieldPos">The position of the field.</param>
+ /// <returns>Value of the field with the given position.</returns>
+ /// <exception cref="IndexOutOfRangeException">
+ /// <paramref name="fieldPos"/>
+ /// </exception>
+ public object GetValue(int fieldPos)
+ {
+ return contents[fieldPos];
+ }
+
+
+ /// <summary>
+ /// Adds the value in the specified field position.
+ /// </summary>
+ /// <param name="fieldPos">Position of the field.</param>
+ /// <param name="fieldValue">The value to add.</param>
+ /// <exception cref="IndexOutOfRangeException">
+ /// <paramref name="fieldPos"/>.
+ /// </exception>
+ public void Add(int fieldPos, object fieldValue) => contents[fieldPos] = fieldValue;
+
+ /// <summary>
+ /// Gets the value in the specified field position.
+ /// </summary>
+ /// <param name="fieldPos">Position of the field.</param>
+ /// <param name="result">
+ /// When this method returns true, contains the value of the specified field;
+ /// otherwise, null.
+ /// </param>
+ /// <returns>
+ /// True if the field position is valid.
+ /// </returns>
+ public bool TryGetValue(int fieldPos, out object result)
+ {
+ if (fieldPos < contents.Length)
+ {
+ result = contents[fieldPos];
+ return true;
+ }
+
+ result = null;
+ return false;
}
/// <inheritdoc/>
@@ -111,7 +172,7 @@ namespace Avro.Generic
public bool Equals(GenericRecord other)
{
return Schema.Equals(other.Schema)
- && mapsEqual(contents, other.contents);
+ && arraysEqual(contents, other.contents);
}
private static bool mapsEqual(IDictionary d1, IDictionary d2)
@@ -171,11 +232,11 @@ namespace Avro.Generic
sb.Append(Schema);
sb.Append(", contents: ");
sb.Append("{ ");
- foreach (KeyValuePair<string, object> kv in contents)
+ foreach (var field in Schema.Fields)
{
- sb.Append(kv.Key);
+ sb.Append(field.Name);
sb.Append(": ");
- sb.Append(kv.Value);
+ sb.Append(contents[field.Pos]);
sb.Append(", ");
}
sb.Append("}");
diff --git a/lang/csharp/src/apache/main/Generic/GenericWriter.cs b/lang/csharp/src/apache/main/Generic/GenericWriter.cs
index 3c0d5a9..79ff3b2 100644
--- a/lang/csharp/src/apache/main/Generic/GenericWriter.cs
+++ b/lang/csharp/src/apache/main/Generic/GenericWriter.cs
@@ -242,7 +242,7 @@ namespace Avro.Generic
protected virtual object GetField(object value, string fieldName, int fieldPos)
{
GenericRecord d = value as GenericRecord;
- return d[fieldName];
+ return d.GetValue(fieldPos);
}
/// <summary>
diff --git a/lang/csharp/src/apache/test/Generic/GenericTests.cs b/lang/csharp/src/apache/test/Generic/GenericTests.cs
index 5d43cf7..f9817f9 100644
--- a/lang/csharp/src/apache/test/Generic/GenericTests.cs
+++ b/lang/csharp/src/apache/test/Generic/GenericTests.cs
@@ -557,7 +557,7 @@ namespace Avro.Test.Generic
gf.Value = (byte[])fieldValue;
fieldValue = gf;
}
- input.Add(fieldName, fieldValue);
+ input.Add(s[fieldName].Pos, fieldValue);
}
return input;
}