<commit_before>package com.twitter.finagle.exp.zookeeper

import com.twitter.util.Try
import com.twitter.finagle.exp.zookeeper.transport.{Buffer, BufferReader}
import org.jboss.netty.buffer.ChannelBuffer
import org.jboss.netty.buffer.ChannelBuffers._
import java.nio.ByteBuffer

/**
 * This File describes every responses
 * A response is usually composed by a header + a body
 * but there are some exceptions where there is only a header (ReplyHeader).
 * For example a CreateResponse is composed by a ReplyHeader and a CreateResponseBody
 * this way : new CreateResponse(h: ReplyHeader, b: CreateResponseBody).
 *
 * That's the reason why header extends Response.
 *
 * A BufferedResponse is a raw container (buffer) of response, as we have many
 * different responses, we use a common wrapper, that we can then decode with
 * the request opCode. With a BufferedResponse and an opCode we can give a Response
 *
 * */

sealed trait Response
sealed trait Header extends Response
sealed trait ResponseBody
sealed trait Decoder[T <: Response] extends (Buffer => Try[T]) {
  def apply(buffer: Buffer): Try[T] = Try(decode(buffer))
  def decode(buffer: Buffer): T
}

case class BufferedResponse(buffer: Buffer) extends Response
case class ConnectResponse(
                            protocolVersion: Int,
                            timeOut: Int,
                             sessionId: Long,
                            passwd: Array[Byte],
                            canRO: Option[Boolean]
                            ) extends Response with ResponseBody

case class CreateResponseBody(path: String) extends ResponseBody
case class CreateResponse(header: ReplyHeader, body: Option[CreateResponseBody]) extends Response
case class ExistsResponseBody(stat: Stat) extends ResponseBody
case class ExistsResponse(header: ReplyHeader, body: Option[ExistsResponseBody]) extends Response
case class ErrorResponseBody(err: Int) extends ResponseBody
case class ErrorResponse(header: ReplyHeader, body: Option[ErrorResponseBody]) extends Response
case class GetACLResponseBody(acl: Array[ACL], stat: Stat) extends ResponseBody
case class GetACLResponse(header: ReplyHeader, body: Option[GetACLResponseBody]) extends Response
case class GetChildrenResponseBody(children: Array[String]) extends ResponseBody
case class GetChildrenResponse(header: ReplyHeader, body: Option[GetChildrenResponseBody]) extends Response
case class GetChildren2ResponseBody(children: Array[String], stat:Stat) extends ResponseBody
case class GetChildren2Response(header: ReplyHeader, body: Option[GetChildren2ResponseBody]) extends Response
case class GetDataResponseBody(data: Array[Byte], stat: Stat) extends ResponseBody
case class GetDataResponse(header: ReplyHeader, body: Option[GetDataResponseBody]) extends Response
case class ReplyHeader(xid: Int, zxid: Long,
                       err: Int) extends Header with ResponseBody
case class SetACLResponseBody(stat: Stat) extends ResponseBody
case class SetACLResponse(header: ReplyHeader, body: Option[SetACLResponseBody]) extends Response
case class SetDataResponseBody(stat: Stat) extends ResponseBody
case class SetDataResponse(header: ReplyHeader, body: Option[SetDataResponseBody]) extends Response
case class SyncResponseBody(path: String) extends ResponseBody
case class SyncResponse(header: ReplyHeader, body: Option[SyncResponseBody]) extends Response
case class WatcherEventBody(typ: Int, state: Int, path: String) extends ResponseBody
case class WatcherEvent(header: ReplyHeader, body: Option[WatcherEventBody]) extends Response

/* To create a BufferedResponse from different objects*/
object BufferedResponse {
  def factory(buffer: Buffer) = new BufferedResponse(buffer)
  def factory(buffer: ChannelBuffer) = new BufferedResponse(Buffer.fromChannelBuffer(buffer))
  def factory(buffer: ByteBuffer) = new BufferedResponse(Buffer.fromChannelBuffer(wrappedBuffer(buffer)))
}

object ConnectResponse extends Decoder[ConnectResponse] {
  override def decode(buffer: Buffer): ConnectResponse = {
    val br = BufferReader(buffer)

    // read packet size
    br.readInt
    val protocolVersion = br.readInt
    val timeOut = br.readInt
    val sessionId = br.readLong
    val passwd: Array[Byte] = br.readBuffer
    val canRO: Option[Boolean] = {
      try {
        Some(br.readBool)
      } catch {
        case ex:Exception => throw ex
      }
    }

    new ConnectResponse(protocolVersion,
      timeOut,
      sessionId,
      passwd,
      canRO)
  }
}
<commit_msg>Renamed ResponseWrapper to ResponseDecoder<commit_after>package com.twitter.finagle.exp.zookeeper

import com.twitter.util.Try
import com.twitter.finagle.exp.zookeeper.transport.{Buffer, BufferReader}
import org.jboss.netty.buffer.ChannelBuffer
import org.jboss.netty.buffer.ChannelBuffers._
import java.nio.ByteBuffer
import com.twitter.finagle.exp.zookeeper.ZookeeperDefinitions.opCode._
import scala.Some

