AssignFreshIds.java org::apache::iceberg::types::AssignFreshIds org::apache::iceberg::types /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.types; importjava.util.Iterator; importjava.util.List; importjava.util.function.Supplier; importorg.apache.iceberg.Schema; importorg.apache.iceberg.relocated.com.google.common.collect.Lists; classAssignFreshIdsextendsTypeUtil.CustomOrderSchemaVisitor<Type>{ privatefinalSchemavisitingSchema; privatefinalSchemabaseSchema; privatefinalTypeUtil.NextIDnextId; AssignFreshIds(TypeUtil.NextIDnextId){ this.visitingSchema=null; this.baseSchema=null; this.nextId=nextId; } AssignFreshIds(SchemavisitingSchema,SchemabaseSchema,TypeUtil.NextIDnextId){ this.visitingSchema=visitingSchema; this.baseSchema=baseSchema; this.nextId=nextId; } privateintidFor(StringfullName){ if(baseSchema!=null&&fullName!=null){ Types.NestedFieldfield=baseSchema.findField(fullName); if(field!=null){ returnfield.fieldId(); } } returnnextId.get(); } privateStringname(intid){ if(visitingSchema!=null){ returnvisitingSchema.findColumnName(id); } returnnull; } @Override publicTypeschema(Schemaschema,Supplier<Type>future){ returnfuture.get(); } @Override publicTypestruct(Types.StructTypestruct,Iterable<Type>futures){ List<Types.NestedField>fields=struct.fields(); intlength=struct.fields().size(); //assignIDsforthisstruct'sfieldsfirst List<Integer>newIds=Lists.newArrayListWithExpectedSize(length); for(inti=0;i<length;i+=1){ newIds.add(idFor(name(fields.get(i).fieldId()))); } List<Types.NestedField>newFields=Lists.newArrayListWithExpectedSize(length); Iterator<Type>types=futures.iterator(); for(inti=0;i<length;i+=1){ Types.NestedFieldfield=fields.get(i); Typetype=types.next(); if(field.isOptional()){ newFields.add(Types.NestedField.optional(newIds.get(i),field.name(),type,field.doc())); }else{ newFields.add(Types.NestedField.required(newIds.get(i),field.name(),type,field.doc())); } } returnTypes.StructType.of(newFields); } @Override publicTypefield(Types.NestedFieldfield,Supplier<Type>future){ returnfuture.get(); } @Override publicTypelist(Types.ListTypelist,Supplier<Type>future){ intnewId=idFor(name(list.elementId())); if(list.isElementOptional()){ returnTypes.ListType.ofOptional(newId,future.get()); }else{ returnTypes.ListType.ofRequired(newId,future.get()); } } @Override publicTypemap(Types.MapTypemap,Supplier<Type>keyFuture,Supplier<Type>valueFuture){ intnewKeyId=idFor(name(map.keyId())); intnewValueId=idFor(name(map.valueId())); if(map.isValueOptional()){ returnTypes.MapType.ofOptional(newKeyId,newValueId,keyFuture.get(),valueFuture.get()); }else{ returnTypes.MapType.ofRequired(newKeyId,newValueId,keyFuture.get(),valueFuture.get()); } } @Override publicTypeprimitive(Type.PrimitiveTypeprimitive){ returnprimitive; } }