Skip to content

Commit

Permalink
fix(journal): take slice into account when indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 7, 2022
1 parent eb76c8f commit 95698c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/journal/src/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ allocateJournal fp (Options _ termBufferLen) = do
meta <- wrapPart bb (logLength - lOG_META_DATA_LENGTH) lOG_META_DATA_LENGTH

writeTermLength meta (fromIntegral termBufferLen)
writeInitialTermId meta 0
writeInitialTermId meta 0 -- XXX: should be random rather than 0.
pageSize <- sysconfPageSize
writePageSize (Metadata meta) (int2Int32 pageSize)

Expand Down
10 changes: 6 additions & 4 deletions src/journal/src/Journal/Internal/ByteBufferPtr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data ByteBuffer = ByteBuffer
, bbPosition :: {-# UNPACK #-} !(IORef Position)
, bbMark :: {-# UNPACK #-} !(IORef Position)
, bbSlice :: {-# UNPACK #-} !(IORef Slice)
-- XXX: ByteOrder / Endianess?
-- XXX: ByteOrder / Endianess? Use `Data.Bits.compliment` to reverse bits?
}

newtype Capacity = Capacity { unCapacity :: Int }
Expand Down Expand Up @@ -119,9 +119,9 @@ remaining bb = do

boundCheck :: HasCallStack => ByteBuffer -> Int -> IO ()
boundCheck bb ix = do
-- XXX: ix + slice?
-- XXX: parametrise on build flag and only do these checks if enabled?
if ix < fromIntegral (getCapacity bb)
Slice slice <- readIORef (bbSlice bb)
if ix - slice < fromIntegral (getCapacity bb)
then return ()
else do
putStrLn (prettyCallStack callStack)
Expand Down Expand Up @@ -176,7 +176,9 @@ wrap bb = newByteBuffer (bbData bb) capa lim (Position 0) (Just (bbSlice bb))
lim = Limit (fromIntegral capa)

wrapPart :: ByteBuffer -> Int -> Int -> IO ByteBuffer
wrapPart bb offset len = newByteBuffer (bbData bb) capa lim pos (Just (bbSlice bb))
wrapPart bb offset len = do
slice <- newIORef (Slice offset)
newByteBuffer (bbData bb) capa lim pos (Just slice)
where
capa = Capacity len
lim = Limit (fromIntegral offset + fromIntegral len)
Expand Down

0 comments on commit 95698c3

Please sign in to comment.