-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(journal): add and use buffer claim datatype
- Loading branch information
1 parent
0e8a33d
commit 8058946
Showing
6 changed files
with
143 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Journal.Internal.BufferClaim where | ||
|
||
import Data.ByteString (ByteString) | ||
import Data.Word (Word8) | ||
import Foreign.Ptr (Ptr, plusPtr) | ||
|
||
import Journal.Internal.ByteBuffer | ||
import Journal.Types | ||
|
||
------------------------------------------------------------------------ | ||
|
||
newtype BufferClaim = BufferClaim ByteBuffer | ||
|
||
newBufferClaim :: ByteBuffer -> TermOffset -> Int -> IO BufferClaim | ||
newBufferClaim src offset len = BufferClaim <$> | ||
wrapPart src (fromIntegral offset) len | ||
|
||
putBytes :: BufferClaim -> ByteString -> IO () | ||
putBytes (BufferClaim bb) bs = putByteString bb bs | ||
|
||
-- NOTE: The underlying @ByteBuffer@ must have been pinned, otherwise we cannot | ||
-- guarantee to get a pointer to it that isn't moved around. | ||
withPtr :: BufferClaim -> (Ptr Word8 -> IO a) -> IO a | ||
withPtr (BufferClaim bb) k = do | ||
Position offset <- readPosition bb | ||
k (bbPtr bb `plusPtr` offset) | ||
|
||
commit :: BufferClaim -> IO () | ||
commit (BufferClaim bb) = do | ||
Position offset <- readPosition bb | ||
let Capacity frameLen = getCapacity bb | ||
writeFrameLength bb (fromIntegral offset) (fromIntegral frameLen) | ||
|
||
abort :: BufferClaim -> IO () | ||
abort (BufferClaim bb) = do | ||
Position offset <- readPosition bb | ||
let Capacity frameLen = getCapacity bb | ||
writeFrameType bb (fromIntegral offset) Padding | ||
writeFrameLength bb (fromIntegral offset) (fromIntegral frameLen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters