Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-1610

Local modifications to collections generated from mapValues will be lost

Attach filesAttach ScreenshotAdd voteVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Patch Available
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None

    Description

      In our current Scala code base we have 40+ usages of mapValues, however it has an important semantic difference with map, which is that "map" creates a new map collection instance, while "mapValues" just create a map view of the original map, and hence any further value changes to the view will be effectively lost.

      Example code:

      scala> case class Test(i: Int, var j: Int) {}
      defined class Test
      
      scala> val a = collection.mutable.Map(1 -> 1)
      a: scala.collection.mutable.Map[Int,Int] = Map(1 -> 1)
      
      scala> val b = a.mapValues(v => Test(v, v))
      b: scala.collection.Map[Int,Test] = Map(1 -> Test(1,1))
      
      scala> val c = a.map(v => v._1 -> Test(v._2, v._2))
      c: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,1))
      
      scala> b.foreach(kv => kv._2.j = kv._2.j + 1)
      
      scala> b
      res1: scala.collection.Map[Int,Test] = Map(1 -> Test(1,1))
      
      scala> c.foreach(kv => kv._2.j = kv._2.j + 1)
      
      scala> c
      res3: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,2))
      
      scala> a.put(1,3)
      res4: Option[Int] = Some(1)
      
      scala> b
      res5: scala.collection.Map[Int,Test] = Map(1 -> Test(3,3))
      
      scala> c
      res6: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,2))
      

      We need to go through all these mapValue to see if they should be changed to map

      Attachments

        1. KAFKA-1610.patch
          25 kB
          Mayuresh Gharat
        2. KAFKA-1610_2014-10-02_12:09:46.patch
          60 kB
          Mayuresh Gharat
        3. KAFKA-1610_2014-10-02_12:07:01.patch
          59 kB
          Mayuresh Gharat
        4. KAFKA-1610_2014-09-30_23:21:46.patch
          53 kB
          Mayuresh Gharat
        5. KAFKA-1610_2014-09-16_15:23:27.patch
          44 kB
          Mayuresh Gharat
        6. KAFKA-1610_2014-09-16_13:08:17.patch
          35 kB
          Mayuresh Gharat
        7. KAFKA-1610_2014-09-03_11:27:50.patch
          55 kB
          Mayuresh Gharat
        8. KAFKA-1610_2014-08-29_10:03:55.patch
          45 kB
          Mayuresh Gharat
        9. KAFKA-1610_2014-08-29_09:51:51.patch
          45 kB
          Mayuresh Gharat

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            mgharat Mayuresh Gharat
            guozhang Guozhang Wang

            Dates

              Created:
              Updated:

              Slack

                Issue deployment