Skip to content

Commit

Permalink
test(journal): narrow down the problem with multiple write/reads
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 17, 2022
1 parent 085e4fa commit b89d151
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/journal/src/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,29 @@ tj = do
tbc :: IO ()
tbc = do
bb <- allocate 16
bc <- newBufferClaim bb 0 16
putBS bc 0 (BSChar8.pack "helloooooooooooo")
bc <- newBufferClaim bb 0 5
putBS bc 0 (BSChar8.pack "hello")
bs <- getByteStringAt bb 0 5
putStrLn (BSChar8.unpack bs)
putStrLn ("'" ++ BSChar8.unpack bs ++ "'")
bc' <- newBufferClaim bb 5 6
putBS bc' 0 (BSChar8.pack "world!")
bs' <- getByteStringAt bb 5 6
putStrLn ("'" ++ BSChar8.unpack bs' ++ "'")

tbb :: IO ()
tbb = do
bb <- allocate 16
putByteStringAt bb 0 (BSChar8.pack "helloooooooooooo")
bs <- getByteStringAt bb 0 5
putStrLn (BSChar8.unpack bs)

tfl :: IO ()
tfl = do
bb <- allocate 16
writeFrameLength bb 0 5
HeaderLength headerLen <- readFrameLength bb 0
putStrLn ("headerLength: " ++ show headerLen)

writeFrameLength bb 4 {- (sizeOf (4 :: Word32)) -} 6
HeaderLength headerLen' <- readFrameLength bb 4 {- (sizeOf (4 :: Word32)) -}
putStrLn ("headerLength': " ++ show headerLen')
4 changes: 4 additions & 0 deletions src/journal/src/Journal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ termAppenderClaim meta termBuffer termId termOffset len = do
handleEndOfLogCondition termBuffer termOffset termLength termId
return Nothing
else do
putStrLn ("termAppenderClaim, termOffset: " ++
show (unTermOffset termOffset))
putStrLn ("termAppenderClaim, frameLength: " ++
show frameLength)
headerWrite termBuffer termOffset (fromIntegral frameLength) termId
Expand All @@ -156,6 +158,8 @@ headerWrite termBuffer termOffset len _termId = do
let versionFlagsType :: Int64
versionFlagsType = fromIntegral cURRENT_VERSION `shiftL` 32
-- XXX: Atomic write?
putStrLn ("headerWrite, versionFlagsType: " ++ show versionFlagsType)
putStrLn ("headerWrite, len: " ++ show (unHeaderLength len))
writeInt64OffAddr termBuffer (fromIntegral termOffset + fRAME_LENGTH_FIELD_OFFSET)
(versionFlagsType .|. ((- fromIntegral len) .&. 0xFFFF_FFFF))
-- XXX: store termId and offset (only need for replication?)
Expand Down
2 changes: 2 additions & 0 deletions src/journal/src/Journal/Internal/BufferClaim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ commit :: BufferClaim -> IO ()
commit (BufferClaim bb) = do
Position offset <- readPosition bb
let Capacity frameLen = getCapacity bb
putStrLn ("commit, offset: " ++ show offset)
putStrLn ("commit, frameLen: " ++ show frameLen)
writeFrameLength bb (fromIntegral offset) (fromIntegral frameLen)

abort :: BufferClaim -> IO ()
Expand Down
7 changes: 4 additions & 3 deletions src/journal/src/Journal/Internal/ByteBufferPtr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ getLazyByteString bb len = do
getByteStringAt :: ByteBuffer -> Int -> Int -> IO BS.ByteString
getByteStringAt bb offset len = do
boundCheck bb (len - 1) -- XXX?
Slice slice <- readIORef (bbSlice bb)
withForeignPtr (bbData bb) $ \sptr ->
BS.create len $ \dptr ->
copyBytes dptr (sptr `plusPtr` offset) len
copyBytes dptr (sptr `plusPtr` (slice + offset)) len

------------------------------------------------------------------------
-- * Relative operations on `Storable` elements
Expand Down Expand Up @@ -364,7 +365,7 @@ primitiveInt f c bb offset@(I# offset#) = do
Slice (I# slice#) <- readIORef (bbSlice bb)
withForeignPtr (bbData bb) $ \(Ptr addr#) ->
IO $ \s ->
case f (addr# `plusAddr#` offset# `plusAddr#` slice#) 0# s of
case f (addr# `plusAddr#` (offset# +# slice#)) 0# s of
(# s', i #) -> (# s', c i #)

primitiveInt32 :: (Addr# -> Int# -> State# RealWorld -> (# State# RealWorld, Int# #))
Expand All @@ -383,7 +384,7 @@ primitiveInt_ f d bb offset@(I# offset#) i = do
Slice (I# slice#) <- readIORef (bbSlice bb)
withForeignPtr (bbData bb) $ \(Ptr addr#) ->
IO $ \s ->
case f (addr# `plusAddr#` offset# `plusAddr#` slice#) 0# value# s of
case f (addr# `plusAddr#` (offset# +# slice#)) 0# value# s of
s' -> (# s', () #)

primitiveInt32_ :: (Addr# -> Int# -> Int# -> State# RealWorld -> State# RealWorld)
Expand Down
2 changes: 1 addition & 1 deletion src/journal/src/Journal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ tagString other = "Unknown: " ++ show other
newtype HeaderVersion = HeaderVersion Word8
deriving newtype (Eq, Binary, Num, Storable, Integral, Real, Ord, Enum)

newtype HeaderLength = HeaderLength Word32
newtype HeaderLength = HeaderLength { unHeaderLength :: Word32 }
deriving newtype (Eq, Ord, Binary, Enum, Real, Integral, Num, Storable)

newtype HeaderIndex = HeaderIndex Word32
Expand Down

0 comments on commit b89d151

Please sign in to comment.