11module Database.LSMTree.Internal.Snapshot (
22 -- * Versioning
33 SnapshotVersion (.. )
4- , major
5- , minor
6- , fromMajorMinor
74 , prettySnapshotVersion
85 , currentSnapshotVersion
96 -- * Snapshot metadata
@@ -69,69 +66,31 @@ import Text.Printf
6966
7067-- | The version of a snapshot.
7168--
72- -- When encoding snapshot metadata, 'currentSnapshotVersion' is included in the
73- -- file. When snapshot metadata is decoded, then the decoded version is checked
74- -- for compatibility against 'currentSnapshotVersion'. Decoding will fail if the
75- -- versions are incompatible.
76- --
77- -- A version @x.y@ has two components: a major version number @x@ and a minor
78- -- version number @y@. The major version number is used to signal breaking
79- -- changes. The minor version number is used to signal non-breaking changes that
80- -- are backwards compatible. To be precise, @x.y@ is guaranteed to be backwards
81- -- compatible with @x'.y'@ as long as @x == x'@ and @y' <= y@. If @x > x'@, then
82- -- backwards compatibility *may* be provided, but is not guaranteed. Forward
83- -- compatibilitty is never guaranteed.
84- --
85- -- If @x.y@ is our current version, and if it @x.y@ is backwards compatible with
86- -- @x'.y'@, then @x.y@ can decode snapshots that were encoded at version
87- -- @x'.y'@.
88- --
89- -- The version number determines the format of the snapshot metadata file, but
90- -- it also doubles as versioning information for the entire snapshot itself. For
91- -- example, if a breaking change is made to the merge scheduling algorithm that
92- -- would lead to errors when loading an older snapshot, then the major version
93- -- should be increased as well.
94- data SnapshotVersion = V0_0
69+ -- A snapshot format version is a number. Version numbers are consecutive and
70+ -- increasing. A single release of the library may support several older
71+ -- snapshot format versions, and thereby provide backwards compatibility.
72+ -- Support for old versions is not guaranteed indefinitely, but backwards
73+ -- compatibility is guaranteed for at least the previous version, and preferably
74+ -- for more. Forwards compatibility is not provided at all: snapshots with a
75+ -- later version than the current version for the library release will always
76+ -- fail.
77+ data SnapshotVersion = V0
9578 deriving stock (Show , Eq )
9679
97- -- >>> major currentSnapshotVersion
98- -- 0
99- major :: SnapshotVersion -> Word
100- major V0_0 = 0
101-
102- -- >>> minor currentSnapshotVersion
103- -- 0
104- minor :: SnapshotVersion -> Word
105- minor V0_0 = 0
106-
107- fromMajorMinor :: Word -> Word -> Maybe SnapshotVersion
108- fromMajorMinor 0 0 = Just V0_0
109- fromMajorMinor _ _ = Nothing
110-
11180-- >>> prettySnapshotVersion currentSnapshotVersion
112- -- "v0.0 "
81+ -- "v0"
11382prettySnapshotVersion :: SnapshotVersion -> String
114- prettySnapshotVersion version = prettyVersion (major version) (minor version)
115-
116- -- >>> prettyVersion 17 32
117- -- "v17.32"
118- prettyVersion :: Word -> Word -> String
119- prettyVersion majo mino =
120- showChar ' v'
121- . shows majo
122- . showChar ' .'
123- . shows mino
124- $ " "
83+ prettySnapshotVersion V0 = " v0"
12584
12685-- >>> currentSnapshotVersion
127- -- V0_0
86+ -- V0
12887currentSnapshotVersion :: SnapshotVersion
129- currentSnapshotVersion = V0_0
88+ currentSnapshotVersion = V0
13089
13190isCompatible :: SnapshotVersion -> Either String ()
13291isCompatible otherVersion = do
13392 case ( currentSnapshotVersion, otherVersion ) of
134- (V0_0 , V0_0 ) -> Right ()
93+ (V0 , V0 ) -> Right ()
13594
13695{- ------------------------------------------------------------------------------
13796 Snapshot metadata
@@ -393,18 +352,17 @@ instance DecodeVersioned a => Decode (Versioned a) where
393352
394353instance Encode SnapshotVersion where
395354 encode ver =
396- encodeListLen 2
397- <> encodeWord (major ver)
398- < > encodeWord (minor ver)
355+ encodeListLen 1
356+ <> case ver of
357+ V0 - > encodeWord 0
399358
400359instance Decode SnapshotVersion where
401360 decode = do
402- _ <- decodeListLenOf 2
403- majo <- decodeWord
404- mino <- decodeWord
405- case fromMajorMinor majo mino of
406- Nothing -> fail (" Unknown version number: " <> prettyVersion majo mino)
407- Just version -> pure version
361+ _ <- decodeListLenOf 1
362+ ver <- decodeWord
363+ case ver of
364+ 0 -> pure V0
365+ _ -> fail (" Unknown version number: " <> show ver)
408366
409367{- ------------------------------------------------------------------------------
410368 Encoding and decoding: SnapshotMetaData
@@ -421,7 +379,7 @@ instance Encode SnapshotMetaData where
421379 <> encode levels
422380
423381instance DecodeVersioned SnapshotMetaData where
424- decodeVersioned ver@ V0_0 = do
382+ decodeVersioned ver@ V0 = do
425383 _ <- decodeListLenOf 4
426384 SnapshotMetaData
427385 <$> decodeVersioned ver <*> decodeVersioned ver
@@ -433,7 +391,7 @@ instance Encode SnapshotLabel where
433391 encode (SnapshotLabel s) = encodeString s
434392
435393instance DecodeVersioned SnapshotLabel where
436- decodeVersioned V0_0 = SnapshotLabel <$> decodeString
394+ decodeVersioned V0 = SnapshotLabel <$> decodeString
437395
438396-- TableType
439397
@@ -442,7 +400,7 @@ instance Encode SnapshotTableType where
442400 encode SnapMonoidalTable = encodeListLen 1 <> encodeWord 1
443401
444402instance DecodeVersioned SnapshotTableType where
445- decodeVersioned V0_0 = do
403+ decodeVersioned V0 = do
446404 _ <- decodeListLenOf 1
447405 tag <- decodeWord
448406 case tag of
@@ -478,7 +436,7 @@ instance Encode TableConfig where
478436 = config
479437
480438instance DecodeVersioned TableConfig where
481- decodeVersioned v@ V0_0 = do
439+ decodeVersioned v@ V0 = do
482440 _ <- decodeListLenOf 7
483441 TableConfig
484442 <$> decodeVersioned v <*> decodeVersioned v <*> decodeVersioned v
@@ -492,7 +450,7 @@ instance Encode MergePolicy where
492450 encodeListLen 1 <> encodeWord 0
493451
494452instance DecodeVersioned MergePolicy where
495- decodeVersioned V0_0 = do
453+ decodeVersioned V0 = do
496454 _ <- decodeListLenOf 1
497455 tag <- decodeWord
498456 case tag of
@@ -505,7 +463,7 @@ instance Encode SizeRatio where
505463 encode Four = encodeWord64 4
506464
507465instance DecodeVersioned SizeRatio where
508- decodeVersioned V0_0 = do
466+ decodeVersioned V0 = do
509467 x <- decodeWord64
510468 case x of
511469 4 -> pure Four
@@ -520,7 +478,7 @@ instance Encode WriteBufferAlloc where
520478 <> encode numEntries
521479
522480instance DecodeVersioned WriteBufferAlloc where
523- decodeVersioned v@ V0_0 = do
481+ decodeVersioned v@ V0 = do
524482 _ <- decodeListLenOf 2
525483 tag <- decodeWord
526484 case tag of
@@ -533,7 +491,7 @@ instance Encode NumEntries where
533491 encode (NumEntries x) = encodeInt x
534492
535493instance DecodeVersioned NumEntries where
536- decodeVersioned V0_0 = NumEntries <$> decodeInt
494+ decodeVersioned V0 = NumEntries <$> decodeInt
537495
538496-- BloomFilterAlloc
539497
@@ -553,7 +511,7 @@ instance Encode BloomFilterAlloc where
553511 <> encode numEntries
554512
555513instance DecodeVersioned BloomFilterAlloc where
556- decodeVersioned v@ V0_0 = do
514+ decodeVersioned v@ V0 = do
557515 n <- decodeListLen
558516 tag <- decodeWord
559517 case (n, tag) of
@@ -569,7 +527,7 @@ instance Encode FencePointerIndex where
569527 encode OrdinaryIndex = encodeListLen 1 <> encodeWord 1
570528
571529instance DecodeVersioned FencePointerIndex where
572- decodeVersioned V0_0 = do
530+ decodeVersioned V0 = do
573531 _ <- decodeListLenOf 1
574532 tag <- decodeWord
575533 case tag of
@@ -592,7 +550,7 @@ instance Encode DiskCachePolicy where
592550 <> encodeWord 2
593551
594552instance DecodeVersioned DiskCachePolicy where
595- decodeVersioned V0_0 = do
553+ decodeVersioned V0 = do
596554 n <- decodeListLen
597555 tag <- decodeWord
598556 case (n, tag) of
@@ -608,7 +566,7 @@ instance Encode MergeSchedule where
608566 encode Incremental = encodeListLen 1 <> encodeWord 1
609567
610568instance DecodeVersioned MergeSchedule where
611- decodeVersioned V0_0 = do
569+ decodeVersioned V0 = do
612570 _ <- decodeListLenOf 1
613571 tag <- decodeWord
614572 case tag of
@@ -628,7 +586,7 @@ instance Encode SnapLevels where
628586 <> V. foldMap encode levels
629587
630588instance DecodeVersioned SnapLevels where
631- decodeVersioned v@ V0_0 = do
589+ decodeVersioned v@ V0 = do
632590 n <- decodeListLen
633591 V. replicateM n (decodeVersioned v)
634592
@@ -642,7 +600,7 @@ instance Encode SnapLevel where
642600
643601
644602instance DecodeVersioned SnapLevel where
645- decodeVersioned v@ V0_0 = do
603+ decodeVersioned v@ V0 = do
646604 _ <- decodeListLenOf 2
647605 SnapLevel <$> decodeVersioned v <*> decodeVersioned v
648606
@@ -654,7 +612,7 @@ instance Encode (V.Vector RunNumber) where
654612 <> V. foldMap encode rns
655613
656614instance DecodeVersioned (V. Vector RunNumber ) where
657- decodeVersioned v@ V0_0 = do
615+ decodeVersioned v@ V0 = do
658616 n <- decodeListLen
659617 V. replicateM n (decodeVersioned v)
660618
@@ -664,7 +622,7 @@ instance Encode RunNumber where
664622 encode (RunNumber x) = encodeWord64 x
665623
666624instance DecodeVersioned RunNumber where
667- decodeVersioned V0_0 = RunNumber <$> decodeWord64
625+ decodeVersioned V0 = RunNumber <$> decodeWord64
668626
669627-- SnapMergingRun
670628
@@ -681,7 +639,7 @@ instance Encode SnapMergingRun where
681639 <> encode x
682640
683641instance DecodeVersioned SnapMergingRun where
684- decodeVersioned v@ V0_0 = do
642+ decodeVersioned v@ V0 = do
685643 n <- decodeListLen
686644 tag <- decodeWord
687645 case (n, tag) of
@@ -696,7 +654,7 @@ instance Encode NumRuns where
696654 encode (NumRuns x) = encodeInt x
697655
698656instance DecodeVersioned NumRuns where
699- decodeVersioned V0_0 = NumRuns <$> decodeInt
657+ decodeVersioned V0 = NumRuns <$> decodeInt
700658
701659-- MergePolicyForLevel
702660
@@ -705,7 +663,7 @@ instance Encode MergePolicyForLevel where
705663 encode LevelLevelling = encodeListLen 1 <> encodeWord 1
706664
707665instance DecodeVersioned MergePolicyForLevel where
708- decodeVersioned V0_0 = do
666+ decodeVersioned V0 = do
709667 _ <- decodeListLenOf 1
710668 tag <- decodeWord
711669 case tag of
@@ -728,7 +686,7 @@ instance Encode SnapMergingRunState where
728686 <> encode l
729687
730688instance DecodeVersioned SnapMergingRunState where
731- decodeVersioned v@ V0_0 = do
689+ decodeVersioned v@ V0 = do
732690 n <- decodeListLen
733691 tag <- decodeWord
734692 case (n, tag) of
@@ -743,7 +701,7 @@ instance Encode TotalCredits where
743701 encode (TotalCredits x) = encodeInt x
744702
745703instance DecodeVersioned TotalCredits where
746- decodeVersioned V0_0 = TotalCredits <$> decodeInt
704+ decodeVersioned V0 = TotalCredits <$> decodeInt
747705
748706 -- Merge.Level
749707
@@ -752,7 +710,7 @@ instance Encode Merge.Level where
752710 encode Merge. LastLevel = encodeListLen 1 <> encodeWord 1
753711
754712instance DecodeVersioned Merge. Level where
755- decodeVersioned V0_0 = do
713+ decodeVersioned V0 = do
756714 _ <- decodeListLenOf 1
757715 tag <- decodeWord
758716 case tag of
0 commit comments