2020
2121import java .util .Map ;
2222import org .apache .iceberg .BaseMetastoreTableOperations ;
23- import org .apache .iceberg .Snapshot ;
2423import org .apache .iceberg .SnapshotRef ;
2524import org .apache .iceberg .TableMetadata ;
2625import org .apache .iceberg .TableMetadataParser ;
3332import org .projectnessie .client .http .HttpClientException ;
3433import org .projectnessie .error .NessieConflictException ;
3534import org .projectnessie .error .NessieNotFoundException ;
36- import org .projectnessie .model .Branch ;
3735import org .projectnessie .model .Content ;
3836import org .projectnessie .model .ContentKey ;
3937import org .projectnessie .model .IcebergTable ;
40- import org .projectnessie .model .ImmutableCommitMeta ;
41- import org .projectnessie .model .ImmutableIcebergTable ;
42- import org .projectnessie .model .Operation ;
4338import org .projectnessie .model .Reference ;
4439import org .slf4j .Logger ;
4540import org .slf4j .LoggerFactory ;
@@ -148,75 +143,22 @@ protected void doRefresh() {
148143
149144 @ Override
150145 protected void doCommit (TableMetadata base , TableMetadata metadata ) {
151- UpdateableReference updateableReference = client .getRef ();
152-
153- updateableReference .checkMutable ();
154-
155- Branch current = updateableReference .getAsBranch ();
156- Branch expectedHead = current ;
157- if (base != null ) {
158- String metadataCommitId = base .property (NESSIE_COMMIT_ID_PROPERTY , expectedHead .getHash ());
159- if (metadataCommitId != null ) {
160- expectedHead = Branch .of (expectedHead .getName (), metadataCommitId );
161- }
162- }
163-
164146 String newMetadataLocation =
165147 (base == null ) && (metadata .metadataFileLocation () != null )
166148 ? metadata .metadataFileLocation ()
167149 : writeNewMetadata (metadata , currentVersion () + 1 );
168150
151+ String refName = client .refName ();
169152 boolean delete = true ;
170153 try {
171- ImmutableIcebergTable .Builder newTableBuilder = ImmutableIcebergTable .builder ();
172- if (table != null ) {
173- newTableBuilder .id (table .getId ());
174- }
175- Snapshot snapshot = metadata .currentSnapshot ();
176- long snapshotId = snapshot != null ? snapshot .snapshotId () : -1L ;
177-
178- IcebergTable newTable =
179- newTableBuilder
180- .snapshotId (snapshotId )
181- .schemaId (metadata .currentSchemaId ())
182- .specId (metadata .defaultSpecId ())
183- .sortOrderId (metadata .defaultSortOrderId ())
184- .metadataLocation (newMetadataLocation )
185- .build ();
186-
187- LOG .debug (
188- "Committing '{}' against '{}', current is '{}': {}" ,
189- key ,
190- expectedHead ,
191- current .getHash (),
192- newTable );
193- ImmutableCommitMeta .Builder builder = ImmutableCommitMeta .builder ();
194- builder .message (buildCommitMsg (base , metadata ));
195- if (isSnapshotOperation (base , metadata )) {
196- builder .putProperties ("iceberg.operation" , snapshot .operation ());
197- }
198- Branch branch =
199- client
200- .getApi ()
201- .commitMultipleOperations ()
202- .operation (Operation .Put .of (key , newTable , table ))
203- .commitMeta (NessieUtil .catalogOptions (builder , catalogOptions ).build ())
204- .branch (expectedHead )
205- .commit ();
206- LOG .info (
207- "Committed '{}' against '{}', expected commit-id was '{}'" ,
208- key ,
209- branch ,
210- expectedHead .getHash ());
211- updateableReference .updateReference (branch );
212-
154+ client .commitTable (base , metadata , newMetadataLocation , table , key );
213155 delete = false ;
214156 } catch (NessieConflictException ex ) {
215157 throw new CommitFailedException (
216158 ex ,
217159 "Cannot commit: Reference hash is out of date. "
218160 + "Update the reference '%s' and try again" ,
219- updateableReference . getName () );
161+ refName );
220162 } catch (HttpClientException ex ) {
221163 // Intentionally catch all nessie-client-exceptions here and not just the "timeout" variant
222164 // to catch all kinds of network errors (e.g. connection reset). Network code implementation
@@ -226,34 +168,14 @@ protected void doCommit(TableMetadata base, TableMetadata metadata) {
226168 throw new CommitStateUnknownException (ex );
227169 } catch (NessieNotFoundException ex ) {
228170 throw new RuntimeException (
229- String .format (
230- "Cannot commit: Reference '%s' no longer exists" , updateableReference .getName ()),
231- ex );
171+ String .format ("Cannot commit: Reference '%s' no longer exists" , refName ), ex );
232172 } finally {
233173 if (delete ) {
234174 io ().deleteFile (newMetadataLocation );
235175 }
236176 }
237177 }
238178
239- private boolean isSnapshotOperation (TableMetadata base , TableMetadata metadata ) {
240- Snapshot snapshot = metadata .currentSnapshot ();
241- return snapshot != null
242- && (base == null
243- || base .currentSnapshot () == null
244- || snapshot .snapshotId () != base .currentSnapshot ().snapshotId ());
245- }
246-
247- private String buildCommitMsg (TableMetadata base , TableMetadata metadata ) {
248- if (isSnapshotOperation (base , metadata )) {
249- return String .format (
250- "Iceberg %s against %s" , metadata .currentSnapshot ().operation (), tableName ());
251- } else if (base != null && metadata .currentSchemaId () != base .currentSchemaId ()) {
252- return String .format ("Iceberg schema change against %s" , tableName ());
253- }
254- return String .format ("Iceberg commit against %s" , tableName ());
255- }
256-
257179 @ Override
258180 public FileIO io () {
259181 return fileIO ;
0 commit comments