|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +module Test.Database.LSMTree.Internal.Snapshot.Codec.Golden |
| 3 | + (tests) where |
| 4 | + |
| 5 | +import Codec.CBOR.Write (toLazyByteString) |
| 6 | +import Control.Monad (when) |
| 7 | +import qualified Data.ByteString.Lazy as BSL (writeFile) |
| 8 | +import Data.Foldable (fold) |
| 9 | +import qualified Data.List as List |
| 10 | +import Data.Vector (Vector) |
| 11 | +import qualified Data.Vector as V |
| 12 | +import Database.LSMTree.Common (BloomFilterAlloc (..), |
| 13 | + DiskCachePolicy (..), NumEntries (..), TableConfig (..), |
| 14 | + WriteBufferAlloc (..), defaultTableConfig) |
| 15 | +import Database.LSMTree.Internal.Config (FencePointerIndex (..), |
| 16 | + MergePolicy (..), MergeSchedule (..), SizeRatio (..)) |
| 17 | +import Database.LSMTree.Internal.Merge (MergeType (..)) |
| 18 | +import Database.LSMTree.Internal.MergeSchedule |
| 19 | + (MergePolicyForLevel (..)) |
| 20 | +import Database.LSMTree.Internal.MergingRun (NumRuns (..)) |
| 21 | +import Database.LSMTree.Internal.RunNumber (RunNumber (..)) |
| 22 | +import Database.LSMTree.Internal.Snapshot |
| 23 | +import Database.LSMTree.Internal.Snapshot.Codec |
| 24 | +import qualified System.FS.API as FS |
| 25 | +import System.FS.API.Types (FsPath, MountPoint (..), fsToFilePath, |
| 26 | + mkFsPath, (<.>)) |
| 27 | +import System.FS.IO (HandleIO, ioHasFS) |
| 28 | +import qualified Test.Tasty as Tasty |
| 29 | +import Test.Tasty (TestName, TestTree, testGroup) |
| 30 | +import qualified Test.Tasty.Golden as Au |
| 31 | + |
| 32 | +-- | Compare the serialization of snapshot metadata with a known reference file. |
| 33 | +tests :: TestTree |
| 34 | +tests = handleOutputFiles . testGroup |
| 35 | + "Test.Database.LSMTree.Internal.Snapshot.Codec.Golden" $ |
| 36 | + [ testCodecSnapshotLabel |
| 37 | + , testCodecSnapshotTableType |
| 38 | + , testCodecTableConfig |
| 39 | + , testCodecSnapLevels |
| 40 | + ] |
| 41 | + |
| 42 | +-- | The mount point is defined as the location of the golden file data directory |
| 43 | +-- relative to the project root. |
| 44 | +goldenDataMountPoint :: MountPoint |
| 45 | +goldenDataMountPoint = MountPoint "test/golden-file-data/snapshot-codec" |
| 46 | + |
| 47 | +-- | Delete output files on test-case success. |
| 48 | +-- Change the option here if this is undesireable. |
| 49 | +handleOutputFiles :: TestTree -> TestTree |
| 50 | +handleOutputFiles = Tasty.localOption Au.OnPass |
| 51 | + |
| 52 | +-- | Internally, the function will infer the correct filepath names. |
| 53 | +snapshotCodecTest |
| 54 | + :: String -- ^ Name of the test |
| 55 | + -> SnapshotMetaData -- ^ Data to be serialized |
| 56 | + -> TestTree |
| 57 | +snapshotCodecTest name datum = |
| 58 | + let -- Various paths |
| 59 | + -- |
| 60 | + -- There are three paths for both the checksum and the snapshot files: |
| 61 | + -- 1. The filepath of type @FsPath@ to which data is written. |
| 62 | + -- 2. The filepath of type @FilePath@ from which data is read. |
| 63 | + -- 3. The filepath of type @FilePath@ against which the data is compared. |
| 64 | + -- |
| 65 | + -- These file types' bindings have the following infix annotations, respectively: |
| 66 | + -- 1. (Fs) for FsPath |
| 67 | + -- 2. (Hs) for "Haskell" path |
| 68 | + -- 3. (Au) for "Golden file" path |
| 69 | + snapshotFsPath = mkFsPath [name] <.> "snapshot" |
| 70 | + snapshotHsPath = fsToFilePath goldenDataMountPoint snapshotFsPath |
| 71 | + snapshotAuPath = snapshotHsPath <> ".golden" |
| 72 | + |
| 73 | + -- IO actions |
| 74 | + runnerIO :: FS.HasFS IO HandleIO |
| 75 | + runnerIO = ioHasFS goldenDataMountPoint |
| 76 | + removeIfExists :: FsPath -> IO () |
| 77 | + removeIfExists fp = |
| 78 | + FS.doesFileExist runnerIO fp >>= (`when` (FS.removeFile runnerIO fp)) |
| 79 | + outputAction :: IO () |
| 80 | + outputAction = do |
| 81 | + -- Ensure that if the output file already exists, we remove it and |
| 82 | + -- re-write out the serialized data. This ensures that there are no |
| 83 | + -- false-positives, false-negatives, or irrelavent I/O exceptions. |
| 84 | + removeIfExists snapshotFsPath |
| 85 | + BSL.writeFile snapshotHsPath . toLazyByteString $ encode datum |
| 86 | + |
| 87 | + in Au.goldenVsFile name snapshotAuPath snapshotHsPath outputAction |
| 88 | + |
| 89 | +testCodecSnapshotLabel :: TestTree |
| 90 | +testCodecSnapshotLabel = |
| 91 | + let assembler (tagA, valA) = |
| 92 | + let (tagB, valB) = basicSnapshotTableType |
| 93 | + (tagC, valC) = basicTableConfig |
| 94 | + valD = basicRunNumber |
| 95 | + (tagE, valE) = basicSnapLevels |
| 96 | + in (fuseAnnotations [tagA, tagB, tagC, tagE ], SnapshotMetaData valA valB valC valD valE) |
| 97 | + in testCodecBuilder "SnapshotLabels" $ assembler <$> enumerateSnapshotLabel |
| 98 | + |
| 99 | +testCodecSnapshotTableType :: TestTree |
| 100 | +testCodecSnapshotTableType = |
| 101 | + let assembler (tagB, valB) = |
| 102 | + let (tagA, valA) = basicSnapshotLabel |
| 103 | + (tagC, valC) = basicTableConfig |
| 104 | + valD = basicRunNumber |
| 105 | + (tagE, valE) = basicSnapLevels |
| 106 | + in (fuseAnnotations [tagA, tagB, tagC, tagE ], SnapshotMetaData valA valB valC valD valE) |
| 107 | + in testCodecBuilder "SnapshotTables" $ assembler <$> enumerateSnapshotTableType |
| 108 | + |
| 109 | +testCodecTableConfig :: TestTree |
| 110 | +testCodecTableConfig = |
| 111 | + let assembler (tagC, valC) = |
| 112 | + let (tagA, valA) = basicSnapshotLabel |
| 113 | + (tagB, valB) = basicSnapshotTableType |
| 114 | + valD = basicRunNumber |
| 115 | + (tagE, valE) = basicSnapLevels |
| 116 | + in (fuseAnnotations [tagA, tagB, tagC, tagE ], SnapshotMetaData valA valB valC valD valE) |
| 117 | + in testCodecBuilder "SnapshotConfig" $ assembler <$> enumerateTableConfig |
| 118 | + |
| 119 | +testCodecSnapLevels :: TestTree |
| 120 | +testCodecSnapLevels = |
| 121 | + let assembler (tagE, valE) = |
| 122 | + let (tagA, valA) = basicSnapshotLabel |
| 123 | + (tagB, valB) = basicSnapshotTableType |
| 124 | + (tagC, valC) = basicTableConfig |
| 125 | + valD = basicRunNumber |
| 126 | + in (fuseAnnotations [tagA, tagB, tagC, tagE ], SnapshotMetaData valA valB valC valD valE) |
| 127 | + in testCodecBuilder "SnapshotLevels" $ assembler <$> enumerateSnapLevels |
| 128 | + |
| 129 | +testCodecBuilder :: TestName -> [(ComponentAnnotation, SnapshotMetaData)] -> TestTree |
| 130 | +testCodecBuilder tName metadata = |
| 131 | + testGroup tName $ uncurry snapshotCodecTest <$> metadata |
| 132 | + |
| 133 | +type ComponentAnnotation = String |
| 134 | + |
| 135 | +fuseAnnotations :: [ComponentAnnotation] -> ComponentAnnotation |
| 136 | +fuseAnnotations = List.intercalate "-" |
| 137 | + |
| 138 | +blank :: ComponentAnnotation |
| 139 | +blank = "__" |
| 140 | + |
| 141 | +{---------------- |
| 142 | +Defaults used when the SnapshotMetaData sub-component is not under test |
| 143 | +----------------} |
| 144 | + |
| 145 | +basicSnapshotLabel :: (ComponentAnnotation, SnapshotLabel) |
| 146 | +basicSnapshotLabel = head enumerateSnapshotLabel |
| 147 | + |
| 148 | +basicSnapshotTableType :: (ComponentAnnotation, SnapshotTableType) |
| 149 | +basicSnapshotTableType = head enumerateSnapshotTableType |
| 150 | + |
| 151 | +basicTableConfig :: (ComponentAnnotation, TableConfig) |
| 152 | +basicTableConfig = ( fuseAnnotations $ "T0" : replicate 4 blank, defaultTableConfig) |
| 153 | + |
| 154 | +basicRunNumber :: RunNumber |
| 155 | +basicRunNumber = enumerateRunNumbers |
| 156 | + |
| 157 | +basicSnapLevels :: (ComponentAnnotation, SnapLevels RunNumber) |
| 158 | +basicSnapLevels = head enumerateSnapLevels |
| 159 | + |
| 160 | +{---------------- |
| 161 | +Enumeration of SnapshotMetaData sub-components |
| 162 | +----------------} |
| 163 | + |
| 164 | +enumerateSnapshotLabel :: [(ComponentAnnotation, SnapshotLabel)] |
| 165 | +enumerateSnapshotLabel = |
| 166 | + [ ("B0", SnapshotLabel "UserProvidedLabel") |
| 167 | + , ("B1", SnapshotLabel "") |
| 168 | + ] |
| 169 | + |
| 170 | +enumerateSnapshotTableType :: [(ComponentAnnotation, SnapshotTableType)] |
| 171 | +enumerateSnapshotTableType = |
| 172 | + [ ("N0", SnapNormalTable) |
| 173 | + , ("N1", SnapMonoidalTable) |
| 174 | + , ("N2", SnapFullTable) |
| 175 | + ] |
| 176 | + |
| 177 | +enumerateTableConfig :: [(ComponentAnnotation, TableConfig)] |
| 178 | +enumerateTableConfig = |
| 179 | + [ ( fuseAnnotations [ "T1", d, e, f, g ] |
| 180 | + , TableConfig |
| 181 | + policy |
| 182 | + ratio |
| 183 | + allocs |
| 184 | + bloom |
| 185 | + fence |
| 186 | + cache |
| 187 | + merge |
| 188 | + ) |
| 189 | + | (_, policy) <- [(blank, MergePolicyLazyLevelling)] |
| 190 | + , (_, ratio ) <- [(blank, Four)] |
| 191 | + , (_, allocs) <- fmap (AllocNumEntries . NumEntries) <$> [(blank, magicNumber1)] |
| 192 | + , (d, bloom ) <- enumerateBloomFilterAlloc |
| 193 | + , (e, fence ) <- [("I0", CompactIndex), ("I1", OrdinaryIndex)] |
| 194 | + , (f, cache ) <- enumerateDiskCachePolicy |
| 195 | + , (g, merge ) <- [("G0", OneShot), ("G1", Incremental)] |
| 196 | + ] |
| 197 | + |
| 198 | +enumerateSnapLevels :: [(ComponentAnnotation, SnapLevels RunNumber)] |
| 199 | +enumerateSnapLevels = fmap (SnapLevels . V.singleton) <$> enumerateSnapLevel |
| 200 | + |
| 201 | +{---------------- |
| 202 | +Enumeration of SnapLevel sub-components |
| 203 | +----------------} |
| 204 | + |
| 205 | +enumerateSnapLevel :: [(ComponentAnnotation, SnapLevel RunNumber)] |
| 206 | +enumerateSnapLevel = do |
| 207 | + (a, run) <- enumerateSnapIncomingRun |
| 208 | + (b, vec) <- enumerateVectorRunNumber |
| 209 | + [( fuseAnnotations [ a, b ], SnapLevel run vec)] |
| 210 | + |
| 211 | +enumerateSnapIncomingRun :: [(ComponentAnnotation, SnapIncomingRun RunNumber)] |
| 212 | +enumerateSnapIncomingRun = |
| 213 | + let |
| 214 | + inSnaps = |
| 215 | + [ (fuseAnnotations ["R1", a, b], SnapMergingRun policy numRuns entries credits sState) |
| 216 | + | (a, policy ) <- [("P0", LevelTiering), ("P1", LevelLevelling)] |
| 217 | + , numRuns <- NumRuns <$> [ magicNumber1 ] |
| 218 | + , entries <- NumEntries <$> [ magicNumber2 ] |
| 219 | + , credits <- SuppliedCredits <$> [ magicNumber1 ] |
| 220 | + , (b, sState ) <- enumerateSnapMergingRunState |
| 221 | + ] |
| 222 | + in fold |
| 223 | + [ [(fuseAnnotations $ "R0" : replicate 4 blank, SnapSingleRun enumerateRunNumbers)] |
| 224 | + , inSnaps |
| 225 | + ] |
| 226 | + |
| 227 | +enumerateSnapMergingRunState :: [(ComponentAnnotation, SnapMergingRunState RunNumber)] |
| 228 | +enumerateSnapMergingRunState = (fuseAnnotations ["C0", blank, blank], SnapCompletedMerge enumerateRunNumbers) : |
| 229 | + [ (fuseAnnotations ["C1", a, b], SnapOngoingMerge runVec mType) |
| 230 | + | (a, runVec ) <- enumerateVectorRunNumber |
| 231 | + , (b, mType ) <- [("M1", MergeMidLevel), ("M2", MergeLastLevel), ("M3", MergeUnion)] |
| 232 | + ] |
| 233 | + |
| 234 | +enumerateVectorRunNumber :: [(ComponentAnnotation, Vector RunNumber)] |
| 235 | +enumerateVectorRunNumber = |
| 236 | + [ ("V0", mempty) |
| 237 | + , ("V1", V.fromList [RunNumber magicNumber1]) |
| 238 | + , ("V2", V.fromList [RunNumber magicNumber1, RunNumber magicNumber2 ]) |
| 239 | + ] |
| 240 | + |
| 241 | +{---------------- |
| 242 | +Enumeration of SnapshotMetaData sub-sub-components and so on... |
| 243 | +----------------} |
| 244 | + |
| 245 | +enumerateBloomFilterAlloc :: [(ComponentAnnotation, BloomFilterAlloc)] |
| 246 | +enumerateBloomFilterAlloc = |
| 247 | + [ ("A0",AllocFixed magicNumber3) |
| 248 | + , ("A1",AllocRequestFPR pi) |
| 249 | + , ("A2",AllocMonkey magicNumber3 . NumEntries $ magicNumber3 `div` 4) |
| 250 | + ] |
| 251 | + |
| 252 | +enumerateDiskCachePolicy :: [(ComponentAnnotation, DiskCachePolicy)] |
| 253 | +enumerateDiskCachePolicy = |
| 254 | + [ ("D0", DiskCacheAll) |
| 255 | + , ("D1", DiskCacheNone) |
| 256 | + , ("D2", DiskCacheLevelsAtOrBelow 1) |
| 257 | + ] |
| 258 | + |
| 259 | +enumerateRunNumbers :: RunNumber |
| 260 | +enumerateRunNumbers = RunNumber magicNumber2 |
| 261 | + |
| 262 | +-- Randomly chosen numbers |
| 263 | +magicNumber1, magicNumber2, magicNumber3 :: Enum e => e |
| 264 | +magicNumber1 = toEnum 42 |
| 265 | +magicNumber2 = toEnum 88 |
| 266 | +magicNumber3 = toEnum 1024 |
0 commit comments