33import java .util .List ;
44import java .util .Objects ;
55import java .util .Set ;
6- import java .util .regex .Pattern ;
76import java .util .stream .Collectors ;
87
8+ import com .azure .cosmos .CosmosClient ;
9+ import com .azure .cosmos .CosmosClientBuilder ;
910import com .microsoft .azure .documentdb .*;
1011import io .github .thunderz99 .cosmos .util .Checker ;
12+ import io .github .thunderz99 .cosmos .util .ConnectionStringUtil ;
13+ import io .github .thunderz99 .cosmos .util .EnvUtil ;
1114import org .apache .commons .collections4 .CollectionUtils ;
1215import org .apache .commons .lang3 .StringUtils ;
1316import org .apache .commons .lang3 .tuple .Pair ;
@@ -32,11 +35,11 @@ public class Cosmos {
3235
3336 DocumentClient client ;
3437
35- String account ;
38+ CosmosClient clientV4 ;
3639
37- static Pattern connectionStringPattern = Pattern . compile ( "AccountEndpoint=(?<endpoint>.+);AccountKey=(?<key>[^;]+);?" ) ;
40+ String account ;
3841
39- public static final String JC_SDK_V4_ENABLE = "JC_SDK_V4_ENABLE " ;
42+ public static final String COSMOS_SDK_V4_ENABLE = "COSMOS_SDK_V4_ENABLE " ;
4043
4144 public static final String ETAG = "_etag" ;
4245
@@ -46,13 +49,30 @@ public Cosmos(String connectionString) {
4649
4750 public Cosmos (String connectionString , List <String > preferredRegions ) {
4851
49- Pair <String , String > pair = parseConnectionString (connectionString );
52+ Pair <String , String > pair = ConnectionStringUtil . parseConnectionString (connectionString );
5053 var endpoint = pair .getLeft ();
5154 var key = pair .getRight ();
5255
5356 this .client = new DocumentClient (endpoint , key , ConnectionPolicy .GetDefault (), ConsistencyLevel .Session );
5457
55- //var v4Enable = Boolean.parseBoolean(EnvUtil.getOrDefault(Cosmos.JC_SDK_V4_ENABLE, "false"));
58+ // default to true
59+ var v4Enable = Boolean .parseBoolean (EnvUtil .getOrDefault (Cosmos .COSMOS_SDK_V4_ENABLE , "true" ));
60+
61+ if (v4Enable ) {
62+ log .info ("COSMOS_SDK_V4_ENABLE is enabled for endpoint:{}" , endpoint );
63+ this .clientV4 = new CosmosClientBuilder ()
64+ .endpoint (endpoint )
65+ .key (key )
66+ .preferredRegions (preferredRegions )
67+ .consistencyLevel (com .azure .cosmos .ConsistencyLevel .SESSION )
68+ .contentResponseOnWriteEnabled (true )
69+ .buildClient ();
70+
71+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
72+ this .clientV4 .close ();
73+ }));
74+ }
75+
5676
5777 }
5878
@@ -332,15 +352,36 @@ static UniqueKey toUniqueKey(String key) {
332352 }
333353
334354
335- static String getDatabaseLink (String db ) {
355+ /**
356+ * Generate database link format used in cosmosdb
357+ *
358+ * @param db db name
359+ * @return databaseLink
360+ */
361+ public static String getDatabaseLink (String db ) {
336362 return String .format ("/dbs/%s" , db );
337363 }
338364
339- static String getCollectionLink (String db , String coll ) {
365+ /**
366+ * Generate database link format used in cosmosdb
367+ *
368+ * @param db db name
369+ * @param coll collection name
370+ * @return collection link
371+ */
372+ public static String getCollectionLink (String db , String coll ) {
340373 return String .format ("/dbs/%s/colls/%s" , db , coll );
341374 }
342375
343- static String getDocumentLink (String db , String coll , String id ) {
376+ /**
377+ * Generate document link format used in cosmosdb
378+ *
379+ * @param db db name
380+ * @param coll collection name
381+ * @param id document id
382+ * @return document link
383+ */
384+ public static String getDocumentLink (String db , String coll , String id ) {
344385 return String .format ("/dbs/%s/colls/%s/docs/%s" , db , coll , id );
345386 }
346387
@@ -383,24 +424,4 @@ static String getAccount(DocumentClient client) throws DocumentClientException {
383424 return client .getDatabaseAccount ().get ("id" ).toString ();
384425 }
385426
386- static Pair <String , String > parseConnectionString (String connectionString ) {
387-
388- var matcher = connectionStringPattern .matcher (connectionString );
389- if (!matcher .find ()) {
390- throw new IllegalStateException (
391- "Make sure connectionString contains 'AccountEndpoint=' and 'AccountKey=' " );
392- }
393- String endpoint = matcher .group ("endpoint" );
394- String key = matcher .group ("key" );
395-
396- Checker .check (StringUtils .isNotBlank (endpoint ), "Make sure connectionString contains 'AccountEndpoint=' " );
397- Checker .check (StringUtils .isNotBlank (key ), "Make sure connectionString contains 'AccountKey='" );
398-
399- if (log .isInfoEnabled ()) {
400- log .info ("endpoint:{}" , endpoint );
401- log .info ("key:{}..." , key .substring (0 , 3 ));
402- }
403-
404- return Pair .of (endpoint , key );
405- }
406427}
0 commit comments