]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/csharp/src/Apache.Arrow/Schema.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / src / Apache.Arrow / Schema.cs
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one or more
2// contributor license agreements. See the NOTICE file distributed with
3// this work for additional information regarding copyright ownership.
4// The ASF licenses this file to You under the Apache License, Version 2.0
5// (the "License"); you may not use this file except in compliance with
6// the License. You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16using System;
17using System.Collections.Generic;
18using System.Diagnostics;
19using System.Linq;
20
21namespace Apache.Arrow
22{
23 public partial class Schema
24 {
25 public IReadOnlyDictionary<string, Field> Fields
26 {
27 get => _fieldsDictionary;
28 }
29
30 private readonly Dictionary<string, Field> _fieldsDictionary;
31
32 public IReadOnlyDictionary<string, string> Metadata { get; }
33
34 public bool HasMetadata =>
35 Metadata != null && Metadata.Count > 0;
36
37 private readonly IList<Field> _fields;
38
39 public Schema(
40 IEnumerable<Field> fields,
41 IEnumerable<KeyValuePair<string, string>> metadata)
42 {
43 if (fields == null)
44 {
45 throw new ArgumentNullException(nameof(fields));
46 }
47
48 _fields = fields.ToList();
49
50 _fieldsDictionary = fields.ToDictionary(
51 field => field.Name, field => field,
52 StringComparer.OrdinalIgnoreCase);
53
54 Metadata = metadata?.ToDictionary(kv => kv.Key, kv => kv.Value);
55 }
56
57 internal Schema(List<Field> fields, IReadOnlyDictionary<string, string> metadata, bool copyCollections)
58 {
59 Debug.Assert(fields != null);
60 Debug.Assert(copyCollections == false, "This internal constructor is to not copy the collections.");
61
62 _fields = fields;
63
64 _fieldsDictionary = fields.ToDictionary(
65 field => field.Name, field => field,
66 StringComparer.OrdinalIgnoreCase);
67
68 Metadata = metadata;
69 }
70
71 public Field GetFieldByIndex(int i)
72 {
73 return _fields[i];
74 }
75
76 public Field GetFieldByName(string name) =>
77 Fields.TryGetValue(name, out Field field) ? field : null;
78
79 public int GetFieldIndex(string name, StringComparer comparer = default)
80 {
81 if (comparer == null)
82 comparer = StringComparer.CurrentCulture;
83
84 return _fields.IndexOf(
85 _fields.Single(x => comparer.Compare(x.Name, name) == 0));
86 }
87
88 public Schema RemoveField(int fieldIndex)
89 {
90 if (fieldIndex < 0 || fieldIndex >= _fields.Count)
91 {
92 throw new ArgumentException("Invalid fieldIndex", nameof(fieldIndex));
93 }
94
95 IList<Field> fields = Utility.DeleteListElement(_fields, fieldIndex);
96
97 return new Schema(fields, Metadata);
98 }
99
100 public Schema InsertField(int fieldIndex, Field newField)
101 {
102 newField = newField ?? throw new ArgumentNullException(nameof(newField));
103 if (fieldIndex < 0 || fieldIndex > _fields.Count)
104 {
105 throw new ArgumentException(nameof(fieldIndex), $"Invalid fieldIndex {fieldIndex} passed in to Schema.AddField");
106 }
107
108 IList<Field> fields = Utility.AddListElement(_fields, fieldIndex, newField);
109
110 return new Schema(fields, Metadata);
111 }
112
113 public Schema SetField(int fieldIndex, Field newField)
114 {
115 if (fieldIndex <0 || fieldIndex >= Fields.Count)
116 {
117 throw new ArgumentException($"Invalid fieldIndex {fieldIndex} passed in to Schema.SetColumn");
118 }
119
120 IList<Field> fields = Utility.SetListElement(_fields, fieldIndex, newField);
121
122 return new Schema(fields, Metadata);
123 }
124 }
125}