Skip to content

Commit

Permalink
improve array listing
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisStaratzis committed Sep 8, 2023
1 parent aab6839 commit 0473cfe
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 67 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Other available properties are:
- ```password(String)```
- ```verifySSL(boolean)```
- ```overwritePrevious(boolean)```
- ```listPublicArrays(boolean)``` lists all public TileDB arrays. Default: ```true```

## Run a simple query
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ apply plugin: 'project-report'


group 'io.tiledb'
version '0.3.1-SNAPSHOT'
version '0.3.2-SNAPSHOT'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/examples/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static void main(String[] args) throws SQLException, ClassNotFoundExcepti
// Properties properties = new Properties();
// properties.setProperty("apiKey", "KEY");
// properties.setProperty("rememberMe", "true");
try (Connection conn = DriverManager.getConnection("jdbc:tiledb-cloud:<NAMESPACE>");
try (Connection conn = DriverManager.getConnection("jdbc:tiledb-cloud:TileDB-Inc");
Statement stmt = conn.createStatement();
ResultSet rs =
stmt.executeQuery("SELECT * FROM `tiledb://TileDB-Inc/quickstart_sparse`"); ) {
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/io/tiledb/TileDBCloudColumnsResultSet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tiledb;

import static io.tiledb.util.Util.replaceArrayNamesWithUUIDs;
import static java.sql.DatabaseMetaData.columnNoNulls;
import static java.sql.DatabaseMetaData.columnNullable;

Expand All @@ -21,28 +22,28 @@ public class TileDBCloudColumnsResultSet implements ResultSet {

private Logger logger = Logger.getLogger(TileDBCloudColumnsResultSet.class.getName());

private String arrayName;
private int numColumns;
private ArrayInfo requestedArrayInfo = null;

private String namespace;
private String completeURI;

private static List<Attribute> attributes; // static for speed
private static List<Dimension> dimensions; // static for speed

private int columnCounter, nullable;
private String currentColumnName, remarks, dataType;

public TileDBCloudColumnsResultSet(String arrayName, String namespace, ArrayApi arrayApi) {
public TileDBCloudColumnsResultSet(String completeURI, ArrayApi arrayApi) {
this.columnCounter = -1;
this.arrayName = arrayName;
this.namespace = namespace;
// the complete URI contains both the name and the UUID
this.completeURI = completeURI;
this.nullable = columnNoNulls;

String URIWithUUID = replaceArrayNamesWithUUIDs(completeURI);
String[] split = URIWithUUID.split("/");
String arrayUUIDClean = split[split.length - 1];
String arrayNamespaceClean = split[split.length - 2];

try {
String[] split = arrayName.split("/");
String arrayNameClean = split[split.length - 1];
ArraySchema result = arrayApi.getArray(namespace, arrayNameClean, "application/json");
ArraySchema result =
arrayApi.getArray(arrayNamespaceClean, arrayUUIDClean, "application/json");
attributes = result.getAttributes();
dimensions = result.getDomain().getDimensions();
} catch (ApiException e) {
Expand Down Expand Up @@ -163,7 +164,7 @@ public InputStream getBinaryStream(int columnIndex) throws SQLException {
public String getString(String columnLabel) throws SQLException {
switch (columnLabel) {
case "TABLE_NAME":
return this.arrayName;
return this.completeURI;
case "COLUMN_NAME":
return this.currentColumnName;
case "NULLABLE":
Expand Down Expand Up @@ -269,7 +270,7 @@ public String getCursorName() throws SQLException {

@Override
public ResultSetMetaData getMetaData() throws SQLException {
return new TileDBCloudColumnsResultSetMetadata(attributes, dimensions, arrayName);
return new TileDBCloudColumnsResultSetMetadata(attributes, dimensions, completeURI);
}

@Override
Expand Down
125 changes: 95 additions & 30 deletions src/main/java/io/tiledb/TileDBCloudConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,39 @@
import io.tiledb.util.Util;
import java.sql.*;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TileDBCloudConnection implements java.sql.Connection {
private ArrayApi arrayApi;
private TileDBClient tileDBClient;
private String namespace;
private boolean listPublicArrays;

Logger logger = Logger.getLogger(TileDBCloudConnection.class.getName());

/** @param namespace The TileDB namespace */
TileDBCloudConnection(String namespace) {
TileDBCloudConnection(String namespace, boolean listPublicArrays) {
this.tileDBClient = new TileDBClient();
this.arrayApi = new ArrayApi(tileDBClient.getApiClient());
this.namespace = namespace;
this.listPublicArrays = listPublicArrays;
}

/**
* @param namespace The TileDB namespace
* @param login The TileDB login object
*/
TileDBCloudConnection(String namespace, TileDBLogin login) {
TileDBCloudConnection(String namespace, TileDBLogin login, boolean listPublicArrays) {
this.tileDBClient = new TileDBClient(login);
this.arrayApi = new ArrayApi(tileDBClient.getApiClient());
this.namespace = namespace;
this.listPublicArrays = listPublicArrays;
}

@Override
Expand All @@ -54,8 +61,7 @@ public PreparedStatement prepareStatement(String s) throws SQLException {
}

/**
* Replace \"\ with \`\ and remove schema name to make the driver fully compatible with Power BI's
* sql syntax.*
* Making the driver fully compatible with Power BI's sql syntax.*
*
* @param sql The input query
* @return The TIleDB compatible query
Expand Down Expand Up @@ -131,34 +137,93 @@ public DatabaseMetaData getMetaData() throws SQLException {
FileType.REGISTERED_TASK_GRAPH.toString(),
FileType.USER_DEFINED_FUNCTION.toString());

try {
ArrayBrowserData resultOwned =
arrayApi.arraysBrowserOwnedGet(
null, null, null, namespace, null, null, null, null, null, excludeFileType, null);
tileDBCloudDatabaseMetadata.setArraysOwned(resultOwned);
} catch (Exception e) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<?>> tasks = new ArrayList<>();

// owned arrays
tasks.add(
executorService.submit(
() -> {
try {
ArrayBrowserData resultOwned =
arrayApi.arraysBrowserOwnedGet(
null,
null,
null,
namespace,
null,
null,
null,
null,
null,
excludeFileType,
null);
tileDBCloudDatabaseMetadata.setArraysOwned(resultOwned);
} catch (Exception e) {
e.printStackTrace();
}
}));

// shared arrays
tasks.add(
executorService.submit(
() -> {
try {
List<String> sharedTo = Arrays.asList(namespace);
ArrayBrowserData resultShared =
arrayApi.arraysBrowserSharedGet(
null,
null,
null,
null,
null,
null,
null,
null,
null,
excludeFileType,
null,
sharedTo);
tileDBCloudDatabaseMetadata.setArraysShared(resultShared);
} catch (Exception e) {
e.printStackTrace();
}
}));

// public arrays
if (listPublicArrays) {
tasks.add(
executorService.submit(
() -> {
try {
ArrayBrowserData resultPublic =
arrayApi.arraysBrowserPublicGet(
null,
null,
null,
null,
"name",
null,
null,
null,
null,
excludeFileType,
null);
tileDBCloudDatabaseMetadata.setArraysPublic(resultPublic);
} catch (Exception e) {
e.printStackTrace();
}
}));
}

try {

List<String> sharedTo = Arrays.asList(namespace);

ArrayBrowserData resultShared =
arrayApi.arraysBrowserSharedGet(
null,
null,
null,
null,
null,
null,
null,
null,
null,
excludeFileType,
null,
sharedTo);
tileDBCloudDatabaseMetadata.setArraysShared(resultShared);
} catch (Exception e) {
// Shutdown the executor and wait for all threads to complete
executorService.shutdown();
for (Future<?> future : tasks) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

return tileDBCloudDatabaseMetadata;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/tiledb/TileDBCloudDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class TileDBCloudDatabaseMetadata implements DatabaseMetaData {

private ArrayBrowserData arraysOwned;
private ArrayBrowserData arraysShared;
private ArrayBrowserData arraysPublic;

private String namespace;

private Logger logger = Logger.getLogger(TileDBCloudDatabaseMetadata.class.getName());
Expand Down Expand Up @@ -628,7 +630,7 @@ public ResultSet getTables(
String catalog, String schemaPattern, String tableNamePattern, String[] types)
throws SQLException {
logger.log(Level.INFO, "Requesting arrays from TileDB");
return new TileDBCloudTablesResultSet(this.arraysOwned, this.arraysShared, this.namespace);
return new TileDBCloudTablesResultSet(this.arraysOwned, this.arraysShared, this.arraysPublic);
}

@Override
Expand All @@ -655,7 +657,7 @@ public ResultSet getColumns(
String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
throws SQLException {
logger.log(Level.INFO, "Requesting columns for array: " + tableNamePattern);
return new TileDBCloudColumnsResultSet(tableNamePattern, namespace, arrayApi);
return new TileDBCloudColumnsResultSet(tableNamePattern, arrayApi);
}

@Override
Expand Down Expand Up @@ -959,6 +961,10 @@ public void setArraysShared(ArrayBrowserData result) {
this.arraysShared = result;
}

public void setArraysPublic(ArrayBrowserData resultPublic) {
this.arraysPublic = resultPublic;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/tiledb/TileDBCloudDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public Connection connect(String s, Properties properties) throws SQLException {
if (parts.length == 4) token = parts[3]; // the fourth part should be the token

// call the appropriate connector depending on the properties given
if (properties.isEmpty() && token.equals("")) return new TileDBCloudConnection(namespace);
return new TileDBCloudConnection(namespace, createLoginObject(properties));
boolean listPublicArrays =
Boolean.parseBoolean((String) properties.getOrDefault("listPublicArrays", "true"));
if (properties.isEmpty() && token.equals(""))
return new TileDBCloudConnection(namespace, listPublicArrays);
return new TileDBCloudConnection(namespace, createLoginObject(properties), listPublicArrays);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/tiledb/TileDBCloudStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.tiledb.cloud.rest_api.model.ResultFormat;
import io.tiledb.cloud.rest_api.model.SQLParameters;
import io.tiledb.java.api.Pair;
import io.tiledb.util.Util;
import java.sql.*;
import java.util.ArrayList;
import org.apache.arrow.vector.ValueVector;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class TileDBCloudStatement implements Statement {
public ResultSet executeQuery(String s) throws SQLException {
// create SQL parameters
SQLParameters sqlParameters = new SQLParameters();
sqlParameters.setQuery(s);
sqlParameters.setQuery(Util.replaceArrayNamesWithUUIDs(s));
// get results in arrow format
sqlParameters.setResultFormat(ResultFormat.ARROW);

Expand Down
Loading

0 comments on commit 0473cfe

Please sign in to comment.