1818
1919package org .apache .amoro .formats .lance ;
2020
21+ import org .apache .amoro .AlreadyExistsException ;
2122import org .apache .amoro .AmoroTable ;
2223import org .apache .amoro .FormatCatalog ;
2324import org .apache .amoro .NoSuchDatabaseException ;
2425import org .apache .amoro .NoSuchTableException ;
2526import org .apache .amoro .table .TableIdentifier ;
27+ import org .apache .arrow .util .Preconditions ;
28+ import org .apache .commons .lang3 .StringUtils ;
2629import org .lance .Dataset ;
2730import org .lance .namespace .LanceNamespace ;
31+ import org .lance .namespace .errors .InvalidInputException ;
32+ import org .lance .namespace .errors .NamespaceAlreadyExistsException ;
33+ import org .lance .namespace .errors .NamespaceNotFoundException ;
2834import org .lance .namespace .errors .TableNotFoundException ;
35+ import org .lance .namespace .model .CreateNamespaceRequest ;
36+ import org .lance .namespace .model .DropNamespaceRequest ;
2937import org .lance .namespace .model .DropTableRequest ;
38+ import org .lance .namespace .model .ListNamespacesRequest ;
39+ import org .lance .namespace .model .ListNamespacesResponse ;
40+ import org .lance .namespace .model .ListTablesRequest ;
41+ import org .lance .namespace .model .ListTablesResponse ;
42+ import org .lance .namespace .model .NamespaceExistsRequest ;
3043import org .lance .namespace .model .TableExistsRequest ;
3144
45+ import java .util .ArrayList ;
3246import java .util .Collections ;
47+ import java .util .LinkedHashSet ;
3348import java .util .List ;
49+ import java .util .Map ;
50+ import java .util .Set ;
3451
3552/** Base implementation of {@link FormatCatalog} for Lance. */
3653public abstract class AbstractLanceCatalog implements FormatCatalog {
3754
55+ protected static final String URI_PROPERTY = "uri" ;
56+
57+ private static final String CATALOG_PROPERTY = "catalog-name" ;
58+
3859 protected final String catalogName ;
3960 protected final LanceNamespace namespace ;
61+ private final List <String > catalogPrefix ;
4062
4163 protected AbstractLanceCatalog (String catalogName , LanceNamespace namespace ) {
64+ this (catalogName , namespace , Collections .emptyList ());
65+ }
66+
67+ protected AbstractLanceCatalog (
68+ String catalogName , LanceNamespace namespace , List <String > catalogPrefix ) {
4269 this .catalogName = catalogName ;
4370 this .namespace = namespace ;
71+ this .catalogPrefix = catalogPrefix ;
72+ }
73+
74+ protected List <String > tableId (String database , String table ) {
75+ return concat (catalogPrefix , database , table );
76+ }
77+
78+ protected List <String > namespaceId (String database ) {
79+ return concat (catalogPrefix , database );
80+ }
81+
82+ protected static List <String > catalogPrefix (
83+ Map <String , String > catalogProperties , String defaultCatalog ) {
84+ String catalog = catalogProperties == null ? null : catalogProperties .get (CATALOG_PROPERTY );
85+ if (StringUtils .isEmpty (catalog )) {
86+ catalog = defaultCatalog ;
87+ }
88+ return StringUtils .isEmpty (catalog )
89+ ? Collections .emptyList ()
90+ : Collections .singletonList (catalog );
4491 }
4592
46- /** Composes the namespace table id used to reference a single table. */
47- protected abstract List <String > tableId (String database , String table );
93+ protected static List <String > concat (List <String > prefix , String ... parts ) {
94+ List <String > id = new ArrayList <>(prefix );
95+ Collections .addAll (id , parts );
96+ return id ;
97+ }
98+
99+ protected static void validateProperties (Map <String , String > properties ) {
100+ Preconditions .checkArgument (
101+ properties != null && !properties .isEmpty (), "Catalog properties must be set." );
102+ }
48103
49- /** Composes the namespace id used to list tables of a database. */
50- protected abstract List <String > tableIdForListTables (String database );
104+ protected AmoroTable <?> openTable (String database , String table ) {
105+ namespace .tableExists (new TableExistsRequest ().id (tableId (database , table )));
106+ Dataset dataset =
107+ Dataset .open ().namespaceClient (namespace ).tableId (tableId (database , table )).build ();
108+ return new LanceTable (
109+ TableIdentifier .of (catalogName , database , table ), dataset , Collections .emptyMap ());
110+ }
111+
112+ protected void dropInNamespace (String database , String table , boolean purge ) {
113+ namespace .dropTable (new DropTableRequest ().id (tableId (database , table )));
114+ }
51115
52116 @ Override
53117 public boolean tableExists (String database , String table ) {
@@ -58,44 +122,111 @@ public boolean tableExists(String database, String table) {
58122 try {
59123 namespace .tableExists (new TableExistsRequest ().id (tableId (database , table )));
60124 return true ;
61- } catch (TableNotFoundException e ) {
125+ } catch (TableNotFoundException | InvalidInputException e ) {
62126 return false ;
63127 }
64128 }
65129
66130 @ Override
67- public AmoroTable <?> loadTable (String database , String tableName ) {
68- if (!tableExists (database , tableName )) {
69- throw new NoSuchTableException ("Table: " + database + "." + tableName + " does not exist" );
70- }
131+ public AmoroTable <?> loadTable (String database , String table ) {
132+ validateDatabase (database );
71133
72- TableIdentifier identifier = TableIdentifier .of (catalogName , database , tableName );
73- Dataset dataset =
74- Dataset .open ().namespaceClient (namespace ).tableId (tableId (database , tableName )).build ();
75- return new LanceTable (identifier , dataset , Collections .emptyMap ());
134+ try {
135+ return openTable (database , table );
136+ } catch (TableNotFoundException | InvalidInputException e ) {
137+ throw new NoSuchTableException ("Table: " + database + "." + table + " does not exist" , e );
138+ }
76139 }
77140
78141 @ Override
79142 public boolean dropTable (String database , String table , boolean purge ) {
80143 validateDatabase (database );
81144
82145 try {
83- namespace . dropTable ( new DropTableRequest (). id ( tableId ( database , table )) );
146+ dropInNamespace ( database , table , purge );
84147 return true ;
85- } catch (TableNotFoundException e ) {
148+ } catch (TableNotFoundException | InvalidInputException e ) {
86149 return false ;
87150 }
88151 }
89152
90153 @ Override
91- public abstract List <String > listTables (String database );
154+ public List <String > listTables (String database ) {
155+ validateDatabase (database );
156+
157+ Set <String > tables = new LinkedHashSet <>();
158+ String pageToken = null ;
159+ do {
160+ ListTablesRequest request = new ListTablesRequest ().id (namespaceId (database ));
161+ if (pageToken != null ) {
162+ request .pageToken (pageToken );
163+ }
164+
165+ ListTablesResponse response = namespace .listTables (request );
166+ if (response == null ) {
167+ break ;
168+ }
169+ if (response .getTables () != null ) {
170+ tables .addAll (response .getTables ());
171+ }
172+ pageToken = response .getPageToken ();
173+ } while (StringUtils .isNotEmpty (pageToken ));
174+
175+ return new ArrayList <>(tables );
176+ }
92177
93178 @ Override
94- public abstract List <String > listDatabases ();
179+ public List <String > listDatabases () {
180+ Set <String > namespaces = new LinkedHashSet <>();
181+ String pageToken = null ;
182+ do {
183+ ListNamespacesRequest request = new ListNamespacesRequest ().id (catalogPrefix );
184+ if (pageToken != null ) {
185+ request .pageToken (pageToken );
186+ }
187+
188+ ListNamespacesResponse response = namespace .listNamespaces (request );
189+ if (response == null ) {
190+ break ;
191+ }
192+ if (response .getNamespaces () != null ) {
193+ namespaces .addAll (response .getNamespaces ());
194+ }
195+
196+ pageToken = response .getPageToken ();
197+ } while (StringUtils .isNotEmpty (pageToken ));
198+
199+ return new ArrayList <>(namespaces );
200+ }
95201
96202 protected void validateDatabase (String database ) {
97203 if (!databaseExists (database )) {
98204 throw new NoSuchDatabaseException ("Database: " + database + " does not exist" );
99205 }
100206 }
207+
208+ @ Override
209+ public boolean databaseExists (String database ) {
210+ try {
211+ namespace .namespaceExists (new NamespaceExistsRequest ().id (namespaceId (database )));
212+ return true ;
213+ } catch (NamespaceNotFoundException e ) {
214+ return false ;
215+ }
216+ }
217+
218+ @ Override
219+ public void createDatabase (String database ) {
220+ try {
221+ namespace .createNamespace (new CreateNamespaceRequest ().id (namespaceId (database )));
222+ } catch (NamespaceAlreadyExistsException e ) {
223+ throw new AlreadyExistsException ("Database: " + database + " already exists" , e );
224+ }
225+ }
226+
227+ @ Override
228+ public void dropDatabase (String database ) {
229+ validateDatabase (database );
230+ namespace .dropNamespace (new DropNamespaceRequest ().id (namespaceId (database )));
231+ }
101232}
0 commit comments