@@ -268,7 +268,8 @@ impl Database {
268
268
}
269
269
}
270
270
271
- return Ok ( table. clone ( ) ) ;
271
+ // Use Arc::clone instead of table.clone() to avoid deep copying
272
+ return Ok ( Arc :: clone ( table) ) ;
272
273
}
273
274
274
275
// If not found and project_id is not "default", try the default table
@@ -286,7 +287,8 @@ impl Database {
286
287
}
287
288
}
288
289
289
- return Ok ( table. clone ( ) ) ;
290
+ // Use Arc::clone instead of table.clone() to avoid deep copying
291
+ return Ok ( Arc :: clone ( table) ) ;
290
292
}
291
293
}
292
294
@@ -303,15 +305,25 @@ impl Database {
303
305
configs. get ( "default" ) . ok_or_else ( || anyhow:: anyhow!( "Project ID '{}' not found" , "default" ) ) ?. clone ( )
304
306
} ;
305
307
306
- let mut table = table_ref. write ( ) . await ;
307
- let ops = DeltaOps ( table. clone ( ) ) ;
308
+ // Scope the write lock to minimize lock time
309
+ let should_checkpoint = {
310
+ let mut table = table_ref. write ( ) . await ;
308
311
309
- let write_op = ops . write ( batch ) . with_partition_columns ( OtelLogsAndSpans :: partitions ( ) ) ;
310
- * table = write_op. await ? ;
312
+ // Create the DeltaOps with a clone of the table
313
+ let write_op = DeltaOps ( table . clone ( ) ) . write ( batch ) . with_partition_columns ( OtelLogsAndSpans :: partitions ( ) ) ;
311
314
312
- // Checkpoint the table every 10 versions
313
- let version = table. version ( ) ;
314
- if version > 0 && version % 10 == 0 {
315
+ let new_table = write_op. await ?;
316
+ let version = new_table. version ( ) ;
317
+ * table = new_table;
318
+
319
+ version > 0 && version % 40 == 0
320
+ } ;
321
+
322
+ // Checkpoint outside the write lock if needed
323
+ if should_checkpoint {
324
+ // Take a read lock for checkpointing
325
+ let table = table_ref. read ( ) . await ;
326
+ let version = table. version ( ) ;
315
327
info ! ( "Checkpointing Delta table at version {}" , version) ;
316
328
checkpoints:: create_checkpoint ( & table, None ) . await ?;
317
329
}
0 commit comments