Skip to content

Commit

Permalink
more type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
4eUeP committed Sep 27, 2023
1 parent bd3dc53 commit 209b409
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 80 deletions.
6 changes: 3 additions & 3 deletions common/base/HStream/Foreign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module HStream.Foreign
-- * Misc
, c_delete_string
, c_delete_vector_of_string
, c_delete_vector_of_int
, c_delete_vector_of_cint
, c_delete_vector_of_int64
, c_delete_vector_of_uint64
, c_delete_std_vec_of_folly_small_vec_of_double
Expand Down Expand Up @@ -249,9 +249,9 @@ withPrimListPairUnsafe pairs f = do

HS_CPP_DELETE(delete_string, StdString)
HS_CPP_DELETE(delete_vector_of_string, (StdVector StdString))
HS_CPP_DELETE(delete_vector_of_int, (StdVector CInt))
HS_CPP_DELETE(delete_vector_of_cint, (StdVector CInt))
HS_CPP_DELETE(delete_vector_of_int64, (StdVector Int64))
HS_CPP_DELETE(delete_vector_of_uint64, (StdVector Int64))
HS_CPP_DELETE(delete_vector_of_uint64, (StdVector Word64))
HS_CPP_DELETE(delete_std_vec_of_folly_small_vec_of_double, (StdVector (FollySmallVector Double)))

