Apache9 commented on a change in pull request #3748:
URL: https://github.com/apache/hbase/pull/3748#discussion_r728575130



##########
File path: 
hbase-compression/hbase-compression-zstd/src/main/java/org/apache/hadoop/hbase/io/compress/zstd/ZstdCodec.java
##########
@@ -123,4 +141,59 @@ static int getBufferSize(Configuration conf) {
     return size > 0 ? size : 256 * 1024; // Don't change this default
   }
 
+  static byte[] getDictionary(final Configuration conf) {
+    // Get the dictionary path, if set
+    final String s = conf.get(ZSTD_DICTIONARY_KEY);
+    if (s == null) {
+      return null;
+    }
+
+    // Create the dictionary loading cache if we haven't already
+    if (DICTIONARY_CACHE == null) {
+      synchronized (ZstdCodec.class) {
+        if (DICTIONARY_CACHE == null) {
+          DICTIONARY_CACHE = CacheBuilder.newBuilder()

Review comment:
       nits: better abstract the creation code to a separated method? It could 
make the code easier to read.

##########
File path: 
hbase-compression/hbase-compression-zstd/src/main/java/org/apache/hadoop/hbase/io/compress/zstd/ZstdCodec.java
##########
@@ -123,4 +141,59 @@ static int getBufferSize(Configuration conf) {
     return size > 0 ? size : 256 * 1024; // Don't change this default
   }
 
+  static byte[] getDictionary(final Configuration conf) {
+    // Get the dictionary path, if set
+    final String s = conf.get(ZSTD_DICTIONARY_KEY);
+    if (s == null) {
+      return null;
+    }
+
+    // Create the dictionary loading cache if we haven't already
+    if (DICTIONARY_CACHE == null) {
+      synchronized (ZstdCodec.class) {
+        if (DICTIONARY_CACHE == null) {
+          DICTIONARY_CACHE = CacheBuilder.newBuilder()
+            .expireAfterAccess(1, TimeUnit.HOURS)
+            .build(
+              new CacheLoader<String, byte[]>() {
+                public byte[] load(String s) throws Exception {
+                  final Path path = new Path(s);
+                  final FileSystem fs = FileSystem.get(path.toUri(), conf);
+                  final FileStatus stat = fs.getFileStatus(path);
+                  if (!stat.isFile()) {
+                    throw new IllegalArgumentException(s + " is not a file");
+                  }
+                  final int limit = conf.getInt(ZSTD_DICTIONARY_MAX_SIZE_KEY,
+                    DEFAULT_ZSTD_DICTIONARY_MAX_SIZE);
+                  if (stat.getLen() > limit) {
+                    throw new IllegalArgumentException("Dictionary " + s + " 
is too large" +
+                      ", size=" + stat.getLen() + ", limit=" + limit);
+                  }
+                  final ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
+                  final byte[] buffer = new byte[8192];
+                  try (final FSDataInputStream in = fs.open(path)) {
+                    int n;
+                    do {
+                      n = in.read(buffer);
+                      if (n > 0) {
+                        baos.write(buffer, 0, n);
+                         }

Review comment:
       nits: indent




-- 
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