You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by hu...@apache.org on 2020/10/10 03:35:59 UTC

[kafka] branch trunk updated: KAFKA-10584:IndexSearchType should use sealed trait instead of Enumeration (#9399)

This is an automated email from the ASF dual-hosted git repository.

huxihx pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a73bf59  KAFKA-10584:IndexSearchType should use sealed trait instead of Enumeration (#9399)
a73bf59 is described below

commit a73bf5931aaf1510f0efa86bc7d61ff8aab046c2
Author: huxi <hu...@hotmail.com>
AuthorDate: Sat Oct 10 11:34:45 2020 +0800

    KAFKA-10584:IndexSearchType should use sealed trait instead of Enumeration (#9399)
    
    https://issues.apache.org/jira/browse/KAFKA-10584
    
    In Scala, we prefer sealed traits over Enumeration since the former gives you exhaustiveness checking. With Scala Enumeration, you don't get a warning if you add a new value that is not handled in a given pattern match.
---
 core/src/main/scala/kafka/log/AbstractIndex.scala | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/core/src/main/scala/kafka/log/AbstractIndex.scala b/core/src/main/scala/kafka/log/AbstractIndex.scala
index 8b6110e..5d7de88 100644
--- a/core/src/main/scala/kafka/log/AbstractIndex.scala
+++ b/core/src/main/scala/kafka/log/AbstractIndex.scala
@@ -24,7 +24,6 @@ import java.nio.{ByteBuffer, MappedByteBuffer}
 import java.util.concurrent.locks.{Lock, ReentrantLock}
 
 import kafka.common.IndexOffsetOverflowException
-import kafka.log.IndexSearchType.IndexSearchEntity
 import kafka.utils.CoreUtils.inLock
 import kafka.utils.{CoreUtils, Logging}
 import org.apache.kafka.common.utils.{ByteBufferUnmapper, OperatingSystem, Utils}
@@ -359,19 +358,19 @@ abstract class AbstractIndex(@volatile private var _file: File, val baseOffset:
    * @param target The index key to look for
    * @return The slot found or -1 if the least entry in the index is larger than the target key or the index is empty
    */
-  protected def largestLowerBoundSlotFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchEntity): Int =
+  protected def largestLowerBoundSlotFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchType): Int =
     indexSlotRangeFor(idx, target, searchEntity)._1
 
   /**
    * Find the smallest entry greater than or equal the target key or value. If none can be found, -1 is returned.
    */
-  protected def smallestUpperBoundSlotFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchEntity): Int =
+  protected def smallestUpperBoundSlotFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchType): Int =
     indexSlotRangeFor(idx, target, searchEntity)._2
 
   /**
    * Lookup lower and upper bounds for the given target.
    */
-  private def indexSlotRangeFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchEntity): (Int, Int) = {
+  private def indexSlotRangeFor(idx: ByteBuffer, target: Long, searchEntity: IndexSearchType): (Int, Int) = {
     // check if the index is empty
     if(_entries == 0)
       return (-1, -1)
@@ -407,7 +406,7 @@ abstract class AbstractIndex(@volatile private var _file: File, val baseOffset:
     binarySearch(0, firstHotEntry)
   }
 
-  private def compareIndexEntry(indexEntry: IndexEntry, target: Long, searchEntity: IndexSearchEntity): Int = {
+  private def compareIndexEntry(indexEntry: IndexEntry, target: Long, searchEntity: IndexSearchType): Int = {
     searchEntity match {
       case IndexSearchType.KEY => java.lang.Long.compare(indexEntry.indexKey, target)
       case IndexSearchType.VALUE => java.lang.Long.compare(indexEntry.indexValue, target)
@@ -434,7 +433,8 @@ object AbstractIndex extends Logging {
   override val loggerName: String = classOf[AbstractIndex].getName
 }
 
-object IndexSearchType extends Enumeration {
-  type IndexSearchEntity = Value
-  val KEY, VALUE = Value
+sealed trait IndexSearchType
+object IndexSearchType {
+  case object KEY extends IndexSearchType
+  case object VALUE extends IndexSearchType
 }