From 198d0a9de2c591378503f6b4af50b98b6c799434 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 20 Aug 2024 13:18:10 +0200 Subject: [PATCH 1/2] Avoid this escape. --- fsa/src/main/java/com/yahoo/fsa/MetaData.java | 64 ++++++++----------- .../fsa/topicpredictor/TopicPredictor.java | 29 ++------- .../concurrent/maintenance/Maintainer.java | 4 +- 3 files changed, 35 insertions(+), 62 deletions(-) diff --git a/fsa/src/main/java/com/yahoo/fsa/MetaData.java b/fsa/src/main/java/com/yahoo/fsa/MetaData.java index 26a7bdcaa9dc..d9a6ba010ae2 100644 --- a/fsa/src/main/java/com/yahoo/fsa/MetaData.java +++ b/fsa/src/main/java/com/yahoo/fsa/MetaData.java @@ -8,13 +8,9 @@ import java.nio.CharBuffer; import java.nio.ByteOrder; import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.nio.charset.Charset; -import com.yahoo.fsa.FSA; - - /** * Class for accessing meta-data (dat-files) used by FSA applications. * @@ -22,54 +18,46 @@ **/ public class MetaData { - private boolean _ok = false; - private MappedByteBuffer _header; - private MappedByteBuffer _data; - private Charset _charset; + private final boolean _ok; + private final MappedByteBuffer _header; + private final MappedByteBuffer _data; + private final Charset _charset; public MetaData(String filename){ - init(filename, "utf-8"); + this(filename, "utf-8"); } - public MetaData(String filename, String charsetname){ - init(filename, charsetname); - } - - public boolean isOk(){ - return _ok; - } - - private void init(String filename, String charsetname){ - + public MetaData(String filename, String charsetname) { + boolean ok = false; + MappedByteBuffer header = null; + MappedByteBuffer data = null; _charset = Charset.forName(charsetname); - FileInputStream file; - try { - file = new FileInputStream(filename); + try (FileInputStream file = new FileInputStream(filename)) { + header = file.getChannel().map(MapMode.READ_ONLY,0,256); + header.order(ByteOrder.LITTLE_ENDIAN); + if (h_magic()!=-2025936501){ + System.out.print("MetaData bad magic " + h_magic() +"\n"); + } else { + data = file.getChannel().map(MapMode.READ_ONLY, 256, h_size()); + data.order(ByteOrder.LITTLE_ENDIAN); + ok = true; + } } catch (FileNotFoundException e) { System.out.print("MetaData file " + filename + " not found.\n"); - return; - } - - try { - _header = file.getChannel().map(MapMode.READ_ONLY,0,256); - _header.order(ByteOrder.LITTLE_ENDIAN); - if(h_magic()!=-2025936501){ - System.out.print("MetaData bad magic " + h_magic() +"\n"); - return; - } - _data = file.getChannel().map(MapMode.READ_ONLY, - 256, - h_size()); - _data.order(ByteOrder.LITTLE_ENDIAN); - _ok=true; } catch (IOException e) { System.out.print("MetaData IO exception.\n"); - return; } + _ok = ok; + _header = header; + _data = data; + } + + public boolean isOk() { + return _ok; } private int h_magic(){ diff --git a/fsa/src/main/java/com/yahoo/fsa/topicpredictor/TopicPredictor.java b/fsa/src/main/java/com/yahoo/fsa/topicpredictor/TopicPredictor.java index 00c0c91bf59e..cdb9323f46b6 100644 --- a/fsa/src/main/java/com/yahoo/fsa/topicpredictor/TopicPredictor.java +++ b/fsa/src/main/java/com/yahoo/fsa/topicpredictor/TopicPredictor.java @@ -4,17 +4,7 @@ import java.util.logging.Logger; import java.util.List; import java.util.LinkedList; -import java.util.Iterator; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.ByteOrder; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.channels.FileChannel.MapMode; -import java.nio.charset.Charset; import com.yahoo.fsa.FSA; import com.yahoo.fsa.MetaData; @@ -27,7 +17,7 @@ * * @author Peter Boros */ -public class TopicPredictor extends MetaData { +public final class TopicPredictor extends MetaData { private static final String packageName = "com.yahoo.fsa.topicpredictor"; @@ -75,19 +65,12 @@ public List getPredictedTopics(String segment, int maxTopics) { int segIdx = getSegmentIndex(segment); int[][] topicArr = getTopicArray(segIdx, maxTopics); - int numTopics = topicArr.length; - int allTopics = getNumTopics(segIdx); - /*Logger.getLogger(packageName). - fine("Segment: '" + segment + "' has " + allTopics + - " topics in automaton, fetched " + numTopics); - */ - for(int i=0; i < numTopics; i++) { - int weight = topicArr[i][1]; - String[] topicInfo= getTopicInfo(topicArr[i][0]); + for (int[] ints : topicArr) { + int weight = ints[1]; + String[] topicInfo = getTopicInfo(ints[0]); String topic = topicInfo[0]; - String vector= topicInfo[1]; - PredictedTopic pt = - new PredictedTopic(topic, (double)weight, vector); + String vector = topicInfo[1]; + PredictedTopic pt = new PredictedTopic(topic, weight, vector); predictedTopics.add(pt); } diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java index 8dbca0bfccde..842fbf7795ff 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java @@ -164,7 +164,9 @@ Duration staggeredDelay(Duration interval, String hostname, List cluster Instant now = clock.instant(); long nodeOffset = clusterHostnames.indexOf(hostname) * interval.toMillis() / clusterHostnames.size(); - long maintainerOffset = getClass().getName().hashCode() % interval.toMillis(); + String c = getClass().getName(); + System.out.println("class=" + c); + long maintainerOffset = c.hashCode() % interval.toMillis(); long totalOffset = nodeOffset + maintainerOffset; return Duration.ofMillis(Math.floorMod(totalOffset - now.toEpochMilli(), interval.toMillis())); } From 642b34dfb277c9a82a5439dd82d32491e8b515b1 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 20 Aug 2024 13:54:51 +0200 Subject: [PATCH 2/2] Drop unintended change from PR. --- .../java/com/yahoo/concurrent/maintenance/Maintainer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java index 842fbf7795ff..8dbca0bfccde 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/Maintainer.java @@ -164,9 +164,7 @@ Duration staggeredDelay(Duration interval, String hostname, List cluster Instant now = clock.instant(); long nodeOffset = clusterHostnames.indexOf(hostname) * interval.toMillis() / clusterHostnames.size(); - String c = getClass().getName(); - System.out.println("class=" + c); - long maintainerOffset = c.hashCode() % interval.toMillis(); + long maintainerOffset = getClass().getName().hashCode() % interval.toMillis(); long totalOffset = nodeOffset + maintainerOffset; return Duration.ofMillis(Math.floorMod(totalOffset - now.toEpochMilli(), interval.toMillis())); }