@@ -34,8 +34,9 @@ module Database.LSMTree.Extras.Generators (
3434 , genRawBytesSized
3535 , packRawBytesPinnedOrUnpinned
3636 , LargeRawBytes (.. )
37+ , isKeyForIndexCompact
3738 , KeyForIndexCompact (.. )
38- , keyForIndexCompactInvariant
39+ , BiasedKeyForIndexCompact ( .. )
3940 ) where
4041
4142import Control.DeepSeq (NFData )
@@ -469,7 +470,6 @@ shrinkSlice (RawBytes pvec) =
469470 , m <- QC. shrink (VP. length pvec - n)
470471 ]
471472
472- -- TODO: makes collisions very unlikely
473473deriving newtype instance Arbitrary SerialisedKey
474474
475475instance Arbitrary SerialisedValue where
@@ -508,15 +508,36 @@ instance Arbitrary LargeRawBytes where
508508
509509deriving newtype instance SerialiseValue LargeRawBytes
510510
511- -- | Minimum length of 8 bytes.
511+ -- Serialised keys for the compact index must be at least 8 bytes long.
512+
513+ genKeyForIndexCompact :: Gen RawBytes
514+ genKeyForIndexCompact =
515+ genRawBytesN =<< QC. sized (\ s -> QC. chooseInt (8 , s + 8 ))
516+
517+ isKeyForIndexCompact :: RawBytes -> Bool
518+ isKeyForIndexCompact rb = RB. size rb >= 8
519+
512520newtype KeyForIndexCompact =
513521 KeyForIndexCompact { getKeyForIndexCompact :: RawBytes }
514522 deriving stock (Eq , Ord , Show )
515523
516524instance Arbitrary KeyForIndexCompact where
525+ arbitrary =
526+ KeyForIndexCompact <$> genKeyForIndexCompact
527+ shrink (KeyForIndexCompact rawBytes) =
528+ [KeyForIndexCompact rawBytes' | rawBytes' <- shrink rawBytes,
529+ isKeyForIndexCompact rawBytes']
530+
531+ deriving newtype instance SerialiseKey KeyForIndexCompact
532+
533+ newtype BiasedKeyForIndexCompact =
534+ BiasedKeyForIndexCompact { getBiasedKeyForIndexCompact :: RawBytes }
535+ deriving stock (Eq , Ord , Show )
536+
537+ instance Arbitrary BiasedKeyForIndexCompact where
517538 -- we try to make collisions and close keys more likely (very crudely)
518- arbitrary = KeyForIndexCompact <$> frequency
519- [ (6 , genRawBytesN =<< QC. sized ( \ s -> QC. chooseInt ( 8 , s + 8 )) )
539+ arbitrary = BiasedKeyForIndexCompact <$> frequency
540+ [ (6 , genKeyForIndexCompact )
520541 , (1 , do
521542 lastByte <- QC. sized $ skewedWithMax . fromIntegral
522543 return (RB. pack ([1 ,3 ,3 ,7 ,0 ,1 ,7 ] <> [lastByte]))
@@ -529,17 +550,13 @@ instance Arbitrary KeyForIndexCompact where
529550 ub2 <- QC. chooseBoundedIntegral (0 , ub1)
530551 QC. chooseBoundedIntegral (0 , ub2)
531552
532- shrink (KeyForIndexCompact rb) =
533- [ k '
553+ shrink (BiasedKeyForIndexCompact rb) =
554+ [ BiasedKeyForIndexCompact rb '
534555 | rb' <- shrink rb
535- , let k' = KeyForIndexCompact rb'
536- , keyForIndexCompactInvariant k'
556+ , isKeyForIndexCompact rb'
537557 ]
538558
539- deriving newtype instance SerialiseKey KeyForIndexCompact
540-
541- keyForIndexCompactInvariant :: KeyForIndexCompact -> Bool
542- keyForIndexCompactInvariant (KeyForIndexCompact rb) = RB. size rb >= 8
559+ deriving newtype instance SerialiseKey BiasedKeyForIndexCompact
543560
544561{- ------------------------------------------------------------------------------
545562 Unsliced
0 commit comments