Skip to content

Commit b15520e

Browse files
authored
Merge pull request #322 from IntersectMBO/jdral/conf-index
Add configuration option for the fence pointer index
2 parents 7630e06 + f940fb0 commit b15520e

File tree

9 files changed

+62
-36
lines changed

9 files changed

+62
-36
lines changed

bench/micro/Bench/Database/LSMTree/Monoidal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ benchConfig = Normal.TableConfig {
5757
, confSizeRatio = Normal.Four
5858
, confWriteBufferAlloc = Normal.AllocNumEntries (Normal.NumEntries 20000)
5959
, confBloomFilterAlloc = Normal.AllocFixed 10
60+
, confFencePointerIndex = Normal.CompactIndex
6061
, confDiskCachePolicy = Normal.DiskCacheAll
6162
}
6263

src/Database/LSMTree/Internal.hs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Database.LSMTree.Internal (
3939
, listSnapshots
4040
-- * Mutiple writable table handles
4141
, duplicate
42-
-- * configuration
42+
-- * Configuration
4343
, TableConfig (..)
4444
, defaultTableConfig
4545
, MergePolicy (..)
@@ -48,6 +48,7 @@ module Database.LSMTree.Internal (
4848
, NumEntries (..)
4949
, BloomFilterAlloc (..)
5050
, defaultBloomFilterAlloc
51+
, FencePointerIndex (..)
5152
, DiskCachePolicy (..)
5253
-- * Exported for cabal-docspec
5354
, MergePolicyForLevel (..)
@@ -1277,22 +1278,24 @@ duplicate th = withOpenTable th $ \thEnv -> do
12771278
--
12781279
-- * Size ratio: 4
12791280
data TableConfig = TableConfig {
1280-
confMergePolicy :: !MergePolicy
1281+
confMergePolicy :: !MergePolicy
12811282
-- Size ratio between the capacities of adjacent levels.
1282-
, confSizeRatio :: !SizeRatio
1283+
, confSizeRatio :: !SizeRatio
12831284
-- | Total number of bytes that the write buffer can use.
12841285
--
12851286
-- The maximum is 4GiB, which should be more than enough for realistic
12861287
-- applications.
1287-
, confWriteBufferAlloc :: !WriteBufferAlloc
1288-
, confBloomFilterAlloc :: !BloomFilterAlloc
1288+
, confWriteBufferAlloc :: !WriteBufferAlloc
1289+
, confBloomFilterAlloc :: !BloomFilterAlloc
1290+
, confFencePointerIndex :: !FencePointerIndex
12891291
-- | The policy for caching key\/value data from disk in memory.
1290-
, confDiskCachePolicy :: !DiskCachePolicy
1292+
, confDiskCachePolicy :: !DiskCachePolicy
12911293
}
1292-
deriving stock Show
1294+
deriving stock (Show, Eq)
12931295

12941296
instance NFData TableConfig where
1295-
rnf (TableConfig a b c d e) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq` rnf e
1297+
rnf (TableConfig a b c d e f) =
1298+
rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq` rnf e `seq` rnf f
12961299

12971300
-- | TODO: this should be removed once we have proper snapshotting with proper
12981301
-- persistence of the config to disk.
@@ -1310,6 +1313,7 @@ defaultTableConfig =
13101313
, confSizeRatio = Four
13111314
, confWriteBufferAlloc = AllocNumEntries (NumEntries 20_000)
13121315
, confBloomFilterAlloc = defaultBloomFilterAlloc
1316+
, confFencePointerIndex = CompactIndex
13131317
, confDiskCachePolicy = DiskCacheAll
13141318
}
13151319

@@ -1451,6 +1455,39 @@ bloomFilterAllocForLevel conf (LevelNo l) =
14511455
0 -> Just x
14521456
_ -> r (k-1)) (const Nothing) xs n
14531457

1458+
-- | Configure the type of fence pointer index.
1459+
--
1460+
-- TODO: this configuration option currently has no effect: 'CompactIndex' is
1461+
-- always used.
1462+
data FencePointerIndex =
1463+
-- | Use a compact fence pointer index.
1464+
--
1465+
-- The compact index type is designed to work with keys that are large
1466+
-- cryptographic hashes, e.g. 32 bytes.
1467+
--
1468+
-- When using the 'IndexCompact', additional constraints apply to the
1469+
-- 'Database.LSMTree.Internal.Serialise.Class.serialiseKey' function. The
1470+
-- __Minimal size__ law should be satisfied:
1471+
--
1472+
-- [Minimal size] @'Database.LSMTree.Internal.RawBytes.size'
1473+
-- ('Database.LSMTree.Internal.Serialise.Class.serialiseKey' x) >= 8@
1474+
--
1475+
-- Use 'Database.LSMTree.Internal.Serialise.Class.serialiseKeyMinimalSize'
1476+
-- to test this law.
1477+
CompactIndex
1478+
-- | Use an ordinary fence pointer index, without any constraints on
1479+
-- serialised keys.
1480+
| OrdinaryIndex
1481+
deriving stock (Show, Eq)
1482+
1483+
instance NFData FencePointerIndex where
1484+
rnf CompactIndex = ()
1485+
rnf OrdinaryIndex = ()
1486+
1487+
-- | TODO: this should be removed once we have proper snapshotting with proper
1488+
-- persistence of the config to disk.
1489+
deriving stock instance Read FencePointerIndex
1490+
14541491
-- | The policy for caching data from disk in memory (using the OS page cache).
14551492
--
14561493
-- Caching data in memory can improve performance if the access pattern has

