3030import org .apache .commons .lang3 .StringUtils ;
3131import org .lance .namespace .LanceNamespace ;
3232import org .lance .namespace .errors .NamespaceNotFoundException ;
33+ import org .lance .namespace .errors .TableAlreadyExistsException ;
3334import org .lance .namespace .errors .TableNotFoundException ;
3435import org .lance .namespace .errors .TableVersionNotFoundException ;
3536import org .lance .namespace .model .AddColumnsEntry ;
5253import org .lance .namespace .model .ListTablesRequest ;
5354import org .lance .namespace .model .ListTablesResponse ;
5455import org .lance .namespace .model .NamespaceExistsRequest ;
56+ import org .lance .namespace .model .RegisterTableRequest ;
5557import org .lance .namespace .model .TableExistsRequest ;
5658import org .lance .namespace .model .TableVersion ;
5759
7779final class LanceNamespaceClient {
7880 private static final String DATABASE_NAMESPACE_DELIMITER = "." ;
7981 private static final int PAGE_SIZE = 1000 ;
82+ private static final String LANCE_FILESYSTEM = AbstractLanceProperties .LANCE_FILESYSTEM ;
8083 private static final String LANCE_REST = AbstractLanceProperties .LANCE_REST ;
8184
8285 private final LanceNamespace namespace ;
@@ -204,18 +207,45 @@ void createDatabase(String dbName, Map<String, String> properties) {
204207
205208 void dropDatabase (String dbName , boolean ifExists , boolean force ) {
206209 try {
207- DropNamespaceRequest request = new DropNamespaceRequest ()
208- .id (buildNamespaceId (dbName ))
209- .mode (ifExists ? "Skip" : "Fail" )
210- .behavior (force ? "Cascade" : "Restrict" );
210+ List <String > namespaceId = buildNamespaceId (dbName );
211211 synchronized (namespaceLock ) {
212- namespace .dropNamespace (request );
212+ if (force ) {
213+ try {
214+ dropNamespaceCascade (namespaceId , ifExists ? "Skip" : "Fail" );
215+ } catch (NamespaceNotFoundException e ) {
216+ if (!ifExists ) {
217+ throw e ;
218+ }
219+ }
220+ return ;
221+ }
222+ namespace .dropNamespace (new DropNamespaceRequest ()
223+ .id (namespaceId )
224+ .mode (ifExists ? "Skip" : "Fail" )
225+ .behavior ("Restrict" ));
213226 }
214227 } catch (DdlException e ) {
215228 throw new RuntimeException (e );
216229 }
217230 }
218231
232+ private void dropNamespaceCascade (List <String > namespaceId , String mode ) {
233+ for (String child : listChildNamespaces (namespaceId )) {
234+ List <String > childId = new ArrayList <>(namespaceId );
235+ childId .add (child );
236+ dropNamespaceCascade (childId , "Fail" );
237+ }
238+ for (String table : listTableNames (namespaceId )) {
239+ List <String > tableId = new ArrayList <>(namespaceId );
240+ tableId .add (table );
241+ namespace .dropTable (new DropTableRequest ().id (tableId ));
242+ }
243+ namespace .dropNamespace (new DropNamespaceRequest ()
244+ .id (namespaceId )
245+ .mode (mode )
246+ .behavior ("Restrict" ));
247+ }
248+
219249 /**
220250 * Lists all direct child namespace names under the given full Lance namespace.
221251 *
@@ -248,33 +278,36 @@ private List<String> listChildNamespaces(List<String> namespaceId) {
248278
249279 List <String > listTableNames (String dbName ) {
250280 try {
251- List <String > namespaceId = buildNamespaceId (dbName );
252- List <String > result = new ArrayList <>();
253- String pageToken = null ;
254- Set <String > consumedTokens = new HashSet <>();
255- do {
256- ListTablesRequest request = new ListTablesRequest ().id (namespaceId ).limit (PAGE_SIZE );
257- if (pageToken != null ) {
258- request .pageToken (pageToken );
259- }
260- ListTablesResponse response ;
261- synchronized (namespaceLock ) {
262- response = namespace .listTables (request );
263- }
264- if (response .getTables () != null ) {
265- result .addAll (response .getTables ());
266- }
267- pageToken = response .getPageToken ();
268- if (StringUtils .isNotEmpty (pageToken ) && !consumedTokens .add (pageToken )) {
269- throw new IllegalStateException ("Lance namespace repeated a pagination token" );
270- }
271- } while (StringUtils .isNotEmpty (pageToken ));
272- return result ;
281+ return listTableNames (buildNamespaceId (dbName ));
273282 } catch (DdlException e ) {
274283 throw new RuntimeException (e );
275284 }
276285 }
277286
287+ private List <String > listTableNames (List <String > namespaceId ) {
288+ List <String > result = new ArrayList <>();
289+ String pageToken = null ;
290+ Set <String > consumedTokens = new HashSet <>();
291+ do {
292+ ListTablesRequest request = new ListTablesRequest ().id (namespaceId ).limit (PAGE_SIZE );
293+ if (pageToken != null ) {
294+ request .pageToken (pageToken );
295+ }
296+ ListTablesResponse response ;
297+ synchronized (namespaceLock ) {
298+ response = namespace .listTables (request );
299+ }
300+ if (response .getTables () != null ) {
301+ result .addAll (response .getTables ());
302+ }
303+ pageToken = response .getPageToken ();
304+ if (StringUtils .isNotEmpty (pageToken ) && !consumedTokens .add (pageToken )) {
305+ throw new IllegalStateException ("Lance namespace repeated a pagination token" );
306+ }
307+ } while (StringUtils .isNotEmpty (pageToken ));
308+ return result ;
309+ }
310+
278311 boolean tableExists (String dbName , String tblName ) {
279312 try {
280313 List <String > tableId = buildTableId (dbName , tblName );
@@ -309,9 +342,7 @@ void createTable(String dbName, String tableName, Map<String, String> properties
309342 void dropTable (String dbName , String tableName ) {
310343 try {
311344 DropTableRequest request = new DropTableRequest ().id (buildTableId (dbName , tableName ));
312- synchronized (namespaceLock ) {
313- namespace .dropTable (request );
314- }
345+ executeTableMutation (dbName , tableName , () -> namespace .dropTable (request ));
315346 } catch (DdlException e ) {
316347 throw new RuntimeException (e );
317348 }
@@ -322,9 +353,7 @@ void addColumns(String dbName, String tableName, List<AddColumnsEntry> columns)
322353 AlterTableAddColumnsRequest request = new AlterTableAddColumnsRequest ()
323354 .id (buildTableId (dbName , tableName ))
324355 .newColumns (columns );
325- synchronized (namespaceLock ) {
326- namespace .alterTableAddColumns (request );
327- }
356+ executeTableMutation (dbName , tableName , () -> namespace .alterTableAddColumns (request ));
328357 } catch (DdlException e ) {
329358 throw new RuntimeException (e );
330359 }
@@ -335,9 +364,7 @@ void alterColumns(String dbName, String tableName, List<AlterColumnsEntry> alter
335364 AlterTableAlterColumnsRequest request = new AlterTableAlterColumnsRequest ()
336365 .id (buildTableId (dbName , tableName ))
337366 .alterations (alterations );
338- synchronized (namespaceLock ) {
339- namespace .alterTableAlterColumns (request );
340- }
367+ executeTableMutation (dbName , tableName , () -> namespace .alterTableAlterColumns (request ));
341368 } catch (DdlException e ) {
342369 throw new RuntimeException (e );
343370 }
@@ -348,14 +375,44 @@ void dropColumns(String dbName, String tableName, List<String> columns) {
348375 AlterTableDropColumnsRequest request = new AlterTableDropColumnsRequest ()
349376 .id (buildTableId (dbName , tableName ))
350377 .columns (columns );
351- synchronized (namespaceLock ) {
352- namespace .alterTableDropColumns (request );
353- }
378+ executeTableMutation (dbName , tableName , () -> namespace .alterTableDropColumns (request ));
354379 } catch (DdlException e ) {
355380 throw new RuntimeException (e );
356381 }
357382 }
358383
384+ private void executeTableMutation (String dbName , String tableName , Runnable mutation )
385+ throws DdlException {
386+ List <String > namespaceId = buildNamespaceId (dbName );
387+ List <String > tableId = new ArrayList <>(namespaceId );
388+ tableId .add (tableName );
389+ synchronized (namespaceLock ) {
390+ try {
391+ mutation .run ();
392+ return ;
393+ } catch (TableNotFoundException originalException ) {
394+ if (!LANCE_FILESYSTEM .equals (catalogType ) || !namespaceId .isEmpty ()) {
395+ throw originalException ;
396+ }
397+ try {
398+ namespace .tableExists (new TableExistsRequest ().id (tableId ));
399+ } catch (TableNotFoundException | NamespaceNotFoundException e ) {
400+ throw originalException ;
401+ }
402+ try {
403+ // DirectoryNamespace only accepts locations relative to its warehouse root.
404+ namespace .registerTable (new RegisterTableRequest ()
405+ .id (tableId )
406+ .location (tableName + ".lance" )
407+ .mode ("Create" ));
408+ } catch (TableAlreadyExistsException e ) {
409+ // Another mutation registered the external dataset first.
410+ }
411+ mutation .run ();
412+ }
413+ }
414+ }
415+
359416 LanceTableAccess resolveTableAccess (String dbName , String tableName ) {
360417 List <String > tableId = tableAccessKey (dbName , tableName );
361418 if (tableAccessTtlNanos == 0 ) {
0 commit comments