Skip to content

Commit fea1896

Browse files
authored
Merge pull request #543 from IntersectMBO/wenkokke/move-check-crc
fix: move checkCRC to CRC32C
2 parents a320d80 + debcbcf commit fea1896

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

src/Database/LSMTree/Internal/CRC32C.hs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ module Database.LSMTree.Internal.CRC32C (
4242
readChecksumsFile,
4343
writeChecksumsFile,
4444
writeChecksumsFile',
45+
46+
-- * Checksum checking
47+
ChecksumError (..),
48+
checkCRC,
49+
expectChecksum,
4550
) where
4651

4752
import Control.Monad
@@ -65,7 +70,8 @@ import GHC.Exts
6570
import qualified GHC.ForeignPtr as Foreign
6671
import System.FS.API
6772
import System.FS.API.Lazy
68-
import System.FS.BlockIO.API (ByteCount)
73+
import System.FS.BlockIO.API (Advice (..), ByteCount, HasBlockIO,
74+
hAdviseAll, hDropCacheAll)
6975

7076

7177
newtype CRC32C = CRC32C Word32
@@ -347,3 +353,53 @@ formatChecksumsFile checksums =
347353
<> BS.word32HexFixed crc
348354
<> BS.char8 '\n'
349355
| (ChecksumsFileName name, CRC32C crc) <- Map.toList checksums ]
356+
357+
data ChecksumError = ChecksumError FsPath CRC32C CRC32C
358+
deriving stock Show
359+
deriving anyclass Exception
360+
361+
-- | Check the CRC32C checksum for a file.
362+
--
363+
-- If the boolean argument is @True@, all file data for this path is evicted
364+
-- from the page cache.
365+
{-# SPECIALISE
366+
checkCRC ::
367+
HasFS IO h
368+
-> HasBlockIO IO h
369+
-> Bool
370+
-> CRC32C
371+
-> FsPath
372+
-> IO ()
373+
#-}
374+
checkCRC ::
375+
forall m h.
376+
(MonadMask m, PrimMonad m)
377+
=> HasFS m h
378+
-> HasBlockIO m h
379+
-> Bool
380+
-> CRC32C
381+
-> FsPath
382+
-> m ()
383+
checkCRC fs hbio dropCache expected fp = withFile fs fp ReadMode $ \h -> do
384+
-- double the file readahead window (only applies to this file descriptor)
385+
hAdviseAll hbio h AdviceSequential
386+
!checksum <- hGetAllCRC32C' fs h defaultChunkSize initialCRC32C
387+
when dropCache $ hDropCacheAll hbio h
388+
expectChecksum fp expected checksum
389+
390+
{-# SPECIALISE
391+
expectChecksum ::
392+
FsPath
393+
-> CRC32C
394+
-> CRC32C
395+
-> IO ()
396+
#-}
397+
expectChecksum ::
398+
MonadThrow m
399+
=> FsPath
400+
-> CRC32C
401+
-> CRC32C
402+
-> m ()
403+
expectChecksum fp expected checksum =
404+
when (expected /= checksum) $
405+
throwIO $ ChecksumError fp expected checksum

src/Database/LSMTree/Internal/Run.hs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ module Database.LSMTree.Internal.Run (
2323
, RunDataCaching (..)
2424
-- * Snapshot
2525
, FileFormatError (..)
26-
, ChecksumError (..)
2726
, openFromDisk
2827
) where
2928

3029
import Control.DeepSeq (NFData (..), rwhnf)
31-
import Control.Monad (when)
3230
import Control.Monad.Class.MonadST (MonadST)
3331
import Control.Monad.Class.MonadSTM (MonadSTM (..))
3432
import Control.Monad.Class.MonadThrow
@@ -236,10 +234,6 @@ fromWriteBuffer fs hbio caching alloc fsPaths buffer blobs = do
236234
Snapshot
237235
-------------------------------------------------------------------------------}
238236

239-
data ChecksumError = ChecksumError FS.FsPath CRC.CRC32C CRC.CRC32C
240-
deriving stock Show
241-
deriving anyclass Exception
242-
243237
data FileFormatError = FileFormatError FS.FsPath String
244238
deriving stock Show
245239
deriving anyclass Exception
@@ -298,14 +292,8 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
298292
-- Note: all file data for this path is evicted from the page cache /if/ the
299293
-- caching argument is 'NoCacheRunData'.
300294
checkCRC :: RunDataCaching -> CRC.CRC32C -> FS.FsPath -> m ()
301-
checkCRC cache expected fp = FS.withFile fs fp FS.ReadMode $ \h -> do
302-
-- double the file readahead window (only applies to this file descriptor)
303-
FS.hAdviseAll hbio h FS.AdviceSequential
304-
!checksum <- CRC.hGetAllCRC32C' fs h CRC.defaultChunkSize CRC.initialCRC32C
305-
when (cache == NoCacheRunData) $
306-
-- drop the file from the OS page cache
307-
FS.hDropCacheAll hbio h
308-
expectChecksum fp expected checksum
295+
checkCRC cache expected fp =
296+
CRC.checkCRC fs hbio (cache == NoCacheRunData) expected fp
309297

310298
-- Note: all file data for this path is evicted from the page cache
311299
readCRC :: CRC.CRC32C -> FS.FsPath -> m SBS.ShortByteString
@@ -316,12 +304,8 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
316304
(sbs, !checksum) <- CRC.hGetExactlyCRC32C_SBS fs h (fromIntegral n) CRC.initialCRC32C
317305
-- drop the file from the OS page cache
318306
FS.hAdviseAll hbio h FS.AdviceDontNeed
319-
expectChecksum fp expected checksum
307+
CRC.expectChecksum fp expected checksum
320308
return sbs
321309

322-
expectChecksum fp expected checksum =
323-
when (expected /= checksum) $
324-
throwIO $ ChecksumError fp expected checksum
325-
326310
expectValidFile _ (Right x) = pure x
327311
expectValidFile fp (Left err) = throwIO $ FileFormatError fp err

src/Database/LSMTree/Internal/Snapshot/Codec.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import Database.LSMTree.Internal.Entry
3434
import Database.LSMTree.Internal.Merge (MergeType (..))
3535
import Database.LSMTree.Internal.MergeSchedule
3636
import Database.LSMTree.Internal.MergingRun (NumRuns (..))
37-
import Database.LSMTree.Internal.Run (ChecksumError (..),
38-
FileFormatError (..))
37+
import Database.LSMTree.Internal.Run (FileFormatError (..))
3938
import Database.LSMTree.Internal.RunNumber
4039
import Database.LSMTree.Internal.Snapshot
4140
import qualified System.FS.API as FS

0 commit comments

Comments
 (0)