<commit_before>module Persistence.Redis
( Key
, Value
, toKey
, fromKey
, toValue
, fromValue
, get
, getWithDefault
, set
, setWithDefault
) where

import Control.Applicative
import qualified Data.ByteString.Char8 as B

import qualified Database.Redis as R

newtype Key   = Key { fromKey' :: B.ByteString }
newtype Value = Value { fromValue' :: B.ByteString }

-- constants

connError :: (Monad m) => m a
connError = fail "connection to Redis server failed"

-- public functions

toKey :: String -> Key
toKey = Key . B.pack

fromKey :: Key -> String
fromKey = B.unpack . fromKey'

toValue :: String -> Value
toValue = Value . B.pack

fromValue :: Value -> String
fromValue = B.unpack . fromValue'

get :: Key -> IO (Maybe Value)
get key =
  redisConn $ do
    val <- R.get (fromKey' key)
    case val of
      Left _    -> connError
      Right v   -> return $ fmap Value v

getWithDefault :: String -> Key -> IO Value
getWithDefault def key = do
    val <- get key
    return $ case val of
      Just v -> v
      _      -> toValue def

set :: Value -> Key -> IO (Either R.Reply R.Status)
set value key = redisConn $ R.set (fromKey' key) (fromValue' value)

setWithDefault :: String -> Value -> Key -> IO Value
setWithDefault def val key = do
    status <- set val key
    case status of
      Left _  -> connError
      Right _ -> return $ toValue def

-- private functions

redisConn :: R.Redis a -> IO a
redisConn r = R.connect R.defaultConnectInfo >>= flip R.runRedis r
<commit_msg>pack redis interface with ByteString/Text conversions<commit_after>module Persistence.Redis
( Key       (..)
, Value     (..)
, Hash      (..)
, RedisTry
, toKey
, fromKey
, toValue
, fromValue
, get
, getWithDefault
, set
, setWithDefault
) where

import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8, decodeUtf8)

import qualified Database.Redis as R

newtype Key   = Key   { fromKey'   :: ByteString }
newtype Value = Value { fromValue' :: ByteString }
newtype Hash  = Hash  { fromHash'  :: [(ByteString, ByteString)] }

type RedisTry = IO (Either R.Reply R.Status)

-- constants

connError :: (Monad m) => m a
connError = fail "connection to Redis server failed"

-- public functions

toKey :: Text -> Key
toKey = Key . encodeUtf8

fromKey :: Key -> Text
fromKey = decodeUtf8 . fromKey'

toValue :: Text -> Value
toValue = Value . encodeUtf8

fromValue :: Value -> Text
fromValue = decodeUtf8 . fromValue'

get :: Key -> IO (Maybe Value)
get key =
  redisConn $ do
    val <- R.get (fromKey' key)
    case val of
      Left _    -> connError
      Right v   -> return $ fmap Value v

getWithDefault :: Text -> Key -> IO Value
getWithDefault def key = do
    val <- get key
    return $ case val of
      Just v -> v
      _      -> toValue def

set :: Value -> Key -> RedisTry
set value key = redisConn $ R.set (fromKey' key) (fromValue' value)

setWithDefault :: Text -> Value -> Key -> IO Value
setWithDefault def val key = do
    status <- set val key
    case status of
      Left _  -> connError
      Right _ -> return $ toValue def

-- private functions

redisConn :: R.Redis a -> IO a
redisConn r = R.connect R.defaultConnectInfo >>= flip R.runRedis r

{'fraction_non_alphanumeric': 0.10743034055727554, 'fraction_numerical': 0.002476780185758514, 'mean_word_length': 2.8146399055489963, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518430', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1034, 'n_words': 367}