/**
 * This File describes every responses
 * A response is usually composed by a header + a body
 * but there are some exceptions where there is only a header (ReplyHeader).
 * For example a CreateResponse is composed by a ReplyHeader and a CreateResponseBody
 * this way : new CreateResponse(h: ReplyHeader, b: CreateResponseBody).
 *
 * That's the reason why header extends Response.
 *
 * A BufferedResponse is a raw container (buffer) of response, as we have many
 * different responses, we use a common wrapper, that we can then decode with
 * the request opCode. With a BufferedResponse and an opCode we can give a Response
 *
 * */

sealed abstract class Response
sealed trait Header extends Response
sealed trait ResponseBody
sealed trait Decoder[T <: Response] extends (Buffer => Try[T]) {
  def apply(buffer: Buffer): Try[T] = Try(decode(buffer))
  def decode(buffer: Buffer): T
}

case class BufferedResponse(buffer: Buffer) extends Response
case class ConnectResponse(
                            protocolVersion: Int,
                            timeOut: Int,
                             sessionId: Long,
                            passwd: Array[Byte],
                            canRO: Option[Boolean]
                            ) extends Response with ResponseBody

case class CreateResponseBody(path: String) extends ResponseBody
case class CreateResponse(header: ReplyHeader, body: Option[CreateResponseBody]) extends Response
case class ExistsResponseBody(stat: Stat) extends ResponseBody
case class ExistsResponse(header: ReplyHeader, body: Option[ExistsResponseBody]) extends Response
case class ErrorResponseBody(err: Int) extends ResponseBody
case class ErrorResponse(header: ReplyHeader, body: Option[ErrorResponseBody]) extends Response
case class GetACLResponseBody(acl: Array[ACL], stat: Stat) extends ResponseBody
case class GetACLResponse(header: ReplyHeader, body: Option[GetACLResponseBody]) extends Response
case class GetChildrenResponseBody(children: Array[String]) extends ResponseBody
case class GetChildrenResponse(header: ReplyHeader, body: Option[GetChildrenResponseBody]) extends Response
case class GetChildren2ResponseBody(children: Array[String], stat:Stat) extends ResponseBody
case class GetChildren2Response(header: ReplyHeader, body: Option[GetChildren2ResponseBody]) extends Response
case class GetDataResponseBody(data: Array[Byte], stat: Stat) extends ResponseBody
case class GetDataResponse(header: ReplyHeader, body: Option[GetDataResponseBody]) extends Response
case class ReplyHeader(xid: Int, zxid: Long,
                       err: Int) extends Header with ResponseBody
case class SetACLResponseBody(stat: Stat) extends ResponseBody
case class SetACLResponse(header: ReplyHeader, body: Option[SetACLResponseBody]) extends Response
case class SetDataResponseBody(stat: Stat) extends ResponseBody
case class SetDataResponse(header: ReplyHeader, body: Option[SetDataResponseBody]) extends Response
case class SyncResponseBody(path: String) extends ResponseBody
case class SyncResponse(header: ReplyHeader, body: Option[SyncResponseBody]) extends Response
case class WatcherEventBody(typ: Int, state: Int, path: String) extends ResponseBody
case class WatcherEvent(header: ReplyHeader, body: Option[WatcherEventBody]) extends Response

/* To create a BufferedResponse from different objects*/
object BufferedResponse {
  def factory(buffer: Buffer) = new BufferedResponse(buffer)
  def factory(buffer: ChannelBuffer) = new BufferedResponse(Buffer.fromChannelBuffer(buffer))
  def factory(buffer: ByteBuffer) = new BufferedResponse(Buffer.fromChannelBuffer(wrappedBuffer(buffer)))
}

object ResponseDecoder {
  /* Decode a BufferedResponse to type Try[T] by pattern matching opCode (XID) */
  def decode[T >: Response](repBuffer: BufferedResponse, opCode: Int): Try[T] = opCode match {
    case `createSession` => ConnectResponse(repBuffer.buffer)
    case `ping` => ReplyHeader(repBuffer.buffer)
    case `closeSession` => ReplyHeader(repBuffer.buffer)
    case `create` => CreateResponse(repBuffer.buffer)
    case `delete` => ReplyHeader(repBuffer.buffer)
    case `exists` => ExistsResponse(repBuffer.buffer)
    case `getACL` => GetACLResponse(repBuffer.buffer)
    case `getChildren` => GetChildrenResponse(repBuffer.buffer)
    case `getChildren2` => GetChildren2Response(repBuffer.buffer)
    case `getData` => GetDataResponse(repBuffer.buffer)
    case `setData` => SetDataResponse(repBuffer.buffer)
    case `setACL` => SetACLResponse(repBuffer.buffer)
    case `sync` => SyncResponse(repBuffer.buffer)
    case `setWatches` => ReplyHeader(repBuffer.buffer)
  }
}

object ConnectResponse extends Decoder[ConnectResponse] {
  override def decode(buffer: Buffer): ConnectResponse = {
    val br = BufferReader(buffer)

    // read packet size
    br.readInt
    val protocolVersion = br.readInt
    val timeOut = br.readInt
    val sessionId = br.readLong
    val passwd: Array[Byte] = br.readBuffer
    val canRO: Option[Boolean] = {
      try {
        Some(br.readBool)
      } catch {
        case ex:Exception => throw ex
      }
    }

    new ConnectResponse(protocolVersion,
      timeOut,
      sessionId,
      passwd,
      canRO)
  }
}

{'fraction_non_alphanumeric': 0.07794902193242442, 'fraction_numerical': 0.0007903576368306659, 'mean_word_length': 4.580485115766263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5562804', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2606, 'n_words': 872}