From 436e23b8d4fb5271263256cb163d8d59722f8d6b Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 8 May 2013 22:06:01 +0200 Subject: [PATCH] Use simplified asserts and better naming addOne / subOne are likely easier to understand without reading the docs. If I would read my emails this would have made it in the last commit. --- .../search/dfs/AggregatedDfs.java | 2 +- .../search/dfs/DfsSearchResult.java | 33 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/dfs/AggregatedDfs.java b/src/main/java/org/elasticsearch/search/dfs/AggregatedDfs.java index ee52b0398e9..7577d95a74a 100644 --- a/src/main/java/org/elasticsearch/search/dfs/AggregatedDfs.java +++ b/src/main/java/org/elasticsearch/search/dfs/AggregatedDfs.java @@ -89,7 +89,7 @@ public class AggregatedDfs implements Streamable { TermStatistics stats = termTermStatisticsEntry.getValue(); out.writeBytesRef(stats.term()); out.writeVLong(stats.docFreq()); - out.writeVLong(DfsSearchResult.makePositive(stats.totalTermFreq())); + out.writeVLong(DfsSearchResult.plusOne(stats.totalTermFreq())); } DfsSearchResult.writeFieldStats(out, fieldStatistics); out.writeVLong(maxDoc); diff --git a/src/main/java/org/elasticsearch/search/dfs/DfsSearchResult.java b/src/main/java/org/elasticsearch/search/dfs/DfsSearchResult.java index 4c8cbb60d20..03c865bd962 100644 --- a/src/main/java/org/elasticsearch/search/dfs/DfsSearchResult.java +++ b/src/main/java/org/elasticsearch/search/dfs/DfsSearchResult.java @@ -149,9 +149,9 @@ public class DfsSearchResult extends TransportResponse implements SearchPhaseRes out.writeString(entry.getKey()); assert entry.getValue().maxDoc() >= 0; out.writeVLong(entry.getValue().maxDoc()); - out.writeVLong(makePositive(entry.getValue().docCount())); - out.writeVLong(makePositive(entry.getValue().sumTotalTermFreq())); - out.writeVLong(makePositive(entry.getValue().sumDocFreq())); + out.writeVLong(addOne(entry.getValue().docCount())); + out.writeVLong(addOne(entry.getValue().sumTotalTermFreq())); + out.writeVLong(addOne(entry.getValue().sumDocFreq())); } } @@ -165,7 +165,7 @@ public class DfsSearchResult extends TransportResponse implements SearchPhaseRes public static void writeSingleTermStats(StreamOutput out, TermStatistics termStatistic) throws IOException { assert termStatistic.docFreq() >= 0; out.writeVLong(termStatistic.docFreq()); - out.writeVLong(makePositive(termStatistic.totalTermFreq())); + out.writeVLong(addOne(termStatistic.totalTermFreq())); } public static Map readFieldStats(StreamInput in) throws IOException { @@ -181,9 +181,9 @@ public class DfsSearchResult extends TransportResponse implements SearchPhaseRes final String field = in.readString(); assert field != null; final long maxDoc = in.readVLong(); - final long docCount = toNotAvailable(in.readVLong()); - final long sumTotalTermFreq = toNotAvailable(in.readVLong()); - final long sumDocFreq = toNotAvailable(in.readVLong()); + final long docCount = subOne(in.readVLong()); + final long sumTotalTermFreq = subOne(in.readVLong()); + final long sumDocFreq = subOne(in.readVLong()); CollectionStatistics stats = new CollectionStatistics(field, maxDoc, docCount, sumTotalTermFreq, sumDocFreq); fieldStatistics.put(field, stats); } @@ -202,7 +202,7 @@ public class DfsSearchResult extends TransportResponse implements SearchPhaseRes BytesRef term = terms[i].bytes(); final long docFreq = in.readVLong(); assert docFreq >= 0; - final long totalTermFreq = toNotAvailable(in.readVLong()); + final long totalTermFreq = subOne(in.readVLong()); termStatistics[i] = new TermStatistics(term, docFreq, totalTermFreq); } } @@ -215,14 +215,19 @@ public class DfsSearchResult extends TransportResponse implements SearchPhaseRes * Since we are using var longs to encode values we add one to each value * to ensure we don't waste space and don't add negative values. */ - public static long makePositive(long value) { - assert Math.signum(value+1) >= 0; - return value+1; + public static long addOne(long value) { + assert value + 1 >= 0; + return value + 1; } - public static long toNotAvailable(long value) { - assert Math.signum(value) >= 0; - return value-1; + + /* + * See #addOne this just subtracting one and asserts that the actual value + * is positive. + */ + public static long subOne(long value) { + assert value >= 0; + return value - 1; } }