Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 151 lines (134 sloc) 5.28 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
package com.twitter.finagle.redis.protocol

import _root_.java.lang.{Long => JLong}
import com.twitter.finagle.redis.ClientError
import com.twitter.finagle.redis.protocol.Commands.trimList
import com.twitter.finagle.redis.util._
import org.jboss.netty.buffer.{ChannelBuffer, ChannelBuffers}

object HDel {
  def apply(args: Seq[Array[Byte]]) = {
    RequireClientProtocol(args.length >= 2, "HDEL requires a hash key and at least one field")
    new HDel(ChannelBuffers.wrappedBuffer(args(0)),
      args.drop(1).map(ChannelBuffers.wrappedBuffer(_)))
  }
}
case class HDel(key: ChannelBuffer, fields: Seq[ChannelBuffer]) extends StrictKeyCommand {
  def command = Commands.HDEL
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HDEL, key) ++ fields)
}

object HGet {
  def apply(args: Seq[Array[Byte]]) = {
    val list = trimList(args, 2, "HGET")
    new HGet(ChannelBuffers.wrappedBuffer(list(0)), ChannelBuffers.wrappedBuffer(list(1)))
  }
}
case class HGet(key: ChannelBuffer, field: ChannelBuffer) extends StrictKeyCommand {
  def command = Commands.HGET
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HGET, key, field))
}

object HGetAll {
  def apply(args: Seq[Array[Byte]]) = {
    RequireClientProtocol(args.nonEmpty, "HGETALL requires a key")
    new HGetAll(ChannelBuffers.wrappedBuffer(args(0)))
  }
}
case class HGetAll(key: ChannelBuffer) extends StrictKeyCommand {
  def command = Commands.HGETALL
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HGETALL, key))
}

object HKeys {
  def apply(keys: Seq[Array[Byte]]) = {
    new HKeys(ChannelBuffers.wrappedBuffer(keys.head))
  }
}
case class HKeys(key: ChannelBuffer) extends StrictKeyCommand {
  def command = Commands.HKEYS
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HKEYS, key))
}

object HMGet {
  def apply(args: Seq[Array[Byte]]) = {
    RequireClientProtocol(args.length >= 2, "HMGET requires a hash key and at least one field")
    new HMGet(ChannelBuffers.wrappedBuffer(args(0)),
      args.drop(1).map(ChannelBuffers.wrappedBuffer(_)))
  }
}
case class HMGet(key: ChannelBuffer, fields: Seq[ChannelBuffer]) extends StrictKeyCommand {
  def command = Commands.HMGET
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HMGET, key) ++ fields)
}

case class HMSet(key: ChannelBuffer, fv: Map[ChannelBuffer, ChannelBuffer]) extends StrictKeyCommand {
  def command = Commands.HMSET

  val fvList: Seq[ChannelBuffer] = fv.flatMap { case(f,v) =>
    f :: v :: Nil
  }(collection.breakOut)

  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HMSET, key) ++ fvList)
}

object HMSet {
  def apply(args: Seq[Array[Byte]]) = {
    RequireClientProtocol(args.length >= 3, "HMSET requires a hash key and at least one field and value")

    val key = ChannelBuffers.wrappedBuffer(args(0))
    val fv = args.drop(1).grouped(2).map {
      case field :: value :: Nil => (ChannelBuffers.wrappedBuffer(field),
        ChannelBuffers.wrappedBuffer(value))
      case _ => throw ClientError("Unexpected uneven pair of elements in HMSET")
    }.toMap

    new HMSet(key, fv)
  }
}


case class HScan(
  key: ChannelBuffer,
  cursor: Long,
  count: Option[JLong] = None,
  pattern: Option[ChannelBuffer] = None
) extends Command {
  def command = Commands.HSCAN
  def toChannelBuffer = {
    val bufs = Seq(CommandBytes.HSCAN, key, StringToChannelBuffer(cursor.toString))
    val withCount = count match {
      case Some(count) => bufs ++ Seq(Count.COUNT_CB, StringToChannelBuffer(count.toString))
      case None => bufs
    }
    val withPattern = pattern match {
      case Some(pattern) => withCount ++ Seq(Pattern.PATTERN_CB, pattern)
      case None => withCount
    }
    RedisCodec.toUnifiedFormat(withPattern)
  }
}
object HScan {
  import ScanCompanion._

  def apply(args: Seq[Array[Byte]]) = {
    RequireClientProtocol(
      args != null && !args.isEmpty,
      "Expected at least 1 arguments for hscan command")
    args match {
      case key :: cursor :: Nil =>
        new HScan(ChannelBuffers.wrappedBuffer(key),
          NumberFormat.toLong(BytesToString(cursor)))
      case key :: cursor :: tail =>
        parseArgs(key, NumberFormat.toLong(BytesToString(cursor)), tail)
      case _ => throw ClientError("Unexpected args to hscan command")
    }
  }

  def parseArgs(key: Array[Byte], cursor: Long, args: Seq[Array[Byte]]) = {
    val sArgs = BytesToString.fromList(args)
    val (args0, args1) = findArgs(sArgs)
    RequireClientProtocol(args0.length > 1, "Length of arguments must be > 1")
    val count = findCount(args0, args1)
    val pattern = findPattern(args0, args1).map(StringToChannelBuffer(_))
    new HScan(ChannelBuffers.wrappedBuffer(key), cursor, count, pattern)
  }
}

object HSet {
  def apply(args: Seq[Array[Byte]]) = {
    val list = trimList(args, 3, "HSET")
    new HSet(
      ChannelBuffers.wrappedBuffer(list(0)),
      ChannelBuffers.wrappedBuffer(list(1)),
      ChannelBuffers.wrappedBuffer(list(2)))
  }
}
case class HSet(key: ChannelBuffer, field: ChannelBuffer, value: ChannelBuffer)
  extends StrictKeyCommand {
  def command = Commands.HSET
  def toChannelBuffer = RedisCodec.toUnifiedFormat(Seq(CommandBytes.HSET, key, field, value))
}
Something went wrong with that request. Please try again.