diff --git a/src/Database/Redis/Schema.hs b/src/Database/Redis/Schema.hs index 7d1c0bf..60b0e6b 100644 --- a/src/Database/Redis/Schema.hs +++ b/src/Database/Redis/Schema.hs @@ -56,7 +56,7 @@ module Database.Redis.Schema , day, hour, minute, second , throw, throwMsg , sInsert, sDelete, sContains, sSize - , Priority(..), zInsert, zSize, zCount, zDelete, zIncrBy, zPopMin, bzPopMin, zRangeByScoreLimit, zScanOpts + , Priority(..), zInsert, zSize, zCount, zDelete, zIncrBy, zPopMin, bzPopMin, zRangeByScoreLimit, zRevRange, zScanOpts, zUnionStoreWeights , txSInsert, txSDelete, txSContains, txSSize , MapItem(..) , RecordField(..), RecordItem(..), Record @@ -977,6 +977,7 @@ txSSize ref = txWrap $ Hedis.scard (toIdentifier ref) -- | Priority for a sorted set newtype Priority = Priority { unPriority :: Double } + deriving newtype (Eq, Ord, Num, Real, Fractional, RealFrac) instance Serializable Priority where fromBS = fmap Priority . fromBS @@ -1015,11 +1016,11 @@ zCount (toIdentifier -> keyBS) (unPriority -> minScore) (unPriority -> maxScore) -- | Increment the value in the sorted set by the given amount. Note that if the value is not present this will add the -- the value to the sorted list with the given amount as its priority. -zIncrBy :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) => ref -> Integer -> a -> RedisM (RefInstance ref) () +zIncrBy :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) => ref -> Integer -> a -> RedisM (RefInstance ref) Priority zIncrBy (toIdentifier -> keyBS) incr (toBS -> val)= - Redis (Hedis.zincrby keyBS incr val) + Hedis.zincrby keyBS incr val >>= expectRight "zincrby" - >>= ignore @Double + <&> Priority -- | Remove given number of smallest elements from a sorted set. -- Available since Redis 5.0.0 @@ -1064,12 +1065,26 @@ zRangeByScoreLimit (toIdentifier -> keyBS) (Priority minV) (Priority maxV) offse >>= expectRight "zrangebyscoreLimit call" >>= expectRight "zrangebyscoreLimit decode" . fromBSMany +zRevRange :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) + => ref -> Integer -> Integer -> RedisM (RefInstance ref) [a] +zRevRange (toIdentifier -> keyBS) start end = + Hedis.zrevrange keyBS start end + >>= expectRight "zrevrange call" + >>= expectRight "zrevrange decode" . fromBSMany + -- | Scan the sorted set by reference using an optional match and count. -zScanOpts :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) => ref -> Maybe Text -> Maybe Integer -> RedisM (RefInstance ref) [a] +zScanOpts :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) + => ref -> Maybe Text -> Maybe Integer -> RedisM (RefInstance ref) [a] zScanOpts (toIdentifier -> keyBS) mMatch mCount = - Redis (Hedis.zscanOpts keyBS Hedis.cursor0 Hedis.ScanOpts { Hedis.scanMatch = toBS <$> mMatch, Hedis.scanCount = mCount}) - >>= expectRight "zscanOpts call" - >>= expectRight "zscanOpts decode" . fromBSMany . map fst . snd + Hedis.zscanOpts keyBS Hedis.cursor0 Hedis.ScanOpts { Hedis.scanMatch = toBS <$> mMatch, Hedis.scanCount = mCount} + >>= expectRight "zscanOpts call" + >>= expectRight "zscanOpts decode" . fromBSMany . map fst . snd + +zUnionStoreWeights :: forall ref a. (Ref ref, ValueType ref ~ [(Priority, a)], Serializable a) => ref -> [(ref, Double)] -> RedisM (RefInstance ref) () +zUnionStoreWeights (toIdentifier -> keyBS) refWithWeights = + Hedis.zunionstoreWeights keyBS [(toIdentifier k, d) | (k, d) <- refWithWeights] Hedis.Sum + >>= expectRight "zUnionStoreWeights call" + >>= ignore @Integer parseMap :: (Ord k, Serializable k, Serializable v) => [(ByteString, ByteString)] -> Maybe (Map k v)