Skip to content

Commit 95ab06a

Browse files
authored
Merge pull request #544 from IntersectMBO/wenkokke/move-fileformaterror
fix: move FileFormatError to Paths
2 parents 3e6bd8d + 806bac7 commit 95ab06a

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

src/Database/LSMTree/Internal/CRC32C.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module Database.LSMTree.Internal.CRC32C (
4747
ChecksumError (..),
4848
checkCRC,
4949
expectChecksum,
50+
51+
-- * File format errors
52+
FileFormatError (..),
53+
expectValidFile,
5054
) where
5155

5256
import Control.Monad
@@ -354,6 +358,12 @@ formatChecksumsFile checksums =
354358
<> BS.char8 '\n'
355359
| (ChecksumsFileName name, CRC32C crc) <- Map.toList checksums ]
356360

361+
362+
363+
{-------------------------------------------------------------------------------
364+
Checksum errors
365+
-------------------------------------------------------------------------------}
366+
357367
data ChecksumError = ChecksumError FsPath CRC32C CRC32C
358368
deriving stock Show
359369
deriving anyclass Exception
@@ -403,3 +413,20 @@ expectChecksum ::
403413
expectChecksum fp expected checksum =
404414
when (expected /= checksum) $
405415
throwIO $ ChecksumError fp expected checksum
416+
417+
418+
{-------------------------------------------------------------------------------
419+
File format errors
420+
-------------------------------------------------------------------------------}
421+
422+
data FileFormatError = FileFormatError FsPath String
423+
deriving stock Show
424+
deriving anyclass Exception
425+
426+
expectValidFile ::
427+
MonadThrow f
428+
=> FsPath
429+
-> Either String a
430+
-> f a
431+
expectValidFile _ (Right x) = pure x
432+
expectValidFile fp (Left err) = throwIO $ FileFormatError fp err

src/Database/LSMTree/Internal/Run.hs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module Database.LSMTree.Internal.Run (
2222
, fromWriteBuffer
2323
, RunDataCaching (..)
2424
-- * Snapshot
25-
, FileFormatError (..)
2625
, openFromDisk
2726
) where
2827

@@ -234,10 +233,6 @@ fromWriteBuffer fs hbio caching alloc fsPaths buffer blobs = do
234233
Snapshot
235234
-------------------------------------------------------------------------------}
236235

237-
data FileFormatError = FileFormatError FS.FsPath String
238-
deriving stock Show
239-
deriving anyclass Exception
240-
241236
{-# SPECIALISE openFromDisk ::
242237
HasFS IO h
243238
-> HasBlockIO IO h
@@ -262,7 +257,7 @@ openFromDisk ::
262257
-- TODO: make exception safe
263258
openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
264259
expectedChecksums <-
265-
expectValidFile (runChecksumsPath runRunFsPaths) . fromChecksumsFile
260+
CRC.expectValidFile (runChecksumsPath runRunFsPaths) . fromChecksumsFile
266261
=<< CRC.readChecksumsFile fs (runChecksumsPath runRunFsPaths)
267262

268263
-- verify checksums of files we don't read yet
@@ -272,10 +267,10 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
272267

273268
-- read and try parsing files
274269
runFilter <-
275-
expectValidFile (forRunFilterRaw paths) . bloomFilterFromSBS
270+
CRC.expectValidFile (forRunFilterRaw paths) . bloomFilterFromSBS
276271
=<< readCRC (forRunFilterRaw expectedChecksums) (forRunFilterRaw paths)
277272
(runNumEntries, runIndex) <-
278-
expectValidFile (forRunIndexRaw paths) . Index.fromSBS
273+
CRC.expectValidFile (forRunIndexRaw paths) . Index.fromSBS
279274
=<< readCRC (forRunIndexRaw expectedChecksums) (forRunIndexRaw paths)
280275

281276
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
@@ -306,6 +301,3 @@ openFromDisk fs hbio runRunDataCaching runRunFsPaths = do
306301
FS.hAdviseAll hbio h FS.AdviceDontNeed
307302
CRC.expectChecksum fp expected checksum
308303
return sbs
309-
310-
expectValidFile _ (Right x) = pure x
311-
expectValidFile fp (Left err) = throwIO $ FileFormatError fp err

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ 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 (FileFormatError (..))
3837
import Database.LSMTree.Internal.RunNumber
3938
import Database.LSMTree.Internal.Snapshot
4039
import qualified System.FS.API as FS

test/Test/Database/LSMTree/Internal/Snapshot/FS.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Test.Database.LSMTree.Internal.Snapshot.FS (tests) where
44
import Codec.CBOR.Read (DeserialiseFailure)
55
import Control.Exception
66
import Database.LSMTree.Internal.CRC32C
7-
import Database.LSMTree.Internal.Run
87
import Database.LSMTree.Internal.Snapshot
98
import Database.LSMTree.Internal.Snapshot.Codec
109
import System.FS.API

0 commit comments

Comments
 (0)