Skip to content

Commit 2143a57

Browse files
dcouttsjorisdral
authored andcommitted
Introduce a V1 snapshot serialisation format version
But don't yet actually change the serialisation format. This is partly just to demonstrate to ourselves how to do it, so there's a pattern to follow in future. Doing this highlights that we cannot generally match on the version, and should only do so in places where the format is actually different between versions. Otherwise we would have to duplicate too much code.
1 parent c825cc9 commit 2143a57

File tree

56 files changed

+63
-40
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+63
-40
lines changed

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

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,33 @@ import Text.Printf
5858
-- for more. Forwards compatibility is not provided at all: snapshots with a
5959
-- later version than the current version for the library release will always
6060
-- fail.
61-
data SnapshotVersion = V0
62-
deriving stock (Show, Eq)
61+
data SnapshotVersion = V0 | V1
62+
deriving stock (Show, Eq, Ord)
6363

6464
-- | Pretty-print a snapshot version
6565
--
6666
-- >>> prettySnapshotVersion currentSnapshotVersion
67-
-- "v0"
67+
-- "v1"
6868
prettySnapshotVersion :: SnapshotVersion -> String
6969
prettySnapshotVersion V0 = "v0"
70+
prettySnapshotVersion V1 = "v1"
7071

7172
-- | The current snapshot version
7273
--
7374
-- >>> currentSnapshotVersion
74-
-- V0
75+
-- V1
7576
currentSnapshotVersion :: SnapshotVersion
76-
currentSnapshotVersion = V0
77+
currentSnapshotVersion = V1
7778

7879
-- | All snapshot versions that the current snapshpt version is compatible with.
7980
--
8081
-- >>> allCompatibleSnapshotVersions
81-
-- [V0]
82+
-- [V0,V1]
8283
--
8384
-- >>> last allCompatibleSnapshotVersions == currentSnapshotVersion
8485
-- True
8586
allCompatibleSnapshotVersions :: [SnapshotVersion]
86-
allCompatibleSnapshotVersions = [V0]
87+
allCompatibleSnapshotVersions = [V0, V1]
8788

8889
isCompatible :: SnapshotVersion -> Either String ()
8990
isCompatible otherVersion
@@ -214,13 +215,15 @@ instance Encode SnapshotVersion where
214215
encodeListLen 1
215216
<> case ver of
216217
V0 -> encodeWord 0
218+
V1 -> encodeWord 1
217219

218220
instance Decode SnapshotVersion where
219221
decode = do
220222
_ <- decodeListLenOf 1
221223
ver <- decodeWord
222224
case ver of
223225
0 -> pure V0
226+
1 -> pure V1
224227
_ -> fail ("Unknown snapshot format version number: " <> show ver)
225228

