Skip to content

Commit

Permalink
Enable scalafmt in Source: Part 10
Browse files Browse the repository at this point in the history
Scalafmt is being enabled in Source: please see http://go/scalafmt for more information.

RB_ID=920474
TBR=true
NO_USER_HOOK=1
  • Loading branch information
Stu Hood authored and jenkins committed Aug 4, 2017
1 parent 9ab0579 commit 8aefcd6
Show file tree
Hide file tree
Showing 44 changed files with 2,656 additions and 1,398 deletions.
Expand Up @@ -9,7 +9,7 @@ import org.scalatest.{MustMatchers, WordSpec}

@RunWith(classOf[JUnitRunner])
class DemoSpec extends WordSpec with MustMatchers {
def printUser(user: User) {println("User %s, id %d".format(user.name, user.id))}
def printUser(user: User) { println("User %s, id %d".format(user.name, user.id)) }

def await[A](f: Future[A]): A = Await.result(f, 5.seconds)

Expand All @@ -22,4 +22,3 @@ class DemoSpec extends WordSpec with MustMatchers {
}
}
}

Expand Up @@ -55,7 +55,7 @@ private[adapt] object AdaptAsmPruner {
def isUnusedField(fieldName: String): Boolean =
useMap.get(fieldName) match {
case Some(used) => !used
case None => false
case None => false
}

// Get rid of unused field members
Expand Down Expand Up @@ -139,7 +139,7 @@ private[adapt] object AdaptAsmPruner {
insn match {
case f: FieldInsnNode =>
f.owner == "com/twitter/scrooge/adapt/AdaptTProtocol$" &&
f.name == "MODULE$"
f.name == "MODULE$"
case _ => false
}

Expand Down Expand Up @@ -217,16 +217,20 @@ private[adapt] object AdaptAsmPruner {
case Opcodes.ICONST_4 => 4
case Opcodes.ICONST_5 => 5
case Opcodes.ICONST_M1 => -1
case _ => throw new IllegalStateException(
s"Unexpected opcode ${i.getOpcode} while trying to read fieldId")
case _ =>
throw new IllegalStateException(
s"Unexpected opcode ${i.getOpcode} while trying to read fieldId"
)
}
case i: IntInsnNode =>
i.getOpcode match {
case Opcodes.BIPUSH => i.operand.toShort
case Opcodes.SIPUSH => i.operand.toShort
}
case _ => throw new IllegalStateException(
s"Unexpected instruction $insn while trying to read fieldId")
case _ =>
throw new IllegalStateException(
s"Unexpected instruction $insn while trying to read fieldId"
)
}

/**
Expand Down Expand Up @@ -277,6 +281,7 @@ private[adapt] object AdaptAsmPruner {
): Unit = {
val iter = di.iterator
while (iter.hasNext) {

/**
* Each field has sections in the template for when it is used or unused.
* Based on whether the field is used we keep corresponding section and
Expand All @@ -287,8 +292,8 @@ private[adapt] object AdaptAsmPruner {
case Some(fieldId) =>
deleteMarkedSection(iter, UsedEnd, !useMap(fieldId))
case _ =>
// Note we don't move the iterator forward here to check below
// if this is an unused marker.
// Note we don't move the iterator forward here to check below
// if this is an unused marker.
}
// Read the marker for unused section
readMarker(iter, UnusedStart) match {
Expand Down Expand Up @@ -349,4 +354,3 @@ private[adapt] object AdaptAsmPruner {
classWriter.toByteArray
}
}

@@ -1,6 +1,11 @@
package com.twitter.scrooge.adapt

import com.twitter.scrooge.{TArrayByteTransport, ThriftStruct, ThriftStructCodec, ThriftStructSerializer}
import com.twitter.scrooge.{
TArrayByteTransport,
ThriftStruct,
ThriftStructCodec,
ThriftStructSerializer
}
import java.util
import org.apache.thrift.protocol.{TBinaryProtocol, TProtocolFactory}

Expand Down Expand Up @@ -30,6 +35,7 @@ object AdaptBinaryThriftStructSerializer {
* Thread local cache protocol for a setting.
*/
private def cachedProtocol(settings: AdaptSettings): ProtocolAndTransport = {

/**
* The protocol is mutable but this is threadsafe because we have a
* separate copy for each thread. This way we reuse the underlying
Expand Down Expand Up @@ -61,9 +67,9 @@ object AdaptBinaryThriftStructSerializer {
* @see [[AdaptTProtocol]]
*/
private[this] class AdaptBinaryThriftStructSerializer[T <: ThriftStruct](
val codec: ThriftStructCodec[T],
settings: AdaptSettings)
extends ThriftStructSerializer[T] {
val codec: ThriftStructCodec[T],
settings: AdaptSettings
) extends ThriftStructSerializer[T] {

// Since we only support the fast path reading from the TArrayByteTransport
// we provide the default if someone hits it to be the TBinaryProtocol
Expand All @@ -84,5 +90,3 @@ object AdaptBinaryThriftStructSerializer {
}
}
}


Expand Up @@ -28,17 +28,20 @@ private[adapt] object AdaptTrackingDecoder {
* at runtime.
*/
private[adapt] class AdaptTrackingDecoder[T <: ThriftStruct](
codec: ThriftStructCodec[T],
fallbackDecoder: Decoder[T],
accessRecordingDecoderBuilder: AccessRecorder => Decoder[T],
settings: AdaptSettings,
classLoader: AdaptClassLoader)
extends AccessRecorder with Decoder[T] {
codec: ThriftStructCodec[T],
fallbackDecoder: Decoder[T],
accessRecordingDecoderBuilder: AccessRecorder => Decoder[T],
settings: AdaptSettings,
classLoader: AdaptClassLoader
) extends AccessRecorder
with Decoder[T] {
import AdaptTrackingDecoder._

private[this] val trackedCount = new AtomicInteger()
private[this] val fieldAccessCounts: Map[Short, AtomicInteger] =
codec.metaData.fields.map { f => (f.id, new AtomicInteger(0)) }.toMap
codec.metaData.fields.map { f =>
(f.id, new AtomicInteger(0))
}.toMap

def fieldAccessed(fieldId: Short): Unit =
fieldAccessCounts(fieldId).getAndIncrement()
Expand All @@ -53,9 +56,10 @@ private[adapt] class AdaptTrackingDecoder[T <: ThriftStruct](
(f, fieldAccessCounts(f.id).get >= settings.useThreshold)
}.toMap

val useMapByName = useMapByField.map { case (f, v) =>
val normalizedName = CaseConverter.toCamelCase(f.name)
(normalizedName, v)
val useMapByName = useMapByField.map {
case (f, v) =>
val normalizedName = CaseConverter.toCamelCase(f.name)
(normalizedName, v)
}

val useMapById = useMapByField.map { case (f, v) => (f.id, v) }
Expand Down Expand Up @@ -83,12 +87,10 @@ private[adapt] class AdaptTrackingDecoder[T <: ThriftStruct](
val adaptDecoderClassBytes =
AdaptAsmPruner.pruneAdaptDecoder(adaptDecoderFqdn, useMapById)

val decoderClass = classLoader.defineClass(
adaptDecoderFqdn, adaptDecoderClassBytes)
val decoderClass = classLoader.defineClass(adaptDecoderFqdn, adaptDecoderClassBytes)
val prunedDecoder = decoderClass.newInstance()

val decodeMethod = decoderClass.getMethod(DecodeMethodName,
classOf[AdaptTProtocol])
val decodeMethod = decoderClass.getMethod(DecodeMethodName, classOf[AdaptTProtocol])

new Decoder[T] {
def apply(prot: AdaptTProtocol): T = {
Expand All @@ -107,6 +109,7 @@ private[adapt] class AdaptTrackingDecoder[T <: ThriftStruct](
if (adaptiveDecoder != null) {
adaptiveDecoder(prot)
} else {

/**
* Note that we only block one event, one that makes trackedCount
* reach settings.trackedReads, to build the decoder. Subsequent
Expand All @@ -123,4 +126,3 @@ private[adapt] class AdaptTrackingDecoder[T <: ThriftStruct](
}
}
}

Expand Up @@ -6,6 +6,7 @@ import scala.collection.mutable
* Taken from scrooge/scrooge-generator/src/main/scala/com/twitter/scrooge/AST/Identifier.scala
*/
object CaseConverter {

/**
* convert string to camel case, with the following fine print:
* - leading underscores are preserved
Expand Down Expand Up @@ -34,13 +35,15 @@ object CaseConverter {
.split('_')
.filterNot(_.isEmpty)
.zipWithIndex
.map { case (part, ind) =>
val first = if (ind == 0) part(0).toLower else part(0).toUpper
val isAllUpperCase = part.forall { c => c.isUpper || !c.isLetter }
val rest = if (isAllUpperCase) part.drop(1).toLowerCase else part.drop(1)
new mutable.StringBuilder(part.size).append(first).append(rest)
.map {
case (part, ind) =>
val first = if (ind == 0) part(0).toLower else part(0).toUpper
val isAllUpperCase = part.forall { c =>
c.isUpper || !c.isLetter
}
val rest = if (isAllUpperCase) part.drop(1).toLowerCase else part.drop(1)
new mutable.StringBuilder(part.size).append(first).append(rest)
}
.mkString
}
}

Expand Up @@ -28,9 +28,10 @@ object TAdaptBinaryProtocol {
* trait to learn more.
*/
class TAdaptBinaryProtocol(
transport: TArrayByteTransport,
context: AdaptContext
) extends TLazyBinaryProtocol(transport) with AdaptTProtocol {
transport: TArrayByteTransport,
context: AdaptContext
) extends TLazyBinaryProtocol(transport)
with AdaptTProtocol {
import TAdaptBinaryProtocol._

def adaptContext: AdaptContext = context
Expand Down
Expand Up @@ -22,11 +22,11 @@ class TrackingAdaptContext(settings: AdaptSettings) extends AdaptContext {
fallbackDecoder,
accessRecordingDecoderBuilder,
settings,
adaptClassLoader)
adaptClassLoader
)
}

def shouldReloadDecoder: Boolean = false

def initCopy(): AdaptContext = new TrackingAdaptContext(settings)
}

0 comments on commit 8aefcd6

Please sign in to comment.