jt2594838 commented on code in PR #601:
URL: https://github.com/apache/tsfile/pull/601#discussion_r2418887152


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/EncryptUtils.java:
##########
@@ -199,37 +203,75 @@ public static String getNormalKeyStr(TSFileConfig conf) {
     }
     md.update("IoTDB is the best".getBytes());
     md.update(conf.getEncryptKey());
-    byte[] data_key = Arrays.copyOfRange(md.digest(), 0, 16);
-    data_key =
-        IEncryptor.getEncryptor(conf.getEncryptType(), 
conf.getEncryptKey()).encrypt(data_key);
+    byte[] dataKey = Arrays.copyOfRange(md.digest(), 0, 16);
+    dataKey = IEncryptor.getEncryptor(conf.getEncryptType(), 
conf.getEncryptKey()).encrypt(dataKey);
+
+    StringBuilder valueStr = new StringBuilder();
+
+    for (byte b : dataKey) {
+      valueStr.append(b).append(",");
+    }
 
+    valueStr.deleteCharAt(valueStr.length() - 1);
+    return valueStr.toString();
+  }
+
+  public static String getKeyStr(byte[] key) {
     StringBuilder valueStr = new StringBuilder();
 
-    for (byte b : data_key) {
+    for (byte b : key) {
       valueStr.append(b).append(",");
     }
 
     valueStr.deleteCharAt(valueStr.length() - 1);
     return valueStr.toString();
   }
 
+  /** Get the second EncryptParameter object according to the config file. */
   public static EncryptParameter getEncryptParameter() {
     if (encryptParam == null) {
       synchronized (EncryptUtils.class) {
         if (encryptParam == null) {
           encryptParam = 
getEncryptParameter(TSFileDescriptor.getInstance().getConfig());
+          if (!encryptParamCache.containsKey(encryptParam)) {
+            encryptParamCache.put(
+                new EncryptParameter(
+                    
TSFileDescriptor.getInstance().getConfig().getEncryptType(),
+                    
TSFileDescriptor.getInstance().getConfig().getEncryptKey()),
+                encryptParam);
+          }
         }
       }
     }
     return encryptParam;
   }
 
+  /** Get the second EncryptParameter object according to the given type and 
first key. */
+  public static EncryptParameter getEncryptParameter(EncryptParameter param) {
+    if (!encryptParamCache.containsKey(param)) {
+      synchronized (EncryptUtils.class) {
+        if (!encryptParamCache.containsKey(param)) {
+          encryptParamCache.put(param, generateEncryptParameter(param));
+        }
+      }
+    }
+    return encryptParamCache.get(param);

Review Comment:
   It is already a concurrentHashMap, may use computeIfAbsent.



##########
java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java:
##########
@@ -478,11 +530,39 @@ public EncryptParameter getEncryptParam() throws 
IOException {
    * @param ioSizeRecorder can be null
    */
   public EncryptParameter getEncryptParam(LongConsumer ioSizeRecorder) throws 
IOException {
-    if (fileMetadataSize != 0) {
-      readFileMetadata(ioSizeRecorder);
-      return tsFileMetaData.getEncryptParam();
+    if (dataEncryptParam != null) {
+      return dataEncryptParam;
+    } else {
+      synchronized (this) {
+        if (dataEncryptParam != null) {
+          return dataEncryptParam;
+        }
+        if (fileMetadataSize != 0) {
+          readFileMetadata(ioSizeRecorder);
+          int encryptLevel = tsFileMetaData.getEncryptLevel();
+          byte[] secondKey = tsFileMetaData.getSecondKey();
+          String encryptType = tsFileMetaData.getEncryptType();
+          if (secondKey == null) {
+            dataEncryptParam = new 
EncryptParameter("org.apache.tsfile.encrypt.UNENCRYPTED", null);
+            return dataEncryptParam;
+          }
+          if (encryptLevel == 1) {
+            dataEncryptParam = new EncryptParameter(encryptType, secondKey);
+            return dataEncryptParam;
+          } else if (encryptLevel == 2) {
+            IDecryptor decryptor = IDecryptor.getDecryptor(param);
+            byte[] dataEncryptKey = decryptor.decrypt(secondKey);
+            dataEncryptParam = new EncryptParameter(encryptType, 
dataEncryptKey);
+            return dataEncryptParam;
+          }
+        }
+        return EncryptUtils.getEncryptParameter(param);
+      }

Review Comment:
   It is not allowed to use one reader concurrently, no need to synchronize



##########
java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java:
##########
@@ -140,6 +140,10 @@ public class TsFileSequenceReader implements AutoCloseable 
{
   private DeserializeConfig deserializeConfig = new DeserializeConfig();
   private volatile boolean cacheTableSchemaMap = false;
 
+  private EncryptParameter param = EncryptUtils.getEncryptParameter();
+
+  private EncryptParameter dataEncryptParam = null;

Review Comment:
   Consider a more distinguishable name for `param`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to