-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change return type of CLIENT INFO and CLIENT LIST #813
Change return type of CLIENT INFO and CLIENT LIST #813
Conversation
1a48bad
to
728938e
Compare
def getFlags(flags: String): Set[ClientFlag] = { | ||
def clientFlag(cf: Char): Option[ClientFlag] = cf match { | ||
case 'A' => Some(ClientFlag.ToBeClosedAsap) | ||
case 'b' => Some(ClientFlag.Blocked) | ||
case 'B' => Some(ClientFlag.BroadcastTrackingMode) | ||
case 'c' => Some(ClientFlag.ToBeClosedAfterReply) | ||
case 'd' => Some(ClientFlag.WatchedKeysModified) | ||
case 'M' => Some(ClientFlag.IsMaster) | ||
case 'O' => Some(ClientFlag.MonitorMode) | ||
case 'P' => Some(ClientFlag.PubSub) | ||
case 'r' => Some(ClientFlag.ReadOnlyMode) | ||
case 'R' => Some(ClientFlag.TrackingTargetClientInvalid) | ||
case 'S' => Some(ClientFlag.Replica) | ||
case 't' => Some(ClientFlag.KeysTrackingEnabled) | ||
case 'u' => Some(ClientFlag.Unblocked) | ||
case 'U' => Some(ClientFlag.UnixDomainSocket) | ||
case 'x' => Some(ClientFlag.MultiExecContext) | ||
case _ => None | ||
} | ||
|
||
flags.foldLeft(Set.empty[ClientFlag])((fs, f) => fs ++ clientFlag(f)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move these two functions to companion object.
lines.map(from).toList | ||
} | ||
|
||
type ClientsInfo = List[ClientInfo] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this.
val events = data("events") | ||
new ClientInfo( | ||
id = data("id").toLong, | ||
name = data.get("name"), | ||
address = data.get("addr").flatMap(Address.fromString), | ||
localAddress = data.get("laddr").flatMap(Address.fromString), | ||
fileDescriptor = data.get("fd").map(_.toLong), | ||
age = data.get("age").map(s => Duration.fromSeconds(s.toLong)), | ||
idle = data.get("idle").map(s => Duration.fromSeconds(s.toLong)), | ||
flags = data.get("flags").fold(Set.empty[ClientFlag])(getFlags), | ||
databaseId = data.get("id").map(_.toLong), | ||
subscriptions = data("sub").toInt, | ||
patternSubscriptions = data("psub").toInt, | ||
multiCommands = data("multi").toInt, | ||
queryBufferLength = data.get("qbuf").map(_.toInt), | ||
queryBufferFree = data.get("qbuf-free").map(_.toInt), | ||
outputListLength = data.get("oll").map(_.toInt), | ||
outputBufferMem = data.get("omem").map(_.toLong), | ||
events = ClientEvents(readable = events.contains("r"), writable = events.contains("w")), | ||
lastCommand = data.get("cmd"), | ||
argvMemory = data.get("argv-mem").map(_.toLong), | ||
totalMemory = data.get("total-mem").map(_.toLong), | ||
redirectionClientId = data.get("redir").map(_.toLong), | ||
user = data.get("user") | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plenty of unsafe calls here.
case object ClientsInfoOutput extends Output[ClientsInfo] { | ||
protected def tryDecode(respValue: RespValue): ClientsInfo = | ||
respValue match { | ||
case RespValue.BulkString(s) => ClientInfo.from(s.asString.split("\r\n").filter(_.nonEmpty)) | ||
case other => throw ProtocolError(s"$other isn't a bulk string") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this to ClientListOutput
. Also prefer Chunk
to List
, e.g.:
case object ClientListOutput extends Output[Chunk[ClientInfo]] { ... }
You might be able to reuse ClientInfoOutput
in its implementation, though it's not necessary.
object Address { | ||
private[redis] final def fromString(addr: String): Option[Address] = | ||
addr.split(":").toList match { | ||
case ip :: port :: Nil => Some(new Address(InetAddress.getByName(ip), port.toInt)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toIntOption
flag match { | ||
case 'A' => Some(ClientFlag.ToBeClosedAsap) | ||
case 'b' => Some(ClientFlag.Blocked) | ||
case 'B' => Some(ClientFlag.BroadcastTrackingMode) | ||
case 'c' => Some(ClientFlag.ToBeClosedAfterReply) | ||
case 'd' => Some(ClientFlag.WatchedKeysModified) | ||
case 'M' => Some(ClientFlag.IsMaster) | ||
case 'O' => Some(ClientFlag.MonitorMode) | ||
case 'P' => Some(ClientFlag.PubSub) | ||
case 'r' => Some(ClientFlag.ReadOnlyMode) | ||
case 'R' => Some(ClientFlag.TrackingTargetClientInvalid) | ||
case 'S' => Some(ClientFlag.Replica) | ||
case 't' => Some(ClientFlag.KeysTrackingEnabled) | ||
case 'u' => Some(ClientFlag.Unblocked) | ||
case 'U' => Some(ClientFlag.UnixDomainSocket) | ||
case 'x' => Some(ClientFlag.MultiExecContext) | ||
case _ => None | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify via Map. It can be private and lazy.
Description:
ClientInfo
andClientsInfo
output types forCLIENT INFO
andCLIENT LIST
commands instead of plain string.