bool2cbool :: Bool -> CBool
Expand Down
2 changes: 1 addition & 1 deletion common/hstream/HStream/Common/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ queryResultMetadata results idx = withForeignPtr results $ \results_ptr -> do
(MBA# reason_val') (MBA# reason_del')
failures <- if len >= 1
then do keys <- finally (peekN (fromIntegral len) key_val)
(c_delete_vector_of_int key_del)
(c_delete_vector_of_cint key_del)
addrs <- finally (peekStdStringToCBytesN (fromIntegral len) addr_val)
(c_delete_vector_of_string addr_del)
reasons <- finally (peekStdStringToCBytesN (fromIntegral len) reason_val)
Expand Down
12 changes: 5 additions & 7 deletions hstream-store/HStream/Store/Internal/Foreign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import GHC.Exts
import GHC.Stack
import Z.Data.CBytes (CBytes)
import qualified Z.Foreign as Z
import Z.Foreign (BA#, MBA#)

-- TODO: Use HStream.Foreign.BA# instead
import HStream.Foreign hiding (BA#, MBA#)
import HStream.Foreign
import qualified HStream.Logger as Log
import qualified HStream.Store.Exception as E
import HStream.Store.Internal.Types
Expand All @@ -32,9 +30,9 @@ cbool2bool = (/= 0)
{-# INLINE cbool2bool #-}

unsafeFreezeBA# :: MBA# a -> BA# a
unsafeFreezeBA# mba# =
unsafeFreezeBA# (MBA# mba#) =
case unsafeFreezeByteArray# mba# realWorld# of
(# _, ba# #) -> ba#
(# _, ba# #) -> BA# ba#

-- Actually, these unsafe functions can be used for both unsafe & safe ffi(?).
withAsyncPrimUnsafe
Expand Down Expand Up @@ -124,7 +122,7 @@ withAsyncPrimMapUnsafe
=> p
-> PeekNFun pk k -> DeleteFun dk
-> PeekNFun pv v -> DeleteFun dv
-> (StablePtr PrimMVar -> Int -> MBA# p -> MapFun a k v vk vv)
-> (StablePtr PrimMVar -> Int -> MBA# p -> MapFun a pk pv dk dv)
-> IO (a, p, [(k, v)])
withAsyncPrimMapUnsafe p peekk delk peekv delv f =
withAsyncPrimMapUnsafe' p peekk delk peekv delv f pure
Expand Down Expand Up @@ -211,7 +209,7 @@ withPrimSafe' :: forall a b. Prim a => a -> (MBA# a -> IO b) -> IO (a, b)
withPrimSafe' v f = do
mpa@(MutablePrimArray mba#) <- newAlignedPinnedPrimArray 1
writePrimArray mpa 0 v
!b <- f mba#
!b <- f (MBA# mba#)
!a <- readPrimArray mpa 0
return (a, b)
{-# INLINE withPrimSafe' #-}
Expand Down
9 changes: 4 additions & 5 deletions hstream-store/HStream/Store/Internal/LogDevice.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ import qualified Z.Data.CBytes as CBytes
import Z.Data.CBytes (CBytes)
import Z.Data.Vector (Bytes)
import qualified Z.Foreign as Z
import Z.Foreign (BA#,
MBA#)

import HStream.Foreign (BA# (..),
MBA# (..))
import qualified HStream.Store.Exception as E
import HStream.Store.Internal.Types

import HStream.Store.Internal.Foreign
import HStream.Store.Internal.LogDevice.Checkpoint
import HStream.Store.Internal.LogDevice.Configuration
Expand All @@ -51,6 +49,7 @@ import HStream.Store.Internal.LogDevice.LogConfigTypes
import HStream.Store.Internal.LogDevice.Reader
import HStream.Store.Internal.LogDevice.VersionedConfigStore
import HStream.Store.Internal.LogDevice.Writer
import HStream.Store.Internal.Types

-------------------------------------------------------------------------------
-- Client
Expand Down Expand Up @@ -260,7 +259,7 @@ findKey client logid key accuracy =
withForeignPtr client $ \client' ->
CBytes.withCBytesUnsafe key $ \key' -> do
(errno, lo_lsn, hi_lsn, _) <- withAsyncPrimUnsafe3' (0 :: ErrorCode) LSN_INVALID LSN_INVALID
(ld_client_find_key client' logid key' $ unFindKeyAccuracy accuracy) E.throwSubmitIfNotOK
(ld_client_find_key client' logid (BA# key') $ unFindKeyAccuracy accuracy) E.throwSubmitIfNotOK
void $ E.throwStreamErrorIfNotOK' errno
return (lo_lsn, hi_lsn)

Expand Down
24 changes: 12 additions & 12 deletions hstream-store/HStream/Store/Internal/LogDevice/Checkpoint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import GHC.Stack (HasCallStack)
import qualified Z.Data.CBytes as ZC
import Z.Data.CBytes (CBytes)
import qualified Z.Foreign as Z
import Z.Foreign (BA#, MBA#)

import qualified HStream.Foreign as F
import HStream.Foreign
import qualified HStream.Store.Exception as E
import qualified HStream.Store.Internal.Foreign as FFI
import HStream.Store.Internal.Types
Expand All @@ -34,7 +33,7 @@ import HStream.Store.Internal.Types
newFileBasedCheckpointStore :: CBytes -> IO LDCheckpointStore
newFileBasedCheckpointStore root_path =
ZC.withCBytesUnsafe root_path $ \path' -> do
i <- c_new_file_based_checkpoint_store path'
i <- c_new_file_based_checkpoint_store (BA# path')
newForeignPtr c_free_checkpoint_store_fun i

newRSMBasedCheckpointStore
Expand Down Expand Up @@ -63,7 +62,7 @@ ckpStoreGetLSN store customid logid =
ZC.withCBytesUnsafe customid $ \customid' ->
withForeignPtr store $ \store' -> do
(errno, lsn, _) <- FFI.withAsyncPrimUnsafe2 (0 :: ErrorCode) LSN_INVALID $
c_checkpoint_store_get_lsn store' customid' logid
c_checkpoint_store_get_lsn store' (BA# customid') logid
_ <- E.throwStreamErrorIfNotOK' errno
return lsn

Expand All @@ -73,9 +72,9 @@ ckpStoreGetAllCheckpoints' store customid =
withForeignPtr store $ \store' -> do
(_, errno, ret) <- FFI.withAsyncPrimMapUnsafe
C_OK
F.peekN F.c_delete_vector_of_uint64
F.peekN F.c_delete_vector_of_uint64
(checkpoint_store_get_all_checkpoints store' customid')
peekN c_delete_vector_of_uint64
peekN c_delete_vector_of_uint64
(checkpoint_store_get_all_checkpoints store' (BA# customid'))
_ <- E.throwStreamErrorIfNotOK' errno
pure ret

Expand All @@ -90,7 +89,7 @@ ckpStoreUpdateLSN' :: Int -> LDCheckpointStore -> CBytes -> C_LogID -> LSN -> IO
ckpStoreUpdateLSN' retries store customid logid sn =
ZC.withCBytesUnsafe customid $ \customid' ->
withForeignPtr store $ \store' -> do
let f = FFI.withAsyncPrimUnsafe (0 :: ErrorCode) $ c_checkpoint_store_update_lsn store' customid' logid sn
let f = FFI.withAsyncPrimUnsafe (0 :: ErrorCode) $ c_checkpoint_store_update_lsn store' (BA# customid') logid sn
void $ FFI.retryWhileAgain f retries

ckpStoreUpdateMultiLSN
Expand All @@ -109,7 +108,8 @@ ckpStoreUpdateMultiLSN' retries store customid sns =
Z.withPrimArrayUnsafe ka $ \ks' len ->
Z.withPrimArrayUnsafe va $ \vs' _len ->
FFI.withAsyncPrimUnsafe (0 :: ErrorCode) $
checkpoint_store_update_multi_lsn store' customid' ks' vs' (fromIntegral len)
checkpoint_store_update_multi_lsn store'
(BA# customid') (BA# ks') (BA# vs') (fromIntegral len)
void $ FFI.retryWhileAgain f retries

ckpStoreRemoveCheckpoints
Expand All @@ -119,15 +119,15 @@ ckpStoreRemoveCheckpoints store customid (VP.Vector offset len (Z.ByteArray ba#)
ZC.withCBytesUnsafe customid $ \customid' ->
withForeignPtr store $ \store' -> do
(errno, _) <- FFI.withAsyncPrimUnsafe (0 :: ErrorCode) $
checkpoint_store_remove_checkpoints store' customid' ba# offset len
checkpoint_store_remove_checkpoints store' (BA# customid') (BA# ba#) offset len
void $ E.throwStreamErrorIfNotOK' errno

ckpStoreRemoveAllCheckpoints :: HasCallStack => LDCheckpointStore -> CBytes -> IO ()
ckpStoreRemoveAllCheckpoints store customid =
ZC.withCBytesUnsafe customid $ \customid' ->
withForeignPtr store $ \store' -> do
(errno, _) <- FFI.withAsyncPrimUnsafe (0 :: ErrorCode) $
checkpoint_store_remove_all_checkpoints store' customid'
checkpoint_store_remove_all_checkpoints store' (BA# customid')
void $ E.throwStreamErrorIfNotOK' errno

foreign import ccall unsafe "hs_logdevice.h new_file_based_checkpoint_store"
Expand Down Expand Up @@ -169,7 +169,7 @@ foreign import ccall unsafe "hs_logdevice.h checkpoint_store_get_all_checkpoints
-> StablePtr PrimMVar -> Int
-> MBA# ErrorCode -- ^ value out: error code
-> MBA# Int -> MBA# (Ptr C_LogID) -> MBA# (Ptr LSN)
-> MBA# (Ptr (F.StdVector C_LogID)) -> MBA# (Ptr (F.StdVector LSN))
-> MBA# (Ptr (StdVector C_LogID)) -> MBA# (Ptr (StdVector LSN))
-> IO ()

foreign import ccall unsafe "hs_logdevice.h checkpoint_store_update_lsn"
Expand Down
Loading

0 comments on commit 209b409

Please sign in to comment.