Skip to content

Commit

Permalink
add zRevRange; derive basic instances for Priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Menno de Boer committed Jul 28, 2023
1 parent fcf510c commit 94cdedd
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Database/Redis/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 94cdedd

Please sign in to comment.