226229
{-------------------------------------------------------------------------------
@@ -239,7 +242,7 @@ instance Encode SnapshotMetaData where
239242
<> encodeMaybe mergingTree
240243

241244
instance DecodeVersioned SnapshotMetaData where
242-
decodeVersioned ver@V0 = do
245+
decodeVersioned ver = do
243246
_ <- decodeListLenOf 5
244247
SnapshotMetaData
245248
<$> decodeVersioned ver
@@ -254,7 +257,7 @@ instance Encode SnapshotLabel where
254257
encode (SnapshotLabel s) = encodeString s
255258

256259
instance DecodeVersioned SnapshotLabel where
257-
decodeVersioned V0 = SnapshotLabel <$> decodeString
260+
decodeVersioned _v = SnapshotLabel <$> decodeString
258261

259262
instance Encode SnapshotRun where
260263
encode SnapshotRun { snapRunNumber, snapRunCaching, snapRunIndex } =
@@ -265,7 +268,7 @@ instance Encode SnapshotRun where
265268
<> encode snapRunIndex
266269

267270
instance DecodeVersioned SnapshotRun where
268-
decodeVersioned v@V0 = do
271+
decodeVersioned v = do
269272
n <- decodeListLen
270273
tag <- decodeWord
271274
case (n, tag) of
@@ -303,7 +306,7 @@ instance Encode TableConfig where
303306
<> encode diskCachePolicy
304307

305308
instance DecodeVersioned TableConfig where
306-
decodeVersioned v@V0 = do
309+
decodeVersioned v = do
307310
_ <- decodeListLenOf 7
308311
confMergePolicy <- decodeVersioned v
309312
confMergeSchedule <- decodeVersioned v
@@ -320,7 +323,7 @@ instance Encode MergePolicy where
320323
encode LazyLevelling = encodeWord 0
321324

322325
instance DecodeVersioned MergePolicy where
323-
decodeVersioned V0 = do
326+
decodeVersioned _v = do
324327
tag <- decodeWord
325328
case tag of
326329
0 -> pure LazyLevelling
@@ -332,7 +335,7 @@ instance Encode SizeRatio where
332335
encode Four = encodeInt 4
333336

334337
instance DecodeVersioned SizeRatio where
335-
decodeVersioned V0 = do
338+
decodeVersioned _v = do
336339
x <- decodeWord64
337340
case x of
338341
4 -> pure Four
@@ -347,7 +350,7 @@ instance Encode WriteBufferAlloc where
347350
<> encodeInt numEntries
348351

349352
instance DecodeVersioned WriteBufferAlloc where
350-
decodeVersioned V0 = do
353+
decodeVersioned _v = do
351354
_ <- decodeListLenOf 2
352355
tag <- decodeWord
353356
case tag of
@@ -365,7 +368,7 @@ instance Encode RunParams where
365368
<> encode runParamIndex
366369

367370
instance DecodeVersioned RunParams where
368-
decodeVersioned v@V0 = do
371+
decodeVersioned v = do
369372
n <- decodeListLen
370373
tag <- decodeWord
371374
case (n, tag) of
@@ -380,7 +383,7 @@ instance Encode RunDataCaching where
380383
encode NoCacheRunData = encodeWord 1
381384

382385
instance DecodeVersioned RunDataCaching where
383-
decodeVersioned V0 = do
386+
decodeVersioned _v = do
384387
tag <- decodeWord
385388
case tag of
386389
0 -> pure CacheRunData
@@ -392,7 +395,7 @@ instance Encode IndexType where
392395
encode Compact = encodeWord 1
393396

394397
instance DecodeVersioned IndexType where
395-
decodeVersioned V0 = do
398+
decodeVersioned _v = do
396399
tag <- decodeWord
397400
case tag of
398401
0 -> pure Ordinary
@@ -410,7 +413,7 @@ instance Encode RunBloomFilterAlloc where
410413
<> encodeDouble fpr
411414

412415
instance DecodeVersioned RunBloomFilterAlloc where
413-
decodeVersioned V0 = do
416+
decodeVersioned _v = do
414417
n <- decodeListLen
415418
tag <- decodeWord
416419
case (n, tag) of
@@ -431,7 +434,7 @@ instance Encode BloomFilterAlloc where
431434
<> encodeDouble x
432435

433436
instance DecodeVersioned BloomFilterAlloc where
434-
decodeVersioned V0 = do
437+
decodeVersioned _v = do
435438
n <- decodeListLen
436439
tag <- decodeWord
437440
case (n, tag) of
@@ -446,7 +449,7 @@ instance Encode FencePointerIndexType where
446449
encode OrdinaryIndex = encodeWord 1
447450

448451
instance DecodeVersioned FencePointerIndexType where
449-
decodeVersioned V0 = do
452+
decodeVersioned _v = do
450453
tag <- decodeWord
451454
case tag of
452455
0 -> pure CompactIndex
@@ -468,7 +471,7 @@ instance Encode DiskCachePolicy where
468471
<> encodeWord 2
469472

470473
instance DecodeVersioned DiskCachePolicy where
471-
decodeVersioned V0 = do
474+
decodeVersioned _v = do
472475
n <- decodeListLen
473476
tag <- decodeWord
474477
case (n, tag) of
@@ -484,7 +487,7 @@ instance Encode MergeSchedule where
484487
encode Incremental = encodeWord 1
485488

486489
instance DecodeVersioned MergeSchedule where
487-
decodeVersioned V0 = do
490+
decodeVersioned _v = do
488491
tag <- decodeWord
489492
case tag of
490493
0 -> pure OneShot
@@ -501,7 +504,7 @@ instance Encode r => Encode (SnapLevels r) where
501504
encode (SnapLevels levels) = encode levels
502505

503506
instance DecodeVersioned r => DecodeVersioned (SnapLevels r) where
504-
decodeVersioned v@V0 = SnapLevels <$> decodeVersioned v
507+
decodeVersioned v = SnapLevels <$> decodeVersioned v
505508

506509
-- SnapLevel
507510

@@ -513,7 +516,7 @@ instance Encode r => Encode (SnapLevel r) where
513516

514517

515518
instance DecodeVersioned r => DecodeVersioned (SnapLevel r) where
516-
decodeVersioned v@V0 = do
519+
decodeVersioned v = do
517520
_ <- decodeListLenOf 2
518521
SnapLevel <$> decodeVersioned v <*> decodeVersioned v
519522

@@ -531,7 +534,7 @@ instance Encode RunNumber where
531534
encode (RunNumber x) = encodeInt x
532535

533536
instance DecodeVersioned RunNumber where
534-
decodeVersioned V0 = RunNumber <$> decodeInt
537+
decodeVersioned _v = RunNumber <$> decodeInt
535538

536539
-- SnapIncomingRun
537540

@@ -549,7 +552,7 @@ instance Encode r => Encode (SnapIncomingRun r) where
549552
<> encode x
550553

551554
instance DecodeVersioned r => DecodeVersioned (SnapIncomingRun r) where
552-
decodeVersioned v@V0 = do
555+
decodeVersioned v = do
553556
n <- decodeListLen
554557
tag <- decodeWord
555558
case (n, tag) of
@@ -566,7 +569,7 @@ instance Encode MergePolicyForLevel where
566569
encode LevelLevelling = encodeWord 1
567570

568571
instance DecodeVersioned MergePolicyForLevel where
569-
decodeVersioned V0 = do
572+
decodeVersioned _v = do
570573
tag <- decodeWord
571574
case tag of
572575
0 -> pure LevelTiering
@@ -590,7 +593,7 @@ instance (Encode t, Encode r) => Encode (SnapMergingRun t r) where
590593
<> encode mt
591594

592595
instance (DecodeVersioned t, DecodeVersioned r) => DecodeVersioned (SnapMergingRun t r) where
593-
decodeVersioned v@V0 = do
596+
decodeVersioned v = do
594597
n <- decodeListLen
595598
tag <- decodeWord
596599
case (n, tag) of
@@ -606,25 +609,25 @@ instance Encode NominalDebt where
606609
encode (NominalDebt x) = encodeInt x
607610

608611
instance DecodeVersioned NominalDebt where
609-
decodeVersioned V0 = NominalDebt <$> decodeInt
612+
decodeVersioned _v = NominalDebt <$> decodeInt
610613

611614
instance Encode NominalCredits where
612615
encode (NominalCredits x) = encodeInt x
613616

614617
instance DecodeVersioned NominalCredits where
615-
decodeVersioned V0 = NominalCredits <$> decodeInt
618+
decodeVersioned _v = NominalCredits <$> decodeInt
616619

617620
instance Encode MergeDebt where
618621
encode (MergeDebt (MergeCredits x)) = encodeInt x
619622

620623
instance DecodeVersioned MergeDebt where
621-
decodeVersioned V0 = (MergeDebt . MergeCredits) <$> decodeInt
624+
decodeVersioned _v = (MergeDebt . MergeCredits) <$> decodeInt
622625

623626
instance Encode MergeCredits where
624627
encode (MergeCredits x) = encodeInt x
625628

626629
instance DecodeVersioned MergeCredits where
627-
decodeVersioned V0 = MergeCredits <$> decodeInt
630+
decodeVersioned _v = MergeCredits <$> decodeInt
628631

629632
-- MergeType
630633

@@ -633,7 +636,7 @@ instance Encode MR.LevelMergeType where
633636
encode MR.MergeLastLevel = encodeWord 1
634637

635638
instance DecodeVersioned MR.LevelMergeType where
636-
decodeVersioned V0 = do
639+
decodeVersioned _v = do
637640
tag <- decodeWord
638641
case tag of
639642
0 -> pure MR.MergeMidLevel
@@ -655,7 +658,7 @@ instance Encode MR.TreeMergeType where
655658
encode MR.MergeUnion = encodeWord 2
656659

657660
instance DecodeVersioned MR.TreeMergeType where
658-
decodeVersioned V0 = do
661+
decodeVersioned _v = do
659662
tag <- decodeWord
660663
case tag of
661664
1 -> pure MR.MergeLevel
@@ -672,7 +675,7 @@ instance Encode r => Encode (SnapMergingTree r) where
672675
encode (SnapMergingTree tState) = encode tState
673676

674677
instance DecodeVersioned r => DecodeVersioned (SnapMergingTree r) where
675-
decodeVersioned ver@V0 = SnapMergingTree <$> decodeVersioned ver
678+
decodeVersioned ver = SnapMergingTree <$> decodeVersioned ver
676679

677680
-- SnapMergingTreeState
678681

@@ -691,7 +694,7 @@ instance Encode r => Encode (SnapMergingTreeState r) where
691694
<> encode smrs
692695

693696
instance DecodeVersioned r => DecodeVersioned (SnapMergingTreeState r) where
694-
decodeVersioned v@V0 = do
697+
decodeVersioned v = do
695698
n <- decodeListLen
696699
tag <- decodeWord
697700
case (n, tag) of
@@ -714,7 +717,7 @@ instance Encode r => Encode (SnapPendingMerge r) where
714717
<> encodeList mts
715718

716719
instance DecodeVersioned r => DecodeVersioned (SnapPendingMerge r) where
717-
decodeVersioned v@V0 = do
720+
decodeVersioned v = do
718721
n <- decodeListLen
719722
tag <- decodeWord
720723
case (n, tag) of
@@ -735,7 +738,7 @@ instance Encode r => Encode (SnapPreExistingRun r) where
735738
<> encode smrs
736739

737740
instance DecodeVersioned r => DecodeVersioned (SnapPreExistingRun r) where
738-
decodeVersioned v@V0 = do
741+
decodeVersioned v = do
739742
n <- decodeListLen
740743
tag <- decodeWord
741744
case (n, tag) of
@@ -753,7 +756,7 @@ encodeMaybe = \case
753756
Just en -> encode en
754757

755758
decodeMaybe :: DecodeVersioned a => SnapshotVersion -> Decoder s (Maybe a)
756-
decodeMaybe v@V0 = do
759+
decodeMaybe v = do
757760
tok <- peekTokenType
758761
case tok of
759762
TypeNull -> Nothing <$ decodeNull

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ testAll test = [
191191
-------------------------------------------------------------------------------}
192192

193193
instance Arbitrary SnapshotVersion where
194-
arbitrary = elements [V0]
194+
arbitrary = elements [V0, V1]
195195
shrink V0 = []
196+
shrink V1 = [V0]
196197

197198
deriving newtype instance Arbitrary a => Arbitrary (Versioned a)
198199

Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��@ !�TD-
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


0 commit comments

Comments
 (0)