11package io .tiledb .util ;
22
3+ import io .tiledb .TileDBCloudTablesResultSet ;
34import java .util .regex .Matcher ;
45import java .util .regex .Pattern ;
56
@@ -15,41 +16,71 @@ public class Util {
1516 public static String SCHEMA_NAME = "All TileDB arrays" ;
1617
1718 /**
18- * Replaces all array names with the corresponding UUID if the input string is of a specific
19- * pattern
19+ * Takes as input a complete query that might or might not contain references to arrays of the
20+ * form [tiledb://../..][a09uhugr] and returns a query in which all these references which are
21+ * specific to this JDBC driver are translated to queries that contain the tiledbURis. This method
22+ * is mainly used when the query is created by a BI tool rather than a pure Java user. BI tools
23+ * display the arrays and when selected use their names to query them. Since the array names are
24+ * not unique in TileDB-Cloud we display the names accompanied with one small part of the original
25+ * UUID. We then map this UUID to the tileDBURI and modify the query so that it only uses
26+ * tileDBURIs. If no array references of the above-mentioned form are found the query is returned
27+ * intact.
2028 *
21- * @param completeURI If the input string is of the form "tiledb://TileDB-Inc/orders ~
22- * tiledb://TileDB-Inc/120f0518-dd8d-467f-8c1b-23aa49929465" this method will modify the input
23- * string to only use the TileDB URI with the UUID. If an input string does not match the
24- * regex pattern specified in the Pattern.compile method, the Matcher will not find any
25- * matches, and the code will simply return the original input string as it is without making
26- * any modifications.
29+ * @param query The complete query
2730 * @return
2831 */
29- public static String replaceArrayNamesWithUUIDs (String completeURI ) {
30- // Define the regular expression pattern for the TileDB URI pattern with spaces around "~"
31- String regex = "(tiledb://[^~]+) ~ ([^\\ s]+)" ;
32- Pattern pattern = Pattern .compile (regex );
32+ public static String useTileDBUris (String query ) {
33+ // Regular expression pattern to match the specified format
34+ Pattern pattern = Pattern .compile ("\\ [tiledb://([^/]+)/([^\\ ]]+)\\ ]\\ [(\\ w+)\\ ]" );
3335
34- Matcher matcher = pattern . matcher ( completeURI );
35- StringBuilder modifiedText = new StringBuilder ( );
36+ // Matcher to find and replace occurrences of the pattern in the input string
37+ Matcher matcher = pattern . matcher ( query );
3638
37- // Find and remove the first part in TileDB URIs
38- int lastEnd = 0 ;
39- while (matcher .find ()) {
40- // Append the text before the match and a space
41- modifiedText .append (completeURI .substring (lastEnd , matcher .start (1 )));
39+ // StringBuffer to hold the modified input string
40+ StringBuffer result = new StringBuffer ();
4241
43- // Append the text after "~" and a space
44- modifiedText .append (matcher .group (2 ));
42+ // Find and replace occurrences of the pattern in the input string
43+ while (matcher .find ()) {
44+ String key = matcher .group (3 );
4545
46- // Update the lastEnd to the end of this match
47- lastEnd = matcher .end ();
46+ String replacement =
47+ TileDBCloudTablesResultSet .uris .getOrDefault (key , "" ); // Get replacement from the map
48+ if (!replacement .equals ("" ))
49+ matcher .appendReplacement (
50+ result , replacement ); // Replace the match with the corresponding value
4851 }
52+ matcher .appendTail (result ); // Append the remaining part of the input string
53+ // Output the modified string
54+ return result .toString ();
55+ }
4956
50- // Append any remaining text after the last match
51- modifiedText .append (completeURI .substring (lastEnd ));
57+ /**
58+ * Takes as input a TileDBURI and returns the first 8 characters of the UUID.
59+ *
60+ * @param tiledbUri the TileDBURI
61+ * @return the first 8 chars of the UUID
62+ */
63+ public static String getUUIDStart (String tiledbUri ) {
64+ // Split the input URI by '/'
65+ String [] uriParts = tiledbUri .split ("/" );
5266
53- return modifiedText .toString ().trim ();
67+ // Extract the desired string and return its first 8 characters
68+ String targetString = uriParts [3 ];
69+ return targetString .substring (0 , Math .min (targetString .length (), 8 ));
70+ }
71+
72+ public static String removeUUID (String arrayName ) {
73+ // Regular expression pattern to match the specified format
74+ Pattern pattern = Pattern .compile ("\\ [tiledb://([^\\ ]]+)\\ ].*" );
75+
76+ // Matcher to find the pattern in the input string
77+ Matcher matcher = pattern .matcher (arrayName );
78+
79+ // Check if the pattern is found and extract the desired part
80+ if (matcher .matches ()) {
81+ return "tiledb://" + matcher .group (1 );
82+ } else {
83+ return arrayName ;
84+ }
5485 }
5586}
0 commit comments