@@ -64,97 +64,135 @@ public bool IsColumnActive(int col)
6464 /// An <see cref="ISchema"/> that takes all column names and types as constructor parameters.
6565 /// The columns do not have metadata.
6666 /// </summary>
67- public sealed class SimpleSchema : ISchema
67+ public abstract class SimpleSchemaBase : ISchema
6868 {
69- private readonly IExceptionContext _ectx ;
69+ protected readonly IExceptionContext Ectx ;
7070 private readonly string [ ] _names ;
71- private readonly ColumnType [ ] _types ;
72- private readonly Dictionary < string , int > _columnNameMap ;
73- private readonly MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > [ ] _keyValueGetters ;
71+ protected readonly ColumnType [ ] Types ;
72+ protected readonly Dictionary < string , int > ColumnNameMap ;
7473
75- public int ColumnCount => _types . Length ;
74+ public int ColumnCount => Types . Length ;
7675
77- public SimpleSchema ( IExceptionContext ectx , params KeyValuePair < string , ColumnType > [ ] columns )
76+ protected SimpleSchemaBase ( IExceptionContext ectx , params KeyValuePair < string , ColumnType > [ ] columns )
7877 {
7978 Contracts . CheckValueOrNull ( ectx ) ;
80- _ectx = ectx ;
81- _ectx . CheckValue ( columns , nameof ( columns ) ) ;
79+ Ectx = ectx ;
80+ Ectx . CheckValue ( columns , nameof ( columns ) ) ;
8281
8382 _names = new string [ columns . Length ] ;
84- _types = new ColumnType [ columns . Length ] ;
85- _columnNameMap = new Dictionary < string , int > ( ) ;
83+ Types = new ColumnType [ columns . Length ] ;
84+ ColumnNameMap = new Dictionary < string , int > ( ) ;
8685 for ( int i = 0 ; i < columns . Length ; i ++ )
8786 {
8887 _names [ i ] = columns [ i ] . Key ;
89- _types [ i ] = columns [ i ] . Value ;
90- if ( _columnNameMap . ContainsKey ( columns [ i ] . Key ) )
88+ Types [ i ] = columns [ i ] . Value ;
89+ if ( ColumnNameMap . ContainsKey ( columns [ i ] . Key ) )
9190 throw ectx . ExceptParam ( nameof ( columns ) , $ "Duplicate column name: '{ columns [ i ] . Key } '") ;
92- _columnNameMap [ columns [ i ] . Key ] = i ;
93- }
94- _keyValueGetters = new MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > [ ColumnCount ] ;
95- }
96-
97- public SimpleSchema ( IExceptionContext ectx , KeyValuePair < string , ColumnType > [ ] columns , Dictionary < string , MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > > keyValues )
98- : this ( ectx , columns )
99- {
100- foreach ( var kvp in keyValues )
101- {
102- var name = kvp . Key ;
103- var getter = kvp . Value ;
104- if ( ! _columnNameMap . TryGetValue ( name , out int col ) )
105- throw _ectx . ExceptParam ( nameof ( keyValues ) , $ "Output schema does not contain column '{ name } '") ;
106- if ( ! _types [ col ] . ItemType . IsKey )
107- throw _ectx . ExceptParam ( nameof ( keyValues ) , $ "Column '{ name } ' is not a key column, so it cannot have key value metadata") ;
108- _keyValueGetters [ col ] = getter ;
91+ ColumnNameMap [ columns [ i ] . Key ] = i ;
10992 }
11093 }
11194
11295 public bool TryGetColumnIndex ( string name , out int col )
11396 {
114- return _columnNameMap . TryGetValue ( name , out col ) ;
97+ return ColumnNameMap . TryGetValue ( name , out col ) ;
11598 }
11699
117100 public string GetColumnName ( int col )
118101 {
119- _ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
102+ Ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
120103 return _names [ col ] ;
121104 }
122105
123106 public ColumnType GetColumnType ( int col )
124107 {
125- _ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
126- return _types [ col ] ;
108+ Ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
109+ return Types [ col ] ;
127110 }
128111
129112 public IEnumerable < KeyValuePair < string , ColumnType > > GetMetadataTypes ( int col )
130113 {
131- _ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
114+ Ectx . Assert ( 0 <= col && col < ColumnCount ) ;
115+ return GetMetadataTypesCore ( col ) ;
116+ }
117+
118+ protected abstract IEnumerable < KeyValuePair < string , ColumnType > > GetMetadataTypesCore ( int col ) ;
119+
120+ public ColumnType GetMetadataTypeOrNull ( string kind , int col )
121+ {
122+ Ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
123+ return GetMetadataTypeOrNullCore ( kind , col ) ;
124+ }
125+
126+ protected abstract ColumnType GetMetadataTypeOrNullCore ( string kind , int col ) ;
127+
128+ public void GetMetadata < TValue > ( string kind , int col , ref TValue value )
129+ {
130+ Ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
131+ GetMetadataCore ( kind , col , ref value ) ;
132+ }
133+
134+ protected abstract void GetMetadataCore < TValue > ( string kind , int col , ref TValue value ) ;
135+ }
136+
137+ /// <summary>
138+ /// An <see cref="ISchema"/> that takes all column names and types as constructor parameters.
139+ /// The columns can optionally have text <see cref="MetadataUtils.Kinds.KeyValues"/> metadata.
140+ /// </summary>
141+ public sealed class SimpleSchema : SimpleSchemaBase
142+ {
143+ private readonly MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > [ ] _keyValueGetters ;
144+
145+ public SimpleSchema ( IExceptionContext ectx , params KeyValuePair < string , ColumnType > [ ] columns )
146+ : base ( ectx , columns )
147+ {
148+ _keyValueGetters = new MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > [ ColumnCount ] ;
149+ }
150+
151+ public SimpleSchema ( IExceptionContext ectx , KeyValuePair < string , ColumnType > [ ] columns ,
152+ Dictionary < string , MetadataUtils . MetadataGetter < VBuffer < ReadOnlyMemory < char > > > > keyValues )
153+ : this ( ectx , columns )
154+ {
155+ foreach ( var kvp in keyValues )
156+ {
157+ var name = kvp . Key ;
158+ var getter = kvp . Value ;
159+ if ( ! ColumnNameMap . TryGetValue ( name , out int col ) )
160+ throw Ectx . ExceptParam ( nameof ( keyValues ) , $ "Output schema does not contain column '{ name } '") ;
161+ if ( ! Types [ col ] . ItemType . IsKey )
162+ throw Ectx . ExceptParam ( nameof ( keyValues ) , $ "Column '{ name } ' is not a key column, so it cannot have key value metadata") ;
163+ _keyValueGetters [ col ] = getter ;
164+ }
165+ }
166+
167+ protected override IEnumerable < KeyValuePair < string , ColumnType > > GetMetadataTypesCore ( int col )
168+ {
169+ Ectx . Assert ( 0 <= col && col < ColumnCount ) ;
132170 if ( _keyValueGetters [ col ] != null )
133171 {
134- _ectx . Assert ( _types [ col ] . ItemType . IsKey ) ;
172+ Ectx . Assert ( Types [ col ] . ItemType . IsKey ) ;
135173 yield return new KeyValuePair < string , ColumnType > ( MetadataUtils . Kinds . KeyValues ,
136- new VectorType ( TextType . Instance , _types [ col ] . ItemType . KeyCount ) ) ;
174+ new VectorType ( TextType . Instance , Types [ col ] . ItemType . KeyCount ) ) ;
137175 }
138176 }
139177
140- public ColumnType GetMetadataTypeOrNull ( string kind , int col )
178+ protected override ColumnType GetMetadataTypeOrNullCore ( string kind , int col )
141179 {
142- _ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
180+ Ectx . Assert ( 0 <= col && col < ColumnCount ) ;
143181 if ( kind == MetadataUtils . Kinds . KeyValues && _keyValueGetters [ col ] != null )
144182 {
145- _ectx . Assert ( _types [ col ] . ItemType . IsKey ) ;
146- return new VectorType ( TextType . Instance , _types [ col ] . ItemType . KeyCount ) ;
183+ Ectx . Assert ( Types [ col ] . ItemType . IsKey ) ;
184+ return new VectorType ( TextType . Instance , Types [ col ] . ItemType . KeyCount ) ;
147185 }
148186 return null ;
149187 }
150188
151- public void GetMetadata < TValue > ( string kind , int col , ref TValue value )
189+ protected override void GetMetadataCore < TValue > ( string kind , int col , ref TValue value )
152190 {
153- _ectx . CheckParam ( 0 <= col && col < ColumnCount , nameof ( col ) ) ;
191+ Ectx . Assert ( 0 <= col && col < ColumnCount ) ;
154192 if ( kind == MetadataUtils . Kinds . KeyValues && _keyValueGetters [ col ] != null )
155193 _keyValueGetters [ col ] . Marshal ( col , ref value ) ;
156194 else
157- throw _ectx . ExceptGetMetadata ( ) ;
195+ throw Ectx . ExceptGetMetadata ( ) ;
158196 }
159197 }
160198}
0 commit comments