diff options
author | Harald Musum <musum@yahooinc.com> | 2023-07-07 10:04:57 +0200 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2023-07-07 10:04:57 +0200 |
commit | b23133428a974c467385d625489eaffef03a201a (patch) | |
tree | a0aa6ae3f6fd98d24a4eb0001fd10820df2fb859 /vdslib/src/main | |
parent | 767e01520450c288b5e8161c08fa2a9ddcfff1df (diff) |
Cleanup, simplify, remove dead code. No functional changes
Diffstat (limited to 'vdslib/src/main')
10 files changed, 70 insertions, 193 deletions
diff --git a/vdslib/src/main/java/com/yahoo/vdslib/BucketDistribution.java b/vdslib/src/main/java/com/yahoo/vdslib/BucketDistribution.java index c49fdb93d20..ba17b947bb8 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/BucketDistribution.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/BucketDistribution.java @@ -4,6 +4,7 @@ package com.yahoo.vdslib; import com.yahoo.document.BucketId; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,16 +15,16 @@ import java.util.logging.Logger; public class BucketDistribution { // A logger object to enable proper logging. - private static Logger log = Logger.getLogger(BucketDistribution.class.getName()); + private static final Logger log = Logger.getLogger(BucketDistribution.class.getName()); // A map from bucket id to column index. - private int[] bucketToColumn; + private final int[] bucketToColumn; // The number of columns to distribute to. private int numColumns; // The number of bits to use for bucket identification. - private int numBucketBits; + private final int numBucketBits; /** * Constructs a new bucket distribution object with a given number of columns and buckets. @@ -68,7 +69,7 @@ public class BucketDistribution { * @return The bucket distribution. */ private static List<Integer> getBucketCount(int numColumns, int numBucketBits) { - List<Integer> ret = new ArrayList<Integer>(numColumns); + List<Integer> ret = new ArrayList<>(numColumns); int cnt = getNumBuckets(numBucketBits) / numColumns; int rst = getNumBuckets(numBucketBits) % numColumns; for (int i = 0; i < numColumns; ++i) { @@ -100,9 +101,7 @@ public class BucketDistribution { * that it all buckets point to that single column. */ public void reset() { - for (int i = 0; i < bucketToColumn.length; ++i) { - bucketToColumn[i] = 0; - } + Arrays.fill(bucketToColumn, 0); numColumns = 1; } @@ -152,32 +151,6 @@ public class BucketDistribution { } /** - * Sets the number of buckets to use for this document distribution object. This will reset and setup this object - * from scratch. The original number of columns is maintained. - * - * @param numBucketBits The new number of bits to use for bucket id. - */ - public synchronized void setNumBucketBits(int numBucketBits) { - if (numBucketBits == this.numBucketBits) { - return; - } - this.numBucketBits = numBucketBits; - bucketToColumn = new int[getNumBuckets(numBucketBits)]; - int numColumns = this.numColumns; - reset(); - setNumColumns(numColumns); - } - - /** - * Returns the number of bits used for bucket identifiers. - * - * @return The number of bits. - */ - public int getNumBucketBits() { - return numBucketBits; - } - - /** * Returns the number of buckets available using the configured number of bucket bits. * * @return The number of buckets. diff --git a/vdslib/src/main/java/com/yahoo/vdslib/DocumentSummary.java b/vdslib/src/main/java/com/yahoo/vdslib/DocumentSummary.java index ab5fe0d9a86..4371e19d090 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/DocumentSummary.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/DocumentSummary.java @@ -5,7 +5,8 @@ import com.yahoo.vespa.objects.BufferSerializer; import com.yahoo.vespa.objects.Deserializer; import java.nio.ByteOrder; -import java.io.UnsupportedEncodingException; + +import static java.nio.charset.StandardCharsets.UTF_8; public class DocumentSummary { @@ -27,23 +28,14 @@ public class DocumentSummary { int summarySize = buf.getInt(null); int end = start; while (cArr[end++] != 0); - try { - byte [] sb = new byte [summarySize]; - System.arraycopy(cArr, end, sb, 0, summarySize); - summaries[i] = new Summary(new String(cArr, start, end-start-1, "utf-8"), sb); - start = end + summarySize; - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 apparently not supported"); - } + byte [] sb = new byte [summarySize]; + System.arraycopy(cArr, end, sb, 0, summarySize); + summaries[i] = new Summary(new String(cArr, start, end-start-1, UTF_8), sb); + start = end + summarySize; } } } - /** Constructs a new message from a byte buffer. */ - public DocumentSummary(byte[] buffer) { - this(BufferSerializer.wrap(buffer)); - } - final public int getSummaryCount() { return summaries.length; } final public Summary getSummary(int hitNo) { return summaries[hitNo]; } @@ -63,7 +55,6 @@ public class DocumentSummary { final public String getDocId() { return docId; } final public byte [] getSummary() { return summary; } - final public void setSummary(byte [] summary) { this.summary = summary; } public int compareTo(Summary s) { return getDocId().compareTo(s.getDocId()); diff --git a/vdslib/src/main/java/com/yahoo/vdslib/MetaEntry.java b/vdslib/src/main/java/com/yahoo/vdslib/MetaEntry.java deleted file mode 100644 index a05f746fe48..00000000000 --- a/vdslib/src/main/java/com/yahoo/vdslib/MetaEntry.java +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vdslib; - -import com.yahoo.io.GrowableByteBuffer; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -public class MetaEntry { - public static int REMOVE_ENTRY = 1; - public static int BODY_STRIPPED = 2; - public static int BODY_IN_HEADER = 4; - public static int UPDATE_ENTRY = 8; - public static int COMPRESSED = 16; - - public static int SIZE = 32; - - public long timestamp = 0; - public int headerPos = 0; - public int headerLen = 0; - public int bodyPos = 0; - public int bodyLen = 0; - public byte flags = 0; - - public MetaEntry() { - } - - public MetaEntry(byte[] buffer, int position) { - ByteBuffer buf = ByteBuffer.wrap(buffer, position, SIZE); - buf.order(ByteOrder.LITTLE_ENDIAN); - - timestamp = buf.getLong(); - headerPos = buf.getInt(); - headerLen = buf.getInt(); - bodyPos = buf.getInt(); - bodyLen = buf.getInt(); - flags = buf.get(); - } - - public void serialize(GrowableByteBuffer buf) { - ByteOrder originalOrder = buf.order(); - buf.order(ByteOrder.LITTLE_ENDIAN); - buf.putLong(timestamp); // 8 - buf.putInt(headerPos); // 12 - buf.putInt(headerLen); // 16 - buf.putInt(bodyPos); // 20 - buf.putInt(bodyLen); // 24 - buf.putInt(flags); // 28 (written as little-endian int, this is on purpose) - buf.putInt(0); // 32 - buf.order(originalOrder); - } -} diff --git a/vdslib/src/main/java/com/yahoo/vdslib/SearchResult.java b/vdslib/src/main/java/com/yahoo/vdslib/SearchResult.java index b7c9b1b71b5..5ef3e82b1e6 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/SearchResult.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/SearchResult.java @@ -1,19 +1,22 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vdslib; + import com.yahoo.data.access.helpers.MatchFeatureData; import com.yahoo.vespa.objects.BufferSerializer; import com.yahoo.vespa.objects.Deserializer; -import java.io.UnsupportedEncodingException; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Map; import java.util.Optional; import java.util.TreeMap; +import static java.nio.charset.StandardCharsets.UTF_8; + public class SearchResult { + public static class Hit implements Comparable<Hit> { - private String docId; + private final String docId; private double rank; private MatchFeatureData.HitValue matchFeatures; public Hit(Hit h) { @@ -40,7 +43,7 @@ public class SearchResult { } } public static class HitWithSortBlob extends Hit { - private byte [] sortBlob; + private final byte [] sortBlob; public HitWithSortBlob(Hit h, byte [] sb) { super(h); sortBlob = sb; @@ -57,12 +60,11 @@ public class SearchResult { return sortBlob.length - b.sortBlob.length; } } - private int totalHits; - private Hit[] hits; - private TreeMap<Integer, byte []> aggregatorList; - private TreeMap<Integer, byte []> groupingList; - private static int EXTENSION_FLAGS_PRESENT = -1; - private static int MATCH_FEATURES_PRESENT_MASK = 1; + private final int totalHits; + private final Hit[] hits; + private final TreeMap<Integer, byte []> groupingList; + private static final int EXTENSION_FLAGS_PRESENT = -1; + private static final int MATCH_FEATURES_PRESENT_MASK = 1; public SearchResult(Deserializer buf) { BufferSerializer bser = (BufferSerializer) buf; // TODO: dirty cast. must do this differently @@ -76,17 +78,12 @@ public class SearchResult { } hits = new Hit[numHits]; if (numHits != 0) { - int docIdBufferLength = buf.getInt(null); byte[] cArr = bser.getBuf().array(); int start = bser.getBuf().arrayOffset() + bser.position(); for(int i=0; i < numHits; i++) { int end = start; while (cArr[end++] != 0); - try { - hits[i] = new Hit(new String(cArr, start, end-start-1, "utf-8"), 0); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 apparently not supported"); - } + hits[i] = new Hit(new String(cArr, start, end-start-1, UTF_8), 0); start = end; } bser.position(start - bser.getBuf().arrayOffset()); @@ -104,16 +101,8 @@ public class SearchResult { hits[i] = new HitWithSortBlob(hits[i], buf.getBytes(null, size[i])); } - int numAggregators = buf.getInt(null); - aggregatorList = new TreeMap<Integer, byte []>(); - for (int i = 0; i < numAggregators; i++) { - int aggrId = buf.getInt(null); - int aggrLength = buf.getInt(null); - aggregatorList.put(aggrId, buf.getBytes(null, aggrLength)); - } - int numGroupings = buf.getInt(null); - groupingList = new TreeMap<Integer, byte []>(); + groupingList = new TreeMap<>(); for (int i = 0; i < numGroupings; i++) { int aggrId = buf.getInt(null); int aggrLength = buf.getInt(null); @@ -159,18 +148,8 @@ public class SearchResult { return featureType == 0; } - /** - * Constructs a new message from a byte buffer. - * - * @param buffer A byte buffer that contains a serialized message. - */ - public SearchResult(byte[] buffer) { - this(BufferSerializer.wrap(buffer)); - } - final public int getHitCount() { return hits.length; } final public int getTotalHitCount() { return (totalHits != 0) ? totalHits : getHitCount(); } final public Hit getHit(int hitNo) { return hits[hitNo]; } - final public Map<Integer, byte []> getAggregatorList() { return aggregatorList; } final public Map<Integer, byte []> getGroupingList() { return groupingList; } } diff --git a/vdslib/src/main/java/com/yahoo/vdslib/distribution/ConfiguredNode.java b/vdslib/src/main/java/com/yahoo/vdslib/distribution/ConfiguredNode.java index 92cf8b025e9..965dd018c4f 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/distribution/ConfiguredNode.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/distribution/ConfiguredNode.java @@ -7,31 +7,28 @@ package com.yahoo.vdslib.distribution; * * @author bratseth */ -public class ConfiguredNode implements Comparable<ConfiguredNode> { +public record ConfiguredNode(int index, boolean retired) implements Comparable<ConfiguredNode> { - private final int index; - - private final boolean retired; - - public ConfiguredNode(int index, boolean retired) { - this.index = index; - this.retired = retired; - } - - /** Return the index (distribution key) of this node */ - public int index() { return index; } + /** + * Return the index (distribution key) of this node + */ + @Override + public int index() {return index;} - /** Returns whether the node is configured to be retired */ - public boolean retired() { return retired; } + /** + * Returns whether the node is configured to be retired + */ + @Override + public boolean retired() {return retired;} @Override - public int hashCode() { return index; } + public int hashCode() {return index;} @Override public boolean equals(Object other) { if (other == this) return true; - if ( ! (other instanceof ConfiguredNode)) return false; - return ((ConfiguredNode)other).index == this.index; + if (! (other instanceof ConfiguredNode)) return false; + return ((ConfiguredNode) other).index == this.index; } @Override diff --git a/vdslib/src/main/java/com/yahoo/vdslib/distribution/Distribution.java b/vdslib/src/main/java/com/yahoo/vdslib/distribution/Distribution.java index a83e2a4f89c..bfa7e919514 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/distribution/Distribution.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/distribution/Distribution.java @@ -26,14 +26,7 @@ import java.util.concurrent.atomic.AtomicReference; public class Distribution { - private static class Config { - Config(Group nodeGraph, int redundancy) { - this.nodeGraph = nodeGraph; - this.redundancy = redundancy; - } - - private final Group nodeGraph; - private final int redundancy; + private record Config(Group nodeGraph, int redundancy) { } private ConfigSubscriber configSub; @@ -197,8 +190,8 @@ public class Distribution { } private static class ScoredGroup implements Comparable<ScoredGroup> { - Group group; - double score; + final Group group; + final double score; ScoredGroup(Group g, double score) { this.group = g; this.score = score; } @@ -266,8 +259,8 @@ public class Distribution { } private static class ResultGroup implements Comparable<ResultGroup> { - Group group; - int redundancy; + final Group group; + final int redundancy; ResultGroup(Group group, int redundancy) { this.group = group; @@ -489,14 +482,11 @@ public class Distribution { public Set<ConfiguredNode> getNodes() { final Set<ConfiguredNode> nodes = new HashSet<>(); - GroupVisitor visitor = new GroupVisitor() { - @Override - public boolean visitGroup(Group g) { - if (g.isLeafGroup()) { - nodes.addAll(g.getNodes()); - } - return true; + GroupVisitor visitor = g -> { + if (g.isLeafGroup()) { + nodes.addAll(g.getNodes()); } + return true; }; visitGroups(visitor); return nodes; diff --git a/vdslib/src/main/java/com/yahoo/vdslib/distribution/Group.java b/vdslib/src/main/java/com/yahoo/vdslib/distribution/Group.java index b926ee3be8d..c1c2eef5c8f 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/distribution/Group.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/distribution/Group.java @@ -9,13 +9,13 @@ import java.text.ParseException; */ public class Group implements Comparable<Group> { - private String name; + private final String name; private Group parent = null; - private int index; + private final int index; private int distributionHash; - private Distribution distribution = null; + private final Distribution distribution; private double capacity; - private Map<Integer, Group> subgroups; + private final Map<Integer, Group> subgroups; private List<ConfiguredNode> nodes; public Group(int index, String name) { @@ -63,8 +63,7 @@ public class Group implements Comparable<Group> { @Override public boolean equals(Object o) { if (o == this) return true; - if ( ! (o instanceof Group)) { return false; } - Group other = (Group) o; + if ( ! (o instanceof Group other)) { return false; } if ( ! name.equals(other.name) || index != other.index || (distribution == null ^ other.distribution == null) @@ -210,7 +209,7 @@ public class Group implements Comparable<Group> { for (int i=0; i<distributionSpec.length; ++i) { String token = st.nextToken(); try{ - distributionSpec[i] = (token.equals("*") ? 0 : Integer.valueOf(token)); + distributionSpec[i] = (token.equals("*") ? 0 : Integer.parseInt(token)); } catch (NumberFormatException e) { throw new ParseException("Illegal distribution spec \"" + serialized + "\". Copy counts must be integer values in the range 1-255.", i); } @@ -243,9 +242,9 @@ public class Group implements Comparable<Group> { int asterixCount = distributionSpec.length - firstAsterix; int[][] preCalculations = new int[maxRedundancy + 1][]; for (int i=1; i<=maxRedundancy; ++i) { - List<Integer> spec = new ArrayList<Integer>(); - for (int j=0; j<distributionSpec.length; ++j) { - spec.add(distributionSpec[j]); + List<Integer> spec = new ArrayList<>(); + for (int k : distributionSpec) { + spec.add(k); } int remainingRedundancy = i; for (int j=0; j<firstAsterix; ++j) { @@ -277,8 +276,7 @@ public class Group implements Comparable<Group> { @Override public boolean equals(Object o) { if (o == this) return true; - if ( ! (o instanceof Distribution)) return false; - Distribution other = (Distribution) o; + if ( ! (o instanceof Distribution other)) return false; return (distributionSpec == other.distributionSpec && preCalculatedResults.length == other.preCalculatedResults.length); } diff --git a/vdslib/src/main/java/com/yahoo/vdslib/distribution/GroupVisitor.java b/vdslib/src/main/java/com/yahoo/vdslib/distribution/GroupVisitor.java index df5a6e5a9d1..1108ce7507d 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/distribution/GroupVisitor.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/distribution/GroupVisitor.java @@ -3,6 +3,6 @@ package com.yahoo.vdslib.distribution; public interface GroupVisitor { - public boolean visitGroup(Group g); + boolean visitGroup(Group g); } diff --git a/vdslib/src/main/java/com/yahoo/vdslib/state/ClusterState.java b/vdslib/src/main/java/com/yahoo/vdslib/state/ClusterState.java index 4bf305e65e0..30a209b6754 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/state/ClusterState.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/state/ClusterState.java @@ -24,7 +24,7 @@ public class ClusterState implements Cloneable { /** * Maintains a bitset where all non-down nodes have a bit set. All nodes that differ from defaultUp - * and defaultDown are store explicit in a hash map. + * and defaultDown are stored explicitly in a hash map. */ private static class Nodes { private int logicalNodeCount; diff --git a/vdslib/src/main/java/com/yahoo/vdslib/state/Diff.java b/vdslib/src/main/java/com/yahoo/vdslib/state/Diff.java index f4eb9ff8dde..8a4bedddaeb 100644 --- a/vdslib/src/main/java/com/yahoo/vdslib/state/Diff.java +++ b/vdslib/src/main/java/com/yahoo/vdslib/state/Diff.java @@ -8,8 +8,9 @@ import java.util.List; * TODO: document this */ public class Diff { + public static class Entry { - String id; + final String id; // Values set for entries that contain diff themselves String preContent; String postContent; @@ -32,22 +33,22 @@ public class Diff { public Entry bold() { bold = true; return this; } public Entry splitLine() { splitLine = true; return this; } } - private List<Entry> diff = new LinkedList<Entry>(); + private final List<Entry> diff = new LinkedList<>(); public void add(Entry e) { diff.add(e); } public boolean differs() { return (!diff.isEmpty()); } - class PrintProperties { + static class PrintProperties { boolean insertLineBreaks = false; - boolean ommitGroupForSingleEntries = true; + final boolean ommitGroupForSingleEntries = true; String lineBreak = "\n"; - String entrySeparator = ", "; - String idValueSeparator = ": "; + final String entrySeparator = ", "; + final String idValueSeparator = ": "; String keyValueSeparator = " => "; - String singleGroupSeparator = ""; - String groupStart = "["; - String groupStop = "]"; + final String singleGroupSeparator = ""; + final String groupStart = "["; + final String groupStop = "]"; String indent = " "; String boldStart = ""; String boldStop = ""; |