src/Database/LSMTree/Internal/Serialise/Class.hs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,10 @@ import Numeric (showInt)
4343
-- [Ordering-preserving] @x \`'compare'\` y == 'serialiseKey' x \`'compare'\` 'serialiseKey' y@
4444
--
4545
-- Raw bytes are lexicographically ordered, so in particular this means that
46-
-- values should be serialised into big-endian formats.
47-
-- This constraint mainly exists for range queries, where the range is specified
48-
-- in terms of unserialised values, but the internal implementation works on the
49-
-- serialised representation.
50-
--
51-
-- === IndexCompact constraints
52-
--
53-
-- When using the 'IndexCompact', additional constraints apply to the
54-
-- serialisation function, so in that case instances should also satisfy the
55-
-- following:
56-
--
57-
-- [Minimal size] @'sizeofRawBytes' >= 8@
46+
-- values should be serialised into big-endian formats. This constraint mainly
47+
-- exists for range queries, where the range is specified in terms of
48+
-- unserialised values, but the internal implementation works on the serialised
49+
-- representation.
5850
class SerialiseKey k where
5951
serialiseKey :: k -> RawBytes
6052
-- TODO: 'deserialiseKey' is only strictly necessary for range queries.

src/Database/LSMTree/Monoidal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Database.LSMTree.Monoidal (
4343
, Internal.NumEntries (..)
4444
, Internal.BloomFilterAlloc (..)
4545
, Internal.defaultBloomFilterAlloc
46+
, Internal.FencePointerIndex (..)
4647
, Internal.DiskCachePolicy (..)
4748
, withTable
4849
, new

src/Database/LSMTree/Normal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module Database.LSMTree.Normal (
4242
, Internal.NumEntries (..)
4343
, Internal.BloomFilterAlloc (..)
4444
, Internal.defaultBloomFilterAlloc
45+
, Internal.FencePointerIndex (..)
4546
, Internal.DiskCachePolicy (..)
4647
, withTable
4748
, new

test/Test/Database/LSMTree/Class/Monoidal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ tests = testGroup "Test.Database.LSMTree.Class.Monoidal"
4646
, R.confSizeRatio = R.Four
4747
, R.confWriteBufferAlloc = R.AllocNumEntries (R.NumEntries 3)
4848
, R.confBloomFilterAlloc = R.AllocFixed 10
49+
, R.confFencePointerIndex = R.CompactIndex
4950
, R.confDiskCachePolicy = R.DiskCacheNone
5051
}
5152
, testWithSessionArgs = \action ->

test/Test/Database/LSMTree/Class/Normal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ tests = testGroup "Test.Database.LSMTree.Class.Normal"
5252
, R.confSizeRatio = R.Four
5353
, R.confWriteBufferAlloc = R.AllocNumEntries (R.NumEntries 3)
5454
, R.confBloomFilterAlloc = R.AllocFixed 10
55+
, R.confFencePointerIndex = R.CompactIndex
5556
, R.confDiskCachePolicy = R.DiskCacheNone
5657
}
5758
, testWithSessionArgs = \action ->

test/Test/Database/LSMTree/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ prop_interimRestoreSessionUniqueRunNames (Positive (Small n)) (NonNegative m) =
127127
-- flushes and merges.
128128
, confWriteBufferAlloc = AllocNumEntries (NumEntries n)
129129
, confBloomFilterAlloc = AllocFixed 10
130+
, confFencePointerIndex = CompactIndex
130131
, confDiskCachePolicy = DiskCacheNone
131132
}
132133

@@ -172,6 +173,7 @@ prop_interimOpenTable dat = ioProperty $
172173
-- flushes and merges.
173174
, confWriteBufferAlloc = AllocNumEntries (NumEntries 3)
174175
, confBloomFilterAlloc = AllocFixed 10
176+
, confFencePointerIndex = CompactIndex
175177
, confDiskCachePolicy = DiskCacheNone
176178
}
177179

test/Test/Database/LSMTree/Normal/StateMachine.hs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,14 @@ instance Arbitrary M.TableConfig where
227227
instance Arbitrary R.TableConfig where
228228
arbitrary :: Gen R.TableConfig
229229
arbitrary = pure $ R.TableConfig {
230-
R.confMergePolicy = R.MergePolicyLazyLevelling
231-
, R.confSizeRatio = R.Four
232-
, R.confWriteBufferAlloc = R.AllocNumEntries (R.NumEntries 30)
233-
, R.confBloomFilterAlloc = R.AllocFixed 10
234-
, R.confDiskCachePolicy = R.DiskCacheNone
230+
R.confMergePolicy = R.MergePolicyLazyLevelling
231+
, R.confSizeRatio = R.Four
232+
, R.confWriteBufferAlloc = R.AllocNumEntries (R.NumEntries 30)
233+
, R.confBloomFilterAlloc = R.AllocFixed 10
234+
, R.confFencePointerIndex = R.CompactIndex
235+
, R.confDiskCachePolicy = R.DiskCacheNone
235236
}
236237

237-
instance Eq R.TableConfig where
238-
R.TableConfig pol1 size1 wbAlloc1 bfAlloc1 cache1
239-
== R.TableConfig pol2 size2 wbAlloc2 bfAlloc2 cache2
240-
= and [
241-
pol1 == pol2
242-
, size1 == size2
243-
, wbAlloc1 == wbAlloc2
244-
, bfAlloc1 == bfAlloc2
245-
, cache1 == cache2
246-
]
247-
248238
{-------------------------------------------------------------------------------
249239
Key and value types
250240
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)