@@ -51,10 +51,9 @@ std::string StructType::ToString() const {
5151std::span<const SchemaField> StructType::fields () const { return fields_; }
5252Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldById (
5353 int32_t field_id) const {
54- ICEBERG_RETURN_UNEXPECTED (
55- LazyInitWithCallOnce (field_by_id_flag_, [this ]() { return InitFieldById (); }));
56- auto it = field_by_id_.find (field_id);
57- if (it == field_by_id_.end ()) return std::nullopt ;
54+ ICEBERG_ASSIGN_OR_RAISE (auto field_by_id, field_by_id_.Get (*this ));
55+ auto it = field_by_id.get ().find (field_id);
56+ if (it == field_by_id.get ().end ()) return std::nullopt ;
5857 return it->second ;
5958}
6059Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByIndex (
@@ -67,18 +66,17 @@ Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByInd
6766Result<std::optional<NestedType::SchemaFieldConstRef>> StructType::GetFieldByName (
6867 std::string_view name, bool case_sensitive) const {
6968 if (case_sensitive) {
70- ICEBERG_RETURN_UNEXPECTED (LazyInitWithCallOnce (
71- field_by_name_flag_, [this ]() { return InitFieldByName (); }));
72- auto it = field_by_name_.find (name);
73- if (it != field_by_name_.end ()) {
69+ ICEBERG_ASSIGN_OR_RAISE (auto field_by_name, field_by_name_.Get (*this ));
70+ auto it = field_by_name.get ().find (name);
71+ if (it != field_by_name.get ().end ()) {
7472 return it->second ;
7573 }
7674 return std::nullopt ;
7775 }
78- ICEBERG_RETURN_UNEXPECTED ( LazyInitWithCallOnce (
79- field_by_lowercase_name_flag_, [ this ]() { return InitFieldByLowerCaseName (); } ));
80- auto it = field_by_lowercase_name_ .find (StringUtils::ToLower (name));
81- if (it != field_by_lowercase_name_ .end ()) {
76+ ICEBERG_ASSIGN_OR_RAISE ( auto field_by_lowercase_name,
77+ field_by_lowercase_name_. Get (* this ));
78+ auto it = field_by_lowercase_name. get () .find (StringUtils::ToLower (name));
79+ if (it != field_by_lowercase_name. get () .end ()) {
8280 return it->second ;
8381 }
8482 return std::nullopt ;
@@ -90,47 +88,44 @@ bool StructType::Equals(const Type& other) const {
9088 const auto & struct_ = static_cast <const StructType&>(other);
9189 return fields_ == struct_.fields_ ;
9290}
93- Status StructType::InitFieldById () const {
94- if (!field_by_id_.empty ()) {
95- return {};
96- }
97- for (const auto & field : fields_) {
98- auto it = field_by_id_.try_emplace (field.field_id (), field);
91+ Result<std::unordered_map<int32_t , StructType::SchemaFieldConstRef>>
92+ StructType::InitFieldById (const StructType& self) {
93+ std::unordered_map<int32_t , SchemaFieldConstRef> field_by_id;
94+ for (const auto & field : self.fields_ ) {
95+ auto it = field_by_id.try_emplace (field.field_id (), field);
9996 if (!it.second ) {
10097 return InvalidSchema (" Duplicate field id found: {} (prev name: {}, curr name: {})" ,
10198 field.field_id (), it.first ->second .get ().name (), field.name ());
10299 }
103100 }
104- return {} ;
101+ return field_by_id ;
105102}
106- Status StructType::InitFieldByName () const {
107- if (!field_by_name_.empty ()) {
108- return {};
109- }
110- for (const auto & field : fields_) {
111- auto it = field_by_name_.try_emplace (field.name (), field);
103+ Result<std::unordered_map<std::string_view, StructType::SchemaFieldConstRef>>
104+ StructType::InitFieldByName (const StructType& self) {
105+ std::unordered_map<std::string_view, StructType::SchemaFieldConstRef> field_by_name;
106+ for (const auto & field : self.fields_ ) {
107+ auto it = field_by_name.try_emplace (field.name (), field);
112108 if (!it.second ) {
113109 return InvalidSchema (" Duplicate field name found: {} (prev id: {}, curr id: {})" ,
114110 it.first ->first , it.first ->second .get ().field_id (),
115111 field.field_id ());
116112 }
117113 }
118- return {} ;
114+ return field_by_name ;
119115}
120- Status StructType::InitFieldByLowerCaseName () const {
121- if (!field_by_lowercase_name_.empty ()) {
122- return {};
123- }
124- for (const auto & field : fields_) {
116+ Result<std::unordered_map<std::string, StructType::SchemaFieldConstRef>>
117+ StructType::InitFieldByLowerCaseName (const StructType& self) {
118+ std::unordered_map<std::string, SchemaFieldConstRef> field_by_lowercase_name;
119+ for (const auto & field : self.fields_ ) {
125120 auto it =
126- field_by_lowercase_name_ .try_emplace (StringUtils::ToLower (field.name ()), field);
121+ field_by_lowercase_name .try_emplace (StringUtils::ToLower (field.name ()), field);
127122 if (!it.second ) {
128123 return InvalidSchema (
129124 " Duplicate lowercase field name found: {} (prev id: {}, curr id: {})" ,
130125 it.first ->first , it.first ->second .get ().field_id (), field.field_id ());
131126 }
132127 }
133- return {} ;
128+ return field_by_lowercase_name ;
134129}
135130
136131ListType::ListType (SchemaField element) : element_(std::move(element)) {
0 commit comments