44
44
import org .apache .gravitino .audit .CallerContext ;
45
45
import org .apache .gravitino .audit .FilesetAuditConstants ;
46
46
import org .apache .gravitino .audit .FilesetDataOperation ;
47
+ import org .apache .gravitino .catalog .ManagedSchemaOperations ;
47
48
import org .apache .gravitino .catalog .hadoop .fs .FileSystemProvider ;
48
49
import org .apache .gravitino .catalog .hadoop .fs .FileSystemUtils ;
49
50
import org .apache .gravitino .connector .CatalogInfo ;
50
51
import org .apache .gravitino .connector .CatalogOperations ;
51
52
import org .apache .gravitino .connector .HasPropertyMetadata ;
52
- import org .apache .gravitino .connector .SupportsSchemas ;
53
53
import org .apache .gravitino .exceptions .AlreadyExistsException ;
54
54
import org .apache .gravitino .exceptions .FilesetAlreadyExistsException ;
55
55
import org .apache .gravitino .exceptions .GravitinoRuntimeException ;
74
74
import org .slf4j .Logger ;
75
75
import org .slf4j .LoggerFactory ;
76
76
77
- public class HadoopCatalogOperations implements CatalogOperations , SupportsSchemas , FilesetCatalog {
77
+ public class HadoopCatalogOperations extends ManagedSchemaOperations
78
+ implements CatalogOperations , FilesetCatalog {
78
79
private static final String SCHEMA_DOES_NOT_EXIST_MSG = "Schema %s does not exist" ;
79
80
private static final String FILESET_DOES_NOT_EXIST_MSG = "Fileset %s does not exist" ;
80
81
private static final String SLASH = "/" ;
@@ -104,7 +105,8 @@ public HadoopCatalogOperations() {
104
105
this (GravitinoEnv .getInstance ().entityStore ());
105
106
}
106
107
107
- public EntityStore getStore () {
108
+ @ Override
109
+ public EntityStore store () {
108
110
return store ;
109
111
}
110
112
@@ -451,19 +453,6 @@ public String getFileLocation(NameIdentifier ident, String subPath)
451
453
return fileLocation ;
452
454
}
453
455
454
- @ Override
455
- public NameIdentifier [] listSchemas (Namespace namespace ) throws NoSuchCatalogException {
456
- try {
457
- List <SchemaEntity > schemas =
458
- store .list (namespace , SchemaEntity .class , Entity .EntityType .SCHEMA );
459
- return schemas .stream ()
460
- .map (s -> NameIdentifier .of (namespace , s .name ()))
461
- .toArray (NameIdentifier []::new );
462
- } catch (IOException e ) {
463
- throw new RuntimeException ("Failed to list schemas under namespace " + namespace , e );
464
- }
465
- }
466
-
467
456
@ Override
468
457
public Schema createSchema (NameIdentifier ident , String comment , Map <String , String > properties )
469
458
throws NoSuchCatalogException , SchemaAlreadyExistsException {
@@ -496,53 +485,7 @@ public Schema createSchema(NameIdentifier ident, String comment, Map<String, Str
496
485
}
497
486
}
498
487
499
- StringIdentifier stringId = StringIdentifier .fromProperties (properties );
500
- Preconditions .checkNotNull (stringId , "Property String identifier should not be null" );
501
-
502
- SchemaEntity schemaEntity =
503
- SchemaEntity .builder ()
504
- .withName (ident .name ())
505
- .withId (stringId .id ())
506
- .withNamespace (ident .namespace ())
507
- .withComment (comment )
508
- .withProperties (properties )
509
- .withAuditInfo (
510
- AuditInfo .builder ()
511
- .withCreator (PrincipalUtils .getCurrentPrincipal ().getName ())
512
- .withCreateTime (Instant .now ())
513
- .build ())
514
- .build ();
515
- try {
516
- store .put (schemaEntity , true /* overwrite */ );
517
- } catch (IOException ioe ) {
518
- throw new RuntimeException ("Failed to create schema " + ident , ioe );
519
- }
520
-
521
- return HadoopSchema .builder ()
522
- .withName (ident .name ())
523
- .withComment (comment )
524
- .withProperties (schemaEntity .properties ())
525
- .withAuditInfo (schemaEntity .auditInfo ())
526
- .build ();
527
- }
528
-
529
- @ Override
530
- public Schema loadSchema (NameIdentifier ident ) throws NoSuchSchemaException {
531
- try {
532
- SchemaEntity schemaEntity = store .get (ident , Entity .EntityType .SCHEMA , SchemaEntity .class );
533
-
534
- return HadoopSchema .builder ()
535
- .withName (ident .name ())
536
- .withComment (schemaEntity .comment ())
537
- .withProperties (schemaEntity .properties ())
538
- .withAuditInfo (schemaEntity .auditInfo ())
539
- .build ();
540
-
541
- } catch (NoSuchEntityException exception ) {
542
- throw new NoSuchSchemaException (exception , SCHEMA_DOES_NOT_EXIST_MSG , ident );
543
- } catch (IOException ioe ) {
544
- throw new RuntimeException ("Failed to load schema " + ident , ioe );
545
- }
488
+ return super .createSchema (ident , comment , properties );
546
489
}
547
490
548
491
@ Override
@@ -556,32 +499,7 @@ public Schema alterSchema(NameIdentifier ident, SchemaChange... changes)
556
499
throw new RuntimeException ("Failed to check if schema " + ident + " exists" , ioe );
557
500
}
558
501
559
- try {
560
- SchemaEntity entity =
561
- store .update (
562
- ident ,
563
- SchemaEntity .class ,
564
- Entity .EntityType .SCHEMA ,
565
- schemaEntity -> updateSchemaEntity (ident , schemaEntity , changes ));
566
-
567
- return HadoopSchema .builder ()
568
- .withName (ident .name ())
569
- .withComment (entity .comment ())
570
- .withProperties (entity .properties ())
571
- .withAuditInfo (entity .auditInfo ())
572
- .build ();
573
-
574
- } catch (IOException ioe ) {
575
- throw new RuntimeException ("Failed to update schema " + ident , ioe );
576
- } catch (NoSuchEntityException nsee ) {
577
- throw new NoSuchSchemaException (nsee , SCHEMA_DOES_NOT_EXIST_MSG , ident );
578
- } catch (AlreadyExistsException aee ) {
579
- throw new RuntimeException (
580
- "Schema with the same name "
581
- + ident .name ()
582
- + " already exists, this is unexpected because schema doesn't support rename" ,
583
- aee );
584
- }
502
+ return super .alterSchema (ident , changes );
585
503
}
586
504
587
505
@ Override
@@ -600,6 +518,16 @@ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmpty
600
518
throw new NonEmptySchemaException ("Schema %s is not empty" , ident );
601
519
}
602
520
521
+ SchemaEntity schemaEntity = store .get (ident , Entity .EntityType .SCHEMA , SchemaEntity .class );
522
+ Map <String , String > properties =
523
+ Optional .ofNullable (schemaEntity .properties ()).orElse (Collections .emptyMap ());
524
+ Path schemaPath = getSchemaPath (ident .name (), properties );
525
+
526
+ boolean dropped = super .dropSchema (ident , cascade );
527
+ if (!dropped ) {
528
+ return false ;
529
+ }
530
+
603
531
// Delete all the managed filesets no matter whether the storage location is under the
604
532
// schema path or not.
605
533
// The reason why we delete the managed fileset's storage location one by one is because we
@@ -635,30 +563,21 @@ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmpty
635
563
}
636
564
});
637
565
638
- SchemaEntity schemaEntity = store .get (ident , Entity .EntityType .SCHEMA , SchemaEntity .class );
639
- Map <String , String > properties =
640
- Optional .ofNullable (schemaEntity .properties ()).orElse (Collections .emptyMap ());
641
-
642
566
// Delete the schema path if it exists and is empty.
643
- Path schemaPath = getSchemaPath (ident .name (), properties );
644
- // Nothing to delete if the schema path is not set.
645
- if (schemaPath == null ) {
646
- return false ;
647
- }
648
-
649
- FileSystem fs = getFileSystem (schemaPath , conf );
650
- // Nothing to delete if the schema path does not exist.
651
- if (!fs .exists (schemaPath )) {
652
- return false ;
653
- }
654
-
655
- FileStatus [] statuses = fs .listStatus (schemaPath );
656
- if (statuses .length == 0 ) {
657
- if (fs .delete (schemaPath , true )) {
658
- LOG .info ("Deleted schema {} location {}" , ident , schemaPath );
659
- } else {
660
- LOG .warn ("Failed to delete schema {} location {}" , ident , schemaPath );
661
- return false ;
567
+ if (schemaPath != null ) {
568
+ FileSystem fs = getFileSystem (schemaPath , conf );
569
+ if (fs .exists (schemaPath )) {
570
+ FileStatus [] statuses = fs .listStatus (schemaPath );
571
+ if (statuses .length == 0 ) {
572
+ if (fs .delete (schemaPath , true )) {
573
+ LOG .info ("Deleted schema {} location {}" , ident , schemaPath );
574
+ } else {
575
+ LOG .warn (
576
+ "Failed to delete schema {} because it has files/folders under location {}" ,
577
+ ident ,
578
+ schemaPath );
579
+ }
580
+ }
662
581
}
663
582
}
664
583
0 commit comments