DomGarguilo commented on code in PR #5197: URL: https://github.com/apache/accumulo/pull/5197#discussion_r2001743560
########## core/src/test/java/org/apache/accumulo/core/clientImpl/bulk/LoadMappingIteratorTest.java: ########## @@ -0,0 +1,132 @@ +/* + * 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 + * + * https://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.accumulo.core.clientImpl.bulk; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.accumulo.core.clientImpl.bulk.BulkSerialize.createGson; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.dataImpl.KeyExtent; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.Text; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; + +public class LoadMappingIteratorTest { + private LoadMappingIterator createLoadMappingIter(Map<KeyExtent,String> loadRanges) + throws IOException { + Map<KeyExtent,Bulk.Files> unorderedMapping = new LinkedHashMap<>(); + + loadRanges.forEach((extent, files) -> { + Bulk.Files testFiles = new Bulk.Files(); + long c = 0L; + for (String f : files.split(" ")) { + c++; + testFiles.add(new Bulk.FileInfo(f, c, c)); + } + + unorderedMapping.put(extent, testFiles); + }); + + // Serialize unordered mapping directly + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + writeLoadMappingWithoutSorting(unorderedMapping, "/some/dir", p -> baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + Review Comment: ```suggestion byte[] serializedData; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writeLoadMappingWithoutSorting(unorderedMapping, "/some/dir", p -> baos); serializedData = baos.toByteArray(); } ByteArrayInputStream bais = new ByteArrayInputStream(serializedData); ``` Doesn't make a huge difference in tests but its best practice to try to close all the resources. (The `bais` should be closed when the iterator is closed) ########## core/src/test/java/org/apache/accumulo/core/clientImpl/bulk/LoadMappingIteratorTest.java: ########## @@ -0,0 +1,132 @@ +/* + * 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 + * + * https://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.accumulo.core.clientImpl.bulk; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.accumulo.core.clientImpl.bulk.BulkSerialize.createGson; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.dataImpl.KeyExtent; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.Text; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; + +public class LoadMappingIteratorTest { + private LoadMappingIterator createLoadMappingIter(Map<KeyExtent,String> loadRanges) + throws IOException { + Map<KeyExtent,Bulk.Files> unorderedMapping = new LinkedHashMap<>(); + + loadRanges.forEach((extent, files) -> { + Bulk.Files testFiles = new Bulk.Files(); + long c = 0L; + for (String f : files.split(" ")) { + c++; + testFiles.add(new Bulk.FileInfo(f, c, c)); + } + + unorderedMapping.put(extent, testFiles); + }); + + // Serialize unordered mapping directly + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + writeLoadMappingWithoutSorting(unorderedMapping, "/some/dir", p -> baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + + return BulkSerialize.readLoadMapping("/some/dir", TableId.of("1"), p -> bais); + } + + KeyExtent nke(String prev, String end) { + Text per = prev == null ? null : new Text(prev); + Text er = end == null ? null : new Text(end); + + return new KeyExtent(TableId.of("1"), er, per); + } + + /** + * Serialize bulk load mapping without sorting. + */ + public static void writeLoadMappingWithoutSorting(Map<KeyExtent,Bulk.Files> loadMapping, + String sourceDir, BulkSerialize.Output output) throws IOException { + final Path lmFile = new Path(sourceDir, Constants.BULK_LOAD_MAPPING); + + try (OutputStream fsOut = output.create(lmFile); JsonWriter writer = + new JsonWriter(new BufferedWriter(new OutputStreamWriter(fsOut, UTF_8)))) { Review Comment: ```suggestion try (OutputStream fsOut = output.create(lmFile); OutputStreamWriter osw = new OutputStreamWriter(fsOut, UTF_8); BufferedWriter bw = new BufferedWriter(osw); JsonWriter writer = new JsonWriter(bw)) { ``` I think its generally best to avoid this sort of nesting in try-with-resources blocks as it makes sure that all the resources created are closed properly. -- 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: notifications-unsubscr...@accumulo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
