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 static List <String > catalogPrefix (
75+ Map <String , String > catalogProperties , String defaultCatalog ) {
76+ String catalog = catalogProperties == null ? null : catalogProperties .get (CATALOG_PROPERTY );
77+ if (StringUtils .isEmpty (catalog )) {
78+ catalog = defaultCatalog ;
79+ }
80+ return StringUtils .isEmpty (catalog )
81+ ? Collections .emptyList ()
82+ : Collections .singletonList (catalog );
4483 }
4584
4685 /** Composes the namespace table id used to reference a single table. */
47- protected abstract List <String > tableId (String database , String table );
86+ protected List <String > tableId (String database , String table ) {
87+ return concat (catalogPrefix , database , table );
88+ }
4889
4990 /** Composes the namespace id used to list tables of a database. */
50- protected abstract List <String > tableIdForListTables (String database );
91+ protected List <String > namespaceId (String database ) {
92+ return concat (catalogPrefix , database );
93+ }
94+
95+ protected static List <String > concat (List <String > prefix , String ... parts ) {
96+ List <String > id = new ArrayList <>(prefix );
97+ Collections .addAll (id , parts );
98+ return id ;
99+ }
100+
101+ protected static void validateProperties (Map <String , String > properties ) {
102+ Preconditions .checkArgument (
103+ properties != null && !properties .isEmpty (), "Catalog properties must be set." );
104+ }
105+
106+ protected AmoroTable <?> openTable (String database , String table ) {
107+ namespace .tableExists (new TableExistsRequest ().id (tableId (database , table )));
108+ Dataset dataset =
109+ Dataset .open ().namespaceClient (namespace ).tableId (tableId (database , table )).build ();
110+ return new LanceTable (
111+ TableIdentifier .of (catalogName , database , table ), dataset , Collections .emptyMap ());
112+ }
113+
114+ protected void dropInNamespace (String database , String table , boolean purge ) {
115+ namespace .dropTable (new DropTableRequest ().id (tableId (database , table )));
116+ }
51117
52118 @ Override
53119 public boolean tableExists (String database , String table ) {
@@ -58,44 +124,111 @@ public boolean tableExists(String database, String table) {
58124 try {
59125 namespace .tableExists (new TableExistsRequest ().id (tableId (database , table )));
60126 return true ;
61- } catch (TableNotFoundException e ) {
127+ } catch (TableNotFoundException | InvalidInputException e ) {
62128 return false ;
63129 }
64130 }
65131
66132 @ 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- }
133+ public AmoroTable <?> loadTable (String database , String table ) {
134+ validateDatabase (database );
71135
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 ());
136+ try {
137+ return openTable (database , table );
138+ } catch (TableNotFoundException | InvalidInputException e ) {
139+ throw new NoSuchTableException ("Table: " + database + "." + table + " does not exist" , e );
140+ }
76141 }
77142
78143 @ Override
79144 public boolean dropTable (String database , String table , boolean purge ) {
80145 validateDatabase (database );
81146
82147 try {
83- namespace . dropTable ( new DropTableRequest (). id ( tableId ( database , table )) );
148+ dropInNamespace ( database , table , purge );
84149 return true ;
85- } catch (TableNotFoundException e ) {
150+ } catch (TableNotFoundException | InvalidInputException e ) {
86151 return false ;
87152 }
88153 }
89154
90155 @ Override
91- public abstract List <String > listTables (String database );
156+ public List <String > listTables (String database ) {
157+ validateDatabase (database );
158+
159+ Set <String > tables = new LinkedHashSet <>();
160+ String pageToken = null ;
161+ do {
162+ ListTablesRequest request = new ListTablesRequest ().id (namespaceId (database ));
163+ if (pageToken != null ) {
164+ request .pageToken (pageToken );
165+ }
166+
167+ ListTablesResponse response = namespace .listTables (request );
168+ if (response == null ) {
169+ break ;
170+ }
171+ if (response .getTables () != null ) {
172+ tables .addAll (response .getTables ());
173+ }
174+ pageToken = response .getPageToken ();
175+ } while (StringUtils .isNotEmpty (pageToken ));
176+
177+ return new ArrayList <>(tables );
178+ }
92179
93180 @ Override
94- public abstract List <String > listDatabases ();
181+ public List <String > listDatabases () {
182+ Set <String > namespaces = new LinkedHashSet <>();
183+ String pageToken = null ;
184+ do {
185+ ListNamespacesRequest request = new ListNamespacesRequest ().id (catalogPrefix );
186+ if (pageToken != null ) {
187+ request .pageToken (pageToken );
188+ }
189+
190+ ListNamespacesResponse response = namespace .listNamespaces (request );
191+ if (response == null ) {
192+ break ;
193+ }
194+ if (response .getNamespaces () != null ) {
195+ namespaces .addAll (response .getNamespaces ());
196+ }
197+
198+ pageToken = response .getPageToken ();
199+ } while (StringUtils .isNotEmpty (pageToken ));
200+
201+ return new ArrayList <>(namespaces );
202+ }
95203
96204 protected void validateDatabase (String database ) {
97205 if (!databaseExists (database )) {
98206 throw new NoSuchDatabaseException ("Database: " + database + " does not exist" );
99207 }
100208 }
209+
210+ @ Override
211+ public boolean databaseExists (String database ) {
212+ try {
213+ namespace .namespaceExists (new NamespaceExistsRequest ().id (namespaceId (database )));
214+ return true ;
215+ } catch (NamespaceNotFoundException e ) {
216+ return false ;
217+ }
218+ }
219+
220+ @ Override
221+ public void createDatabase (String database ) {
222+ try {
223+ namespace .createNamespace (new CreateNamespaceRequest ().id (namespaceId (database )));
224+ } catch (NamespaceAlreadyExistsException e ) {
225+ throw new AlreadyExistsException ("Database: " + database + " already exists" , e );
226+ }
227+ }
228+
229+ @ Override
230+ public void dropDatabase (String database ) {
231+ validateDatabase (database );
232+ namespace .dropNamespace (new DropNamespaceRequest ().id (namespaceId (database )));
233+ }
101234}
0 commit comments