jt2594838 commented on code in PR #213:
URL: https://github.com/apache/tsfile/pull/213#discussion_r1735464812
##########
java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java:
##########
@@ -1808,41 +1807,11 @@ public ByteBuffer readPage(PageHeader header,
CompressionType type) throws IOExc
if (header.getUncompressedSize() == 0) {
return buffer;
}
- if (decryptor == null || decryptor.getEncryptionType() ==
EncryptionType.UNENCRYPTED) {
- if (type == CompressionType.UNCOMPRESSED) {
- return buffer;
- } else {
- IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
- ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- unCompressor.uncompress(
- buffer.array(), buffer.position(), buffer.remaining(),
uncompressedBuffer.array(), 0);
- return uncompressedBuffer;
- }
- } else {
- if (type == CompressionType.UNCOMPRESSED) {
- ByteBuffer decryptedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- System.arraycopy(
- decryptor.decrypt(buffer.array(), buffer.position(),
buffer.remaining()),
- 0,
- decryptedBuffer.array(),
- 0,
- buffer.remaining());
- return decryptedBuffer;
- } else {
- ByteBuffer decryptedBuffer =
ByteBuffer.allocate(header.getCompressedSize());
- System.arraycopy(
- decryptor.decrypt(buffer.array(), buffer.position(),
buffer.remaining()),
- 0,
- decryptedBuffer.array(),
- 0,
- buffer.remaining());
- IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
- ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- unCompressor.uncompress(
- decryptedBuffer.array(), 0, buffer.remaining(),
uncompressedBuffer.array(), 0);
- return uncompressedBuffer;
- }
- }
+ byte[] decryptedData = decryptor.decrypt(buffer.array(),
buffer.position(), buffer.remaining());
+ IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
+ ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
+ unCompressor.uncompress(decryptedData, 0, decryptedData.length,
uncompressedBuffer.array(), 0);
+ return uncompressedBuffer;
}
Review Comment:
This does not avoid copying, I guess.
```java
ByteBuffer finalBuffer = decrypt(decryptor, buffer);
finalBuffer = uncompress(compressionType, buffer,
header.getUncompressedSize());
return finalBuffer;
}
private static ByteBuffer decrypt(IDecryptor decryptor, ByteBuffer buffer)
{
if (decryptor == null || decryptor.getEncryptionType() ==
EncryptionType.UNENCRYPTED) {
return buffer;
}
return ByteBuffer.wrap(decryptor.decrypt(buffer.array(),
buffer.arrayOffset() + buffer.position(), buffer.remaining()));
}
private static ByteBuffer uncompress(CompressionType compressionType,
ByteBuffer buffer, int uncompressedSize) {
if (compressionType == UNCOMPRESSED) {
return buffer;
}
IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
ByteBuffer uncompressedBuffer = ByteBuffer.allocate(uncompressedSize);
unCompressor.uncompress(
buffer.array(), buffer.arrayOffset() + buffer.position(),
buffer.remaining(), uncompressedBuffer.array(), 0);
return uncompressedBuffer;
}
```
The main idea is that a sequential structure is much easier to understand
than a nesting structure. Try making your code simpler.
##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/EncryptUtils.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tsfile.encrypt;
+
+import org.apache.tsfile.common.conf.TSFileDescriptor;
+import org.apache.tsfile.exception.encrypt.EncryptException;
+import org.apache.tsfile.file.metadata.enums.EncryptionType;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.security.MessageDigest;
+
+public class EncryptUtils {
+ public static String getEncryptKeyFromPath(String path) {
+ try (BufferedReader br = new BufferedReader(new FileReader(path))) {
+ StringBuilder sb = new StringBuilder();
+ String line;
+ boolean first = true;
+ while ((line = br.readLine()) != null) {
+ if (first) {
+ sb.append(line);
+ first = false;
+ } else {
+ sb.append("\n").append(line);
+ }
+ }
+ return sb.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static byte[] hexStringToByteArray(String hexString) {
+ int len = hexString.length();
+ byte[] byteArray = new byte[len / 2];
+
+ for (int i = 0; i < len; i += 2) {
+ byteArray[i / 2] =
+ (byte)
+ ((Character.digit(hexString.charAt(i), 16) << 4)
+ + Character.digit(hexString.charAt(i + 1), 16));
+ }
+
+ return byteArray;
+ }
+
+ public static String byteArrayToHexString(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+
+ for (byte b : bytes) {
+ sb.append(String.format("%02X", b));
+ }
+
+ return sb.toString();
+ }
+
+ public static String getNormalKeyStr() {
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ md.update("IoTDB is the best".getBytes());
+
md.update(TSFileDescriptor.getInstance().getConfig().getEncryptKey().getBytes());
+ byte[] data_key = md.digest();
+ StringBuilder valueStr = new StringBuilder();
+
+ for (byte b : data_key) {
+ valueStr.append(b).append(",");
+ }
+
+ valueStr.deleteCharAt(valueStr.length() - 1);
+ String str = valueStr.toString();
Review Comment:
This may be stored as a static member, no need to recalculate every time.
The same below.
##########
java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java:
##########
@@ -1808,41 +1807,11 @@ public ByteBuffer readPage(PageHeader header,
CompressionType type) throws IOExc
if (header.getUncompressedSize() == 0) {
return buffer;
}
- if (decryptor == null || decryptor.getEncryptionType() ==
EncryptionType.UNENCRYPTED) {
- if (type == CompressionType.UNCOMPRESSED) {
- return buffer;
- } else {
- IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
- ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- unCompressor.uncompress(
- buffer.array(), buffer.position(), buffer.remaining(),
uncompressedBuffer.array(), 0);
- return uncompressedBuffer;
- }
- } else {
- if (type == CompressionType.UNCOMPRESSED) {
- ByteBuffer decryptedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- System.arraycopy(
- decryptor.decrypt(buffer.array(), buffer.position(),
buffer.remaining()),
- 0,
- decryptedBuffer.array(),
- 0,
- buffer.remaining());
- return decryptedBuffer;
- } else {
- ByteBuffer decryptedBuffer =
ByteBuffer.allocate(header.getCompressedSize());
- System.arraycopy(
- decryptor.decrypt(buffer.array(), buffer.position(),
buffer.remaining()),
- 0,
- decryptedBuffer.array(),
- 0,
- buffer.remaining());
- IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
- ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
- unCompressor.uncompress(
- decryptedBuffer.array(), 0, buffer.remaining(),
uncompressedBuffer.array(), 0);
- return uncompressedBuffer;
- }
- }
+ byte[] decryptedData = decryptor.decrypt(buffer.array(),
buffer.position(), buffer.remaining());
+ IUnCompressor unCompressor = IUnCompressor.getUnCompressor(type);
+ ByteBuffer uncompressedBuffer =
ByteBuffer.allocate(header.getUncompressedSize());
+ unCompressor.uncompress(decryptedData, 0, decryptedData.length,
uncompressedBuffer.array(), 0);
+ return uncompressedBuffer;
}
Review Comment:
You may put the static methods in another place and share them with other
functions that need them, if any.
Beware of the offset when directly using the underlying array from a
ByteBuffer. It should be ByteBuffer.arrayOffset() + ByteBuffer.position()
instead of ByteBuffer.position().
--
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]