kafka-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mayuresh Gharat (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (KAFKA-1610) Local modifications to collections generated from mapValues will be lost
Date Wed, 01 Oct 2014 06:22:34 GMT

    [ https://issues.apache.org/jira/browse/KAFKA-1610?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14154426#comment-14154426
] 

Mayuresh Gharat commented on KAFKA-1610:
----------------------------------------

Updated reviewboard https://reviews.apache.org/r/25136/diff/
 against branch origin/trunk

> Local modifications to collections generated from mapValues will be lost
> ------------------------------------------------------------------------
>
>                 Key: KAFKA-1610
>                 URL: https://issues.apache.org/jira/browse/KAFKA-1610
>             Project: Kafka
>          Issue Type: Bug
>            Reporter: Guozhang Wang
>            Assignee: Mayuresh Gharat
>              Labels: newbie
>             Fix For: 0.9.0
>
>         Attachments: KAFKA-1610.patch, KAFKA-1610_2014-08-29_09:51:51.patch, KAFKA-1610_2014-08-29_10:03:55.patch,
KAFKA-1610_2014-09-03_11:27:50.patch, KAFKA-1610_2014-09-16_13:08:17.patch, KAFKA-1610_2014-09-16_15:23:27.patch,
KAFKA-1610_2014-09-30_23:21:46.patch
>
>
> 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:
> {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))
> {code}
> We need to go through all these mapValue to see if they should be changed to map



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message