|
| 1 | +package redis |
| 2 | +package algebra |
| 3 | + |
| 4 | +import scalaz.{Free, Functor, Inject, InjectFunctions, NonEmptyList}, Free.Return |
| 5 | + |
| 6 | +import data.Status |
| 7 | + |
| 8 | +sealed abstract class HashAlgebra[A] |
| 9 | + |
| 10 | +final case class Hdel[A](key: ByteString, fields: NonEmptyList[ByteString], h: Long => A) extends HashAlgebra[A] |
| 11 | + |
| 12 | +final case class Hexists[A](key: ByteString, field: ByteString, h: Boolean => A) extends HashAlgebra[A] |
| 13 | + |
| 14 | +final case class Hget[A](key: ByteString, field: ByteString, h: Option[ByteString] => A) extends HashAlgebra[A] |
| 15 | + |
| 16 | +final case class Hgetall[A](key: ByteString, h: Seq[(ByteString, ByteString)] => A) extends HashAlgebra[A] |
| 17 | + |
| 18 | +final case class Hincrby[A](key: ByteString, field: ByteString, increment: Long, h: Long => A) extends HashAlgebra[A] |
| 19 | + |
| 20 | +final case class Hincrbyfloat[A](key: ByteString, field: ByteString, increment: BigDecimal, h: BigDecimal => A) extends HashAlgebra[A] |
| 21 | + |
| 22 | +final case class Hkeys[A](key: ByteString, h: Seq[ByteString] => A) extends HashAlgebra[A] |
| 23 | + |
| 24 | +final case class Hlen[A](key: ByteString, h: Long => A) extends HashAlgebra[A] |
| 25 | + |
| 26 | +final case class Hmget[A](key: ByteString, fields: NonEmptyList[ByteString], h: Seq[Option[ByteString]] => A) extends HashAlgebra[A] |
| 27 | + |
| 28 | +final case class Hmset[A](key: ByteString, pairs: NonEmptyList[(ByteString, ByteString)], h: Status => A) extends HashAlgebra[A] |
| 29 | + |
| 30 | +final case class Hset[A](key: ByteString, field: ByteString, value: ByteString, h: Boolean => A) extends HashAlgebra[A] |
| 31 | + |
| 32 | +final case class Hsetnx[A](key: ByteString, field: ByteString, value: ByteString, h: Boolean => A) extends HashAlgebra[A] |
| 33 | + |
| 34 | +final case class Hvals[A](key: ByteString, h: Seq[ByteString] => A) extends HashAlgebra[A] |
| 35 | + |
| 36 | +trait HashInstances { |
| 37 | + implicit val hashAlgebraFunctor: Functor[HashAlgebra] = |
| 38 | + new Functor[HashAlgebra] { |
| 39 | + def map[A, B](a: HashAlgebra[A])(f: A => B): HashAlgebra[B] = |
| 40 | + a match { |
| 41 | + case Hdel(k, s, h) => Hdel(k, s, x => f(h(x))) |
| 42 | + case Hexists(k, s, h) => Hexists(k, s, x => f(h(x))) |
| 43 | + case Hget(k, s, h) => Hget(k, s, x => f(h(x))) |
| 44 | + case Hgetall(k, h) => Hgetall(k, x => f(h(x))) |
| 45 | + case Hincrby(k, s, i, h) => Hincrby(k, s, i, x => f(h(x))) |
| 46 | + case Hincrbyfloat(k, s, i, h) => Hincrbyfloat(k, s, i, x => f(h(x))) |
| 47 | + case Hkeys(k, h) => Hkeys(k, x => f(h(x))) |
| 48 | + case Hlen(k, h) => Hlen(k, x => f(h(x))) |
| 49 | + case Hmget(k, s, h) => Hmget(k, s, x => f(h(x))) |
| 50 | + case Hmset(k, p, h) => Hmset(k, p, x => f(h(x))) |
| 51 | + case Hset(k, s, v, h) => Hset(k, s, v, x => f(h(x))) |
| 52 | + case Hsetnx(k, s, v, h) => Hsetnx(k, s, v, x => f(h(x))) |
| 53 | + case Hvals(k, h) => Hvals(k, x => f(h(x))) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +trait HashFunctions extends InjectFunctions { |
| 59 | + def hdel[F[_]: Functor](key: ByteString, fields: NonEmptyList[ByteString])(implicit I: Inject[HashAlgebra, F]): Free[F, Long] = |
| 60 | + inject[F, HashAlgebra, Long](Hdel(key, fields, Return(_))) |
| 61 | + |
| 62 | + def hexists[F[_]: Functor](key: ByteString, field: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Boolean] = |
| 63 | + inject[F, HashAlgebra, Boolean](Hexists(key, field, Return(_))) |
| 64 | + |
| 65 | + def hget[F[_]: Functor](key: ByteString, field: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Option[ByteString]] = |
| 66 | + inject[F, HashAlgebra, Option[ByteString]](Hget(key, field, Return(_))) |
| 67 | + |
| 68 | + def hgetall[F[_]: Functor](key: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Seq[(ByteString, ByteString)]] = |
| 69 | + inject[F, HashAlgebra, Seq[(ByteString, ByteString)]](Hgetall(key, Return(_))) |
| 70 | + |
| 71 | + def hincrby[F[_]: Functor](key: ByteString, field: ByteString, increment: Long)(implicit I: Inject[HashAlgebra, F]): Free[F, Long] = |
| 72 | + inject[F, HashAlgebra, Long](Hincrby(key, field, increment, Return(_))) |
| 73 | + |
| 74 | + def hincrbyfloat[F[_]: Functor](key: ByteString, field: ByteString, increment: BigDecimal)(implicit I: Inject[HashAlgebra, F]): Free[F, BigDecimal] = |
| 75 | + inject[F, HashAlgebra, BigDecimal](Hincrbyfloat(key, field, increment, Return(_))) |
| 76 | + |
| 77 | + def hkeys[F[_]: Functor](key: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Seq[ByteString]] = |
| 78 | + inject[F, HashAlgebra, Seq[ByteString]](Hkeys(key, Return(_))) |
| 79 | + |
| 80 | + def hlen[F[_]: Functor](key: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Long] = |
| 81 | + inject[F, HashAlgebra, Long](Hlen(key, Return(_))) |
| 82 | + |
| 83 | + def hmget[F[_]: Functor](key: ByteString, fields: NonEmptyList[ByteString])(implicit I: Inject[HashAlgebra, F]): Free[F, Seq[Option[ByteString]]] = |
| 84 | + inject[F, HashAlgebra, Seq[Option[ByteString]]](Hmget(key, fields, Return(_))) |
| 85 | + |
| 86 | + def hmset[F[_]: Functor](key: ByteString, pairs: NonEmptyList[(ByteString, ByteString)])(implicit I: Inject[HashAlgebra, F]): Free[F, Status] = |
| 87 | + inject[F, HashAlgebra, Status](Hmset(key, pairs, Return(_))) |
| 88 | + |
| 89 | + def hset[F[_]: Functor](key: ByteString, field: ByteString, value: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Boolean] = |
| 90 | + inject[F, HashAlgebra, Boolean](Hset(key, field, value, Return(_))) |
| 91 | + |
| 92 | + def hsetnx[F[_]: Functor](key: ByteString, field: ByteString, value: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Boolean] = |
| 93 | + inject[F, HashAlgebra, Boolean](Hsetnx(key, field, value, Return(_))) |
| 94 | + |
| 95 | + def hvals[F[_]: Functor](key: ByteString)(implicit I: Inject[HashAlgebra, F]): Free[F, Seq[ByteString]] = |
| 96 | + inject[F, HashAlgebra, Seq[ByteString]](Hvals(key, Return(_))) |
| 97 | +} |
0 commit comments